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 47KB

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