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.

InterTypeDeclaration.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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.ast;
  13. import java.lang.reflect.Modifier;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.aspectj.ajdt.internal.compiler.lookup.EclipseFactory;
  17. import org.aspectj.ajdt.internal.compiler.lookup.EclipseTypeMunger;
  18. import org.aspectj.ajdt.internal.compiler.lookup.InterTypeScope;
  19. import org.aspectj.ajdt.internal.core.builder.EclipseSourceContext;
  20. import org.aspectj.bridge.context.CompilationAndWeavingContext;
  21. import org.aspectj.bridge.context.ContextToken;
  22. import org.aspectj.org.eclipse.jdt.core.compiler.CharOperation;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.ClassFile;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult;
  25. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Annotation;
  26. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference;
  27. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference;
  28. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
  29. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
  30. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference;
  31. import org.aspectj.org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
  32. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ClassScope;
  33. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodScope;
  34. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
  35. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding;
  36. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding;
  37. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
  38. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
  39. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
  40. import org.aspectj.weaver.AjAttribute;
  41. import org.aspectj.weaver.ResolvedMember;
  42. import org.aspectj.weaver.ResolvedTypeMunger;
  43. import org.aspectj.weaver.Shadow;
  44. /**
  45. * Base type for all inter-type declarations including methods, fields and constructors.
  46. *
  47. * @author Jim Hugunin
  48. */
  49. public abstract class InterTypeDeclaration extends AjMethodDeclaration {
  50. protected TypeReference onType;
  51. protected ReferenceBinding onTypeBinding;
  52. protected ResolvedTypeMunger munger;
  53. public int declaredModifiers; // so others can see (these differ from the modifiers in the superclass)
  54. protected char[] declaredSelector;
  55. /**
  56. * If targetting a generic type and wanting to use its type variables, an ITD can use an alternative name for
  57. * them. This is a list of strings representing the alternative names - the position in the list is used to
  58. * match it to the real type variable in the target generic type.
  59. */
  60. protected List typeVariableAliases;
  61. protected InterTypeScope interTypeScope;
  62. /**
  63. * When set to true, the scope hierarchy for the field/method declaration has been correctly modified to
  64. * include an intertypescope which resolves things relative to the targetted type.
  65. */
  66. private boolean scopeSetup = false;
  67. // XXXAJ5 - When the compiler is changed, these will exist somewhere in it...
  68. private final static short ACC_ANNOTATION = 0x2000;
  69. private final static short ACC_ENUM = 0x4000;
  70. public InterTypeDeclaration(CompilationResult result, TypeReference onType) {
  71. super(result);
  72. setOnType(onType);
  73. modifiers = ClassFileConstants.AccPublic | ClassFileConstants.AccStatic;
  74. }
  75. public void setOnType(TypeReference onType) {
  76. this.onType = onType;
  77. determineTypeVariableAliases();
  78. }
  79. public void setDeclaredModifiers(int modifiers) {
  80. this.declaredModifiers = modifiers;
  81. }
  82. public void setSelector(char[] selector) {
  83. declaredSelector = selector;
  84. this.selector = CharOperation.concat(selector, Integer.toHexString(sourceStart).toCharArray());
  85. this.selector = CharOperation.concat(getPrefix(),this.selector);
  86. }
  87. // return the selector prefix for this itd that is to be used before resolution replaces it with a "proper" name
  88. protected abstract char[] getPrefix();
  89. public void addAtAspectJAnnotations() {
  90. if (munger == null) return;
  91. Annotation ann = AtAspectJAnnotationFactory.createITDAnnotation(
  92. munger.getSignature().getDeclaringType().getName().toCharArray(),
  93. declaredModifiers,declaredSelector,declarationSourceStart);
  94. AtAspectJAnnotationFactory.addAnnotation(this,ann,this.scope);
  95. }
  96. /**
  97. * Checks that the target for the ITD is not an annotation. If it is, an error message
  98. * is signaled. We return true if it is annotation so the caller knows to stop processing.
  99. * kind is 'constructor', 'field', 'method'
  100. */
  101. public boolean isTargetAnnotation(ClassScope classScope,String kind) {
  102. if ((onTypeBinding.getAccessFlags() & ACC_ANNOTATION)!=0) {
  103. classScope.problemReporter().signalError(sourceStart,sourceEnd,
  104. "can't make inter-type "+kind+" declarations on annotation types.");
  105. ignoreFurtherInvestigation = true;
  106. return true;
  107. }
  108. return false;
  109. }
  110. /**
  111. * Checks that the target for the ITD is not an enum. If it is, an error message
  112. * is signaled. We return true if it is enum so the caller knows to stop processing.
  113. */
  114. public boolean isTargetEnum(ClassScope classScope,String kind) {
  115. if ((onTypeBinding.getAccessFlags() & ACC_ENUM)!=0) {
  116. classScope.problemReporter().signalError(sourceStart,sourceEnd,
  117. "can't make inter-type "+kind+" declarations on enum types.");
  118. ignoreFurtherInvestigation = true;
  119. return true;
  120. }
  121. return false;
  122. }
  123. public void resolve(ClassScope upperScope) {
  124. if (ignoreFurtherInvestigation) return;
  125. if (!scopeSetup) {
  126. interTypeScope = new InterTypeScope(upperScope, onTypeBinding,typeVariableAliases);
  127. scope.parent = interTypeScope;
  128. this.scope.isStatic = Modifier.isStatic(declaredModifiers);
  129. scopeSetup = true;
  130. }
  131. fixSuperCallsForInterfaceContext(upperScope);
  132. if (ignoreFurtherInvestigation) return;
  133. super.resolve((ClassScope)scope.parent);//newParent);
  134. fixSuperCallsInBody();
  135. }
  136. private void fixSuperCallsForInterfaceContext(ClassScope scope) {
  137. if (onTypeBinding.isInterface()) {
  138. ContextToken tok = CompilationAndWeavingContext.enteringPhase(CompilationAndWeavingContext.FIXING_SUPER_CALLS, selector);
  139. InterSuperFixerVisitor v =
  140. new InterSuperFixerVisitor(this,
  141. EclipseFactory.fromScopeLookupEnvironment(scope), scope);
  142. this.traverse(v, scope);
  143. CompilationAndWeavingContext.leavingPhase(tok);
  144. }
  145. }
  146. /**
  147. * Called from AspectDeclarations.buildInterTypeAndPerClause
  148. */
  149. public abstract EclipseTypeMunger build(ClassScope classScope);
  150. public void fixSuperCallsInBody() {
  151. ContextToken tok = CompilationAndWeavingContext.enteringPhase(CompilationAndWeavingContext.FIXING_SUPER_CALLS_IN_ITDS, selector);
  152. SuperFixerVisitor v = new SuperFixerVisitor(this, onTypeBinding);
  153. this.traverse(v, (ClassScope)null);
  154. munger.setSuperMethodsCalled(v.superMethodsCalled);
  155. CompilationAndWeavingContext.leavingPhase(tok);
  156. }
  157. protected void resolveOnType(ClassScope classScope) {
  158. checkSpec();
  159. if (onType==null) return; // error reported elsewhere.
  160. // If they did supply a parameterized single type reference, we need to do
  161. // some extra checks...
  162. if (onType instanceof ParameterizedSingleTypeReference || onType instanceof ParameterizedQualifiedTypeReference) {
  163. resolveTypeParametersForITDOnGenericType(classScope);
  164. } else {
  165. onTypeBinding = (ReferenceBinding)onType.getTypeBindingPublic(classScope);
  166. if (!onTypeBinding.isValidBinding()) {
  167. classScope.problemReporter().invalidType(onType, onTypeBinding);
  168. ignoreFurtherInvestigation = true;
  169. }
  170. if (onTypeBinding.isParameterizedType()) {
  171. // might be OK... pr132349
  172. ParameterizedTypeBinding ptb = (ParameterizedTypeBinding)onTypeBinding;
  173. if (ptb.isNestedType()) {
  174. if (ptb.typeVariables()==null || ptb.typeVariables().length==0) {
  175. if (ptb.enclosingType().isRawType()) onTypeBinding = ptb.type;
  176. }
  177. }
  178. }
  179. }
  180. }
  181. /**
  182. * Transform the parameterized type binding (e.g. SomeType<A,B,C>) to a
  183. * real type (e.g. SomeType). The only kind of parameterization allowed
  184. * is with type variables and those are references to type variables on
  185. * the target type. Once we have worked out the base generic type intended
  186. * then we do lots of checks to verify the declaration was well formed.
  187. */
  188. private void resolveTypeParametersForITDOnGenericType(ClassScope classScope) {
  189. // Collapse the parameterized reference to its generic type
  190. if (onType instanceof ParameterizedSingleTypeReference) {
  191. ParameterizedSingleTypeReference pref = (ParameterizedSingleTypeReference) onType;
  192. long pos = (((long)pref.sourceStart) << 32) | pref.sourceEnd;
  193. onType = new SingleTypeReference(pref.token,pos);
  194. } else {
  195. ParameterizedQualifiedTypeReference pref = (ParameterizedQualifiedTypeReference) onType;
  196. long pos = (((long)pref.sourceStart) << 32) | pref.sourceEnd;
  197. onType = new QualifiedTypeReference(pref.tokens,new long[]{pos});
  198. }
  199. onTypeBinding = (ReferenceBinding)onType.getTypeBindingPublic(classScope);
  200. if (!onTypeBinding.isValidBinding()) {
  201. classScope.problemReporter().invalidType(onType, onTypeBinding);
  202. ignoreFurtherInvestigation = true;
  203. }
  204. if (onTypeBinding.isRawType()) {
  205. onTypeBinding = ((RawTypeBinding)onTypeBinding).type;
  206. }
  207. int aliasCount = (typeVariableAliases==null?0:typeVariableAliases.size());
  208. // Cannot specify a parameterized target type for the ITD if the target
  209. // type is not generic.
  210. if (aliasCount!=0 && !onTypeBinding.isGenericType()) {
  211. scope.problemReporter().signalError(sourceStart,sourceEnd,
  212. "Type parameters can not be specified in the ITD target type - the target type "+onTypeBinding.debugName()+" is not generic.");
  213. ignoreFurtherInvestigation = true;
  214. return;
  215. }
  216. // Check they have supplied the right number of type parameters on the ITD target type
  217. if (aliasCount>0) {
  218. if (onTypeBinding.typeVariables().length != aliasCount) { // typeParameters.length) { phantom contains the fake ones from the ontype, typeparameters will also include extra things if it is a generic method
  219. scope.problemReporter().signalError(sourceStart, sourceEnd,
  220. "Incorrect number of type parameters supplied. The generic type "+onTypeBinding.debugName()+" has "+
  221. onTypeBinding.typeVariables().length+" type parameters, not "+aliasCount+".");
  222. ignoreFurtherInvestigation = true;
  223. return;
  224. }
  225. }
  226. // check if they used stupid names for type variables
  227. if (aliasCount>0) {
  228. for (int i = 0; i < aliasCount; i++) {
  229. String array_element = (String)typeVariableAliases.get(i);
  230. SingleTypeReference str = new SingleTypeReference(array_element.toCharArray(),0);
  231. TypeBinding tb = str.getTypeBindingPublic(classScope);
  232. if (tb!=null && !(tb instanceof ProblemReferenceBinding)) {// && !(tb instanceof TypeVariableBinding)) {
  233. scope.problemReporter().signalError(sourceStart,sourceEnd,
  234. "Intertype declarations can only be made on the generic type, not on a parameterized type. The name '"+
  235. array_element+"' cannot be used as a type parameter, since it refers to a real type.");
  236. ignoreFurtherInvestigation = true;
  237. return;
  238. }
  239. }
  240. }
  241. // TypeVariableBinding[] tVarsInGenericType = onTypeBinding.typeVariables();
  242. // typeVariableAliases = new ArrayList(); /* Name>GenericTypeVariablePosition */ // FIXME ASC DONT THINK WE NEED TO BUILD IT HERE AS WELL...
  243. // TypeReference[] targs = pref.typeArguments;
  244. // if (targs!=null) {
  245. // for (int i = 0; i < targs.length; i++) {
  246. // TypeReference tref = targs[i];
  247. // typeVariableAliases.add(CharOperation.toString(tref.getTypeName()));//tVarsInGenericType[i]);
  248. // }
  249. // }
  250. }
  251. protected void checkSpec() {
  252. if (Modifier.isProtected(declaredModifiers)) {
  253. scope.problemReporter().signalError(sourceStart, sourceEnd,
  254. "protected inter-type declarations are not allowed");
  255. ignoreFurtherInvestigation = true;
  256. }
  257. }
  258. protected List makeEffectiveSignatureAttribute(
  259. ResolvedMember sig,
  260. Shadow.Kind kind,
  261. boolean weaveBody)
  262. {
  263. List l = new ArrayList(1);
  264. l.add(new EclipseAttributeAdapter(
  265. new AjAttribute.EffectiveSignatureAttribute(sig, kind, weaveBody)));
  266. return l;
  267. }
  268. protected void setMunger(ResolvedTypeMunger munger) {
  269. munger.getSignature().setPosition(sourceStart, sourceEnd);
  270. munger.getSignature().setSourceContext(new EclipseSourceContext(compilationResult));
  271. this.munger = munger;
  272. }
  273. protected int generateInfoAttributes(ClassFile classFile) {
  274. List l;
  275. Shadow.Kind kind = getShadowKindForBody();
  276. if (kind != null) {
  277. l = makeEffectiveSignatureAttribute(munger.getSignature(), kind, true);
  278. } else {
  279. l = new ArrayList(0);
  280. }
  281. addDeclarationStartLineAttribute(l,classFile);
  282. return classFile.generateMethodInfoAttributes(binding, l);
  283. }
  284. protected abstract Shadow.Kind getShadowKindForBody();
  285. public ResolvedMember getSignature() {
  286. if (munger==null) return null; // Can be null in an erroneous program I think
  287. return munger.getSignature();
  288. }
  289. public char[] getDeclaredSelector() {
  290. return declaredSelector;
  291. }
  292. public TypeReference getOnType() {
  293. return onType;
  294. }
  295. /**
  296. * Create the list of aliases based on what was supplied as parameters for the ontype.
  297. * For example, if the declaration is 'List<N> SomeType<N>.foo' then the alias list
  298. * will simply contain 'N' and 'N' will mean 'the first type variable declared for
  299. * type SomeType'
  300. */
  301. public void determineTypeVariableAliases() {
  302. if (onType!=null) {
  303. // TODO loses distinction about which level the type variables are at... is that a problem?
  304. if (onType instanceof ParameterizedSingleTypeReference) {
  305. ParameterizedSingleTypeReference paramRef = (ParameterizedSingleTypeReference) onType;
  306. TypeReference[] rb = paramRef.typeArguments;
  307. typeVariableAliases = new ArrayList();
  308. for (int i = 0; i < rb.length; i++) {
  309. typeVariableAliases.add(CharOperation.toString(rb[i].getTypeName()));
  310. }
  311. } else if (onType instanceof ParameterizedQualifiedTypeReference) {
  312. ParameterizedQualifiedTypeReference paramRef = (ParameterizedQualifiedTypeReference) onType;
  313. typeVariableAliases = new ArrayList();
  314. for (int j = 0; j < paramRef.typeArguments.length; j++) {
  315. TypeReference[] rb = paramRef.typeArguments[j];
  316. for (int i = 0; rb!=null && i < rb.length; i++) {
  317. typeVariableAliases.add(CharOperation.toString(rb[i].getTypeName()));
  318. }
  319. }
  320. }
  321. }
  322. }
  323. /**
  324. * Called just before the compiler is going to start resolving elements of a declaration, this method
  325. * adds an intertypescope between the methodscope and classscope so that elements of the type targetted
  326. * by the ITD can be resolved. For example, if type variables are referred to in the ontype for the ITD,
  327. * they have to be resolved against the ontype, not the aspect containing the ITD.
  328. */
  329. public void ensureScopeSetup() {
  330. if (scopeSetup) return; // don't do it again
  331. MethodScope scope = this.scope;
  332. TypeReference ot = onType;
  333. ReferenceBinding rb = null;
  334. if (ot instanceof ParameterizedQualifiedTypeReference) { // pr132349
  335. ParameterizedQualifiedTypeReference pref = (ParameterizedQualifiedTypeReference) ot;
  336. if (pref.typeArguments!=null && pref.typeArguments.length!=0) {
  337. boolean usingNonTypeVariableInITD = false;
  338. // Check if any of them are not type variables
  339. for (int i = 0; i < pref.typeArguments.length; i++) {
  340. TypeReference[] refs = pref.typeArguments[i];
  341. for (int j = 0; refs!=null && j < refs.length; j++) {
  342. TypeBinding tb = refs[j].getTypeBindingPublic(scope.parent);
  343. if (!tb.isTypeVariable() && !(tb instanceof ProblemReferenceBinding)) {
  344. usingNonTypeVariableInITD = true;
  345. }
  346. }
  347. }
  348. if (usingNonTypeVariableInITD) {
  349. scope.problemReporter().signalError(sourceStart,sourceEnd,
  350. "Cannot make inter-type declarations on parameterized types");
  351. // to prevent disgusting cascading errors after this problem - lets null out what leads to them (pr105038)
  352. this.arguments=null;
  353. this.returnType=new SingleTypeReference(TypeReference.VOID,0L);
  354. this.ignoreFurtherInvestigation=true;
  355. ReferenceBinding closestMatch = null;
  356. rb = new ProblemReferenceBinding(ot.getParameterizedTypeName(),closestMatch,0);
  357. onType=null;
  358. }
  359. }
  360. }
  361. // Work out the real base type
  362. if (ot instanceof ParameterizedSingleTypeReference) {
  363. ParameterizedSingleTypeReference pref = (ParameterizedSingleTypeReference) ot;
  364. long pos = (((long)pref.sourceStart) << 32) | pref.sourceEnd;
  365. ot = new SingleTypeReference(pref.token,pos);
  366. } else if (ot instanceof ParameterizedQualifiedTypeReference) {
  367. ParameterizedQualifiedTypeReference pref = (ParameterizedQualifiedTypeReference) ot;
  368. long pos = (((long)pref.sourceStart) << 32) | pref.sourceEnd;
  369. ot = new QualifiedTypeReference(pref.tokens,new long[]{pos});//SingleTypeReference(pref.Quatoken,pos);
  370. }
  371. // resolve it
  372. if (rb==null) {
  373. rb = (ReferenceBinding)ot.getTypeBindingPublic(scope.parent);
  374. }
  375. // pr203646 - if we have ended up with the raw type, get back to the underlying generic one.
  376. if (rb.isRawType() && rb.isMemberType()) {
  377. // if the real target type used a type variable alias then we can do this OK, but need to switch things around, we want the generic type
  378. rb = ((RawTypeBinding)rb).type;
  379. }
  380. if (rb instanceof TypeVariableBinding) {
  381. scope.problemReporter().signalError(sourceStart,sourceEnd,
  382. "Cannot make inter-type declarations on type variables, use an interface and declare parents");
  383. // to prevent disgusting cascading errors after this problem - lets null out what leads to them (pr105038)
  384. this.arguments=null;
  385. this.returnType=new SingleTypeReference(TypeReference.VOID,0L);
  386. this.ignoreFurtherInvestigation=true;
  387. ReferenceBinding closestMatch = null;
  388. if (((TypeVariableBinding)rb).firstBound!=null) {
  389. closestMatch = ((TypeVariableBinding)rb).firstBound.enclosingType();
  390. }
  391. rb = new ProblemReferenceBinding(rb.compoundName,closestMatch,0);
  392. }
  393. // if resolution failed, give up - someone else is going to report an error
  394. if (rb instanceof ProblemReferenceBinding) return;
  395. interTypeScope = new InterTypeScope(scope.parent, rb, typeVariableAliases);
  396. // FIXME asc verify the choice of lines here...
  397. // Two versions of this next line.
  398. // First one tricks the JDT variable processing code so that it won't complain if
  399. // you refer to a type variable from a static ITD - it *is* a problem and it *will* be caught, but later and
  400. // by the AJDT code so we can put out a much nicer message.
  401. scope.isStatic = (typeVariableAliases!=null?false:Modifier.isStatic(declaredModifiers));
  402. // this is the original version in case tricking the JDT causes grief (if you reinstate this variant, you
  403. // will need to change the expected messages output for some of the generic ITD tests)
  404. // scope.isStatic = Modifier.isStatic(declaredModifiers);
  405. scope.parent = interTypeScope;
  406. scopeSetup = true;
  407. }
  408. }