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.

ResolvedMemberImpl.java 44KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /* *******************************************************************
  2. * Copyright (c) 2002-2010 Contributors
  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. package org.aspectj.weaver;
  10. import java.io.IOException;
  11. import java.lang.reflect.Modifier;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.HashSet;
  15. import java.util.Iterator;
  16. import java.util.LinkedHashSet;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Set;
  20. import org.aspectj.bridge.ISourceLocation;
  21. /**
  22. * Represent a resolved member. Components of it are expected to exist. This member will correspond to a real member *unless* it is
  23. * being used to represent the effect of an ITD.
  24. *
  25. * @author PARC
  26. * @author Andy Clement
  27. */
  28. public class ResolvedMemberImpl extends MemberImpl implements IHasPosition, ResolvedMember {
  29. private String[] parameterNames = null;
  30. private boolean isResolved = false;
  31. protected UnresolvedType[] checkedExceptions = UnresolvedType.NONE;
  32. /**
  33. * if this member is a parameterized version of a member in a generic type, then this field holds a reference to the member we
  34. * parameterize.
  35. */
  36. protected ResolvedMember backingGenericMember = null;
  37. protected AnnotationAJ[] annotations = null;
  38. protected ResolvedType[] annotationTypes = null;
  39. protected AnnotationAJ[][] parameterAnnotations = null;
  40. protected ResolvedType[][] parameterAnnotationTypes = null;
  41. // Some members are 'created' to represent other things (for example ITDs).
  42. // These members have their annotations stored elsewhere, and this flag indicates
  43. // that is the case. It is up to the caller to work out where that is!
  44. // Once determined the caller may choose to stash the annotations in this
  45. // member...
  46. private boolean isAnnotatedElsewhere = false;
  47. private boolean isAjSynthetic = false;
  48. // generic methods have type variables
  49. protected TypeVariable[] typeVariables;
  50. // these three fields hold the source location of this member
  51. protected int start, end;
  52. protected ISourceContext sourceContext = null;
  53. // XXX deprecate this in favor of the constructor below
  54. public ResolvedMemberImpl(MemberKind kind, UnresolvedType declaringType, int modifiers, UnresolvedType returnType, String name,
  55. UnresolvedType[] parameterTypes) {
  56. super(kind, declaringType, modifiers, returnType, name, parameterTypes);
  57. }
  58. public ResolvedMemberImpl(MemberKind kind, UnresolvedType declaringType, int modifiers, UnresolvedType returnType, String name,
  59. UnresolvedType[] parameterTypes, UnresolvedType[] checkedExceptions) {
  60. super(kind, declaringType, modifiers, returnType, name, parameterTypes);
  61. this.checkedExceptions = checkedExceptions;
  62. }
  63. public ResolvedMemberImpl(MemberKind kind, UnresolvedType declaringType, int modifiers, UnresolvedType returnType, String name,
  64. UnresolvedType[] parameterTypes, UnresolvedType[] checkedExceptions, ResolvedMember backingGenericMember) {
  65. this(kind, declaringType, modifiers, returnType, name, parameterTypes, checkedExceptions);
  66. this.backingGenericMember = backingGenericMember;
  67. this.isAjSynthetic = backingGenericMember.isAjSynthetic();
  68. }
  69. public ResolvedMemberImpl(MemberKind kind, UnresolvedType declaringType, int modifiers, String name, String signature) {
  70. super(kind, declaringType, modifiers, name, signature);
  71. }
  72. /**
  73. * Compute the full set of signatures for a member. This walks up the hierarchy giving the ResolvedMember in each defining type
  74. * in the hierarchy. A shadowMember can be created with a target type (declaring type) that does not actually define the member.
  75. * This is ok as long as the member is inherited in the declaring type. Each declaring type in the line to the actual declaring
  76. * type is added as an additional signature. For example:
  77. *
  78. * class A { void foo(); } class B extends A {}
  79. *
  80. * shadowMember : void B.foo()
  81. *
  82. * gives { void B.foo(), void A.foo() }
  83. *
  84. * @param joinPointSignature
  85. * @param inAWorld
  86. */
  87. public static JoinPointSignature[] getJoinPointSignatures(Member joinPointSignature, World inAWorld) {
  88. // Walk up hierarchy creating one member for each type up to and
  89. // including the
  90. // first defining type
  91. ResolvedType originalDeclaringType = joinPointSignature.getDeclaringType().resolve(inAWorld);
  92. ResolvedMemberImpl firstDefiningMember = (ResolvedMemberImpl) joinPointSignature.resolve(inAWorld);
  93. if (firstDefiningMember == null) {
  94. return JoinPointSignature.EMPTY_ARRAY;
  95. }
  96. // declaringType can be unresolved if we matched a synthetic member
  97. // generated by Aj...
  98. // should be fixed elsewhere but add this resolve call on the end for
  99. // now so that we can
  100. // focus on one problem at a time...
  101. ResolvedType firstDefiningType = firstDefiningMember.getDeclaringType().resolve(inAWorld);
  102. if (firstDefiningType != originalDeclaringType) {
  103. if (joinPointSignature.getKind() == Member.CONSTRUCTOR) {
  104. return JoinPointSignature.EMPTY_ARRAY;
  105. }
  106. // else if (shadowMember.isStatic()) {
  107. // return new ResolvedMember[] {firstDefiningMember};
  108. // }
  109. }
  110. List<ResolvedType> declaringTypes = new ArrayList<ResolvedType>();
  111. accumulateTypesInBetween(originalDeclaringType, firstDefiningType, declaringTypes);
  112. Set<ResolvedMember> memberSignatures = new LinkedHashSet<ResolvedMember>();
  113. for (ResolvedType declaringType : declaringTypes) {
  114. memberSignatures.add(new JoinPointSignature(firstDefiningMember, declaringType));
  115. }
  116. if (shouldWalkUpHierarchyFor(firstDefiningMember)) {
  117. // now walk up the hierarchy from the firstDefiningMember and
  118. // include the signature for
  119. // every type between the firstDefiningMember and the root defining
  120. // member.
  121. Iterator<ResolvedType> superTypeIterator = firstDefiningType.getDirectSupertypes();
  122. List<ResolvedType> typesAlreadyVisited = new ArrayList<ResolvedType>();
  123. accumulateMembersMatching(firstDefiningMember, superTypeIterator, typesAlreadyVisited, memberSignatures, false);
  124. }
  125. JoinPointSignature[] ret = new JoinPointSignature[memberSignatures.size()];
  126. memberSignatures.toArray(ret);
  127. return ret;
  128. }
  129. private static boolean shouldWalkUpHierarchyFor(Member aMember) {
  130. if (aMember.getKind() == Member.CONSTRUCTOR) {
  131. return false;
  132. }
  133. if (aMember.getKind() == Member.FIELD) {
  134. return false;
  135. }
  136. if (Modifier.isStatic(aMember.getModifiers())) {
  137. return false;
  138. }
  139. return true;
  140. }
  141. /**
  142. * Build a list containing every type between subtype and supertype, inclusively.
  143. */
  144. private static void accumulateTypesInBetween(ResolvedType subType, ResolvedType superType, List<ResolvedType> types) {
  145. types.add(subType);
  146. if (subType == superType) {
  147. return;
  148. } else {
  149. for (Iterator<ResolvedType> iter = subType.getDirectSupertypes(); iter.hasNext();) {
  150. ResolvedType parent = iter.next();
  151. if (superType.isAssignableFrom(parent)) {
  152. accumulateTypesInBetween(parent, superType, types);
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. * We have a resolved member, possibly with type parameter references as parameters or return type. We need to find all its
  159. * ancestor members. When doing this, a type parameter matches regardless of bounds (bounds can be narrowed down the hierarchy).
  160. */
  161. private static void accumulateMembersMatching(ResolvedMemberImpl memberToMatch, Iterator<ResolvedType> typesToLookIn,
  162. List<ResolvedType> typesAlreadyVisited, Set<ResolvedMember> foundMembers, boolean ignoreGenerics) {
  163. while (typesToLookIn.hasNext()) {
  164. ResolvedType toLookIn = typesToLookIn.next();
  165. if (!typesAlreadyVisited.contains(toLookIn)) {
  166. typesAlreadyVisited.add(toLookIn);
  167. ResolvedMemberImpl foundMember = (ResolvedMemberImpl) toLookIn.lookupResolvedMember(memberToMatch, true,
  168. ignoreGenerics);
  169. if (foundMember != null && isVisibleTo(memberToMatch, foundMember)) {
  170. List<ResolvedType> declaringTypes = new ArrayList<ResolvedType>();
  171. // declaring type can be unresolved if the member can from
  172. // an ITD...
  173. ResolvedType resolvedDeclaringType = foundMember.getDeclaringType().resolve(toLookIn.getWorld());
  174. accumulateTypesInBetween(toLookIn, resolvedDeclaringType, declaringTypes);
  175. for (ResolvedType declaringType : declaringTypes) {
  176. // typesAlreadyVisited.add(declaringType);
  177. foundMembers.add(new JoinPointSignature(foundMember, declaringType));
  178. }
  179. if (!ignoreGenerics && toLookIn.isParameterizedType() && (foundMember.backingGenericMember != null)) {
  180. foundMembers.add(new JoinPointSignature(foundMember.backingGenericMember, foundMember.declaringType
  181. .resolve(toLookIn.getWorld())));
  182. }
  183. accumulateMembersMatching(foundMember, toLookIn.getDirectSupertypes(), typesAlreadyVisited, foundMembers,
  184. ignoreGenerics);
  185. // if this was a parameterized type, look in the generic
  186. // type that backs it too
  187. }
  188. }
  189. }
  190. }
  191. /**
  192. * Returns true if the parent member is visible to the child member In the same declaring type this is always true, otherwise if
  193. * parent is private it is false.
  194. *
  195. * @param childMember
  196. * @param parentMember
  197. * @return
  198. */
  199. private static boolean isVisibleTo(ResolvedMember childMember, ResolvedMember parentMember) {
  200. if (childMember.getDeclaringType().equals(parentMember.getDeclaringType())) {
  201. return true;
  202. }
  203. if (Modifier.isPrivate(parentMember.getModifiers())) {
  204. return false;
  205. } else {
  206. return true;
  207. }
  208. }
  209. // ----
  210. @Override
  211. public final int getModifiers(World world) {
  212. return modifiers;
  213. }
  214. @Override
  215. public final int getModifiers() {
  216. return modifiers;
  217. }
  218. // ----
  219. @Override
  220. public final UnresolvedType[] getExceptions(World world) {
  221. return getExceptions();
  222. }
  223. public UnresolvedType[] getExceptions() {
  224. return checkedExceptions;
  225. }
  226. public ShadowMunger getAssociatedShadowMunger() {
  227. return null;
  228. }
  229. // ??? true or false?
  230. public boolean isAjSynthetic() {
  231. return isAjSynthetic;
  232. }
  233. protected void setAjSynthetic(boolean b) {
  234. isAjSynthetic = b;
  235. }
  236. public boolean hasAnnotations() {
  237. return (annotationTypes != null);
  238. }
  239. /**
  240. * Check if this member has an annotation of the specified type. If the member has a backing generic member then this member
  241. * represents a parameterization of a member in a generic type and the annotations available on the backing generic member
  242. * should be used.
  243. *
  244. * @param ofType the type of the annotation being searched for
  245. * @return true if the annotation is found on this member or its backing generic member
  246. */
  247. public boolean hasAnnotation(UnresolvedType ofType) {
  248. // The ctors don't allow annotations to be specified ... yet - but
  249. // that doesn't mean it is an error to call this method.
  250. // Normally the weaver will be working with subtypes of
  251. // this type - BcelField/BcelMethod
  252. if (backingGenericMember != null) {
  253. if (annotationTypes != null) {
  254. throw new BCException("Unexpectedly found a backing generic member and a local set of annotations");
  255. }
  256. return backingGenericMember.hasAnnotation(ofType);
  257. }
  258. if (annotationTypes != null) {
  259. for (int i = 0, max = annotationTypes.length; i < max; i++) {
  260. if (annotationTypes[i].equals(ofType)) {
  261. return true;
  262. }
  263. }
  264. }
  265. return false;
  266. }
  267. public ResolvedType[] getAnnotationTypes() {
  268. // The ctors don't allow annotations to be specified ... yet - but
  269. // that doesn't mean it is an error to call this method.
  270. // Normally the weaver will be working with subtypes of
  271. // this type - BcelField/BcelMethod
  272. if (backingGenericMember != null) {
  273. if (annotationTypes != null) {
  274. throw new BCException("Unexpectedly found a backing generic member and a local set of annotations");
  275. }
  276. return backingGenericMember.getAnnotationTypes();
  277. }
  278. return annotationTypes;
  279. }
  280. public String getAnnotationDefaultValue() {
  281. throw new UnsupportedOperationException(
  282. "You should resolve this member and call getAnnotationDefaultValue() on the result...");
  283. }
  284. @Override
  285. public AnnotationAJ[] getAnnotations() {
  286. if (backingGenericMember != null) {
  287. return backingGenericMember.getAnnotations();
  288. }
  289. if (annotations!=null) {
  290. return annotations;
  291. }
  292. return super.getAnnotations();
  293. }
  294. public AnnotationAJ getAnnotationOfType(UnresolvedType ofType) {
  295. if (annotations!=null) {
  296. // this means they have been set (we are likely a placeholder for an ITD, so a fake member)
  297. for (AnnotationAJ annotation: annotations) {
  298. if (annotation.getType().equals(ofType)) {
  299. return annotation;
  300. }
  301. }
  302. return null;
  303. }
  304. throw new UnsupportedOperationException("You should resolve this member and call getAnnotationOfType() on the result...");
  305. }
  306. public void setAnnotations(AnnotationAJ[] annotations) {
  307. this.annotations = annotations;
  308. }
  309. public void setAnnotationTypes(ResolvedType[] annotationTypes) {
  310. this.annotationTypes = annotationTypes;
  311. }
  312. public ResolvedType[][] getParameterAnnotationTypes() {
  313. return parameterAnnotationTypes;
  314. }
  315. public AnnotationAJ[][] getParameterAnnotations() {
  316. if (backingGenericMember != null) {
  317. return backingGenericMember.getParameterAnnotations();
  318. }
  319. throw new BCException("Cannot return parameter annotations for a " + this.getClass().getName() + " member");
  320. // return super.getParameterAnnotations();
  321. }
  322. public void addAnnotation(AnnotationAJ annotation) {
  323. if (annotationTypes == null) {
  324. annotationTypes = new ResolvedType[1];
  325. annotationTypes[0] = annotation.getType();
  326. annotations = new AnnotationAJ[1];
  327. annotations[0] = annotation;
  328. } else {
  329. int len = annotations.length;
  330. AnnotationAJ[] ret = new AnnotationAJ[len + 1];
  331. System.arraycopy(annotations, 0, ret, 0, len);
  332. ret[len] = annotation;
  333. annotations = ret;
  334. ResolvedType[] newAnnotationTypes = new ResolvedType[len + 1];
  335. System.arraycopy(annotationTypes, 0, newAnnotationTypes, 0, len);
  336. newAnnotationTypes[len] = annotation.getType();
  337. annotationTypes = newAnnotationTypes;
  338. }
  339. }
  340. public boolean isBridgeMethod() {
  341. return (modifiers & Constants.ACC_BRIDGE) != 0 && getKind().equals(METHOD);
  342. }
  343. public boolean isVarargsMethod() {
  344. return (modifiers & Constants.ACC_VARARGS) != 0;
  345. }
  346. public void setVarargsMethod() {
  347. modifiers = modifiers | Constants.ACC_VARARGS;
  348. }
  349. public boolean isSynthetic() {
  350. // See Bcelmethod.isSynthetic() which takes account of preJava5
  351. // Synthetic modifier
  352. return (modifiers & 4096) != 0; // do we know better?
  353. }
  354. public void write(CompressingDataOutputStream s) throws IOException {
  355. getKind().write(s);
  356. s.writeBoolean(s.canCompress()); // boolean indicates if parts of this are compressed references
  357. // write out the signature of the declaring type of this member
  358. if (s.canCompress()) {
  359. s.writeCompressedSignature(getDeclaringType().getSignature());
  360. } else {
  361. getDeclaringType().write(s);
  362. }
  363. // write out the modifiers
  364. s.writeInt(modifiers);
  365. // write out the name and the signature of this member
  366. if (s.canCompress()) {
  367. s.writeCompressedName(getName());
  368. s.writeCompressedSignature(getSignature());
  369. } else {
  370. s.writeUTF(getName());
  371. s.writeUTF(getSignature());
  372. }
  373. // write out the array clauses
  374. UnresolvedType.writeArray(getExceptions(), s);
  375. s.writeInt(getStart());
  376. s.writeInt(getEnd());
  377. s.writeBoolean(isVarargsMethod());
  378. // Write out any type variables...
  379. if (typeVariables == null) {
  380. s.writeByte(0);
  381. } else {
  382. s.writeByte(typeVariables.length);
  383. for (int i = 0; i < typeVariables.length; i++) {
  384. typeVariables[i].write(s);
  385. }
  386. }
  387. String gsig = getGenericSignature();
  388. // change this to a byte: 255=false 0>254 means true and encodes the number of parameters
  389. if (getSignature().equals(gsig)) {
  390. s.writeByte(0xff);
  391. } else {
  392. s.writeByte(parameterTypes.length);
  393. for (int i = 0; i < parameterTypes.length; i++) {
  394. if (s.canCompress()) {
  395. s.writeCompressedSignature(parameterTypes[i].getSignature());
  396. } else {
  397. UnresolvedType array_element = parameterTypes[i];
  398. array_element.write(s);
  399. }
  400. }
  401. if (s.canCompress()) {
  402. s.writeCompressedSignature(returnType.getSignature());
  403. } else {
  404. returnType.write(s);
  405. }
  406. }
  407. }
  408. /**
  409. * Return the member generic signature that would be suitable for inclusion in a class file Signature attribute. For: <T>
  410. * List<String> getThem(T t) {} we would create: <T:Ljava/lang/Object;>(TT;)Ljava/util/List<Ljava/lang/String;>;;
  411. *
  412. * @return the generic signature for the member that could be inserted into a class file
  413. */
  414. public String getSignatureForAttribute() {
  415. StringBuffer sb = new StringBuffer();
  416. if (typeVariables != null) {
  417. sb.append("<");
  418. for (int i = 0; i < typeVariables.length; i++) {
  419. sb.append(typeVariables[i].getSignatureForAttribute()); // need
  420. // a
  421. // 'getSignatureForAttribute()'
  422. }
  423. sb.append(">");
  424. }
  425. sb.append("(");
  426. for (int i = 0; i < parameterTypes.length; i++) {
  427. ResolvedType ptype = (ResolvedType) parameterTypes[i];
  428. sb.append(ptype.getSignatureForAttribute());
  429. }
  430. sb.append(")");
  431. sb.append(((ResolvedType) returnType).getSignatureForAttribute());
  432. return sb.toString();
  433. }
  434. public String getGenericSignature() {
  435. StringBuffer sb = new StringBuffer();
  436. if (typeVariables != null) {
  437. sb.append("<");
  438. for (int i = 0; i < typeVariables.length; i++) {
  439. sb.append(typeVariables[i].getSignature());
  440. }
  441. sb.append(">");
  442. }
  443. sb.append("(");
  444. for (int i = 0; i < parameterTypes.length; i++) {
  445. UnresolvedType ptype = parameterTypes[i];
  446. sb.append(ptype.getSignature());
  447. }
  448. sb.append(")");
  449. sb.append(returnType.getSignature());
  450. return sb.toString();
  451. }
  452. public static void writeArray(ResolvedMember[] members, CompressingDataOutputStream s) throws IOException {
  453. s.writeInt(members.length);
  454. for (int i = 0, len = members.length; i < len; i++) {
  455. members[i].write(s);
  456. }
  457. }
  458. public static ResolvedMemberImpl readResolvedMember(VersionedDataInputStream s, ISourceContext sourceContext)
  459. throws IOException {
  460. MemberKind mk = MemberKind.read(s);
  461. boolean compressed = (s.isAtLeast169() ? s.readBoolean() : false);
  462. UnresolvedType declaringType = compressed ? UnresolvedType.forSignature(s.readUtf8(s.readShort())) : UnresolvedType.read(s);
  463. int modifiers = s.readInt();
  464. String name = compressed ? s.readUtf8(s.readShort()) : s.readUTF();
  465. String signature = compressed ? s.readUtf8(s.readShort()) : s.readUTF();
  466. ResolvedMemberImpl m = new ResolvedMemberImpl(mk, declaringType, modifiers, name, signature);
  467. m.checkedExceptions = UnresolvedType.readArray(s);
  468. m.start = s.readInt();
  469. m.end = s.readInt();
  470. m.sourceContext = sourceContext;
  471. if (s.getMajorVersion() >= AjAttribute.WeaverVersionInfo.WEAVER_VERSION_MAJOR_AJ150) {
  472. if (s.getMajorVersion() >= AjAttribute.WeaverVersionInfo.WEAVER_VERSION_MAJOR_AJ150M4) {
  473. boolean isvarargs = s.readBoolean();
  474. if (isvarargs) {
  475. m.setVarargsMethod();
  476. }
  477. }
  478. int tvcount = s.isAtLeast169() ? s.readByte() : s.readInt();
  479. if (tvcount != 0) {
  480. m.typeVariables = new TypeVariable[tvcount];
  481. for (int i = 0; i < tvcount; i++) {
  482. m.typeVariables[i] = TypeVariable.read(s);
  483. m.typeVariables[i].setDeclaringElement(m);
  484. m.typeVariables[i].setRank(i);
  485. }
  486. }
  487. if (s.getMajorVersion() >= AjAttribute.WeaverVersionInfo.WEAVER_VERSION_MAJOR_AJ150M4) {
  488. int pcount = -1;
  489. boolean hasAGenericSignature = false;
  490. if (s.isAtLeast169()) {
  491. pcount = s.readByte();
  492. hasAGenericSignature = (pcount >= 0 && pcount < 255);
  493. } else {
  494. hasAGenericSignature = s.readBoolean();
  495. }
  496. if (hasAGenericSignature) {
  497. int ps = (s.isAtLeast169() ? pcount : s.readInt());
  498. UnresolvedType[] params = new UnresolvedType[ps];
  499. for (int i = 0; i < params.length; i++) {
  500. if (compressed) {
  501. params[i] = TypeFactory.createTypeFromSignature(s.readSignature());
  502. } else {
  503. params[i] = TypeFactory.createTypeFromSignature(s.readUTF());
  504. }
  505. }
  506. UnresolvedType rt = compressed ? TypeFactory.createTypeFromSignature(s.readSignature()) : TypeFactory
  507. .createTypeFromSignature(s.readUTF());
  508. m.parameterTypes = params;
  509. m.returnType = rt;
  510. }
  511. }
  512. }
  513. return m;
  514. }
  515. public static ResolvedMember[] readResolvedMemberArray(VersionedDataInputStream s, ISourceContext context) throws IOException {
  516. int len = s.readInt();
  517. ResolvedMember[] members = new ResolvedMember[len];
  518. for (int i = 0; i < len; i++) {
  519. members[i] = ResolvedMemberImpl.readResolvedMember(s, context);
  520. }
  521. return members;
  522. }
  523. // OPTIMIZE dont like how resolve(world) on ResolvedMemberImpl does
  524. // something different to world.resolve(member)
  525. @Override
  526. public ResolvedMember resolve(World world) {
  527. if (isResolved) {
  528. return this;
  529. }
  530. // make sure all the pieces of a resolvedmember really are resolved
  531. try {
  532. if (typeVariables != null && typeVariables.length > 0) {
  533. for (int i = 0; i < typeVariables.length; i++) {
  534. typeVariables[i] = typeVariables[i].resolve(world);
  535. }
  536. }
  537. world.setTypeVariableLookupScope(this);
  538. // if (annotationTypes != null) {
  539. // Set<ResolvedType> r = new HashSet<ResolvedType>();
  540. // for (UnresolvedType element : annotationTypes) {
  541. // // for (Iterator iter = annotationTypes.iterator(); iter.hasNext();) {
  542. // // UnresolvedType element = (UnresolvedType) iter.next();
  543. // r.add(world.resolve(element));
  544. // }
  545. // annotationTypes = r;
  546. // }
  547. declaringType = declaringType.resolve(world);
  548. if (declaringType.isRawType()) {
  549. declaringType = ((ReferenceType) declaringType).getGenericType();
  550. }
  551. if (parameterTypes != null && parameterTypes.length > 0) {
  552. for (int i = 0; i < parameterTypes.length; i++) {
  553. parameterTypes[i] = parameterTypes[i].resolve(world);
  554. }
  555. }
  556. returnType = returnType.resolve(world);
  557. } finally {
  558. world.setTypeVariableLookupScope(null);
  559. }
  560. isResolved = true;
  561. return this;
  562. }
  563. public ISourceContext getSourceContext(World world) {
  564. return getDeclaringType().resolve(world).getSourceContext();
  565. }
  566. public String[] getParameterNames() {
  567. return parameterNames;
  568. }
  569. public final void setParameterNames(String[] pnames) {
  570. parameterNames = pnames;
  571. }
  572. @Override
  573. public final String[] getParameterNames(World world) {
  574. return getParameterNames();
  575. }
  576. public AjAttribute.EffectiveSignatureAttribute getEffectiveSignature() {
  577. return null;
  578. }
  579. public ISourceLocation getSourceLocation() {
  580. // System.out.println("get context: " + this + " is " + sourceContext);
  581. if (getSourceContext() == null) {
  582. // System.err.println("no context: " + this);
  583. return null;
  584. }
  585. return getSourceContext().makeSourceLocation(this);
  586. }
  587. public int getEnd() {
  588. return end;
  589. }
  590. public ISourceContext getSourceContext() {
  591. return sourceContext;
  592. }
  593. public int getStart() {
  594. return start;
  595. }
  596. public void setPosition(int sourceStart, int sourceEnd) {
  597. this.start = sourceStart;
  598. this.end = sourceEnd;
  599. }
  600. public void setDeclaringType(ReferenceType rt) {
  601. declaringType = rt;
  602. }
  603. public void setSourceContext(ISourceContext sourceContext) {
  604. this.sourceContext = sourceContext;
  605. }
  606. public boolean isAbstract() {
  607. return Modifier.isAbstract(modifiers);
  608. }
  609. public boolean isPublic() {
  610. return Modifier.isPublic(modifiers);
  611. }
  612. public boolean isDefault() {
  613. int mods = getModifiers();
  614. return !(Modifier.isPublic(mods) || Modifier.isProtected(mods) || Modifier.isPrivate(mods));
  615. }
  616. public boolean isVisible(ResolvedType fromType) {
  617. UnresolvedType declaringType = getDeclaringType();
  618. ResolvedType type = null;
  619. if (fromType.equals(declaringType)) {
  620. type = fromType;
  621. } else {
  622. World world = fromType.getWorld();
  623. type = declaringType.resolve(world);
  624. }
  625. return ResolvedType.isVisible(getModifiers(), type, fromType);
  626. }
  627. public void setCheckedExceptions(UnresolvedType[] checkedExceptions) {
  628. this.checkedExceptions = checkedExceptions;
  629. }
  630. public void setAnnotatedElsewhere(boolean b) {
  631. isAnnotatedElsewhere = b;
  632. }
  633. public boolean isAnnotatedElsewhere() {
  634. return isAnnotatedElsewhere;
  635. }
  636. /**
  637. * Get the UnresolvedType for the return type, taking generic signature into account
  638. */
  639. @Override
  640. public UnresolvedType getGenericReturnType() {
  641. return getReturnType();
  642. }
  643. /**
  644. * Get the TypeXs of the parameter types, taking generic signature into account
  645. */
  646. @Override
  647. public UnresolvedType[] getGenericParameterTypes() {
  648. return getParameterTypes();
  649. }
  650. public ResolvedMemberImpl parameterizedWith(UnresolvedType[] typeParameters, ResolvedType newDeclaringType,
  651. boolean isParameterized) {
  652. return parameterizedWith(typeParameters, newDeclaringType, isParameterized, null);
  653. }
  654. /**
  655. * Return a resolvedmember in which all the type variables in the signature have been replaced with the given bindings. The
  656. * 'isParameterized' flag tells us whether we are creating a raw type version or not. if (isParameterized) then List<T> will
  657. * turn into List<String> (for example) - if (!isParameterized) then List<T> will turn into List.
  658. */
  659. public ResolvedMemberImpl parameterizedWith(UnresolvedType[] typeParameters, ResolvedType newDeclaringType,
  660. boolean isParameterized, List<String> aliases) {
  661. // PR308773
  662. // this check had problems for the inner type of a generic type because the inner type can be represented
  663. // by a 'simple type' if it is only sharing type variables with the outer and has none of its own. To avoid the
  664. // check going bang in this case we check for $ (crap...) - we can't check the outer because the declaring type
  665. // is considered unresolved...
  666. if (// isParameterized && <-- might need this bit...
  667. !getDeclaringType().isGenericType() && getDeclaringType().getName().indexOf("$") == -1) {
  668. throw new IllegalStateException("Can't ask to parameterize a member of non-generic type: " + getDeclaringType()
  669. + " kind(" + getDeclaringType().typeKind + ")");
  670. }
  671. TypeVariable[] typeVariables = getDeclaringType().getTypeVariables();
  672. if (isParameterized && (typeVariables.length != typeParameters.length)) {
  673. throw new IllegalStateException("Wrong number of type parameters supplied");
  674. }
  675. Map<String, UnresolvedType> typeMap = new HashMap<String, UnresolvedType>();
  676. boolean typeParametersSupplied = typeParameters != null && typeParameters.length > 0;
  677. if (typeVariables != null) {
  678. // If no 'replacements' were supplied in the typeParameters array
  679. // then collapse
  680. // type variables to their first bound.
  681. for (int i = 0; i < typeVariables.length; i++) {
  682. UnresolvedType ut = (!typeParametersSupplied ? typeVariables[i].getFirstBound() : typeParameters[i]);
  683. typeMap.put(typeVariables[i].getName(), ut);
  684. }
  685. }
  686. // For ITDs on generic types that use type variables from the target type, the aliases
  687. // record the alternative names used throughout the ITD expression that must map to
  688. // the same value as the type variables real name.
  689. if (aliases != null) {
  690. int posn = 0;
  691. for (String typeVariableAlias : aliases) {
  692. typeMap.put(typeVariableAlias, (!typeParametersSupplied ? typeVariables[posn].getFirstBound()
  693. : typeParameters[posn]));
  694. posn++;
  695. }
  696. }
  697. UnresolvedType parameterizedReturnType = parameterize(getGenericReturnType(), typeMap, isParameterized,
  698. newDeclaringType.getWorld());
  699. UnresolvedType[] parameterizedParameterTypes = new UnresolvedType[getGenericParameterTypes().length];
  700. UnresolvedType[] genericParameterTypes = getGenericParameterTypes();
  701. for (int i = 0; i < parameterizedParameterTypes.length; i++) {
  702. parameterizedParameterTypes[i] = parameterize(genericParameterTypes[i], typeMap, isParameterized,
  703. newDeclaringType.getWorld());
  704. }
  705. ResolvedMemberImpl ret = new ResolvedMemberImpl(getKind(), newDeclaringType, getModifiers(), parameterizedReturnType,
  706. getName(), parameterizedParameterTypes, getExceptions(), this);
  707. ret.setTypeVariables(getTypeVariables());
  708. ret.setSourceContext(getSourceContext());
  709. ret.setPosition(getStart(), getEnd());
  710. ret.setParameterNames(getParameterNames());
  711. return ret;
  712. }
  713. /**
  714. * Replace occurrences of type variables in the signature with values contained in the map. The map is of the form
  715. * A=String,B=Integer and so a signature List<A> Foo.m(B i) {} would become List<String> Foo.m(Integer i) {}
  716. */
  717. public ResolvedMember parameterizedWith(Map<String, UnresolvedType> m, World w) {
  718. // if (//isParameterized && <-- might need this bit...
  719. // !getDeclaringType().isGenericType()) {
  720. // throw new IllegalStateException(
  721. // "Can't ask to parameterize a member of non-generic type: "
  722. // +getDeclaringType()+" kind("+
  723. // getDeclaringType().typeKind+")");
  724. // }
  725. declaringType = declaringType.resolve(w);
  726. if (declaringType.isRawType()) {
  727. declaringType = ((ResolvedType) declaringType).getGenericType();
  728. // TypeVariable[] typeVariables = getDeclaringType().getTypeVariables();
  729. // if (isParameterized && (typeVariables.length !=
  730. // typeParameters.length)) {
  731. // throw new
  732. // IllegalStateException("Wrong number of type parameters supplied");
  733. // }
  734. // Map typeMap = new HashMap();
  735. // boolean typeParametersSupplied = typeParameters!=null &&
  736. // typeParameters.length>0;
  737. // if (typeVariables!=null) {
  738. // // If no 'replacements' were supplied in the typeParameters array
  739. // then collapse
  740. // // type variables to their first bound.
  741. // for (int i = 0; i < typeVariables.length; i++) {
  742. // UnresolvedType ut =
  743. // (!typeParametersSupplied?typeVariables[i].getFirstBound
  744. // ():typeParameters[i]);
  745. // typeMap.put(typeVariables[i].getName(),ut);
  746. // }
  747. // }
  748. // // For ITDs on generic types that use type variables from the target
  749. // type, the aliases
  750. // // record the alternative names used throughout the ITD expression
  751. // that must map to
  752. // // the same value as the type variables real name.
  753. // if (aliases!=null) {
  754. // int posn = 0;
  755. // for (Iterator iter = aliases.iterator(); iter.hasNext();) {
  756. // String typeVariableAlias = (String) iter.next();
  757. // typeMap.put(typeVariableAlias,(!typeParametersSupplied?typeVariables[
  758. // posn].getFirstBound():typeParameters[posn]));
  759. // posn++;
  760. // }
  761. // }
  762. }
  763. UnresolvedType parameterizedReturnType = parameterize(getGenericReturnType(), m, true, w);
  764. UnresolvedType[] parameterizedParameterTypes = new UnresolvedType[getGenericParameterTypes().length];
  765. UnresolvedType[] genericParameterTypes = getGenericParameterTypes();
  766. for (int i = 0; i < parameterizedParameterTypes.length; i++) {
  767. parameterizedParameterTypes[i] = parameterize(genericParameterTypes[i], m, true, w);
  768. }
  769. ResolvedMemberImpl ret = new ResolvedMemberImpl(getKind(), declaringType, getModifiers(), parameterizedReturnType,
  770. getName(), parameterizedParameterTypes, getExceptions(), this);
  771. ret.setTypeVariables(getTypeVariables());
  772. ret.setSourceContext(getSourceContext());
  773. ret.setPosition(getStart(), getEnd());
  774. ret.setParameterNames(getParameterNames());
  775. return ret;
  776. }
  777. public void setTypeVariables(TypeVariable[] tvars) {
  778. typeVariables = tvars;
  779. }
  780. public TypeVariable[] getTypeVariables() {
  781. return typeVariables;
  782. }
  783. protected UnresolvedType parameterize(UnresolvedType aType, Map<String, UnresolvedType> typeVariableMap,
  784. boolean inParameterizedType, World w) {
  785. if (aType instanceof TypeVariableReference) {
  786. String variableName = ((TypeVariableReference) aType).getTypeVariable().getName();
  787. if (!typeVariableMap.containsKey(variableName)) {
  788. return aType; // if the type variable comes from the method (and
  789. // not the type) thats OK
  790. }
  791. return typeVariableMap.get(variableName);
  792. } else if (aType.isParameterizedType()) {
  793. if (inParameterizedType) {
  794. if (w != null) {
  795. aType = aType.resolve(w);
  796. } else {
  797. UnresolvedType dType = getDeclaringType();
  798. aType = aType.resolve(((ResolvedType) dType).getWorld());
  799. }
  800. return aType.parameterize(typeVariableMap);
  801. } else {
  802. return aType.getRawType();
  803. }
  804. } else if (aType.isArray()) {
  805. // The component type might be a type variable (pr150095)
  806. int dims = 1;
  807. String sig = aType.getSignature();
  808. // while (sig.charAt(dims) == '[')
  809. // dims++;
  810. UnresolvedType arrayType = null;
  811. UnresolvedType componentSig = UnresolvedType.forSignature(sig.substring(dims));
  812. UnresolvedType parameterizedComponentSig = parameterize(componentSig, typeVariableMap, inParameterizedType, w);
  813. if (parameterizedComponentSig.isTypeVariableReference()
  814. && parameterizedComponentSig instanceof UnresolvedTypeVariableReferenceType
  815. && typeVariableMap.containsKey(((UnresolvedTypeVariableReferenceType) parameterizedComponentSig)
  816. .getTypeVariable().getName())) { // pr250632
  817. // TODO ASC bah, this code is rubbish - i should fix it properly
  818. StringBuffer newsig = new StringBuffer();
  819. newsig.append("[T");
  820. newsig.append(((UnresolvedTypeVariableReferenceType) parameterizedComponentSig).getTypeVariable().getName());
  821. newsig.append(";");
  822. arrayType = UnresolvedType.forSignature(newsig.toString());
  823. } else {
  824. arrayType = ResolvedType.makeArray(parameterizedComponentSig, dims);
  825. }
  826. return arrayType;
  827. }
  828. return aType;
  829. }
  830. /**
  831. * If this member is defined by a parameterized super-type, return the erasure of that member. For example: interface I<T> { T
  832. * foo(T aTea); } class C implements I<String> { String foo(String aString) { return "something"; } } The resolved member for
  833. * C.foo has signature String foo(String). The erasure of that member is Object foo(Object) -- use upper bound of type variable.
  834. * A type is a supertype of itself.
  835. */
  836. // public ResolvedMember getErasure() {
  837. // if (calculatedMyErasure) return myErasure;
  838. // calculatedMyErasure = true;
  839. // ResolvedType resolvedDeclaringType = (ResolvedType) getDeclaringType();
  840. // // this next test is fast, and the result is cached.
  841. // if (!resolvedDeclaringType.hasParameterizedSuperType()) {
  842. // return null;
  843. // } else {
  844. // // we have one or more parameterized super types.
  845. // // this member may be defined by one of them... we need to find out.
  846. // Collection declaringTypes =
  847. // this.getDeclaringTypes(resolvedDeclaringType.getWorld());
  848. // for (Iterator iter = declaringTypes.iterator(); iter.hasNext();) {
  849. // ResolvedType aDeclaringType = (ResolvedType) iter.next();
  850. // if (aDeclaringType.isParameterizedType()) {
  851. // // we've found the (a?) parameterized type that defines this member.
  852. // // now get the erasure of it
  853. // ResolvedMemberImpl matchingMember = (ResolvedMemberImpl)
  854. // aDeclaringType.lookupMemberNoSupers(this);
  855. // if (matchingMember != null && matchingMember.backingGenericMember !=
  856. // null) {
  857. // myErasure = matchingMember.backingGenericMember;
  858. // return myErasure;
  859. // }
  860. // }
  861. // }
  862. // }
  863. // return null;
  864. // }
  865. //
  866. // private ResolvedMember myErasure = null;
  867. // private boolean calculatedMyErasure = false;
  868. public boolean hasBackingGenericMember() {
  869. return backingGenericMember != null;
  870. }
  871. public ResolvedMember getBackingGenericMember() {
  872. return backingGenericMember;
  873. }
  874. /**
  875. * For ITDs, we use the default factory methods to build a resolved member, then alter a couple of characteristics using this
  876. * method - this is safe.
  877. */
  878. public void resetName(String newName) {
  879. this.name = newName;
  880. }
  881. public void resetKind(MemberKind newKind) {
  882. this.kind = newKind;
  883. }
  884. public void resetModifiers(int newModifiers) {
  885. this.modifiers = newModifiers;
  886. }
  887. public void resetReturnTypeToObjectArray() {
  888. returnType = UnresolvedType.OBJECTARRAY;
  889. }
  890. /**
  891. * Returns true if this member matches the other. The matching takes into account name and parameter types only. When comparing
  892. * parameter types, we allow any type variable to match any other type variable regardless of bounds.
  893. */
  894. public boolean matches(ResolvedMember aCandidateMatch, boolean ignoreGenerics) {
  895. ResolvedMemberImpl candidateMatchImpl = (ResolvedMemberImpl) aCandidateMatch;
  896. if (!getName().equals(aCandidateMatch.getName())) {
  897. return false;
  898. }
  899. UnresolvedType[] parameterTypes = getGenericParameterTypes();
  900. UnresolvedType[] candidateParameterTypes = aCandidateMatch.getGenericParameterTypes();
  901. if (parameterTypes.length != candidateParameterTypes.length) {
  902. return false;
  903. }
  904. boolean b = false;
  905. /*
  906. * if (ignoreGenerics) { String myParameterSignature = getParameterSigWithBoundsRemoved(); String
  907. * candidateParameterSignature = candidateMatchImpl.getParameterSigWithBoundsRemoved(); if
  908. * (myParameterSignature.equals(candidateParameterSignature)) { b = true; } else { myParameterSignature =
  909. * (hasBackingGenericMember() ? backingGenericMember.getParameterSignatureErased() : getParameterSignatureErased());
  910. * candidateParameterSignature = (candidateMatchImpl.hasBackingGenericMember() ? candidateMatchImpl.backingGenericMember
  911. * .getParameterSignatureErased() : candidateMatchImpl.getParameterSignatureErased()); // System.out.println("my psig = " +
  912. * myParameterSignature); // System.out.println("can psig = " + candidateParameterSignature); b =
  913. * myParameterSignature.equals(candidateParameterSignature); } } else {
  914. */
  915. String myParameterSignature = getParameterSigWithBoundsRemoved();
  916. String candidateParameterSignature = candidateMatchImpl.getParameterSigWithBoundsRemoved();
  917. if (myParameterSignature.equals(candidateParameterSignature)) {
  918. b = true;
  919. } else {
  920. // try erasure
  921. myParameterSignature = getParameterSignatureErased();
  922. candidateParameterSignature = candidateMatchImpl.getParameterSignatureErased();
  923. // myParameterSignature = (hasBackingGenericMember() ? backingGenericMember.getParameterSignatureErased()
  924. // : getParameterSignatureErased());
  925. // candidateParameterSignature = (candidateMatchImpl.hasBackingGenericMember() ?
  926. // candidateMatchImpl.backingGenericMember
  927. // .getParameterSignatureErased() : candidateMatchImpl.getParameterSignatureErased());
  928. // System.out.println("my psig = " + myParameterSignature);
  929. // System.out.println("can psig = " + candidateParameterSignature);
  930. b = myParameterSignature.equals(candidateParameterSignature);
  931. // }
  932. }
  933. // System.out.println("Checking param signatures: " + b);
  934. return b;
  935. }
  936. /**
  937. * converts e.g. <T extends Number>.... List<T> to just Ljava/util/List<T;>; whereas the full signature would be
  938. * Ljava/util/List<T:Ljava/lang/Number;>;
  939. */
  940. private String myParameterSignatureWithBoundsRemoved = null;
  941. /**
  942. * converts e.g. <T extends Number>.... List<T> to just Ljava/util/List;
  943. */
  944. private String myParameterSignatureErasure = null;
  945. // does NOT produce a meaningful java signature, but does give a unique
  946. // string suitable for
  947. // comparison.
  948. private String getParameterSigWithBoundsRemoved() {
  949. if (myParameterSignatureWithBoundsRemoved != null) {
  950. return myParameterSignatureWithBoundsRemoved;
  951. }
  952. StringBuffer sig = new StringBuffer();
  953. UnresolvedType[] myParameterTypes = getGenericParameterTypes();
  954. for (int i = 0; i < myParameterTypes.length; i++) {
  955. appendSigWithTypeVarBoundsRemoved(myParameterTypes[i], sig, new HashSet<UnresolvedType>());
  956. }
  957. myParameterSignatureWithBoundsRemoved = sig.toString();
  958. return myParameterSignatureWithBoundsRemoved;
  959. }
  960. /**
  961. * Return the erased form of the signature with bounds collapsed for type variables, etc. Does not include the return type, @see
  962. * getParam
  963. */
  964. public String getParameterSignatureErased() {
  965. if (myParameterSignatureErasure == null) {
  966. StringBuilder sig = new StringBuilder();
  967. for (UnresolvedType parameter : getParameterTypes()) {
  968. sig.append(parameter.getErasureSignature());
  969. }
  970. myParameterSignatureErasure = sig.toString();
  971. }
  972. return myParameterSignatureErasure;
  973. }
  974. public String getSignatureErased() {
  975. StringBuffer sb = new StringBuffer();
  976. sb.append("(");
  977. sb.append(getParameterSignatureErased());
  978. sb.append(")");
  979. sb.append(getReturnType().getErasureSignature());
  980. return sb.toString();
  981. }
  982. // does NOT produce a meaningful java signature, but does give a unique
  983. // string suitable for
  984. // comparison.
  985. public static void appendSigWithTypeVarBoundsRemoved(UnresolvedType aType, StringBuffer toBuffer,
  986. Set<UnresolvedType> alreadyUsedTypeVars) {
  987. if (aType.isTypeVariableReference()) {
  988. TypeVariableReferenceType typeVariableRT = (TypeVariableReferenceType) aType;
  989. // pr204505
  990. if (alreadyUsedTypeVars.contains(aType)) {
  991. toBuffer.append("...");
  992. } else {
  993. alreadyUsedTypeVars.add(aType);
  994. appendSigWithTypeVarBoundsRemoved(typeVariableRT.getTypeVariable().getFirstBound(), toBuffer, alreadyUsedTypeVars);
  995. }
  996. // toBuffer.append("T;");
  997. } else if (aType.isParameterizedType()) {
  998. toBuffer.append(aType.getRawType().getSignature());
  999. toBuffer.append("<");
  1000. for (int i = 0; i < aType.getTypeParameters().length; i++) {
  1001. appendSigWithTypeVarBoundsRemoved(aType.getTypeParameters()[i], toBuffer, alreadyUsedTypeVars);
  1002. }
  1003. toBuffer.append(">;");
  1004. } else {
  1005. toBuffer.append(aType.getSignature());
  1006. }
  1007. }
  1008. /**
  1009. * Useful for writing tests, returns *everything* we know about this member.
  1010. */
  1011. public String toDebugString() {
  1012. StringBuffer r = new StringBuffer();
  1013. // modifiers
  1014. int mods = modifiers;
  1015. if ((mods & 4096) > 0) {
  1016. mods = mods - 4096; // remove synthetic (added in the ASM case but
  1017. }
  1018. // not in the BCEL case...)
  1019. if ((mods & 512) > 0) {
  1020. mods = mods - 512; // remove interface (added in the BCEL case but
  1021. }
  1022. // not in the ASM case...)
  1023. if ((mods & 131072) > 0) {
  1024. mods = mods - 131072; // remove deprecated (added in the ASM case
  1025. }
  1026. // but not in the BCEL case...)
  1027. String modsStr = Modifier.toString(mods);
  1028. if (modsStr.length() != 0) {
  1029. r.append(modsStr).append("(" + mods + ")").append(" ");
  1030. }
  1031. // type variables
  1032. if (typeVariables != null && typeVariables.length > 0) {
  1033. r.append("<");
  1034. for (int i = 0; i < typeVariables.length; i++) {
  1035. if (i > 0) {
  1036. r.append(",");
  1037. }
  1038. TypeVariable t = typeVariables[i];
  1039. r.append(t.toDebugString());
  1040. }
  1041. r.append("> ");
  1042. }
  1043. // 'declaring' type
  1044. r.append(getGenericReturnType().toDebugString());
  1045. r.append(' ');
  1046. // name
  1047. r.append(declaringType.getName());
  1048. r.append('.');
  1049. r.append(name);
  1050. // parameter signature if a method
  1051. if (kind != FIELD) {
  1052. r.append("(");
  1053. UnresolvedType[] params = getGenericParameterTypes();
  1054. boolean parameterNamesExist = showParameterNames && parameterNames != null && parameterNames.length == params.length;
  1055. if (params.length != 0) {
  1056. for (int i = 0, len = params.length; i < len; i++) {
  1057. if (i > 0) {
  1058. r.append(", ");
  1059. }
  1060. r.append(params[i].toDebugString());
  1061. if (parameterNamesExist) {
  1062. r.append(" ").append(parameterNames[i]);
  1063. }
  1064. }
  1065. }
  1066. r.append(")");
  1067. }
  1068. return r.toString();
  1069. }
  1070. // SECRETAPI - controlling whether parameter names come out in the debug
  1071. // string (for testing purposes)
  1072. public static boolean showParameterNames = true;
  1073. public String toGenericString() {
  1074. StringBuffer buf = new StringBuffer();
  1075. buf.append(getGenericReturnType().getSimpleName());
  1076. buf.append(' ');
  1077. buf.append(declaringType.getName());
  1078. buf.append('.');
  1079. buf.append(name);
  1080. if (kind != FIELD) {
  1081. buf.append("(");
  1082. UnresolvedType[] params = getGenericParameterTypes();
  1083. if (params.length != 0) {
  1084. buf.append(params[0].getSimpleName());
  1085. for (int i = 1, len = params.length; i < len; i++) {
  1086. buf.append(", ");
  1087. buf.append(params[i].getSimpleName());
  1088. }
  1089. }
  1090. buf.append(")");
  1091. }
  1092. return buf.toString();
  1093. }
  1094. public boolean isCompatibleWith(Member am) {
  1095. if (kind != METHOD || am.getKind() != METHOD) {
  1096. return true;
  1097. }
  1098. if (!name.equals(am.getName())) {
  1099. return true;
  1100. }
  1101. if (!equalTypes(getParameterTypes(), am.getParameterTypes())) {
  1102. return true;
  1103. }
  1104. return getReturnType().equals(am.getReturnType());
  1105. }
  1106. private static boolean equalTypes(UnresolvedType[] a, UnresolvedType[] b) {
  1107. int len = a.length;
  1108. if (len != b.length) {
  1109. return false;
  1110. }
  1111. for (int i = 0; i < len; i++) {
  1112. if (!a[i].equals(b[i])) {
  1113. return false;
  1114. }
  1115. }
  1116. return true;
  1117. }
  1118. public TypeVariable getTypeVariableNamed(String name) {
  1119. // Check locally...
  1120. if (typeVariables != null) {
  1121. for (int i = 0; i < typeVariables.length; i++) {
  1122. if (typeVariables[i].getName().equals(name)) {
  1123. return typeVariables[i];
  1124. }
  1125. }
  1126. }
  1127. // check the declaring type!
  1128. return declaringType.getTypeVariableNamed(name);
  1129. // Do generic aspects with ITDs that share type variables with the
  1130. // aspect and the target type and have their own tvars cause
  1131. // this to be messier?
  1132. }
  1133. public void evictWeavingState() {
  1134. }
  1135. public boolean isEquivalentTo(Object other) {
  1136. return this.equals(other);
  1137. }
  1138. public boolean isDefaultConstructor() {
  1139. return false;
  1140. }
  1141. }