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.

EclipseTypeMunger.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.lang.reflect.Modifier;
  14. import java.util.Map;
  15. import org.aspectj.bridge.ISourceLocation;
  16. import org.aspectj.org.eclipse.jdt.core.compiler.CharOperation;
  17. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
  18. import org.aspectj.org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding;
  20. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
  25. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TagBits;
  26. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
  27. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.UnresolvedReferenceBinding;
  28. import org.aspectj.weaver.ConcreteTypeMunger;
  29. import org.aspectj.weaver.NewConstructorTypeMunger;
  30. import org.aspectj.weaver.NewFieldTypeMunger;
  31. import org.aspectj.weaver.NewMemberClassTypeMunger;
  32. import org.aspectj.weaver.NewMethodTypeMunger;
  33. import org.aspectj.weaver.ResolvedMember;
  34. import org.aspectj.weaver.ResolvedType;
  35. import org.aspectj.weaver.ResolvedTypeMunger;
  36. import org.aspectj.weaver.UnresolvedType;
  37. import org.aspectj.weaver.World;
  38. public class EclipseTypeMunger extends ConcreteTypeMunger {
  39. private ResolvedType targetTypeX;
  40. // protected ReferenceBinding targetBinding = null;
  41. public AbstractMethodDeclaration sourceMethod;
  42. private EclipseFactory world;
  43. private ISourceLocation sourceLocation;
  44. public EclipseTypeMunger(EclipseFactory world, ResolvedTypeMunger munger, ResolvedType aspectType,
  45. AbstractMethodDeclaration sourceMethod) {
  46. super(munger, aspectType);
  47. this.world = world;
  48. this.sourceMethod = sourceMethod;
  49. // A null sourceMethod can be because of binary weaving
  50. if (sourceMethod != null) {
  51. this.sourceLocation = new EclipseSourceLocation(sourceMethod.compilationResult, sourceMethod.sourceStart,
  52. sourceMethod.sourceEnd);
  53. // Piece of magic that tells type mungers where they came from.
  54. // Won't be persisted unless ResolvedTypeMunger.persistSourceLocation is true.
  55. munger.setSourceLocation(sourceLocation);
  56. // use a different ctor for type level inter type decls I think
  57. // } else {
  58. // this.sourceLocation = aspectType.getSourceLocation();
  59. // munger.setSourceLocation(sourceLocation);
  60. }
  61. // was
  62. targetTypeX = munger.getDeclaringType().resolve(world.getWorld());
  63. // targetTypeX = munger.getSignature().getDeclaringType().resolve(world.getWorld());
  64. // AMC, needed until generic and raw have distinct sigs...
  65. if (targetTypeX.isParameterizedType() || targetTypeX.isRawType()) {
  66. targetTypeX = targetTypeX.getGenericType();
  67. // targetBinding = (ReferenceBinding)world.makeTypeBinding(targetTypeX);
  68. }
  69. }
  70. public static boolean supportsKind(ResolvedTypeMunger.Kind kind) {
  71. return kind == ResolvedTypeMunger.Field || kind == ResolvedTypeMunger.Method || kind == ResolvedTypeMunger.Constructor
  72. || kind == ResolvedTypeMunger.InnerClass;
  73. }
  74. @Override
  75. public String toString() {
  76. return "(EclipseTypeMunger " + getMunger() + ")";
  77. }
  78. /**
  79. * Modifies signatures of a TypeBinding through its ClassScope, i.e. adds Method|FieldBindings, plays with inheritance, ...
  80. */
  81. public boolean munge(SourceTypeBinding sourceType, ResolvedType onType) {
  82. ResolvedType rt = onType;
  83. if (rt.isRawType() || rt.isParameterizedType()) {
  84. rt = rt.getGenericType();
  85. }
  86. boolean isExactTargetType = rt.equals(targetTypeX);
  87. if (!isExactTargetType) {
  88. // might be the topmost implementor of an interface we care about
  89. if (munger.getKind() != ResolvedTypeMunger.Method) {
  90. return false;
  91. }
  92. if (onType.isInterface()) {
  93. return false;
  94. }
  95. if (!munger.needsAccessToTopmostImplementor()) {
  96. return false;
  97. }
  98. // so we do need access, and this type could be it...
  99. if (!onType.isTopmostImplementor(targetTypeX)) {
  100. return false;
  101. }
  102. // we are the topmost implementor of an interface type that needs munging
  103. // but we only care about public methods here (we only do this at all to
  104. // drive the JDT MethodVerifier correctly)
  105. if (!Modifier.isPublic(munger.getSignature().getModifiers())) {
  106. return false;
  107. }
  108. }
  109. // System.out.println("munging: " + sourceType);
  110. // System.out.println("match: " + world.fromEclipse(sourceType) +
  111. // " with " + targetTypeX);
  112. if (munger.getKind() == ResolvedTypeMunger.Field) {
  113. mungeNewField(sourceType, (NewFieldTypeMunger) munger);
  114. } else if (munger.getKind() == ResolvedTypeMunger.Method) {
  115. return mungeNewMethod(sourceType, onType, (NewMethodTypeMunger) munger, isExactTargetType);
  116. } else if (munger.getKind() == ResolvedTypeMunger.Constructor) {
  117. mungeNewConstructor(sourceType, (NewConstructorTypeMunger) munger);
  118. } else if (munger.getKind() == ResolvedTypeMunger.InnerClass) {
  119. mungeNewInnerClass(sourceType, onType, (NewMemberClassTypeMunger) munger, isExactTargetType);
  120. } else {
  121. throw new RuntimeException("unimplemented: " + munger.getKind());
  122. }
  123. return true;
  124. }
  125. private boolean mungeNewMethod(SourceTypeBinding sourceType, ResolvedType onType, NewMethodTypeMunger munger,
  126. boolean isExactTargetType) {
  127. InterTypeMethodBinding binding = new InterTypeMethodBinding(world, munger, aspectType, sourceMethod);
  128. if (!isExactTargetType) {
  129. // we're munging an interface ITD onto a topmost implementor
  130. ResolvedMember existingMember = onType.lookupMemberIncludingITDsOnInterfaces(getSignature());
  131. if (existingMember != null) {
  132. // already have an implementation, so don't do anything
  133. if (onType == existingMember.getDeclaringType() && Modifier.isFinal(munger.getSignature().getModifiers())) {
  134. // final modifier on default implementation is taken to mean that
  135. // no-one else can provide an implementation
  136. if (!(sourceType instanceof BinaryTypeBinding)) {
  137. // If sourceType is a BinaryTypeBinding, this can indicate we are re-applying the ITDs to the target
  138. // as we 'pull it in' to resolve something. This means the clash here is with itself ! So if the ITD
  139. // was final when initially added to the target this error logic will trigger. We can't easily
  140. // identify it was added via ITD, so I'm going to make this quick change to say avoid this error for
  141. // BinaryTypeBindings
  142. CompilationUnitScope cuScope = sourceType.scope.compilationUnitScope();
  143. MethodBinding offendingBinding = sourceType.getExactMethod(binding.selector, binding.parameters, cuScope);
  144. sourceType.scope.problemReporter().finalMethodCannotBeOverridden(offendingBinding, binding);
  145. }
  146. }
  147. // so that we find methods from our superinterfaces later on...
  148. findOrCreateInterTypeMemberFinder(sourceType);
  149. return false;
  150. }
  151. }
  152. // retain *only* the visibility modifiers and abstract when putting methods on an interface...
  153. if (sourceType.isInterface()) {
  154. boolean isAbstract = (binding.modifiers & ClassFileConstants.AccAbstract) != 0;
  155. binding.modifiers = (binding.modifiers & (ClassFileConstants.AccPublic | ClassFileConstants.AccProtected | ClassFileConstants.AccPrivate));
  156. if (isAbstract) {
  157. binding.modifiers |= ClassFileConstants.AccAbstract;
  158. }
  159. }
  160. if (munger.getSignature().isVarargsMethod()) {
  161. binding.modifiers |= ClassFileConstants.AccVarargs;
  162. }
  163. findOrCreateInterTypeMemberFinder(sourceType).addInterTypeMethod(binding);
  164. return true;
  165. }
  166. // very similar to code in AspectDeclaration
  167. private boolean mungeNewInnerClass(SourceTypeBinding sourceType, ResolvedType onType, NewMemberClassTypeMunger munger,
  168. boolean isExactTargetType) {
  169. SourceTypeBinding aspectTypeBinding = (SourceTypeBinding) world.makeTypeBinding(aspectType);
  170. char[] mungerMemberTypeName = ("$" + munger.getMemberTypeName()).toCharArray();
  171. ReferenceBinding innerTypeBinding = null;
  172. for (ReferenceBinding innerType : aspectTypeBinding.memberTypes) {
  173. char[] compounded = CharOperation.concatWith(innerType.compoundName, '.');
  174. if (org.aspectj.org.eclipse.jdt.core.compiler.CharOperation.endsWith(compounded, mungerMemberTypeName)) {
  175. innerTypeBinding = innerType;
  176. break;
  177. }
  178. }
  179. // may be unresolved if the aspect type binding was a BinaryTypeBinding
  180. if (innerTypeBinding instanceof UnresolvedReferenceBinding) {
  181. innerTypeBinding = (ReferenceBinding)BinaryTypeBinding.resolveType(innerTypeBinding, world.getLookupEnvironment(), true);
  182. }
  183. // rb = new InterTypeMemberClassBinding(world, munger, aspectType, aspectTypeBinding, onType, munger.getMemberTypeName(),
  184. // sourceType);
  185. // TODO adjust modifier?
  186. // TODO deal with itd of it onto an interface
  187. findOrCreateInterTypeMemberClassFinder(sourceType).addInterTypeMemberType(innerTypeBinding);
  188. return true;
  189. }
  190. private void mungeNewConstructor(SourceTypeBinding sourceType, NewConstructorTypeMunger munger) {
  191. if (shouldTreatAsPublic()) {
  192. MethodBinding binding = world.makeMethodBinding(munger.getSignature(), munger.getTypeVariableAliases());
  193. findOrCreateInterTypeMemberFinder(sourceType).addInterTypeMethod(binding);
  194. TypeVariableBinding[] typeVariables = binding.typeVariables;
  195. for (int i = 0; i < typeVariables.length; i++) {
  196. TypeVariableBinding tv = typeVariables[i];
  197. String name = new String(tv.sourceName);
  198. TypeVariableBinding[] tv2 = sourceMethod.binding.typeVariables;
  199. for (int j = 0; j < tv2.length; j++) {
  200. if (new String(tv2[j].sourceName).equals(name)) {
  201. typeVariables[i].declaringElement = binding;
  202. }
  203. }
  204. }
  205. for (int i = 0; i < typeVariables.length; i++) {
  206. if (typeVariables[i].declaringElement == null) {
  207. throw new RuntimeException("Declaring element not set");
  208. }
  209. }
  210. // classScope.referenceContext.binding.addMethod(binding);
  211. } else {
  212. InterTypeMethodBinding binding = new InterTypeMethodBinding(world, munger, aspectType, sourceMethod);
  213. findOrCreateInterTypeMemberFinder(sourceType).addInterTypeMethod(binding);
  214. }
  215. }
  216. private void mungeNewField(SourceTypeBinding sourceType, NewFieldTypeMunger munger) {
  217. if (shouldTreatAsPublic() && !targetTypeX.isInterface()) {
  218. FieldBinding binding = world.makeFieldBinding(munger);
  219. findOrCreateInterTypeMemberFinder(sourceType).addInterTypeField(binding);
  220. // classScope.referenceContext.binding.addField(binding);
  221. } else {
  222. InterTypeFieldBinding binding = new InterTypeFieldBinding(world, munger, aspectType, sourceMethod);
  223. InterTypeMemberFinder finder = findOrCreateInterTypeMemberFinder(sourceType);
  224. // Downgrade this field munger if name is already 'claimed'
  225. if (finder.definesField(munger.getSignature().getName())) {
  226. munger.version = NewFieldTypeMunger.VersionOne;
  227. }
  228. finder.addInterTypeField(binding);
  229. }
  230. }
  231. private boolean shouldTreatAsPublic() {
  232. // ??? this is where we could fairly easily choose to treat package-protected
  233. // ??? introductions like public ones when the target type and the aspect
  234. // ??? are in the same package
  235. return Modifier.isPublic(munger.getSignature().getModifiers());
  236. }
  237. private InterTypeMemberFinder findOrCreateInterTypeMemberFinder(SourceTypeBinding sourceType) {
  238. InterTypeMemberFinder finder = (InterTypeMemberFinder) sourceType.memberFinder;
  239. if (finder == null) {
  240. finder = new InterTypeMemberFinder();
  241. sourceType.memberFinder = finder;
  242. finder.sourceTypeBinding = sourceType;
  243. }
  244. return finder;
  245. }
  246. private IntertypeMemberTypeFinder findOrCreateInterTypeMemberClassFinder(SourceTypeBinding sourceType) {
  247. IntertypeMemberTypeFinder finder = (IntertypeMemberTypeFinder) sourceType.typeFinder;
  248. if (finder == null) {
  249. finder = new IntertypeMemberTypeFinder();
  250. sourceType.typeFinder = finder;
  251. finder.targetTypeBinding = sourceType;
  252. sourceType.tagBits &= ~TagBits.HasNoMemberTypes; // ensure it thinks it has one
  253. }
  254. return finder;
  255. }
  256. @Override
  257. public ISourceLocation getSourceLocation() {
  258. return sourceLocation;
  259. }
  260. public void setSourceLocation(ISourceLocation sourceLocation) {
  261. this.sourceLocation = sourceLocation;
  262. }
  263. /**
  264. * @return AbstractMethodDeclaration
  265. */
  266. public AbstractMethodDeclaration getSourceMethod() {
  267. return sourceMethod;
  268. }
  269. @Override
  270. public ConcreteTypeMunger parameterizedFor(ResolvedType target) {
  271. return new EclipseTypeMunger(world, munger.parameterizedFor(target), aspectType, sourceMethod);
  272. }
  273. public ConcreteTypeMunger parameterizeWith(Map<String,UnresolvedType> m, World w) {
  274. return new EclipseTypeMunger(world, munger.parameterizeWith(m, w), aspectType, sourceMethod);
  275. }
  276. }