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.

MethodAnnotationsTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM
  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.apache.bcel.classfile.tests;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.util.Iterator;
  16. import org.aspectj.apache.bcel.classfile.JavaClass;
  17. import org.aspectj.apache.bcel.classfile.Method;
  18. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  19. import org.aspectj.apache.bcel.classfile.annotation.NameValuePair;
  20. import org.aspectj.apache.bcel.util.SyntheticRepository;
  21. public class MethodAnnotationsTest extends BcelTestCase {
  22. protected void setUp() throws Exception {
  23. super.setUp();
  24. }
  25. public void testMethodAnnotations() throws ClassNotFoundException {
  26. JavaClass clazz = getClassFromJar("AnnotatedMethods");
  27. checkAnnotatedMethod(clazz,"method1","SimpleAnnotation","id","1");
  28. checkAnnotatedMethod(clazz,"method2","SimpleAnnotation","id","2");
  29. }
  30. public void testMethodAnnotationsReadWrite() throws ClassNotFoundException,IOException {
  31. JavaClass clazz = getClassFromJar("AnnotatedMethods");
  32. checkAnnotatedMethod(clazz,"method1","SimpleAnnotation","id","1");
  33. checkAnnotatedMethod(clazz,"method2","SimpleAnnotation","id","2");
  34. // Write it out
  35. File tfile = createTestdataFile("AnnotatedMethods.class");
  36. clazz.dump(tfile);
  37. SyntheticRepository repos2 = createRepos(".");
  38. JavaClass clazz2 = repos2.loadClass("AnnotatedMethods");
  39. checkAnnotatedMethod(clazz,"method1","SimpleAnnotation","id","1");
  40. checkAnnotatedMethod(clazz,"method2","SimpleAnnotation","id","2");
  41. assertTrue(tfile.delete());
  42. }
  43. // helper methods
  44. public void checkAnnotatedMethod(JavaClass clazz,String methodname,
  45. String annotationName,String annotationElementName,String annotationElementValue) {
  46. Method[] methods = clazz.getMethods();
  47. for (Method m : methods) {
  48. AnnotationGen[] methodAnnotations = m.getAnnotations();
  49. if (m.getName().equals(methodname)) {
  50. checkAnnotation(methodAnnotations[0], annotationName, annotationElementName, annotationElementValue);
  51. }
  52. }
  53. }
  54. private void checkAnnotation(AnnotationGen a,String name,String elementname,String elementvalue) {
  55. assertTrue("Expected annotation to have name "+name+" but it had name "+a.getTypeName(),
  56. a.getTypeName().equals(name));
  57. assertTrue("Expected annotation to have one element but it had "+a.getValues().size(),a.getValues().size()==1);
  58. NameValuePair envp = a.getValues().get(0);
  59. assertTrue("Expected element name "+elementname+" but was "+envp.getNameString(),
  60. elementname.equals(envp.getNameString()));
  61. assertTrue("Expected element value "+elementvalue+" but was "+envp.getValue().stringifyValue(),
  62. elementvalue.equals(envp.getValue().stringifyValue()));
  63. }
  64. // helper methods
  65. public void checkValue(AnnotationGen a,String name,String tostring) {
  66. for (NameValuePair element : a.getValues()) {
  67. if (element.getNameString().equals(name)) {
  68. if (!element.getValue().stringifyValue().equals(tostring)) {
  69. fail("Expected element " + name + " to have value " + tostring + " but it had value " + element.getValue().stringifyValue());
  70. }
  71. return;
  72. }
  73. }
  74. fail("Didnt find named element "+name);
  75. }
  76. protected void tearDown() throws Exception {
  77. super.tearDown();
  78. }
  79. }