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

15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
11 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
11 years ago
15 years ago
11 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
11 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
11 years ago
15 years ago
14 years ago
15 years ago
15 years ago
12 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
15 years ago
14 years ago
14 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
15 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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<>();
  111. accumulateTypesInBetween(originalDeclaringType, firstDefiningType, declaringTypes);
  112. Set<ResolvedMember> memberSignatures = new LinkedHashSet<>();
  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<>();
  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<>();
  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 (ResolvedType annotationType : annotationTypes) {
  260. if (annotationType.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 (TypeVariable typeVariable : typeVariables) {
  384. typeVariable.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 (UnresolvedType parameterType : parameterTypes) {
  394. if (s.canCompress()) {
  395. s.writeCompressedSignature(parameterType.getSignature());
  396. } else {
  397. UnresolvedType array_element = parameterType;
  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: &lt;T&gt;
  410. * List&lt;String&gt; getThem(T t) {} we would create: &lt;T:Ljava/lang/Object;&gt;(TT;)Ljava/util/List&lt;Ljava/lang/String;&gt;;;
  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 (TypeVariable typeVariable : typeVariables) {
  419. sb.append(typeVariable.getSignatureForAttribute()); // need
  420. // a
  421. // 'getSignatureForAttribute()'
  422. }
  423. sb.append(">");
  424. }
  425. sb.append("(");
  426. for (UnresolvedType parameterType : parameterTypes) {
  427. ResolvedType ptype = (ResolvedType) parameterType;
  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 (TypeVariable typeVariable : typeVariables) {
  439. sb.append(typeVariable.getSignature());
  440. }
  441. sb.append(">");
  442. }
  443. sb.append("(");
  444. for (UnresolvedType ptype : parameterTypes) {
  445. sb.append(ptype.getSignature());
  446. }
  447. sb.append(")");
  448. sb.append(returnType.getSignature());
  449. return sb.toString();
  450. }
  451. public static void writeArray(ResolvedMember[] members, CompressingDataOutputStream s) throws IOException {
  452. s.writeInt(members.length);
  453. for (ResolvedMember member : members) {
  454. member.write(s);
  455. }
  456. }
  457. public static ResolvedMemberImpl readResolvedMember(VersionedDataInputStream s, ISourceContext sourceContext)
  458. throws IOException {
  459. MemberKind mk = MemberKind.read(s);
  460. boolean compressed = (s.isAtLeast169() ? s.readBoolean() : false);
  461. UnresolvedType declaringType = compressed ? UnresolvedType.forSignature(s.readUtf8(s.readShort())) : UnresolvedType.read(s);
  462. int modifiers = s.readInt();
  463. String name = compressed ? s.readUtf8(s.readShort()) : s.readUTF();
  464. String signature = compressed ? s.readUtf8(s.readShort()) : s.readUTF();
  465. ResolvedMemberImpl m = new ResolvedMemberImpl(mk, declaringType, modifiers, name, signature);
  466. m.checkedExceptions = UnresolvedType.readArray(s);
  467. m.start = s.readInt();
  468. m.end = s.readInt();
  469. m.sourceContext = sourceContext;
  470. if (s.getMajorVersion() >= AjAttribute.WeaverVersionInfo.WEAVER_VERSION_MAJOR_AJ150) {
  471. if (s.getMajorVersion() >= AjAttribute.WeaverVersionInfo.WEAVER_VERSION_MAJOR_AJ150M4) {
  472. boolean isvarargs = s.readBoolean();
  473. if (isvarargs) {
  474. m.setVarargsMethod();
  475. }
  476. }
  477. int tvcount = s.isAtLeast169() ? s.readByte() : s.readInt();
  478. if (tvcount != 0) {
  479. m.typeVariables = new TypeVariable[tvcount];
  480. for (int i = 0; i < tvcount; i++) {
  481. m.typeVariables[i] = TypeVariable.read(s);
  482. m.typeVariables[i].setDeclaringElement(m);
  483. m.typeVariables[i].setRank(i);
  484. }
  485. }
  486. if (s.getMajorVersion() >= AjAttribute.WeaverVersionInfo.WEAVER_VERSION_MAJOR_AJ150M4) {
  487. int pcount = -1;
  488. boolean hasAGenericSignature = false;
  489. if (s.isAtLeast169()) {
  490. pcount = s.readByte();
  491. hasAGenericSignature = (pcount >= 0 && pcount < 255);
  492. } else {
  493. hasAGenericSignature = s.readBoolean();
  494. }
  495. if (hasAGenericSignature) {
  496. int ps = (s.isAtLeast169() ? pcount : s.readInt());
  497. UnresolvedType[] params = new UnresolvedType[ps];
  498. for (int i = 0; i < params.length; i++) {
  499. if (compressed) {
  500. params[i] = TypeFactory.createTypeFromSignature(s.readSignature());
  501. } else {
  502. params[i] = TypeFactory.createTypeFromSignature(s.readUTF());
  503. }
  504. }
  505. UnresolvedType rt = compressed ? TypeFactory.createTypeFromSignature(s.readSignature()) : TypeFactory
  506. .createTypeFromSignature(s.readUTF());
  507. m.parameterTypes = params;
  508. m.returnType = rt;
  509. }
  510. }
  511. }
  512. return m;
  513. }
  514. public static ResolvedMember[] readResolvedMemberArray(VersionedDataInputStream s, ISourceContext context) throws IOException {
  515. int len = s.readInt();
  516. ResolvedMember[] members = new ResolvedMember[len];
  517. for (int i = 0; i < len; i++) {
  518. members[i] = ResolvedMemberImpl.readResolvedMember(s, context);
  519. }
  520. return members;
  521. }
  522. // OPTIMIZE dont like how resolve(world) on ResolvedMemberImpl does
  523. // something different to world.resolve(member)
  524. @Override
  525. public ResolvedMember resolve(World world) {
  526. if (isResolved) {
  527. return this;
  528. }
  529. // make sure all the pieces of a resolvedmember really are resolved
  530. try {
  531. if (typeVariables != null && typeVariables.length > 0) {
  532. for (int i = 0; i < typeVariables.length; i++) {
  533. typeVariables[i] = typeVariables[i].resolve(world);
  534. }
  535. }
  536. world.setTypeVariableLookupScope(this);
  537. // if (annotationTypes != null) {
  538. // Set<ResolvedType> r = new HashSet<ResolvedType>();
  539. // for (UnresolvedType element : annotationTypes) {
  540. // // for (Iterator iter = annotationTypes.iterator(); iter.hasNext();) {
  541. // // UnresolvedType element = (UnresolvedType) iter.next();
  542. // r.add(world.resolve(element));
  543. // }
  544. // annotationTypes = r;
  545. // }
  546. declaringType = declaringType.resolve(world);
  547. if (declaringType.isRawType()) {
  548. declaringType = ((ReferenceType) declaringType).getGenericType();
  549. }
  550. if (parameterTypes != null && parameterTypes.length > 0) {
  551. for (int i = 0; i < parameterTypes.length; i++) {
  552. parameterTypes[i] = parameterTypes[i].resolve(world);
  553. }
  554. }
  555. returnType = returnType.resolve(world);
  556. } finally {
  557. world.setTypeVariableLookupScope(null);
  558. }
  559. isResolved = true;
  560. return this;
  561. }
  562. public ISourceContext getSourceContext(World world) {
  563. return getDeclaringType().resolve(world).getSourceContext();
  564. }
  565. public String[] getParameterNames() {
  566. return parameterNames;
  567. }
  568. public final void setParameterNames(String[] pnames) {
  569. parameterNames = pnames;
  570. }
  571. @Override
  572. public final String[] getParameterNames(World world) {
  573. return getParameterNames();
  574. }
  575. public AjAttribute.EffectiveSignatureAttribute getEffectiveSignature() {
  576. return null;
  577. }
  578. public ISourceLocation getSourceLocation() {
  579. // System.out.println("get context: " + this + " is " + sourceContext);
  580. if (getSourceContext() == null) {
  581. // System.err.println("no context: " + this);
  582. return null;
  583. }
  584. return getSourceContext().makeSourceLocation(this);
  585. }
  586. public int getEnd() {
  587. return end;
  588. }
  589. public ISourceContext getSourceContext() {
  590. return sourceContext;
  591. }
  592. public int getStart() {
  593. return start;
  594. }
  595. public void setPosition(int sourceStart, int sourceEnd) {
  596. this.start = sourceStart;
  597. this.end = sourceEnd;
  598. }
  599. public void setDeclaringType(ReferenceType rt) {
  600. declaringType = rt;
  601. }
  602. public void setSourceContext(ISourceContext sourceContext) {
  603. this.sourceContext = sourceContext;
  604. }
  605. public boolean isAbstract() {
  606. return Modifier.isAbstract(modifiers);
  607. }
  608. public boolean isPublic() {
  609. return Modifier.isPublic(modifiers);
  610. }
  611. public boolean isDefault() {
  612. int mods = getModifiers();
  613. return !(Modifier.isPublic(mods) || Modifier.isProtected(mods) || Modifier.isPrivate(mods));
  614. }
  615. public boolean isVisible(ResolvedType fromType) {
  616. UnresolvedType declaringType = getDeclaringType();
  617. ResolvedType type = null;
  618. if (fromType.equals(declaringType)) {
  619. type = fromType;
  620. } else {
  621. World world = fromType.getWorld();
  622. type = declaringType.resolve(world);
  623. }
  624. return ResolvedType.isVisible(getModifiers(), type, fromType);
  625. }
  626. public void setCheckedExceptions(UnresolvedType[] checkedExceptions) {
  627. this.checkedExceptions = checkedExceptions;
  628. }
  629. public void setAnnotatedElsewhere(boolean b) {
  630. isAnnotatedElsewhere = b;
  631. }
  632. public boolean isAnnotatedElsewhere() {
  633. return isAnnotatedElsewhere;
  634. }
  635. /**
  636. * Get the UnresolvedType for the return type, taking generic signature into account
  637. */
  638. @Override
  639. public UnresolvedType getGenericReturnType() {
  640. return getReturnType();
  641. }
  642. /**
  643. * Get the TypeXs of the parameter types, taking generic signature into account
  644. */
  645. @Override
  646. public UnresolvedType[] getGenericParameterTypes() {
  647. return getParameterTypes();
  648. }
  649. public ResolvedMemberImpl parameterizedWith(UnresolvedType[] typeParameters, ResolvedType newDeclaringType,
  650. boolean isParameterized) {
  651. return parameterizedWith(typeParameters, newDeclaringType, isParameterized, null);
  652. }
  653. /**
  654. * Return a resolvedmember in which all the type variables in the signature have been replaced with the given bindings. The
  655. * 'isParameterized' flag tells us whether we are creating a raw type version or not. if (isParameterized) then List&lt;T&gt; will
  656. * turn into List&lt;String&gt; (for example) - if (!isParameterized) then List&lt;T&gt; will turn into List.
  657. */
  658. public ResolvedMemberImpl parameterizedWith(UnresolvedType[] typeParameters, ResolvedType newDeclaringType,
  659. boolean isParameterized, List<String> aliases) {
  660. // PR308773
  661. // this check had problems for the inner type of a generic type because the inner type can be represented
  662. // by a 'simple type' if it is only sharing type variables with the outer and has none of its own. To avoid the
  663. // check going bang in this case we check for $ (crap...) - we can't check the outer because the declaring type
  664. // is considered unresolved...
  665. if (// isParameterized && <-- might need this bit...
  666. !getDeclaringType().isGenericType() && !getDeclaringType().getName().contains("$")) {
  667. throw new IllegalStateException("Can't ask to parameterize a member of non-generic type: " + getDeclaringType()
  668. + " kind(" + getDeclaringType().typeKind + ")");
  669. }
  670. TypeVariable[] typeVariables = getDeclaringType().getTypeVariables();
  671. if (isParameterized && (typeVariables.length != typeParameters.length)) {
  672. throw new IllegalStateException("Wrong number of type parameters supplied");
  673. }
  674. Map<String, UnresolvedType> typeMap = new HashMap<>();
  675. boolean typeParametersSupplied = typeParameters != null && typeParameters.length > 0;
  676. if (typeVariables != null) {
  677. // If no 'replacements' were supplied in the typeParameters array
  678. // then collapse
  679. // type variables to their first bound.
  680. for (int i = 0; i < typeVariables.length; i++) {
  681. UnresolvedType ut = (!typeParametersSupplied ? typeVariables[i].getFirstBound() : typeParameters[i]);
  682. typeMap.put(typeVariables[i].getName(), ut);
  683. }
  684. }
  685. // For ITDs on generic types that use type variables from the target type, the aliases
  686. // record the alternative names used throughout the ITD expression that must map to
  687. // the same value as the type variables real name.
  688. if (aliases != null) {
  689. int posn = 0;
  690. for (String typeVariableAlias : aliases) {
  691. typeMap.put(typeVariableAlias, (!typeParametersSupplied ? typeVariables[posn].getFirstBound()
  692. : typeParameters[posn]));
  693. posn++;
  694. }
  695. }
  696. UnresolvedType parameterizedReturnType = parameterize(getGenericReturnType(), typeMap, isParameterized,
  697. newDeclaringType.getWorld());
  698. UnresolvedType[] parameterizedParameterTypes = new UnresolvedType[getGenericParameterTypes().length];
  699. UnresolvedType[] genericParameterTypes = getGenericParameterTypes();
  700. for (int i = 0; i < parameterizedParameterTypes.length; i++) {
  701. parameterizedParameterTypes[i] = parameterize(genericParameterTypes[i], typeMap, isParameterized,
  702. newDeclaringType.getWorld());
  703. }
  704. ResolvedMemberImpl ret = new ResolvedMemberImpl(getKind(), newDeclaringType, getModifiers(), parameterizedReturnType,
  705. getName(), parameterizedParameterTypes, getExceptions(), this);
  706. ret.setTypeVariables(getTypeVariables());
  707. ret.setSourceContext(getSourceContext());
  708. ret.setPosition(getStart(), getEnd());
  709. ret.setParameterNames(getParameterNames());
  710. return ret;
  711. }
  712. /**
  713. * Replace occurrences of type variables in the signature with values contained in the map. The map is of the form
  714. * A=String,B=Integer and so a signature List&lt;A&gt; Foo.m(B i) {} would become List&lt;String&gt; Foo.m(Integer i) {}
  715. */
  716. public ResolvedMember parameterizedWith(Map<String, UnresolvedType> m, World w) {
  717. // if (//isParameterized && <-- might need this bit...
  718. // !getDeclaringType().isGenericType()) {
  719. // throw new IllegalStateException(
  720. // "Can't ask to parameterize a member of non-generic type: "
  721. // +getDeclaringType()+" kind("+
  722. // getDeclaringType().typeKind+")");
  723. // }
  724. declaringType = declaringType.resolve(w);
  725. if (declaringType.isRawType()) {
  726. declaringType = ((ResolvedType) declaringType).getGenericType();
  727. // TypeVariable[] typeVariables = getDeclaringType().getTypeVariables();
  728. // if (isParameterized && (typeVariables.length !=
  729. // typeParameters.length)) {
  730. // throw new
  731. // IllegalStateException("Wrong number of type parameters supplied");
  732. // }
  733. // Map typeMap = new HashMap();
  734. // boolean typeParametersSupplied = typeParameters!=null &&
  735. // typeParameters.length>0;
  736. // if (typeVariables!=null) {
  737. // // If no 'replacements' were supplied in the typeParameters array
  738. // then collapse
  739. // // type variables to their first bound.
  740. // for (int i = 0; i < typeVariables.length; i++) {
  741. // UnresolvedType ut =
  742. // (!typeParametersSupplied?typeVariables[i].getFirstBound
  743. // ():typeParameters[i]);
  744. // typeMap.put(typeVariables[i].getName(),ut);
  745. // }
  746. // }
  747. // // For ITDs on generic types that use type variables from the target
  748. // type, the aliases
  749. // // record the alternative names used throughout the ITD expression
  750. // that must map to
  751. // // the same value as the type variables real name.
  752. // if (aliases!=null) {
  753. // int posn = 0;
  754. // for (Iterator iter = aliases.iterator(); iter.hasNext();) {
  755. // String typeVariableAlias = (String) iter.next();
  756. // typeMap.put(typeVariableAlias,(!typeParametersSupplied?typeVariables[
  757. // posn].getFirstBound():typeParameters[posn]));
  758. // posn++;
  759. // }
  760. // }
  761. }
  762. UnresolvedType parameterizedReturnType = parameterize(getGenericReturnType(), m, true, w);
  763. UnresolvedType[] parameterizedParameterTypes = new UnresolvedType[getGenericParameterTypes().length];
  764. UnresolvedType[] genericParameterTypes = getGenericParameterTypes();
  765. for (int i = 0; i < parameterizedParameterTypes.length; i++) {
  766. parameterizedParameterTypes[i] = parameterize(genericParameterTypes[i], m, true, w);
  767. }
  768. ResolvedMemberImpl ret = new ResolvedMemberImpl(getKind(), declaringType, getModifiers(), parameterizedReturnType,
  769. getName(), parameterizedParameterTypes, getExceptions(), this);
  770. ret.setTypeVariables(getTypeVariables());
  771. ret.setSourceContext(getSourceContext());
  772. ret.setPosition(getStart(), getEnd());
  773. ret.setParameterNames(getParameterNames());
  774. return ret;
  775. }
  776. public void setTypeVariables(TypeVariable[] tvars) {
  777. typeVariables = tvars;
  778. }
  779. public TypeVariable[] getTypeVariables() {
  780. return typeVariables;
  781. }
  782. protected UnresolvedType parameterize(UnresolvedType aType, Map<String, UnresolvedType> typeVariableMap,
  783. boolean inParameterizedType, World w) {
  784. if (aType instanceof TypeVariableReference) {
  785. String variableName = ((TypeVariableReference) aType).getTypeVariable().getName();
  786. if (!typeVariableMap.containsKey(variableName)) {
  787. return aType; // if the type variable comes from the method (and
  788. // not the type) thats OK
  789. }
  790. return typeVariableMap.get(variableName);
  791. } else if (aType.isParameterizedType()) {
  792. if (inParameterizedType) {
  793. if (w != null) {
  794. aType = aType.resolve(w);
  795. } else {
  796. UnresolvedType dType = getDeclaringType();
  797. aType = aType.resolve(((ResolvedType) dType).getWorld());
  798. }
  799. return aType.parameterize(typeVariableMap);
  800. } else {
  801. return aType.getRawType();
  802. }
  803. } else if (aType.isArray()) {
  804. // The component type might be a type variable (pr150095)
  805. int dims = 1;
  806. String sig = aType.getSignature();
  807. // while (sig.charAt(dims) == '[')
  808. // dims++;
  809. UnresolvedType arrayType = null;
  810. UnresolvedType componentSig = UnresolvedType.forSignature(sig.substring(dims));
  811. UnresolvedType parameterizedComponentSig = parameterize(componentSig, typeVariableMap, inParameterizedType, w);
  812. if (parameterizedComponentSig.isTypeVariableReference()
  813. && parameterizedComponentSig instanceof UnresolvedTypeVariableReferenceType
  814. && typeVariableMap.containsKey(((UnresolvedTypeVariableReferenceType) parameterizedComponentSig)
  815. .getTypeVariable().getName())) { // pr250632
  816. // TODO ASC bah, this code is rubbish - i should fix it properly
  817. StringBuffer newsig = new StringBuffer();
  818. newsig.append("[T");
  819. newsig.append(((UnresolvedTypeVariableReferenceType) parameterizedComponentSig).getTypeVariable().getName());
  820. newsig.append(";");
  821. arrayType = UnresolvedType.forSignature(newsig.toString());
  822. } else {
  823. arrayType = ResolvedType.makeArray(parameterizedComponentSig, dims);
  824. }
  825. return arrayType;
  826. }
  827. return aType;
  828. }
  829. /**
  830. * If this member is defined by a parameterized super-type, return the erasure of that member. For example: interface I&lt;T&gt; { T
  831. * foo(T aTea); } class C implements I&lt;String&gt; { String foo(String aString) { return "something"; } } The resolved member for
  832. * C.foo has signature String foo(String). The erasure of that member is Object foo(Object) -- use upper bound of type variable.
  833. * A type is a supertype of itself.
  834. */
  835. // public ResolvedMember getErasure() {
  836. // if (calculatedMyErasure) return myErasure;
  837. // calculatedMyErasure = true;
  838. // ResolvedType resolvedDeclaringType = (ResolvedType) getDeclaringType();
  839. // // this next test is fast, and the result is cached.
  840. // if (!resolvedDeclaringType.hasParameterizedSuperType()) {
  841. // return null;
  842. // } else {
  843. // // we have one or more parameterized super types.
  844. // // this member may be defined by one of them... we need to find out.
  845. // Collection declaringTypes =
  846. // this.getDeclaringTypes(resolvedDeclaringType.getWorld());
  847. // for (Iterator iter = declaringTypes.iterator(); iter.hasNext();) {
  848. // ResolvedType aDeclaringType = (ResolvedType) iter.next();
  849. // if (aDeclaringType.isParameterizedType()) {
  850. // // we've found the (a?) parameterized type that defines this member.
  851. // // now get the erasure of it
  852. // ResolvedMemberImpl matchingMember = (ResolvedMemberImpl)
  853. // aDeclaringType.lookupMemberNoSupers(this);
  854. // if (matchingMember != null && matchingMember.backingGenericMember !=
  855. // null) {
  856. // myErasure = matchingMember.backingGenericMember;
  857. // return myErasure;
  858. // }
  859. // }
  860. // }
  861. // }
  862. // return null;
  863. // }
  864. //
  865. // private ResolvedMember myErasure = null;
  866. // private boolean calculatedMyErasure = false;
  867. public boolean hasBackingGenericMember() {
  868. return backingGenericMember != null;
  869. }
  870. public ResolvedMember getBackingGenericMember() {
  871. return backingGenericMember;
  872. }
  873. /**
  874. * For ITDs, we use the default factory methods to build a resolved member, then alter a couple of characteristics using this
  875. * method - this is safe.
  876. */
  877. public void resetName(String newName) {
  878. this.name = newName;
  879. }
  880. public void resetKind(MemberKind newKind) {
  881. this.kind = newKind;
  882. }
  883. public void resetModifiers(int newModifiers) {
  884. this.modifiers = newModifiers;
  885. }
  886. public void resetReturnTypeToObjectArray() {
  887. returnType = UnresolvedType.OBJECTARRAY;
  888. }
  889. /**
  890. * Returns true if this member matches the other. The matching takes into account name and parameter types only. When comparing
  891. * parameter types, we allow any type variable to match any other type variable regardless of bounds.
  892. */
  893. public boolean matches(ResolvedMember aCandidateMatch, boolean ignoreGenerics) {
  894. ResolvedMemberImpl candidateMatchImpl = (ResolvedMemberImpl) aCandidateMatch;
  895. if (!getName().equals(aCandidateMatch.getName())) {
  896. return false;
  897. }
  898. UnresolvedType[] parameterTypes = getGenericParameterTypes();
  899. UnresolvedType[] candidateParameterTypes = aCandidateMatch.getGenericParameterTypes();
  900. if (parameterTypes.length != candidateParameterTypes.length) {
  901. return false;
  902. }
  903. boolean b = false;
  904. /*
  905. * if (ignoreGenerics) { String myParameterSignature = getParameterSigWithBoundsRemoved(); String
  906. * candidateParameterSignature = candidateMatchImpl.getParameterSigWithBoundsRemoved(); if
  907. * (myParameterSignature.equals(candidateParameterSignature)) { b = true; } else { myParameterSignature =
  908. * (hasBackingGenericMember() ? backingGenericMember.getParameterSignatureErased() : getParameterSignatureErased());
  909. * candidateParameterSignature = (candidateMatchImpl.hasBackingGenericMember() ? candidateMatchImpl.backingGenericMember
  910. * .getParameterSignatureErased() : candidateMatchImpl.getParameterSignatureErased()); // System.out.println("my psig = " +
  911. * myParameterSignature); // System.out.println("can psig = " + candidateParameterSignature); b =
  912. * myParameterSignature.equals(candidateParameterSignature); } } else {
  913. */
  914. String myParameterSignature = getParameterSigWithBoundsRemoved();
  915. String candidateParameterSignature = candidateMatchImpl.getParameterSigWithBoundsRemoved();
  916. if (myParameterSignature.equals(candidateParameterSignature)) {
  917. b = true;
  918. } else {
  919. // try erasure
  920. myParameterSignature = getParameterSignatureErased();
  921. candidateParameterSignature = candidateMatchImpl.getParameterSignatureErased();
  922. // myParameterSignature = (hasBackingGenericMember() ? backingGenericMember.getParameterSignatureErased()
  923. // : getParameterSignatureErased());
  924. // candidateParameterSignature = (candidateMatchImpl.hasBackingGenericMember() ?
  925. // candidateMatchImpl.backingGenericMember
  926. // .getParameterSignatureErased() : candidateMatchImpl.getParameterSignatureErased());
  927. // System.out.println("my psig = " + myParameterSignature);
  928. // System.out.println("can psig = " + candidateParameterSignature);
  929. b = myParameterSignature.equals(candidateParameterSignature);
  930. // }
  931. }
  932. // System.out.println("Checking param signatures: " + b);
  933. return b;
  934. }
  935. /**
  936. * converts e.g. <T extends Number>.... List<T> to just Ljava/util/List<T;>; whereas the full signature would be
  937. * Ljava/util/List<T:Ljava/lang/Number;>;
  938. */
  939. private String myParameterSignatureWithBoundsRemoved = null;
  940. /**
  941. * converts e.g. <T extends Number>.... List<T> to just Ljava/util/List;
  942. */
  943. private String myParameterSignatureErasure = null;
  944. // does NOT produce a meaningful java signature, but does give a unique
  945. // string suitable for
  946. // comparison.
  947. private String getParameterSigWithBoundsRemoved() {
  948. if (myParameterSignatureWithBoundsRemoved != null) {
  949. return myParameterSignatureWithBoundsRemoved;
  950. }
  951. StringBuffer sig = new StringBuffer();
  952. UnresolvedType[] myParameterTypes = getGenericParameterTypes();
  953. for (UnresolvedType myParameterType : myParameterTypes) {
  954. appendSigWithTypeVarBoundsRemoved(myParameterType, sig, new HashSet<>());
  955. }
  956. myParameterSignatureWithBoundsRemoved = sig.toString();
  957. return myParameterSignatureWithBoundsRemoved;
  958. }
  959. /**
  960. * Return the erased form of the signature with bounds collapsed for type variables, etc. Does not include the return type, @see
  961. * getParam
  962. */
  963. public String getParameterSignatureErased() {
  964. if (myParameterSignatureErasure == null) {
  965. StringBuilder sig = new StringBuilder();
  966. for (UnresolvedType parameter : getParameterTypes()) {
  967. sig.append(parameter.getErasureSignature());
  968. }
  969. myParameterSignatureErasure = sig.toString();
  970. }
  971. return myParameterSignatureErasure;
  972. }
  973. public String getSignatureErased() {
  974. StringBuffer sb = new StringBuffer();
  975. sb.append("(");
  976. sb.append(getParameterSignatureErased());
  977. sb.append(")");
  978. sb.append(getReturnType().getErasureSignature());
  979. return sb.toString();
  980. }
  981. // does NOT produce a meaningful java signature, but does give a unique
  982. // string suitable for
  983. // comparison.
  984. public static void appendSigWithTypeVarBoundsRemoved(UnresolvedType aType, StringBuffer toBuffer,
  985. Set<UnresolvedType> alreadyUsedTypeVars) {
  986. if (aType.isTypeVariableReference()) {
  987. TypeVariableReferenceType typeVariableRT = (TypeVariableReferenceType) aType;
  988. // pr204505
  989. if (alreadyUsedTypeVars.contains(aType)) {
  990. toBuffer.append("...");
  991. } else {
  992. alreadyUsedTypeVars.add(aType);
  993. appendSigWithTypeVarBoundsRemoved(typeVariableRT.getTypeVariable().getFirstBound(), toBuffer, alreadyUsedTypeVars);
  994. }
  995. // toBuffer.append("T;");
  996. } else if (aType.isParameterizedType()) {
  997. toBuffer.append(aType.getRawType().getSignature());
  998. toBuffer.append("<");
  999. for (int i = 0; i < aType.getTypeParameters().length; i++) {
  1000. appendSigWithTypeVarBoundsRemoved(aType.getTypeParameters()[i], toBuffer, alreadyUsedTypeVars);
  1001. }
  1002. toBuffer.append(">;");
  1003. } else {
  1004. toBuffer.append(aType.getSignature());
  1005. }
  1006. }
  1007. /**
  1008. * Useful for writing tests, returns *everything* we know about this member.
  1009. */
  1010. public String toDebugString() {
  1011. StringBuffer r = new StringBuffer();
  1012. // modifiers
  1013. int mods = modifiers;
  1014. if ((mods & 4096) > 0) {
  1015. mods = mods - 4096; // remove synthetic (added in the ASM case but
  1016. }
  1017. // not in the BCEL case...)
  1018. if ((mods & 512) > 0) {
  1019. mods = mods - 512; // remove interface (added in the BCEL case but
  1020. }
  1021. // not in the ASM case...)
  1022. if ((mods & 131072) > 0) {
  1023. mods = mods - 131072; // remove deprecated (added in the ASM case
  1024. }
  1025. // but not in the BCEL case...)
  1026. String modsStr = Modifier.toString(mods);
  1027. if (modsStr.length() != 0) {
  1028. r.append(modsStr).append("(" + mods + ")").append(" ");
  1029. }
  1030. // type variables
  1031. if (typeVariables != null && typeVariables.length > 0) {
  1032. r.append("<");
  1033. for (int i = 0; i < typeVariables.length; i++) {
  1034. if (i > 0) {
  1035. r.append(",");
  1036. }
  1037. TypeVariable t = typeVariables[i];
  1038. r.append(t.toDebugString());
  1039. }
  1040. r.append("> ");
  1041. }
  1042. // 'declaring' type
  1043. r.append(getGenericReturnType().toDebugString());
  1044. r.append(' ');
  1045. // name
  1046. r.append(declaringType.getName());
  1047. r.append('.');
  1048. r.append(name);
  1049. // parameter signature if a method
  1050. if (kind != FIELD) {
  1051. r.append("(");
  1052. UnresolvedType[] params = getGenericParameterTypes();
  1053. boolean parameterNamesExist = showParameterNames && parameterNames != null && parameterNames.length == params.length;
  1054. if (params.length != 0) {
  1055. for (int i = 0, len = params.length; i < len; i++) {
  1056. if (i > 0) {
  1057. r.append(", ");
  1058. }
  1059. r.append(params[i].toDebugString());
  1060. if (parameterNamesExist) {
  1061. r.append(" ").append(parameterNames[i]);
  1062. }
  1063. }
  1064. }
  1065. r.append(")");
  1066. }
  1067. return r.toString();
  1068. }
  1069. // SECRETAPI - controlling whether parameter names come out in the debug
  1070. // string (for testing purposes)
  1071. public static boolean showParameterNames = true;
  1072. public String toGenericString() {
  1073. StringBuffer buf = new StringBuffer();
  1074. buf.append(getGenericReturnType().getSimpleName());
  1075. buf.append(' ');
  1076. buf.append(declaringType.getName());
  1077. buf.append('.');
  1078. buf.append(name);
  1079. if (kind != FIELD) {
  1080. buf.append("(");
  1081. UnresolvedType[] params = getGenericParameterTypes();
  1082. if (params.length != 0) {
  1083. buf.append(params[0].getSimpleName());
  1084. for (int i = 1, len = params.length; i < len; i++) {
  1085. buf.append(", ");
  1086. buf.append(params[i].getSimpleName());
  1087. }
  1088. }
  1089. buf.append(")");
  1090. }
  1091. return buf.toString();
  1092. }
  1093. public boolean isCompatibleWith(Member am) {
  1094. if (kind != METHOD || am.getKind() != METHOD) {
  1095. return true;
  1096. }
  1097. if (!name.equals(am.getName())) {
  1098. return true;
  1099. }
  1100. if (!equalTypes(getParameterTypes(), am.getParameterTypes())) {
  1101. return true;
  1102. }
  1103. return getReturnType().equals(am.getReturnType());
  1104. }
  1105. private static boolean equalTypes(UnresolvedType[] a, UnresolvedType[] b) {
  1106. int len = a.length;
  1107. if (len != b.length) {
  1108. return false;
  1109. }
  1110. for (int i = 0; i < len; i++) {
  1111. if (!a[i].equals(b[i])) {
  1112. return false;
  1113. }
  1114. }
  1115. return true;
  1116. }
  1117. public TypeVariable getTypeVariableNamed(String name) {
  1118. // Check locally...
  1119. if (typeVariables != null) {
  1120. for (TypeVariable typeVariable : typeVariables) {
  1121. if (typeVariable.getName().equals(name)) {
  1122. return typeVariable;
  1123. }
  1124. }
  1125. }
  1126. // check the declaring type!
  1127. return declaringType.getTypeVariableNamed(name);
  1128. // Do generic aspects with ITDs that share type variables with the
  1129. // aspect and the target type and have their own tvars cause
  1130. // this to be messier?
  1131. }
  1132. public void evictWeavingState() {
  1133. }
  1134. public boolean isEquivalentTo(Object other) {
  1135. return this.equals(other);
  1136. }
  1137. public boolean isDefaultConstructor() {
  1138. return false;
  1139. }
  1140. }