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.

EclipseFactory.java 49KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * Mik Kersten 2004-07-26 extended to allow overloading of
  12. * hierarchy builder
  13. * ******************************************************************/
  14. package org.aspectj.ajdt.internal.compiler.lookup;
  15. import java.lang.reflect.Modifier;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import org.aspectj.ajdt.internal.compiler.ast.AspectDeclaration;
  21. import org.aspectj.ajdt.internal.compiler.ast.AstUtil;
  22. import org.aspectj.ajdt.internal.core.builder.AjBuildManager;
  23. import org.aspectj.bridge.IMessage.Kind;
  24. import org.aspectj.bridge.ISourceLocation;
  25. import org.aspectj.org.eclipse.jdt.core.Flags;
  26. import org.aspectj.org.eclipse.jdt.core.compiler.CharOperation;
  27. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ASTNode;
  28. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
  29. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
  30. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.EmptyStatement;
  31. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
  32. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Wildcard;
  33. import org.aspectj.org.eclipse.jdt.internal.compiler.impl.Constant;
  34. import org.aspectj.org.eclipse.jdt.internal.compiler.impl.ReferenceContext;
  35. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
  36. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.Binding;
  37. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
  38. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding;
  39. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment;
  40. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
  41. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
  42. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ProblemReasons;
  43. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding;
  44. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding;
  45. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
  46. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.Scope;
  47. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
  48. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SyntheticFieldBinding;
  49. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
  50. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
  51. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.WildcardBinding;
  52. import org.aspectj.weaver.BCException;
  53. import org.aspectj.weaver.BoundedReferenceType;
  54. import org.aspectj.weaver.ConcreteTypeMunger;
  55. import org.aspectj.weaver.IHasPosition;
  56. import org.aspectj.weaver.Member;
  57. import org.aspectj.weaver.MemberKind;
  58. import org.aspectj.weaver.NameMangler;
  59. import org.aspectj.weaver.NewFieldTypeMunger;
  60. import org.aspectj.weaver.NewMethodTypeMunger;
  61. import org.aspectj.weaver.ReferenceType;
  62. import org.aspectj.weaver.ResolvedMember;
  63. import org.aspectj.weaver.ResolvedMemberImpl;
  64. import org.aspectj.weaver.ResolvedType;
  65. import org.aspectj.weaver.ResolvedTypeMunger;
  66. import org.aspectj.weaver.Shadow;
  67. import org.aspectj.weaver.TypeFactory;
  68. import org.aspectj.weaver.TypeVariable;
  69. import org.aspectj.weaver.TypeVariableDeclaringElement;
  70. import org.aspectj.weaver.TypeVariableReference;
  71. import org.aspectj.weaver.UnresolvedType;
  72. import org.aspectj.weaver.UnresolvedType.TypeKind;
  73. import org.aspectj.weaver.UnresolvedTypeVariableReferenceType;
  74. import org.aspectj.weaver.WildcardedUnresolvedType;
  75. import org.aspectj.weaver.World;
  76. import org.aspectj.weaver.patterns.DeclareAnnotation;
  77. import org.aspectj.weaver.patterns.DeclareParents;
  78. /**
  79. * @author Jim Hugunin
  80. */
  81. public class EclipseFactory {
  82. public static boolean DEBUG = false;
  83. public static int debug_mungerCount = -1;
  84. private final AjBuildManager buildManager;
  85. private final LookupEnvironment lookupEnvironment;
  86. private final boolean xSerializableAspects;
  87. private final World world;
  88. public PushinCollector pushinCollector;
  89. public List<ConcreteTypeMunger> finishedTypeMungers = null;
  90. // We can get clashes if we don't treat raw types differently - we end up looking
  91. // up a raw and getting the generic type (pr115788)
  92. private final Map<UnresolvedType, TypeBinding> typexToBinding = new HashMap<>();
  93. private final Map<UnresolvedType, TypeBinding> rawTypeXToBinding = new HashMap<>();
  94. // XXX currently unused
  95. // private Map/*TypeBinding, ResolvedType*/ bindingToResolvedTypeX = new HashMap();
  96. public static EclipseFactory fromLookupEnvironment(LookupEnvironment env) {
  97. AjLookupEnvironment aenv = (AjLookupEnvironment) env;
  98. return aenv.factory;
  99. }
  100. public LookupEnvironment getLookupEnvironment() {
  101. return this.lookupEnvironment;
  102. }
  103. public static EclipseFactory fromScopeLookupEnvironment(Scope scope) {
  104. return fromLookupEnvironment(AstUtil.getCompilationUnitScope(scope).environment);
  105. }
  106. public EclipseFactory(LookupEnvironment lookupEnvironment, AjBuildManager buildManager) {
  107. this.lookupEnvironment = lookupEnvironment;
  108. this.buildManager = buildManager;
  109. this.world = buildManager.getWorld();
  110. this.pushinCollector = PushinCollector.createInstance(this.world);
  111. this.xSerializableAspects = buildManager.buildConfig.isXserializableAspects();
  112. }
  113. public EclipseFactory(LookupEnvironment lookupEnvironment, World world, boolean xSer) {
  114. this.lookupEnvironment = lookupEnvironment;
  115. this.world = world;
  116. this.xSerializableAspects = xSer;
  117. this.pushinCollector = PushinCollector.createInstance(this.world);
  118. this.buildManager = null;
  119. }
  120. public World getWorld() {
  121. return world;
  122. }
  123. public void showMessage(Kind kind, String message, ISourceLocation loc1, ISourceLocation loc2) {
  124. getWorld().showMessage(kind, message, loc1, loc2);
  125. }
  126. public ResolvedType fromEclipse(ReferenceBinding binding) {
  127. if (binding == null) {
  128. return ResolvedType.MISSING;
  129. }
  130. // ??? this seems terribly inefficient
  131. // System.err.println("resolving: " + binding.getClass() + ", name = " + getName(binding));
  132. ResolvedType ret = getWorld().resolve(fromBinding(binding));
  133. // System.err.println(" got: " + ret);
  134. return ret;
  135. }
  136. public ResolvedType fromTypeBindingToRTX(TypeBinding tb) {
  137. if (tb == null) {
  138. return ResolvedType.MISSING;
  139. }
  140. ResolvedType ret = getWorld().resolve(fromBinding(tb));
  141. return ret;
  142. }
  143. public ResolvedType[] fromEclipse(ReferenceBinding[] bindings) {
  144. if (bindings == null) {
  145. return ResolvedType.NONE;
  146. }
  147. int len = bindings.length;
  148. ResolvedType[] ret = new ResolvedType[len];
  149. for (int i = 0; i < len; i++) {
  150. ret[i] = fromEclipse(bindings[i]);
  151. }
  152. return ret;
  153. }
  154. public static String getName(TypeBinding binding) {
  155. if (binding instanceof TypeVariableBinding) {
  156. // The first bound may be null - so default to object?
  157. TypeVariableBinding tvb = (TypeVariableBinding) binding;
  158. if (tvb.firstBound != null) {
  159. return getName(tvb.firstBound);
  160. } else {
  161. return getName(tvb.superclass);
  162. }
  163. }
  164. if (binding instanceof ReferenceBinding) {
  165. return new String(CharOperation.concatWith(((ReferenceBinding) binding).compoundName, '.'));
  166. }
  167. String packageName = new String(binding.qualifiedPackageName());
  168. String className = new String(binding.qualifiedSourceName()).replace('.', '$');
  169. if (packageName.length() > 0) {
  170. className = packageName + "." + className;
  171. }
  172. // XXX doesn't handle arrays correctly (or primitives?)
  173. return new String(className);
  174. }
  175. /**
  176. * Some generics notes:
  177. *
  178. * Andy 6-May-05 We were having trouble with parameterized types in a couple of places - due to TypeVariableBindings. When we
  179. * see a TypeVariableBinding now we default to either the firstBound if it is specified or java.lang.Object. Not sure when/if
  180. * this gets us unstuck? It does mean we forget that it is a type variable when going back the other way from the UnresolvedType
  181. * and that would seem a bad thing - but I've yet to see the reason we need to remember the type variable. Adrian 10-July-05
  182. * When we forget it's a type variable we come unstuck when getting the declared members of a parameterized type - since we
  183. * don't know it's a type variable we can't replace it with the type parameter.
  184. */
  185. // ??? going back and forth between strings and bindings is a waste of cycles
  186. public UnresolvedType fromBinding(TypeBinding binding) {
  187. if (binding instanceof HelperInterfaceBinding) {
  188. return ((HelperInterfaceBinding) binding).getTypeX();
  189. }
  190. if (binding == null || binding.qualifiedSourceName() == null) {
  191. return ResolvedType.MISSING;
  192. }
  193. // first piece of generics support!
  194. if (binding instanceof TypeVariableBinding) {
  195. TypeVariableBinding tb = (TypeVariableBinding) binding;
  196. UnresolvedTypeVariableReferenceType utvrt = (UnresolvedTypeVariableReferenceType) fromTypeVariableBinding(tb);
  197. return utvrt;
  198. }
  199. // handle arrays since the component type may need special treatment too...
  200. if (binding instanceof ArrayBinding) {
  201. ArrayBinding aBinding = (ArrayBinding) binding;
  202. UnresolvedType componentType = fromBinding(aBinding.leafComponentType);
  203. return UnresolvedType.makeArray(componentType, aBinding.dimensions);
  204. }
  205. if (binding instanceof WildcardBinding) {
  206. WildcardBinding eWB = (WildcardBinding) binding;
  207. // Repair the bound
  208. // e.g. If the bound for the wildcard is a typevariable, e.g. '? extends E' then
  209. // the type variable in the unresolvedtype will be correct only in name. In that
  210. // case let's set it correctly based on the one in the eclipse WildcardBinding
  211. UnresolvedType theBound = null;
  212. if (eWB.bound instanceof TypeVariableBinding) {
  213. theBound = fromTypeVariableBinding((TypeVariableBinding) eWB.bound);
  214. } else {
  215. theBound = fromBinding(eWB.bound);
  216. }
  217. // if (eWB.boundKind == WildCard.SUPER) {
  218. //
  219. // }
  220. WildcardedUnresolvedType theType = (WildcardedUnresolvedType) TypeFactory.createTypeFromSignature(CharOperation
  221. .charToString(eWB.genericTypeSignature()));
  222. // if (theType.isGenericWildcard() && theType.isSuper()) theType.setLowerBound(theBound);
  223. // if (theType.isGenericWildcard() && theType.isExtends()) theType.setUpperBound(theBound);
  224. return theType;
  225. }
  226. if (binding instanceof ParameterizedTypeBinding) {
  227. if (binding instanceof RawTypeBinding) {
  228. // special case where no parameters are specified!
  229. return UnresolvedType.forRawTypeName(getName(binding));
  230. }
  231. ParameterizedTypeBinding ptb = (ParameterizedTypeBinding) binding;
  232. UnresolvedType[] arguments = null;
  233. if (ptb.arguments != null) { // null can mean this is an inner type of a Parameterized Type with no bounds of its own
  234. // (pr100227)
  235. arguments = new UnresolvedType[ptb.arguments.length];
  236. for (int i = 0; i < arguments.length; i++) {
  237. arguments[i] = fromBinding(ptb.arguments[i]);
  238. }
  239. }
  240. String baseTypeSignature = null;
  241. ResolvedType baseType = getWorld().resolve(UnresolvedType.forName(getName(binding)), true);
  242. if (!baseType.isMissing()) {
  243. // can legitimately be missing if a bound refers to a type we haven't added to the world yet...
  244. // pr168044 - sometimes (whilst resolving types) we are working with 'half finished' types and so (for example) the
  245. // underlying generic type for a raw type hasnt been set yet
  246. // if (!baseType.isGenericType() && arguments!=null) baseType = baseType.getGenericType();
  247. // pr384398 - secondary testcase in 1.7.1 tests - this needs work as this code
  248. // currently discards the parameterization on the outer type, which is important info
  249. // ReferenceBinding enclosingTypeBinding = ptb.enclosingType();
  250. // if (enclosingTypeBinding!=null) {
  251. // UnresolvedType ttt = fromBinding(enclosingTypeBinding);
  252. // baseTypeSignature = ttt.getSignature();
  253. // baseTypeSignature= baseTypeSignature.substring(0,baseTypeSignature.length()-1);
  254. // baseTypeSignature = baseTypeSignature + "."+new String(ptb.sourceName)+";";
  255. // } else {
  256. baseTypeSignature = baseType.getErasureSignature();
  257. // }
  258. } else {
  259. baseTypeSignature = UnresolvedType.forName(getName(binding)).getSignature();
  260. }
  261. // Create an unresolved parameterized type. We can't create a resolved one as the
  262. // act of resolution here may cause recursion problems since the parameters may
  263. // be type variables that we haven't fixed up yet.
  264. if (arguments == null) {
  265. arguments = UnresolvedType.NONE;
  266. // for pr384398
  267. if (!hasAnyArguments(ptb)) {
  268. return UnresolvedType.forRawTypeName(getName(binding));
  269. }
  270. }
  271. // StringBuffer parameterizedSig = new StringBuffer();
  272. // parameterizedSig.append(ResolvedType.PARAMETERIZED_TYPE_IDENTIFIER);
  273. //
  274. // // String parameterizedSig = new
  275. // StringBuffer().append(ResolvedType.PARAMETERIZED_TYPE_IDENTIFIER).append(CharOperation
  276. // .charToString(binding.genericTypeSignature()).substring(1)).toString();
  277. // return TypeFactory.createUnresolvedParameterizedType(parameterizedSig,baseTypeSignature,arguments);
  278. return TypeFactory.createUnresolvedParameterizedType(baseTypeSignature, arguments);
  279. }
  280. // Convert the source type binding for a generic type into a generic UnresolvedType
  281. // notice we can easily determine the type variables from the eclipse object
  282. // and we can recover the generic signature from it too - so we pass those
  283. // to the forGenericType() method.
  284. if (binding.isGenericType() && !binding.isParameterizedType() && !binding.isRawType()) {
  285. TypeVariableBinding[] tvbs = binding.typeVariables();
  286. TypeVariable[] tVars = new TypeVariable[tvbs.length];
  287. for (int i = 0; i < tvbs.length; i++) {
  288. TypeVariableBinding eclipseV = tvbs[i];
  289. tVars[i] = ((TypeVariableReference) fromTypeVariableBinding(eclipseV)).getTypeVariable();
  290. }
  291. // TODO asc generics - temporary guard....
  292. if (!(binding instanceof SourceTypeBinding)) {
  293. throw new RuntimeException("Cant get the generic sig for " + binding.debugName());
  294. }
  295. return UnresolvedType.forGenericType(getName(binding), tVars,
  296. CharOperation.charToString(((SourceTypeBinding) binding).genericSignature()));
  297. }
  298. // LocalTypeBinding have a name $Local$, we can get the real name by using the signature....
  299. if (binding instanceof LocalTypeBinding) {
  300. LocalTypeBinding ltb = (LocalTypeBinding) binding;
  301. if (ltb.constantPoolName() != null && ltb.constantPoolName().length > 0) {
  302. return UnresolvedType.forSignature(new String(binding.signature()));
  303. } else {
  304. // we're reporting a problem and don't have a resolved name for an
  305. // anonymous local type yet, report the issue on the enclosing type
  306. return UnresolvedType.forSignature(new String(ltb.enclosingType.signature()));
  307. }
  308. }
  309. // was: UnresolvedType.forName(getName(binding));
  310. UnresolvedType ut = UnresolvedType.forSignature(new String(binding.signature()));
  311. return ut;
  312. }
  313. /**
  314. * Search up a parameterized type binding for any arguments at any level.
  315. */
  316. private boolean hasAnyArguments(ParameterizedTypeBinding ptb) {
  317. if (ptb.arguments!=null && ptb.arguments.length>0) {
  318. return true;
  319. }
  320. ReferenceBinding enclosingType = ptb.enclosingType();
  321. if (enclosingType instanceof ParameterizedTypeBinding) {
  322. return hasAnyArguments((ParameterizedTypeBinding)enclosingType);
  323. } else {
  324. return false;
  325. }
  326. }
  327. /**
  328. * Some type variables refer to themselves recursively, this enables us to avoid recursion problems.
  329. */
  330. private static Map<TypeVariableBinding,UnresolvedType> typeVariableBindingsInProgress = new HashMap<>();
  331. /**
  332. * Convert from the eclipse form of type variable (TypeVariableBinding) to the AspectJ form (TypeVariable).
  333. */
  334. private UnresolvedType fromTypeVariableBinding(TypeVariableBinding aTypeVariableBinding) {
  335. // first, check for recursive call to this method for the same tvBinding
  336. if (typeVariableBindingsInProgress.containsKey(aTypeVariableBinding)) {
  337. return typeVariableBindingsInProgress.get(aTypeVariableBinding);
  338. }
  339. // Check if its a type variable binding that we need to recover to an alias...
  340. if (typeVariablesForAliasRecovery != null) {
  341. String aliasname = typeVariablesForAliasRecovery.get(aTypeVariableBinding);
  342. if (aliasname != null) {
  343. UnresolvedTypeVariableReferenceType ret = new UnresolvedTypeVariableReferenceType();
  344. ret.setTypeVariable(new TypeVariable(aliasname));
  345. return ret;
  346. }
  347. }
  348. if (typeVariablesForThisMember.containsKey(new String(aTypeVariableBinding.sourceName))) {
  349. return typeVariablesForThisMember.get(new String(aTypeVariableBinding.sourceName));
  350. }
  351. // Create the UnresolvedTypeVariableReferenceType for the type variable
  352. String name = CharOperation.charToString(aTypeVariableBinding.sourceName());
  353. UnresolvedTypeVariableReferenceType ret = new UnresolvedTypeVariableReferenceType();
  354. typeVariableBindingsInProgress.put(aTypeVariableBinding, ret);
  355. TypeVariable tv = new TypeVariable(name);
  356. ret.setTypeVariable(tv);
  357. // Dont set any bounds here, you'll get in a recursive mess
  358. // TODO -- what about lower bounds??
  359. UnresolvedType superclassType = fromBinding(aTypeVariableBinding.superclass());
  360. UnresolvedType[] superinterfaces = null;
  361. if (aTypeVariableBinding == null || aTypeVariableBinding.superInterfaces == null) {
  362. // sign of another bug that will be reported elsewhere
  363. superinterfaces = UnresolvedType.NONE;
  364. } else {
  365. superinterfaces = new UnresolvedType[aTypeVariableBinding.superInterfaces.length];
  366. for (int i = 0; i < superinterfaces.length; i++) {
  367. superinterfaces[i] = fromBinding(aTypeVariableBinding.superInterfaces[i]);
  368. }
  369. }
  370. tv.setSuperclass(superclassType);
  371. tv.setAdditionalInterfaceBounds(superinterfaces);
  372. tv.setRank(aTypeVariableBinding.rank);
  373. if (aTypeVariableBinding.declaringElement instanceof MethodBinding) {
  374. tv.setDeclaringElementKind(TypeVariable.METHOD);
  375. // tv.setDeclaringElement(fromBinding((MethodBinding)aTypeVariableBinding.declaringElement);
  376. } else {
  377. tv.setDeclaringElementKind(TypeVariable.TYPE);
  378. // // tv.setDeclaringElement(fromBinding(aTypeVariableBinding.declaringElement));
  379. }
  380. if (aTypeVariableBinding.declaringElement instanceof MethodBinding) {
  381. typeVariablesForThisMember.put(new String(aTypeVariableBinding.sourceName), ret);
  382. }
  383. typeVariableBindingsInProgress.remove(aTypeVariableBinding);
  384. return ret;
  385. }
  386. public UnresolvedType[] fromBindings(TypeBinding[] bindings) {
  387. if (bindings == null) {
  388. return UnresolvedType.NONE;
  389. }
  390. int len = bindings.length;
  391. UnresolvedType[] ret = new UnresolvedType[len];
  392. for (int i = 0; i < len; i++) {
  393. ret[i] = fromBinding(bindings[i]);
  394. }
  395. return ret;
  396. }
  397. public static ASTNode astForLocation(IHasPosition location) {
  398. return new EmptyStatement(location.getStart(), location.getEnd());
  399. }
  400. public List<DeclareParents> getDeclareParents() {
  401. return getWorld().getDeclareParents();
  402. }
  403. public List<DeclareAnnotation> getDeclareAnnotationOnTypes() {
  404. return getWorld().getDeclareAnnotationOnTypes();
  405. }
  406. public List<DeclareAnnotation> getDeclareAnnotationOnFields() {
  407. return getWorld().getDeclareAnnotationOnFields();
  408. }
  409. public List<DeclareAnnotation> getDeclareAnnotationOnMethods() {
  410. return getWorld().getDeclareAnnotationOnMethods();
  411. }
  412. public boolean areTypeMungersFinished() {
  413. return finishedTypeMungers != null;
  414. }
  415. public void finishTypeMungers() {
  416. // make sure that type mungers are
  417. List<ConcreteTypeMunger> ret = new ArrayList<>();
  418. List<ConcreteTypeMunger> baseTypeMungers = getWorld().getCrosscuttingMembersSet().getTypeMungers();
  419. // XXX by Andy: why do we mix up the mungers here? it means later we know about two sets
  420. // and the late ones are a subset of the complete set? (see pr114436)
  421. // XXX by Andy removed this line finally, see pr141956
  422. // baseTypeMungers.addAll(getWorld().getCrosscuttingMembersSet().getLateTypeMungers());
  423. debug_mungerCount = baseTypeMungers.size();
  424. for (ConcreteTypeMunger munger : baseTypeMungers) {
  425. EclipseTypeMunger etm = makeEclipseTypeMunger(munger);
  426. if (etm != null) {
  427. if (munger.getMunger().getKind() == ResolvedTypeMunger.InnerClass) {
  428. ret.add(0, etm);
  429. } else {
  430. ret.add(etm);
  431. }
  432. }
  433. }
  434. for (ConcreteTypeMunger ctm: ret) {
  435. ResolvedMember rm = ctm.getSignature();
  436. if (rm!=null) {
  437. rm.resolve(this.getWorld());
  438. }
  439. }
  440. finishedTypeMungers = ret;
  441. }
  442. public EclipseTypeMunger makeEclipseTypeMunger(ConcreteTypeMunger concrete) {
  443. // System.err.println("make munger: " + concrete);
  444. // !!! can't do this if we want incremental to work right
  445. // if (concrete instanceof EclipseTypeMunger) return (EclipseTypeMunger)concrete;
  446. // System.err.println(" was not eclipse");
  447. if (concrete.getMunger() != null && EclipseTypeMunger.supportsKind(concrete.getMunger().getKind())) {
  448. AbstractMethodDeclaration method = null;
  449. if (concrete instanceof EclipseTypeMunger) {
  450. method = ((EclipseTypeMunger) concrete).getSourceMethod();
  451. }
  452. EclipseTypeMunger ret = new EclipseTypeMunger(this, concrete.getMunger(), concrete.getAspectType(), method);
  453. if (ret.getSourceLocation() == null) {
  454. ret.setSourceLocation(concrete.getSourceLocation());
  455. }
  456. return ret;
  457. } else {
  458. return null;
  459. }
  460. }
  461. public List<ConcreteTypeMunger> getTypeMungers() {
  462. // ??? assert finishedTypeMungers != null
  463. return finishedTypeMungers;
  464. }
  465. public ResolvedMemberImpl makeResolvedMember(MethodBinding binding) {
  466. return makeResolvedMember(binding, binding.declaringClass);
  467. }
  468. public ResolvedMemberImpl makeResolvedMember(MethodBinding binding, Shadow.Kind shadowKind) {
  469. MemberKind memberKind = binding.isConstructor() ? Member.CONSTRUCTOR : Member.METHOD;
  470. if (shadowKind == Shadow.AdviceExecution) {
  471. memberKind = Member.ADVICE;
  472. }
  473. return makeResolvedMember(binding, binding.declaringClass, memberKind);
  474. }
  475. /**
  476. * Conversion from a methodbinding (eclipse) to a resolvedmember (aspectj) is now done in the scope of some type variables.
  477. * Before converting the parts of a methodbinding (params, return type) we store the type variables in this structure, then
  478. * should any component of the method binding refer to them, we grab them from the map.
  479. */
  480. private final Map<String,UnresolvedType> typeVariablesForThisMember = new HashMap<>();
  481. /**
  482. * This is a map from typevariablebindings (eclipsey things) to the names the user originally specified in their ITD. For
  483. * example if the target is 'interface I<N extends Number> {}' and the ITD was 'public void I<X>.m(List<X> lxs) {}' then this
  484. * map would contain a pointer from the eclipse type 'N extends Number' to the letter 'X'.
  485. */
  486. private Map<TypeVariableBinding, String> typeVariablesForAliasRecovery;
  487. /**
  488. * Construct a resolvedmember from a methodbinding. The supplied map tells us about any typevariablebindings that replaced
  489. * typevariables whilst the compiler was resolving types - this only happens if it is a generic itd that shares type variables
  490. * with its target type.
  491. */
  492. public ResolvedMemberImpl makeResolvedMemberForITD(MethodBinding binding, TypeBinding declaringType, Map<TypeVariableBinding,String> recoveryAliases) {
  493. ResolvedMemberImpl result = null;
  494. try {
  495. typeVariablesForAliasRecovery = recoveryAliases;
  496. result = makeResolvedMember(binding, declaringType);
  497. } finally {
  498. typeVariablesForAliasRecovery = null;
  499. }
  500. return result;
  501. }
  502. public ResolvedMemberImpl makeResolvedMember(MethodBinding binding, TypeBinding declaringType) {
  503. return makeResolvedMember(binding, declaringType, binding.isConstructor() ? Member.CONSTRUCTOR : Member.METHOD);
  504. }
  505. public ResolvedMemberImpl makeResolvedMember(MethodBinding binding, TypeBinding declaringType, MemberKind memberKind) {
  506. // System.err.println("member for: " + binding + ", " + new String(binding.declaringClass.sourceName));
  507. // Convert the type variables and store them
  508. UnresolvedType[] ajTypeRefs = null;
  509. typeVariablesForThisMember.clear();
  510. // This is the set of type variables available whilst building the resolved member...
  511. if (binding.typeVariables != null) {
  512. ajTypeRefs = new UnresolvedType[binding.typeVariables.length];
  513. for (int i = 0; i < binding.typeVariables.length; i++) {
  514. ajTypeRefs[i] = fromBinding(binding.typeVariables[i]);
  515. typeVariablesForThisMember.put(new String(binding.typeVariables[i].sourceName),/*
  516. * new
  517. * Integer(binding.typeVariables[
  518. * i].rank),
  519. */ajTypeRefs[i]);
  520. }
  521. }
  522. // AMC these next two lines shouldn't be needed once we sort out generic types properly in the world map
  523. ResolvedType realDeclaringType = world.resolve(fromBinding(declaringType));
  524. if (realDeclaringType.isRawType()) {
  525. realDeclaringType = realDeclaringType.getGenericType();
  526. }
  527. ResolvedMemberImpl ret = new EclipseResolvedMember(binding, memberKind, realDeclaringType, binding.modifiers,
  528. fromBinding(binding.returnType), new String(binding.selector), fromBindings(binding.parameters),
  529. fromBindings(binding.thrownExceptions), this);
  530. if (binding.isVarargs()) {
  531. ret.setVarargsMethod();
  532. }
  533. if (ajTypeRefs != null) {
  534. TypeVariable[] tVars = new TypeVariable[ajTypeRefs.length];
  535. for (int i = 0; i < ajTypeRefs.length; i++) {
  536. tVars[i] = ((TypeVariableReference) ajTypeRefs[i]).getTypeVariable();
  537. }
  538. ret.setTypeVariables(tVars);
  539. }
  540. typeVariablesForThisMember.clear();
  541. ret.resolve(world);
  542. return ret;
  543. }
  544. public ResolvedMember makeResolvedMember(FieldBinding binding) {
  545. return makeResolvedMember(binding, binding.declaringClass);
  546. }
  547. public ResolvedMember makeResolvedMember(FieldBinding binding, TypeBinding receiverType) {
  548. // AMC these next two lines shouldn't be needed once we sort out generic types properly in the world map
  549. ResolvedType realDeclaringType = world.resolve(fromBinding(receiverType));
  550. if (realDeclaringType.isRawType()) {
  551. realDeclaringType = realDeclaringType.getGenericType();
  552. }
  553. ResolvedMemberImpl ret = new EclipseResolvedMember(binding, Member.FIELD, realDeclaringType, binding.modifiers,
  554. world.resolve(fromBinding(binding.type)), new String(binding.name), UnresolvedType.NONE);
  555. return ret;
  556. }
  557. public TypeBinding makeTypeBinding(UnresolvedType typeX) {
  558. TypeBinding ret = null;
  559. // looking up type variables can get us into trouble
  560. if (!typeX.isTypeVariableReference() && !isParameterizedWithTypeVariables(typeX)) {
  561. if (typeX.isRawType()) {
  562. ret = rawTypeXToBinding.get(typeX);
  563. } else {
  564. ret = typexToBinding.get(typeX);
  565. }
  566. }
  567. if (ret == null) {
  568. ret = makeTypeBinding1(typeX);
  569. if (ret != null) {// && !(ret instanceof ProblemReferenceBinding)) {
  570. if (!(typeX instanceof BoundedReferenceType) && !(typeX instanceof UnresolvedTypeVariableReferenceType)) {
  571. if (typeX.isRawType()) {
  572. rawTypeXToBinding.put(typeX, ret);
  573. } else {
  574. typexToBinding.put(typeX, ret);
  575. }
  576. }
  577. }
  578. }
  579. return ret;
  580. }
  581. // return true if this is type variables are in the type arguments
  582. private boolean isParameterizedWithTypeVariables(UnresolvedType typeX) {
  583. if (!typeX.isParameterizedType()) {
  584. return false;
  585. }
  586. UnresolvedType[] typeArguments = typeX.getTypeParameters();
  587. if (typeArguments != null) {
  588. for (UnresolvedType typeArgument : typeArguments) {
  589. if (typeArgument.isTypeVariableReference()) {
  590. return true;
  591. }
  592. }
  593. }
  594. return false;
  595. }
  596. // When converting a parameterized type from our world to the eclipse world, these get set so that
  597. // resolution of the type parameters may known in what context it is occurring (pr114744)
  598. private ReferenceBinding baseTypeForParameterizedType;
  599. private int indexOfTypeParameterBeingConverted;
  600. private TypeBinding makeTypeBinding1(UnresolvedType typeX) {
  601. if (typeX.isPrimitiveType()) {
  602. if (typeX.equals(UnresolvedType.BOOLEAN)) {
  603. return TypeBinding.BOOLEAN;
  604. }
  605. if (typeX.equals(UnresolvedType.BYTE)) {
  606. return TypeBinding.BYTE;
  607. }
  608. if (typeX.equals(UnresolvedType.CHAR)) {
  609. return TypeBinding.CHAR;
  610. }
  611. if (typeX.equals(UnresolvedType.DOUBLE)) {
  612. return TypeBinding.DOUBLE;
  613. }
  614. if (typeX.equals(UnresolvedType.FLOAT)) {
  615. return TypeBinding.FLOAT;
  616. }
  617. if (typeX.equals(UnresolvedType.INT)) {
  618. return TypeBinding.INT;
  619. }
  620. if (typeX.equals(UnresolvedType.LONG)) {
  621. return TypeBinding.LONG;
  622. }
  623. if (typeX.equals(UnresolvedType.SHORT)) {
  624. return TypeBinding.SHORT;
  625. }
  626. if (typeX.equals(UnresolvedType.VOID)) {
  627. return TypeBinding.VOID;
  628. }
  629. throw new RuntimeException("weird primitive type " + typeX);
  630. } else if (typeX.isArray()) {
  631. int dim = 0;
  632. while (typeX.isArray()) {
  633. dim++;
  634. typeX = typeX.getComponentType();
  635. }
  636. return lookupEnvironment.createArrayType(makeTypeBinding(typeX), dim);
  637. } else if (typeX.isParameterizedType()) {
  638. // Converting back to a binding from a UnresolvedType
  639. UnresolvedType[] typeParameters = typeX.getTypeParameters();
  640. ReferenceBinding baseTypeBinding = lookupBinding(typeX.getBaseName());
  641. TypeBinding[] argumentBindings = new TypeBinding[typeParameters.length];
  642. baseTypeForParameterizedType = baseTypeBinding;
  643. for (int i = 0; i < argumentBindings.length; i++) {
  644. indexOfTypeParameterBeingConverted = i;
  645. argumentBindings[i] = makeTypeBinding(typeParameters[i]);
  646. }
  647. indexOfTypeParameterBeingConverted = 0;
  648. baseTypeForParameterizedType = null;
  649. ParameterizedTypeBinding ptb = lookupEnvironment.createParameterizedType(baseTypeBinding, argumentBindings,
  650. baseTypeBinding.enclosingType());
  651. return ptb;
  652. } else if (typeX.isTypeVariableReference()) {
  653. // return makeTypeVariableBinding((TypeVariableReference)typeX);
  654. return makeTypeVariableBindingFromAJTypeVariable(((TypeVariableReference) typeX).getTypeVariable());
  655. } else if (typeX.isRawType()) {
  656. ReferenceBinding baseTypeBinding = lookupBinding(typeX.getBaseName());
  657. RawTypeBinding rtb = lookupEnvironment.createRawType(baseTypeBinding, baseTypeBinding.enclosingType());
  658. return rtb;
  659. } else if (typeX.isGenericWildcard()) {
  660. if (typeX instanceof WildcardedUnresolvedType) {
  661. WildcardedUnresolvedType wut = (WildcardedUnresolvedType) typeX;
  662. int boundkind = Wildcard.UNBOUND;
  663. TypeBinding bound = null;
  664. if (wut.isExtends()) {
  665. boundkind = Wildcard.EXTENDS;
  666. bound = makeTypeBinding(wut.getUpperBound());
  667. } else if (wut.isSuper()) {
  668. boundkind = Wildcard.SUPER;
  669. bound = makeTypeBinding(wut.getLowerBound());
  670. }
  671. TypeBinding[] otherBounds = null;
  672. // TODO 2 ought to support extra bounds for WildcardUnresolvedType
  673. // if (wut.getAdditionalBounds()!=null && wut.getAdditionalBounds().length!=0) otherBounds =
  674. // makeTypeBindings(wut.getAdditionalBounds());
  675. WildcardBinding wb = lookupEnvironment.createWildcard(baseTypeForParameterizedType,
  676. indexOfTypeParameterBeingConverted, bound, otherBounds, boundkind);
  677. return wb;
  678. } else if (typeX instanceof BoundedReferenceType) {
  679. // translate from boundedreferencetype to WildcardBinding
  680. BoundedReferenceType brt = (BoundedReferenceType) typeX;
  681. // Work out 'kind' for the WildcardBinding
  682. int boundkind = Wildcard.UNBOUND;
  683. TypeBinding bound = null;
  684. if (brt.isExtends()) {
  685. boundkind = Wildcard.EXTENDS;
  686. bound = makeTypeBinding(brt.getUpperBound());
  687. } else if (brt.isSuper()) {
  688. boundkind = Wildcard.SUPER;
  689. bound = makeTypeBinding(brt.getLowerBound());
  690. }
  691. TypeBinding[] otherBounds = null;
  692. if (brt.getAdditionalBounds() != null && brt.getAdditionalBounds().length != 0) {
  693. otherBounds = makeTypeBindings(brt.getAdditionalBounds());
  694. }
  695. WildcardBinding wb = lookupEnvironment.createWildcard(baseTypeForParameterizedType,
  696. indexOfTypeParameterBeingConverted, bound, otherBounds, boundkind);
  697. return wb;
  698. } else {
  699. throw new BCException("This type " + typeX + " (class " + typeX.getClass().getName()
  700. + ") should not be claiming to be a wildcard!");
  701. }
  702. } else {
  703. return lookupBinding(typeX.getName());
  704. }
  705. }
  706. private ReferenceBinding lookupBinding(String sname) {
  707. char[][] name = CharOperation.splitOn('.', sname.toCharArray());
  708. ReferenceBinding rb = lookupEnvironment.getType(name);
  709. if (rb == null && !sname.equals(UnresolvedType.MISSING_NAME)) {
  710. return new ProblemReferenceBinding(name, null, ProblemReasons.NotFound);
  711. }
  712. return rb;
  713. }
  714. public TypeBinding[] makeTypeBindings(UnresolvedType[] types) {
  715. int len = types.length;
  716. TypeBinding[] ret = new TypeBinding[len];
  717. for (int i = 0; i < len; i++) {
  718. ret[i] = makeTypeBinding(types[i]);
  719. }
  720. return ret;
  721. }
  722. // just like the code above except it returns an array of ReferenceBindings
  723. private ReferenceBinding[] makeReferenceBindings(UnresolvedType[] types) {
  724. int len = types.length;
  725. ReferenceBinding[] ret = new ReferenceBinding[len];
  726. for (int i = 0; i < len; i++) {
  727. ret[i] = (ReferenceBinding) makeTypeBinding(types[i]);
  728. }
  729. return ret;
  730. }
  731. // field related
  732. public FieldBinding makeFieldBinding(NewFieldTypeMunger nftm) {
  733. return internalMakeFieldBinding(nftm.getSignature(), nftm.getTypeVariableAliases());
  734. }
  735. /**
  736. * Convert a resolvedmember into an eclipse field binding
  737. */
  738. public FieldBinding makeFieldBinding(ResolvedMember member, List<String> aliases) {
  739. return internalMakeFieldBinding(member, aliases);
  740. }
  741. /**
  742. * Convert a resolvedmember into an eclipse field binding
  743. */
  744. public FieldBinding makeFieldBinding(ResolvedMember member) {
  745. return internalMakeFieldBinding(member, null);
  746. }
  747. // OPTIMIZE tidy this up, must be able to optimize for the synthetic case, if we passed in the binding for the declaring type,
  748. // that would make things easier
  749. /**
  750. * Build a new Eclipse SyntheticFieldBinding for an AspectJ ResolvedMember.
  751. */
  752. public SyntheticFieldBinding createSyntheticFieldBinding(SourceTypeBinding owningType, ResolvedMember member) {
  753. SyntheticFieldBinding sfb = new SyntheticFieldBinding(member.getName().toCharArray(),
  754. makeTypeBinding(member.getReturnType()), member.getModifiers() | Flags.AccSynthetic, owningType,
  755. Constant.NotAConstant, -1); // index
  756. // filled in
  757. // later
  758. owningType.addSyntheticField(sfb);
  759. return sfb;
  760. }
  761. /**
  762. * Take a normal AJ member and convert it into an eclipse fieldBinding. Taking into account any aliases that it may include due
  763. * to being a generic itd. Any aliases are put into the typeVariableToBinding map so that they will be substituted as
  764. * appropriate in the returned fieldbinding.
  765. */
  766. public FieldBinding internalMakeFieldBinding(ResolvedMember member, List<String> aliases) {
  767. typeVariableToTypeBinding.clear();
  768. ReferenceBinding declaringType = (ReferenceBinding) makeTypeBinding(member.getDeclaringType());
  769. // If there are aliases, place them in the map
  770. if (aliases != null && aliases.size() > 0 && declaringType.typeVariables() != null
  771. && declaringType.typeVariables().length != 0) {
  772. int i = 0;
  773. for (String element: aliases) {
  774. // for (Iterator iter = aliases.iterator(); iter.hasNext();) {
  775. // String element = (String) iter.next();
  776. typeVariableToTypeBinding.put(element, declaringType.typeVariables()[i++]);
  777. }
  778. }
  779. currentType = declaringType;
  780. FieldBinding fb = null;
  781. if (member.getName().startsWith(NameMangler.PREFIX)) {
  782. fb = new SyntheticFieldBinding(member.getName().toCharArray(), makeTypeBinding(member.getReturnType()),
  783. member.getModifiers() | Flags.AccSynthetic, currentType, Constant.NotAConstant, -1); // index filled in later
  784. } else {
  785. fb = new FieldBinding(member.getName().toCharArray(), makeTypeBinding(member.getReturnType()), member.getModifiers(),
  786. currentType, Constant.NotAConstant);
  787. }
  788. typeVariableToTypeBinding.clear();
  789. currentType = null;
  790. if (member.getName().startsWith(NameMangler.PREFIX)) {
  791. fb.modifiers |= Flags.AccSynthetic;
  792. }
  793. return fb;
  794. }
  795. private ReferenceBinding currentType = null;
  796. // method binding related
  797. public MethodBinding makeMethodBinding(NewMethodTypeMunger nmtm) {
  798. return internalMakeMethodBinding(nmtm.getSignature(), nmtm.getTypeVariableAliases());
  799. }
  800. /**
  801. * Convert a resolvedmember into an eclipse method binding.
  802. */
  803. public MethodBinding makeMethodBinding(ResolvedMember member, List<String> aliases) {
  804. return internalMakeMethodBinding(member, aliases);
  805. }
  806. /**
  807. * Creates a method binding for a resolvedmember taking into account type variable aliases - this variant can take an
  808. * aliasTargetType and should be used when the alias target type cannot be retrieved from the resolvedmember.
  809. */
  810. public MethodBinding makeMethodBinding(ResolvedMember member, List aliases, UnresolvedType aliasTargetType) {
  811. return internalMakeMethodBinding(member, aliases, aliasTargetType);
  812. }
  813. /**
  814. * Convert a resolvedmember into an eclipse method binding.
  815. */
  816. public MethodBinding makeMethodBinding(ResolvedMember member) {
  817. return internalMakeMethodBinding(member, null); // there are no aliases
  818. }
  819. public MethodBinding internalMakeMethodBinding(ResolvedMember member, List<String> aliases) {
  820. return internalMakeMethodBinding(member, aliases, member.getDeclaringType());
  821. }
  822. /**
  823. * Take a normal AJ member and convert it into an eclipse methodBinding. Taking into account any aliases that it may include due
  824. * to being a generic ITD. Any aliases are put into the typeVariableToBinding map so that they will be substituted as
  825. * appropriate in the returned methodbinding
  826. */
  827. public MethodBinding internalMakeMethodBinding(ResolvedMember member, List<String> aliases, UnresolvedType aliasTargetType) {
  828. typeVariableToTypeBinding.clear();
  829. TypeVariableBinding[] tvbs = null;
  830. if (member.getTypeVariables() != null) {
  831. if (member.getTypeVariables().length == 0) {
  832. tvbs = Binding.NO_TYPE_VARIABLES;
  833. } else {
  834. tvbs = makeTypeVariableBindingsFromAJTypeVariables(member.getTypeVariables());
  835. // QQQ do we need to bother fixing up the declaring element here?
  836. }
  837. }
  838. ReferenceBinding declaringType = (ReferenceBinding) makeTypeBinding(member.getDeclaringType());
  839. // If there are aliases, place them in the map
  840. if (aliases != null && aliases.size() != 0
  841. // Not sure what this check is trying to check for?
  842. // In latest JDT (2-Feb-2018) there seem to be some code (in LookupEnvironment ~860 and
  843. // TypeSystem ~340) related to avoiding raw'ifying the enclosing type if the type to
  844. // be rawed is static. These changes cause these checks to fail and the sophisticated
  845. // test variant V fails. It checks declaring type typevariables and then
  846. // uses aliasTargetType ... I'm switching it to check aliasTargetType
  847. // && declaringType.typeVariables() != null
  848. // && declaringType.typeVariables().length != 0
  849. && aliasTargetType != null
  850. && aliasTargetType.getTypeVariables() != null
  851. && aliasTargetType.getTypeVariables().length!=0
  852. ) {
  853. int i = 0;
  854. ReferenceBinding aliasTarget = (ReferenceBinding) makeTypeBinding(aliasTargetType);
  855. if (aliasTarget.isRawType()) {
  856. aliasTarget = ((RawTypeBinding) aliasTarget).genericType();
  857. }
  858. for (String alias: aliases) {
  859. typeVariableToTypeBinding.put(alias, aliasTarget.typeVariables()[i++]);
  860. }
  861. }
  862. currentType = declaringType;
  863. MethodBinding mb = new MethodBinding(member.getModifiers(), member.getName().toCharArray(),
  864. makeTypeBinding(member.getReturnType()),
  865. makeTypeBindings(member.getParameterTypes()),
  866. makeReferenceBindings(member.getExceptions()), declaringType);
  867. if (tvbs != null) {
  868. mb.typeVariables = tvbs;
  869. }
  870. typeVariableToTypeBinding.clear();
  871. currentType = null;
  872. if (NameMangler.isSyntheticMethod(member.getName(), true)) {
  873. mb.modifiers |= Flags.AccSynthetic;
  874. }
  875. return mb;
  876. }
  877. /**
  878. * Convert a bunch of type variables in one go, from AspectJ form to Eclipse form.
  879. */
  880. // private TypeVariableBinding[] makeTypeVariableBindings(UnresolvedType[] typeVariables) {
  881. // int len = typeVariables.length;
  882. // TypeVariableBinding[] ret = new TypeVariableBinding[len];
  883. // for (int i = 0; i < len; i++) {
  884. // ret[i] = makeTypeVariableBinding((TypeVariableReference)typeVariables[i]);
  885. // }
  886. // return ret;
  887. // }
  888. private TypeVariableBinding[] makeTypeVariableBindingsFromAJTypeVariables(TypeVariable[] typeVariables) {
  889. int len = typeVariables.length;
  890. TypeVariableBinding[] ret = new TypeVariableBinding[len];
  891. for (int i = 0; i < len; i++) {
  892. ret[i] = makeTypeVariableBindingFromAJTypeVariable(typeVariables[i]);
  893. }
  894. return ret;
  895. }
  896. // only accessed through private methods in this class. Ensures all type variables we encounter
  897. // map back to the same type binding - this is important later when Eclipse code is processing
  898. // a methodbinding trying to come up with possible bindings for the type variables.
  899. // key is currently the name of the type variable...is that ok?
  900. private final Map<String,TypeVariableBinding> typeVariableToTypeBinding = new HashMap<>();
  901. // /**
  902. // * Converts from an TypeVariableReference to a TypeVariableBinding. A TypeVariableReference
  903. // * in AspectJ world holds a TypeVariable and it is this type variable that is converted
  904. // * to the TypeVariableBinding.
  905. // */
  906. // private TypeVariableBinding makeTypeVariableBinding(TypeVariableReference tvReference) {
  907. // TypeVariable tv = tvReference.getTypeVariable();
  908. // TypeVariableBinding tvBinding = (TypeVariableBinding)typeVariableToTypeBinding.get(tv.getName());
  909. // if (currentType!=null) {
  910. // TypeVariableBinding tvb = currentType.getTypeVariable(tv.getName().toCharArray());
  911. // if (tvb!=null) return tvb;
  912. // }
  913. // if (tvBinding==null) {
  914. // Binding declaringElement = null;
  915. // // this will cause an infinite loop or NPE... not required yet luckily.
  916. // // if (tVar.getDeclaringElement() instanceof Member) {
  917. // // declaringElement = makeMethodBinding((ResolvedMember)tVar.getDeclaringElement());
  918. // // } else {
  919. // // declaringElement = makeTypeBinding((UnresolvedType)tVar.getDeclaringElement());
  920. // // }
  921. //
  922. // tvBinding = new TypeVariableBinding(tv.getName().toCharArray(),null,tv.getRank());
  923. //
  924. // typeVariableToTypeBinding.put(tv.getName(),tvBinding);
  925. // tvBinding.superclass=(ReferenceBinding)makeTypeBinding(tv.getUpperBound());
  926. // tvBinding.firstBound=makeTypeBinding(tv.getFirstBound());
  927. // if (tv.getAdditionalInterfaceBounds()==null) {
  928. // tvBinding.superInterfaces=TypeVariableBinding.NO_SUPERINTERFACES;
  929. // } else {
  930. // TypeBinding tbs[] = makeTypeBindings(tv.getAdditionalInterfaceBounds());
  931. // ReferenceBinding[] rbs= new ReferenceBinding[tbs.length];
  932. // for (int i = 0; i < tbs.length; i++) {
  933. // rbs[i] = (ReferenceBinding)tbs[i];
  934. // }
  935. // tvBinding.superInterfaces=rbs;
  936. // }
  937. // }
  938. // return tvBinding;
  939. // }
  940. private TypeVariableBinding makeTypeVariableBindingFromAJTypeVariable(TypeVariable tv) {
  941. TypeVariableBinding tvBinding = typeVariableToTypeBinding.get(tv.getName());
  942. if (currentType != null) {
  943. TypeVariableBinding tvb = currentType.getTypeVariable(tv.getName().toCharArray());
  944. if (tvb != null) {
  945. return tvb;
  946. }
  947. }
  948. if (tvBinding == null) {
  949. Binding declaringElement = null;
  950. // this will cause an infinite loop or NPE... not required yet luckily.
  951. // if (tVar.getDeclaringElement() instanceof Member) {
  952. // declaringElement = makeMethodBinding((ResolvedMember)tVar.getDeclaringElement());
  953. // } else {
  954. // declaringElement = makeTypeBinding((UnresolvedType)tVar.getDeclaringElement());
  955. // }
  956. tvBinding = new TypeVariableBinding(tv.getName().toCharArray(), declaringElement, tv.getRank(),this.lookupEnvironment);
  957. typeVariableToTypeBinding.put(tv.getName(), tvBinding);
  958. if (tv.getSuperclass() != null
  959. && (!tv.getSuperclass().getSignature().equals("Ljava/lang/Object;") || tv.getSuperInterfaces() != null)) {
  960. tvBinding.superclass = (ReferenceBinding) makeTypeBinding(tv.getSuperclass());
  961. }
  962. tvBinding.firstBound = makeTypeBinding(tv.getFirstBound());
  963. if (tv.getSuperInterfaces() == null) {
  964. tvBinding.superInterfaces = TypeVariableBinding.NO_SUPERINTERFACES;
  965. } else {
  966. TypeBinding tbs[] = makeTypeBindings(tv.getSuperInterfaces());
  967. ReferenceBinding[] rbs = new ReferenceBinding[tbs.length];
  968. for (int i = 0; i < tbs.length; i++) {
  969. rbs[i] = (ReferenceBinding) tbs[i];
  970. }
  971. tvBinding.superInterfaces = rbs;
  972. }
  973. }
  974. return tvBinding;
  975. }
  976. public MethodBinding makeMethodBindingForCall(Member member) {
  977. return new MethodBinding(member.getModifiers() & ~Modifier.INTERFACE, member.getName().toCharArray(),
  978. makeTypeBinding(member.getReturnType()), makeTypeBindings(member.getParameterTypes()), new ReferenceBinding[0],
  979. (ReferenceBinding) makeTypeBinding(member.getDeclaringType()));
  980. }
  981. public void finishedCompilationUnit(CompilationUnitDeclaration unit) {
  982. if ((buildManager != null) && buildManager.doGenerateModel()) {
  983. AjBuildManager.getAsmHierarchyBuilder().buildStructureForCompilationUnit(unit, buildManager.getStructureModel(),
  984. buildManager.buildConfig);
  985. }
  986. }
  987. public void addTypeBinding(TypeBinding binding) {
  988. typexToBinding.put(fromBinding(binding), binding);
  989. }
  990. public void addTypeBindingAndStoreInWorld(SourceTypeBinding binding) {
  991. UnresolvedType ut = fromBinding(binding);
  992. typexToBinding.put(ut, binding);
  993. ReferenceType rt = world.lookupOrCreateName(ut);
  994. // Ensure a delegate is set (#558995)
  995. // TODO the delegate is perhaps not 100% ideal as the decl is the aspect?
  996. TypeDeclaration decl = binding.scope.referenceContext;
  997. CompilationUnitDeclaration referenceCompilationUnit = binding.scope.referenceCompilationUnit();
  998. EclipseSourceType t = new EclipseSourceType(rt, this, binding, decl, referenceCompilationUnit);
  999. rt.setDelegate(t);
  1000. }
  1001. public Shadow makeShadow(ASTNode location, ReferenceContext context) {
  1002. return EclipseShadow.makeShadow(this, location, context);
  1003. }
  1004. public Shadow makeShadow(ReferenceContext context) {
  1005. return EclipseShadow.makeShadow(this, (ASTNode) context, context);
  1006. }
  1007. public void addSourceTypeBinding(SourceTypeBinding binding, CompilationUnitDeclaration unit) {
  1008. TypeDeclaration decl = binding.scope.referenceContext;
  1009. // Deal with the raw/basic type to give us an entry in the world type map
  1010. UnresolvedType unresolvedRawType = null;
  1011. if (binding.isGenericType()) {
  1012. unresolvedRawType = UnresolvedType.forRawTypeName(getName(binding));
  1013. } else if (binding.isLocalType()) {
  1014. LocalTypeBinding ltb = (LocalTypeBinding) binding;
  1015. if (ltb.constantPoolName() != null && ltb.constantPoolName().length > 0) {
  1016. unresolvedRawType = UnresolvedType.forSignature(new String(binding.signature()));
  1017. } else {
  1018. unresolvedRawType = UnresolvedType.forName(getName(binding));
  1019. }
  1020. } else {
  1021. unresolvedRawType = UnresolvedType.forName(getName(binding));
  1022. }
  1023. ReferenceType resolvedRawType = getWorld().lookupOrCreateName(unresolvedRawType);
  1024. // A type can change from simple > generic > simple across a set of compiles. We need
  1025. // to ensure the entry in the typemap is promoted and demoted correctly. The call
  1026. // to setGenericType() below promotes a simple to a raw. This call demotes it back
  1027. // to simple
  1028. // pr125405
  1029. if (!binding.isRawType() && !binding.isGenericType() && resolvedRawType.getTypekind() == TypeKind.RAW) {
  1030. resolvedRawType.demoteToSimpleType();
  1031. }
  1032. EclipseSourceType t = new EclipseSourceType(resolvedRawType, this, binding, decl, unit);
  1033. // For generics, go a bit further - build a typex for the generic type
  1034. // give it the same delegate and link it to the raw type
  1035. if (binding.isGenericType()) {
  1036. UnresolvedType unresolvedGenericType = fromBinding(binding); // fully aware of any generics info
  1037. ResolvedType resolvedGenericType = world.resolve(unresolvedGenericType, true);
  1038. ReferenceType complexName = null;
  1039. if (!resolvedGenericType.isMissing()) {
  1040. complexName = (ReferenceType) resolvedGenericType;
  1041. complexName = complexName.getGenericType();
  1042. if (complexName == null) {
  1043. complexName = new ReferenceType(unresolvedGenericType, world);
  1044. }
  1045. } else {
  1046. complexName = new ReferenceType(unresolvedGenericType, world);
  1047. }
  1048. resolvedRawType.setGenericType(complexName);
  1049. complexName.setDelegate(t);
  1050. }
  1051. resolvedRawType.setDelegate(t);
  1052. if (decl instanceof AspectDeclaration) {
  1053. ((AspectDeclaration) decl).typeX = resolvedRawType;
  1054. ((AspectDeclaration) decl).concreteName = t;
  1055. }
  1056. ReferenceBinding[] memberTypes = binding.memberTypes;
  1057. for (ReferenceBinding memberType : memberTypes) {
  1058. addSourceTypeBinding((SourceTypeBinding) memberType, unit);
  1059. }
  1060. }
  1061. // XXX this doesn't feel like it belongs here, but it breaks a hard dependency on
  1062. // exposing AjBuildManager (needed by AspectDeclaration).
  1063. public boolean isXSerializableAspects() {
  1064. return xSerializableAspects;
  1065. }
  1066. public ResolvedMember fromBinding(MethodBinding binding) {
  1067. return new ResolvedMemberImpl(Member.METHOD, fromBinding(binding.declaringClass), binding.modifiers,
  1068. fromBinding(binding.returnType), CharOperation.charToString(binding.selector), fromBindings(binding.parameters));
  1069. }
  1070. public TypeVariableDeclaringElement fromBinding(Binding declaringElement) {
  1071. if (declaringElement instanceof TypeBinding) {
  1072. return fromBinding(((TypeBinding) declaringElement));
  1073. } else {
  1074. return fromBinding((MethodBinding) declaringElement);
  1075. }
  1076. }
  1077. public void cleanup() {
  1078. this.typexToBinding.clear();
  1079. this.rawTypeXToBinding.clear();
  1080. this.finishedTypeMungers = null;
  1081. }
  1082. public void minicleanup() {
  1083. this.typexToBinding.clear();
  1084. this.rawTypeXToBinding.clear();
  1085. }
  1086. public int getItdVersion() {
  1087. return world.getItdVersion();
  1088. }
  1089. }