You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InterTypeScope.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.internal.compiler.lookup;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
  17. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ClassScope;
  18. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding;
  20. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.Scope;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
  25. import org.aspectj.weaver.BCException;
  26. public class InterTypeScope extends ClassScope {
  27. ReferenceBinding onType;
  28. List aliases;
  29. Map<TypeVariableBinding, String> /* real type variable > alias letter */usedAliases; // Used later when reconstructing the
  30. // resolved member
  31. public InterTypeScope(Scope parent, ReferenceBinding onType) {
  32. super(parent, null);
  33. referenceContext = new TypeDeclaration(null);
  34. referenceContext.binding = makeSourceTypeBinding(onType);
  35. this.onType = onType;
  36. }
  37. public InterTypeScope(Scope parent, ReferenceBinding rb, List list) {
  38. this(parent, rb);
  39. this.aliases = list;
  40. }
  41. public String getAnyAliasForTypeVariableBinding(TypeVariableBinding tvb) {
  42. if (usedAliases == null)
  43. return null;
  44. return usedAliases.get(tvb);
  45. }
  46. // this method depends on the fact that BinaryTypeBinding extends SourceTypeBinding
  47. private SourceTypeBinding makeSourceTypeBinding(ReferenceBinding onType) {
  48. if (onType instanceof SourceTypeBinding)
  49. return (SourceTypeBinding) onType;
  50. else if (onType instanceof ParameterizedTypeBinding) {
  51. ReferenceBinding rb = ((ParameterizedTypeBinding) onType).type;
  52. if (rb instanceof SourceTypeBinding)
  53. return (SourceTypeBinding) rb;
  54. else
  55. throw new BCException("In parameterized type " + onType + ", can't handle reference binding " + rb);
  56. } else if (onType instanceof ProblemReferenceBinding) {
  57. return null;
  58. } else if (onType instanceof TypeVariableBinding) {
  59. // Problem will have already been reported, cant ITD on a type variable.
  60. return null;
  61. }
  62. throw new BCException("can't handle: " + onType);
  63. }
  64. public SourceTypeBinding invocationType() {
  65. return parent.enclosingSourceType();
  66. }
  67. public int addDepth() {
  68. return 0;
  69. }
  70. public TypeVariableBinding findTypeVariable(char[] name, SourceTypeBinding sourceType) {
  71. if (sourceType == null) {
  72. return null;
  73. }
  74. String variableName = new String(name);
  75. int aliased = (aliases == null ? -1 : aliases.indexOf(variableName));
  76. if (aliased != -1) {
  77. if (aliased > sourceType.typeVariables.length || sourceType.typeVariables.length == 0) {
  78. TypeVariableBinding tvb = new TypeVariableBinding("fake".toCharArray(), null, 0,this.environment());
  79. tvb.superclass = getJavaLangObject();
  80. tvb.fPackage = new PlainPackageBinding(environment());
  81. return tvb;
  82. // error is going to be reported by someone else!
  83. }
  84. TypeVariableBinding tvb = sourceType.typeVariables()[aliased];
  85. tvb.fPackage = sourceType.fPackage;
  86. if (usedAliases == null)
  87. usedAliases = new HashMap<>();
  88. usedAliases.put(tvb, variableName);
  89. return tvb;
  90. } else {
  91. TypeVariableBinding variableBinding = sourceType.getTypeVariable(name);
  92. if (variableBinding == null) { // GENERICITDFIX
  93. // Inside generic aspect, might want the type var attached to us
  94. variableBinding = parent.findTypeVariable(name, ((ClassScope) parent).referenceContext.binding);
  95. }
  96. return variableBinding;
  97. }
  98. }
  99. public Map getRecoveryAliases() {
  100. return usedAliases;
  101. }
  102. @Override
  103. public boolean isInterTypeScope() {
  104. return true;
  105. }
  106. }