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.

AsmDelegateTests.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* *******************************************************************
  2. * Copyright (c) 2006 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. * Contributors:
  10. * Andy Clement initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.util.Enumeration;
  16. import java.util.zip.ZipEntry;
  17. import java.util.zip.ZipFile;
  18. import org.aspectj.weaver.AbstractReferenceTypeDelegate;
  19. import org.aspectj.weaver.AbstractWorldTestCase;
  20. import org.aspectj.weaver.AjAttribute;
  21. import org.aspectj.weaver.AnnotationX;
  22. import org.aspectj.weaver.BcweaverTests;
  23. import org.aspectj.weaver.ReferenceType;
  24. import org.aspectj.weaver.ReferenceTypeDelegate;
  25. import org.aspectj.weaver.ResolvedMember;
  26. import org.aspectj.weaver.ResolvedMemberImpl;
  27. import org.aspectj.weaver.ResolvedType;
  28. import org.aspectj.weaver.ShadowMunger;
  29. import org.aspectj.weaver.TypeVariable;
  30. import org.aspectj.weaver.World;
  31. import org.aspectj.weaver.AjAttribute.EffectiveSignatureAttribute;
  32. import org.aspectj.weaver.asm.AsmDelegate;
  33. import org.aspectj.weaver.asm.AsmField;
  34. import org.aspectj.weaver.asm.AsmMethod;
  35. /**
  36. * This is a test case for the nameType parts of worlds.
  37. */
  38. public class AsmDelegateTests extends AbstractWorldTestCase {
  39. private final BcelWorld world = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");
  40. public AsmDelegateTests(String name) {
  41. super(name);
  42. }
  43. protected World getWorld() {
  44. return world;
  45. }
  46. // --- testcode
  47. public void testWeDontGoBang() {
  48. world.fallbackToLoadingBcelDelegatesForAspects = false;
  49. ResolvedType rt = world.resolve("SimpleAspect");
  50. ReferenceTypeDelegate delegate = ((ReferenceType)rt).getDelegate();
  51. assertTrue("Should be an ASM delegate but is "+delegate.getClass(),
  52. delegate.getClass().toString().equals("class org.aspectj.weaver.asm.AsmDelegate"));
  53. }
  54. public void testDifferentiatingBetweenAspectAndClass() {
  55. ReferenceType rtAspect = (ReferenceType)world.resolve("SimpleAspect");
  56. ReferenceType rtString = (ReferenceType)world.resolve("java.lang.String");
  57. assertTrue("SimpleAspect should be an aspect",rtAspect.isAspect());
  58. assertTrue("String should not be an aspect",!rtString.isAspect());
  59. assertTrue("Should be a persingleton "+rtAspect.getPerClause(),rtAspect.getPerClause().toString().startsWith("persingleton"));
  60. }
  61. public void testRecognizingDifferentTypes() {
  62. ResolvedType rtAnnotation = world.resolve("SimpleAnnotation");
  63. ResolvedType rtEnum = world.resolve("SimpleEnum");
  64. ResolvedType rtString = world.resolve("java.lang.String");
  65. assertTrue("Should be an annotation type",rtAnnotation.isAnnotation());
  66. assertTrue("Should be an enum type",rtEnum.isEnum());
  67. assertTrue("Should not be an annotation or enum type",!(rtString.isAnnotation() || rtString.isEnum()));
  68. }
  69. public void testAnnotationsBehaving() {
  70. ReferenceType rtAnnotation = (ReferenceType)world.resolve("SimpleAnnotation");
  71. assertTrue("Should be SOURCE but is "+rtAnnotation.getRetentionPolicy(),rtAnnotation.getRetentionPolicy().equals("SOURCE"));
  72. ReferenceType rtAnnotation2 = (ReferenceType)world.resolve("SimpleAnnotation2");
  73. assertTrue("Should be CLASS but is "+rtAnnotation2.getRetentionPolicy(),rtAnnotation2.getRetentionPolicy().equals("CLASS"));
  74. ReferenceType rtAnnotation3 = (ReferenceType)world.resolve("SimpleAnnotation3");
  75. assertTrue("Should be RUNTIME but is "+rtAnnotation3.getRetentionPolicy(),rtAnnotation3.getRetentionPolicy().equals("RUNTIME"));
  76. ReferenceType rtAnnotation4 = (ReferenceType)world.resolve("SimpleAnnotation4");
  77. assertTrue("Should be CLASS but is "+rtAnnotation4.getRetentionPolicy(),rtAnnotation4.getRetentionPolicy().equals("CLASS"));
  78. }
  79. public void testInterfaceflag() {
  80. ReferenceType rtString = (ReferenceType)world.resolve("java.lang.String");
  81. assertTrue("String should not be an interface",!rtString.isInterface());
  82. ReferenceType rtSerializable = (ReferenceType)world.resolve("java.io.Serializable");
  83. assertTrue("Serializable should be an interface",rtSerializable.isInterface());
  84. }
  85. public void testCompareDelegates() {
  86. BcelWorld slowWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");
  87. slowWorld.setFastDelegateSupport(false);
  88. BcelWorld fastWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");
  89. ReferenceType bcelList = (ReferenceType)slowWorld.resolve("java.util.List");
  90. ReferenceType asmList = (ReferenceType)fastWorld.resolve("java.util.List");
  91. assertTrue("Should be a bcel delegate? "+bcelList.getDelegate().getClass(),
  92. bcelList.getDelegate().getClass().toString().equals("class org.aspectj.weaver.bcel.BcelObjectType"));
  93. assertTrue("Should be an asm delegate? "+asmList.getDelegate().getClass(),
  94. asmList.getDelegate().getClass().toString().equals("class org.aspectj.weaver.asm.AsmDelegate"));
  95. TypeVariable[] bcelTVars = bcelList.getTypeVariables();
  96. TypeVariable[] asmTVars = asmList.getTypeVariables();
  97. for (int i = 0; i < asmTVars.length; i++) {
  98. TypeVariable variable = asmTVars[i];
  99. }
  100. String bcelsig = bcelList.getSignature();
  101. String asmsig = asmList.getSignature();
  102. assertTrue("Signatures should be the same but "+bcelsig+"!="+asmsig,bcelsig.equals(asmsig));
  103. String bcelerasuresig = bcelList.getErasureSignature();
  104. String asmerasuresig = asmList.getErasureSignature();
  105. assertTrue("Erasure Signatures should be the same but "+bcelerasuresig+"!="+asmerasuresig,bcelerasuresig.equals(asmerasuresig));
  106. ResolvedMember[] bcelfields = bcelList.getDeclaredFields();
  107. ResolvedMember[] asmfields = asmList.getDeclaredFields();
  108. if (bcelfields.length!=asmfields.length) {
  109. fail("Dont have the same number of fields? bcel="+bcelfields.length+" asm="+asmfields.length);
  110. }
  111. for (int i = 0; i < asmfields.length; i++) {
  112. ResolvedMember member = asmfields[i];
  113. if (!bcelfields[i].equals(asmfields[i])) {
  114. fail("Differing fields: "+bcelfields[i]+" and "+asmfields[i]);
  115. }
  116. }
  117. }
  118. public void testCompareDelegatesComplex() {
  119. BcelWorld slowWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");
  120. slowWorld.setFastDelegateSupport(false);
  121. BcelWorld fastWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");
  122. ReferenceType bComplex = (ReferenceType)slowWorld.resolve("Complex");
  123. ReferenceType aComplex = (ReferenceType)fastWorld.resolve("Complex");
  124. checkEquivalent("",(AbstractReferenceTypeDelegate)aComplex.getDelegate(),(AbstractReferenceTypeDelegate)bComplex.getDelegate());
  125. }
  126. public void testCompareDelegatesMonster() {
  127. BcelWorld slowWorld = new BcelWorld("../lib/aspectj/lib/aspectjtools.jar");slowWorld.setFastDelegateSupport(false);
  128. BcelWorld fastWorld = new BcelWorld("../lib/aspectj/lib/aspectjtools.jar");
  129. ResolvedMemberImpl.showParameterNames=false;
  130. try {
  131. File f = new File("../lib/aspectj/lib/aspectjtools.jar");
  132. assertTrue("Couldnt find aspectjtools to test. Tried: "+f.getAbsolutePath(),f.exists());
  133. ZipFile zf = new ZipFile(f);
  134. int i = 0;
  135. Enumeration entries = zf.entries();
  136. while (entries.hasMoreElements()) {
  137. ZipEntry zfe = (ZipEntry)entries.nextElement();
  138. String classfileName = zfe.getName();
  139. if (classfileName.endsWith(".class")) {
  140. String clazzname = classfileName.substring(0,classfileName.length()-6).replace('/','.');
  141. ReferenceType b = (ReferenceType)slowWorld.resolve(clazzname);
  142. ReferenceType a = (ReferenceType)fastWorld.resolve(clazzname);
  143. checkEquivalent("Comparison number #"+(i++)+" ",(AbstractReferenceTypeDelegate)a.getDelegate(),(AbstractReferenceTypeDelegate)b.getDelegate());
  144. }
  145. }
  146. //System.err.println();("Successfully compared "+i+" entries!!");
  147. } catch (IOException e) {
  148. e.printStackTrace();
  149. fail(e.getMessage());
  150. }
  151. }
  152. public void testCompareDelegatesLoadingPerformance() {
  153. BcelWorld slowWorld = new BcelWorld("../lib/aspectj/lib/aspectjtools.jar");slowWorld.setFastDelegateSupport(false);
  154. BcelWorld fastWorld = new BcelWorld("../lib/aspectj/lib/aspectjtools.jar");
  155. ResolvedMemberImpl.showParameterNames=false;
  156. try {
  157. File f = new File("../lib/aspectj/lib/aspectjtools.jar");
  158. assertTrue("Couldnt find aspectjtools to test. Tried: "+f.getAbsolutePath(),f.exists());
  159. ZipFile zf = new ZipFile(f);
  160. int i = 0;
  161. long stime = System.currentTimeMillis();
  162. Enumeration entries = zf.entries();
  163. while (entries.hasMoreElements()) {
  164. ZipEntry zfe = (ZipEntry)entries.nextElement();
  165. String classfileName = zfe.getName();
  166. if (classfileName.endsWith(".class")) {
  167. String clazzname = classfileName.substring(0,classfileName.length()-6).replace('/','.');
  168. ReferenceType b = (ReferenceType)slowWorld.resolve(clazzname);
  169. i++;
  170. }
  171. }
  172. long etime = System.currentTimeMillis();
  173. System.err.println("Time taken to load "+i+" entries with BCEL="+(etime-stime)+"ms");
  174. //System.err.println();("Successfully compared "+i+" entries!!");
  175. } catch (IOException e) {
  176. e.printStackTrace();
  177. fail(e.getMessage());
  178. }
  179. try {
  180. File f = new File("../lib/aspectj/lib/aspectjtools.jar");
  181. assertTrue("Couldnt find aspectjtools to test. Tried: "+f.getAbsolutePath(),f.exists());
  182. ZipFile zf = new ZipFile(f);
  183. int i = 0;
  184. long stime = System.currentTimeMillis();
  185. Enumeration entries = zf.entries();
  186. while (entries.hasMoreElements()) {
  187. ZipEntry zfe = (ZipEntry)entries.nextElement();
  188. String classfileName = zfe.getName();
  189. if (classfileName.endsWith(".class")) {
  190. String clazzname = classfileName.substring(0,classfileName.length()-6).replace('/','.');
  191. ReferenceType b = (ReferenceType)fastWorld.resolve(clazzname);
  192. i++;
  193. }
  194. }
  195. long etime = System.currentTimeMillis();
  196. System.err.println("Time taken to load "+i+" entries with ASM="+(etime-stime)+"ms");
  197. //System.err.println();("Successfully compared "+i+" entries!!");
  198. } catch (IOException e) {
  199. e.printStackTrace();
  200. fail(e.getMessage());
  201. }
  202. }
  203. private void checkEquivalent(String prefix,AbstractReferenceTypeDelegate asmType,AbstractReferenceTypeDelegate bcelType) {
  204. assertTrue("Should be a bcel delegate? "+bcelType.getClass(),
  205. bcelType.getClass().toString().equals("class org.aspectj.weaver.bcel.BcelObjectType"));
  206. assertTrue("Should be an asm delegate? "+asmType.getClass(),
  207. asmType.getClass().toString().equals("class org.aspectj.weaver.asm.AsmDelegate"));
  208. String asmString = asmType.stringifyDelegate();
  209. String bcelString= bcelType.stringifyDelegate();
  210. if (!asmString.equals(bcelString)) {
  211. fail(prefix+"Delegates don't match for "+bcelType.getResolvedTypeX()+"\n ASM=\n"+asmString+"\n BCEL=\n"+bcelString);
  212. }
  213. }
  214. private void compareAnnotations(String n,World bcelWorld,World asmWorld) {
  215. ReferenceType bcelT = (ReferenceType)bcelWorld.resolve(n);
  216. ReferenceType asmT = (ReferenceType)asmWorld.resolve(n);
  217. ensureTheSame(bcelT.getAnnotations(),asmT.getAnnotations());
  218. }
  219. private void ensureTheSame(AnnotationX[] bcelSet,AnnotationX[] asmSet) {
  220. String bcelString = stringify(bcelSet);
  221. String asmString = stringify(asmSet);
  222. if (bcelSet.length!=asmSet.length) {
  223. fail("Lengths are different!!! Not a good start. \nBcel reports: \n"+bcelString+" Asm reports: \n"+asmString);
  224. }
  225. assertTrue("Different answers. \nBcel reports: \n"+bcelString+" Asm reports: \n"+asmString,bcelString.equals(asmString));
  226. }
  227. public String stringify(AnnotationX[] annotations) {
  228. if (annotations==null) return "";
  229. StringBuffer sb = new StringBuffer();
  230. for (int i = 0; i < annotations.length; i++) {
  231. AnnotationX annotationX = annotations[i];
  232. sb.append(i+") "+annotationX.toString());
  233. sb.append("\n");
  234. }
  235. return sb.toString();
  236. }
  237. public void testDifferentAnnotationKinds() {
  238. BcelWorld slowWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");slowWorld.setFastDelegateSupport(false);
  239. BcelWorld fastWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");
  240. compareAnnotations("AnnotatedClass",slowWorld,fastWorld);
  241. compareAnnotations("AnnotatedFields",slowWorld,fastWorld);
  242. compareAnnotations("AnnotatedMethods",slowWorld,fastWorld);
  243. compareAnnotations("AnnotatedWithClassClass",slowWorld,fastWorld);
  244. compareAnnotations("AnnotatedWithCombinedAnnotation",slowWorld,fastWorld);
  245. compareAnnotations("AnnotatedWithEnumClass",slowWorld,fastWorld);
  246. compareAnnotations("AnnotationClassElement",slowWorld,fastWorld);
  247. compareAnnotations("AnnotationEnumElement",slowWorld,fastWorld);
  248. compareAnnotations("ComplexAnnotation",slowWorld,fastWorld);
  249. compareAnnotations("CombinedAnnotation",slowWorld,fastWorld);
  250. compareAnnotations("ComplexAnnotatedClass",slowWorld,fastWorld);
  251. }
  252. /**
  253. * Load up the AspectFromHell and take it apart...
  254. */
  255. public void testLoadingAttributesForTypes() {
  256. BcelWorld slowWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");
  257. slowWorld.setFastDelegateSupport(false);
  258. BcelWorld fastWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");
  259. fastWorld.fallbackToLoadingBcelDelegatesForAspects = false;
  260. ReferenceType bcelT = (ReferenceType)slowWorld.resolve("AspectFromHell");
  261. ReferenceType asmT = (ReferenceType)fastWorld.resolve("AspectFromHell");
  262. AsmDelegate asmD = (AsmDelegate)asmT.getDelegate();
  263. String [] asmAttributeNames = asmD.getAttributeNames();
  264. BcelObjectType bcelD = (BcelObjectType)bcelT.getDelegate();
  265. String [] bcelAttributeNames = bcelD.getAttributeNames();
  266. // Won't be exactly the same number as ASM currently processes some and then discards them - effectively those stored in the delegate
  267. // are the 'not yet processed' ones
  268. // should be 6 type mungers
  269. AjAttribute[] asmTypeMungers = asmD.getAttributes("org.aspectj.weaver.TypeMunger");
  270. AjAttribute[] bcelTypeMungers = bcelD.getAttributes("org.aspectj.weaver.TypeMunger");
  271. assertTrue("Should be 6 type mungers but asm="+asmTypeMungers.length+" bcel="+bcelTypeMungers.length,asmTypeMungers.length==6 && bcelTypeMungers.length==6);
  272. }
  273. public void testLoadingAttributesForMethods() {
  274. boolean debug = false;
  275. BcelWorld slowWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");slowWorld.setFastDelegateSupport(false);
  276. BcelWorld fastWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");
  277. fastWorld.fallbackToLoadingBcelDelegatesForAspects = false;
  278. ReferenceType bcelT = (ReferenceType)slowWorld.resolve("AspectFromHell");
  279. ReferenceType asmT = (ReferenceType)fastWorld.resolve("AspectFromHell");
  280. ResolvedMember[] bcelMethods = bcelT.getDeclaredMethods();
  281. ResolvedMember[] asmMethods = asmT.getDeclaredMethods();
  282. for (int i = 0; i < bcelMethods.length; i++) {
  283. BcelMethod bmember = (BcelMethod)bcelMethods[i];
  284. AsmMethod amember = (AsmMethod)asmMethods[i];
  285. assertTrue("Seem to be in a muddle. a="+amember.getName()+" b="+bmember.getName(),
  286. amember.getName().equals(bmember.getName()));
  287. if (debug) System.err.println("Looking at "+bmember);
  288. String[] bcelMemberAttributes = bmember.getAttributeNames(true);
  289. String[] asmMemberAttributes = amember.getAttributeNames(true);
  290. // System.err.println("BCEL=>\n"+stringifyStringArray(bcelMemberAttributes));
  291. // System.err.println(" ASM=>\n"+stringifyStringArray(asmMemberAttributes));
  292. compareAttributeNames(bcelMemberAttributes,asmMemberAttributes);
  293. // Let's check the member ones of interest:
  294. // org.aspectj.weaver.AjSynthetic
  295. if (bmember.isAjSynthetic()) {
  296. assertTrue("Why isnt the ASM method ajsynthetic? "+amember.toDebugString(),amember.isAjSynthetic());
  297. } else {
  298. assertTrue("Why is the ASM method ajsynthetic? "+amember.toDebugString(),!amember.isAjSynthetic());
  299. }
  300. // org.aspectj.weaver.EffectiveSignature
  301. EffectiveSignatureAttribute bcelEsa = bmember.getEffectiveSignature();
  302. EffectiveSignatureAttribute asmEsa = amember.getEffectiveSignature();
  303. if (bcelEsa==null) {
  304. assertTrue("Why isnt the ASM effective signature null? "+asmEsa,asmEsa==null);
  305. } else {
  306. if (asmEsa==null) fail("ASM effective signature is null, when BCEL effective signature is "+bcelEsa.toString());
  307. assertTrue("Should be the same?? b="+bcelEsa.toString()+" a="+asmEsa.toString(),bcelEsa.toString().equals(asmEsa.toString()));
  308. }
  309. // org.aspectj.weaver.MethodDeclarationLineNumber
  310. int bLine = bmember.getDeclarationLineNumber();
  311. int aLine = amember.getDeclarationLineNumber();
  312. assertTrue("Should be the same number: "+bLine+" "+aLine,bLine==aLine);
  313. // org.aspectj.weaver.Advice
  314. ShadowMunger bcelSM = bmember.getAssociatedShadowMunger();
  315. ShadowMunger asmSM = amember.getAssociatedShadowMunger();
  316. if (bcelSM==null) {
  317. assertTrue("Why isnt the ASM effective signature null? "+asmSM,asmSM==null);
  318. } else {
  319. if (asmSM==null) fail("ASM effective signature is null, when BCEL effective signature is "+bcelSM.toString());
  320. assertTrue("Should be the same?? b="+bcelSM.toString()+" a="+asmSM.toString(),bcelSM.toString().equals(asmSM.toString()));
  321. }
  322. // new AjASMAttribute("org.aspectj.weaver.SourceContext"),
  323. }
  324. }
  325. // @SimpleAnnotation(id=1) int i;
  326. // @SimpleAnnotation(id=2) String s;
  327. public void testLoadingAnnotationsForFields() {
  328. boolean debug = false;
  329. BcelWorld slowWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");slowWorld.setFastDelegateSupport(false);
  330. BcelWorld fastWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");
  331. ReferenceType bcelT = (ReferenceType)slowWorld.resolve("AnnotatedFields");
  332. ReferenceType asmT = (ReferenceType)fastWorld.resolve("AnnotatedFields");
  333. ResolvedMember[] bcelFields = bcelT.getDeclaredFields();
  334. ResolvedMember[] asmFields = asmT.getDeclaredFields();
  335. for (int i = 0; i < bcelFields.length; i++) {
  336. BcelField bmember = (BcelField)bcelFields[i];
  337. AsmField amember = (AsmField)asmFields[i];
  338. assertTrue("Seem to be in a muddle. a="+amember.getName()+" b="+bmember.getName(),
  339. amember.getName().equals(bmember.getName()));
  340. if (debug) System.err.println("Looking at "+bmember);
  341. ResolvedType[] bAnns = bmember.getAnnotationTypes();
  342. ResolvedType[] aAnns = amember.getAnnotationTypes();
  343. assertTrue("Should have found an annotation on the bcel field?",bAnns!=null && bAnns.length==1);
  344. assertTrue("Should have found an annotation on the asm field?",aAnns!=null && aAnns.length==1);
  345. assertTrue("BCEL annotation should be 'SimpleAnnotation3' but is "+bAnns[0].toString(),bAnns[0].toString().equals("SimpleAnnotation3"));
  346. assertTrue("ASM annotation should be 'SimpleAnnotation3' but is "+aAnns[0].toString(),aAnns[0].toString().equals("SimpleAnnotation3"));
  347. AnnotationX[] bXs = bmember.getAnnotations();
  348. AnnotationX[] aXs = amember.getAnnotations();
  349. assertTrue("Should have found an annotation on the bcel field?",bXs!=null && bXs.length==1);
  350. assertTrue("Should have found an annotation on the asm field?",aXs!=null && aXs.length==1);
  351. String exp = null;
  352. if (i==0) exp = "ANNOTATION [LSimpleAnnotation3;] [runtimeVisible] [id=1]";
  353. else if (i==1) exp="ANNOTATION [LSimpleAnnotation3;] [runtimeVisible] [id=2]";
  354. assertTrue("BCEL annotation should be '"+exp+"' but is "+bXs[0].toString(),bXs[0].toString().equals(exp));
  355. assertTrue("ASM annotation should be '"+exp+"' but is "+aXs[0].toString(),aXs[0].toString().equals(exp));
  356. }
  357. }
  358. // @SimpleAnnotation(id=1)
  359. // public void method1() { }
  360. //
  361. // @SimpleAnnotation(id=2)
  362. // public void method2() { }
  363. public void testLoadingAnnotationsForMethods() {
  364. boolean debug = false;
  365. BcelWorld slowWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");slowWorld.setFastDelegateSupport(false);
  366. BcelWorld fastWorld = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");
  367. ReferenceType bcelT = (ReferenceType)slowWorld.resolve("AnnotatedMethods");
  368. ReferenceType asmT = (ReferenceType)fastWorld.resolve("AnnotatedMethods");
  369. ResolvedMember[] bcelMethods = bcelT.getDeclaredMethods();
  370. ResolvedMember[] asmMethods = asmT.getDeclaredMethods();
  371. for (int i = 0; i < bcelMethods.length; i++) {
  372. BcelMethod bmember = (BcelMethod)bcelMethods[i];
  373. AsmMethod amember = (AsmMethod)asmMethods[i];
  374. if (!bmember.getName().startsWith("method")) continue;
  375. assertTrue("Seem to be in a muddle. a="+amember.getName()+" b="+bmember.getName(),
  376. amember.getName().equals(bmember.getName()));
  377. if (debug) System.err.println("Looking at "+bmember);
  378. ResolvedType[] bAnns = bmember.getAnnotationTypes();
  379. ResolvedType[] aAnns = amember.getAnnotationTypes();
  380. assertTrue("Should have found an annotation on the bcel method?",bAnns!=null && bAnns.length==1);
  381. assertTrue("Should have found an annotation on the asm method?",aAnns!=null && aAnns.length==1);
  382. assertTrue("BCEL annotation should be 'SimpleAnnotation3' but is "+bAnns[0].toString(),bAnns[0].toString().equals("SimpleAnnotation3"));
  383. assertTrue("ASM annotation should be 'SimpleAnnotation3' but is "+aAnns[0].toString(),aAnns[0].toString().equals("SimpleAnnotation3"));
  384. AnnotationX[] bXs = bmember.getAnnotations();
  385. AnnotationX[] aXs = amember.getAnnotations();
  386. assertTrue("Should have found an annotation on the bcel method?",bXs!=null && bXs.length==1);
  387. assertTrue("Should have found an annotation on the asm method?",aXs!=null && aXs.length==1);
  388. String exp = null;
  389. if (i==1) exp = "ANNOTATION [LSimpleAnnotation3;] [runtimeVisible] [id=1]";
  390. else if (i==2) exp="ANNOTATION [LSimpleAnnotation3;] [runtimeVisible] [id=2]";
  391. assertTrue("BCEL annotation should be '"+exp+"' but is "+bXs[0].toString(),bXs[0].toString().equals(exp));
  392. assertTrue("ASM annotation should be '"+exp+"' but is "+aXs[0].toString(),aXs[0].toString().equals(exp));
  393. }
  394. }
  395. private void compareAttributeNames(String[] asmlist,String[] bcellist) {
  396. String astring = stringifyStringArray(asmlist);
  397. String bstring = stringifyStringArray(bcellist);
  398. if (asmlist.length!=bcellist.length) {
  399. fail("Differing lengths.\nBCEL=>\n"+bstring+" ASM=>\n"+astring);
  400. }
  401. }
  402. private String stringifyStringArray(String[] s) {
  403. StringBuffer r = new StringBuffer();
  404. for (int i = 0; i < s.length; i++) {
  405. r.append(s[i]).append("\n");
  406. }
  407. return r.toString();
  408. }
  409. }