Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MethodAnnotationsTest.java 3.7KB

19 лет назад
18 лет назад
19 лет назад
18 лет назад
19 лет назад
15 лет назад
19 лет назад
19 лет назад
19 лет назад
15 лет назад
19 лет назад
15 лет назад
19 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 (int i = 0; i < methods.length; i++) {
  48. Method m = methods[i];
  49. AnnotationGen[] methodAnnotations = m.getAnnotations();
  50. if (m.getName().equals(methodname)) {
  51. checkAnnotation(methodAnnotations[0],annotationName,annotationElementName,annotationElementValue);
  52. }
  53. }
  54. }
  55. private void checkAnnotation(AnnotationGen a,String name,String elementname,String elementvalue) {
  56. assertTrue("Expected annotation to have name "+name+" but it had name "+a.getTypeName(),
  57. a.getTypeName().equals(name));
  58. assertTrue("Expected annotation to have one element but it had "+a.getValues().size(),a.getValues().size()==1);
  59. NameValuePair envp = a.getValues().get(0);
  60. assertTrue("Expected element name "+elementname+" but was "+envp.getNameString(),
  61. elementname.equals(envp.getNameString()));
  62. assertTrue("Expected element value "+elementvalue+" but was "+envp.getValue().stringifyValue(),
  63. elementvalue.equals(envp.getValue().stringifyValue()));
  64. }
  65. // helper methods
  66. public void checkValue(AnnotationGen a,String name,String tostring) {
  67. for (Iterator<NameValuePair> i = a.getValues().iterator(); i.hasNext();) {
  68. NameValuePair element = i.next();
  69. if (element.getNameString().equals(name)) {
  70. if (!element.getValue().stringifyValue().equals(tostring)) {
  71. fail("Expected element "+name+" to have value "+tostring+" but it had value "+element.getValue().stringifyValue());
  72. }
  73. return;
  74. }
  75. }
  76. fail("Didnt find named element "+name);
  77. }
  78. protected void tearDown() throws Exception {
  79. super.tearDown();
  80. }
  81. }