Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RuntimeVisibleParameterAnnotationAttributeTest.java 5.2KB

19 anos atrás
19 anos atrás
19 anos atrás
14 anos atrás
19 anos atrás
19 anos atrás
19 anos atrás
19 anos atrás
19 anos atrás
19 anos atrás
19 anos atrás
19 anos atrás
19 anos atrás
19 anos atrás
19 anos atrás
14 anos atrás
19 anos atrás
19 anos atrás
19 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.Attribute;
  17. import org.aspectj.apache.bcel.classfile.JavaClass;
  18. import org.aspectj.apache.bcel.classfile.Method;
  19. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  20. import org.aspectj.apache.bcel.classfile.annotation.NameValuePair;
  21. import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisParamAnnos;
  22. import org.aspectj.apache.bcel.util.SyntheticRepository;
  23. public class RuntimeVisibleParameterAnnotationAttributeTest extends BcelTestCase {
  24. protected void setUp() throws Exception {
  25. super.setUp();
  26. }
  27. public void testAccessingRuntimeVisibleParameterAnnotations() throws ClassNotFoundException {
  28. JavaClass clazz = getClassFromJar("AnnotatedParameters");
  29. Attribute[] rvaAttr = findAttribute("RuntimeVisibleParameterAnnotations",clazz);
  30. Method[] methods = clazz.getMethods();
  31. for (Method m : methods) {
  32. if (m.getName().equals("foo")) {
  33. RuntimeVisParamAnnos paramAnns =
  34. (RuntimeVisParamAnnos) findAttribute("RuntimeVisibleParameterAnnotations", m.getAttributes());
  35. assertTrue("foo takes two parameters, not " + paramAnns.getParameterAnnotations().size(),
  36. paramAnns.getParameterAnnotations().size() == 2);
  37. AnnotationGen[] firstParamAnnotations = paramAnns.getAnnotationsOnParameter(0);
  38. checkAnnotation(firstParamAnnotations[0], "SimpleAnnotation", "id", "2");
  39. AnnotationGen[] secondParamAnnotations = paramAnns.getAnnotationsOnParameter(1);
  40. checkAnnotation(secondParamAnnotations[0], "SimpleAnnotation", "id", "3");
  41. checkAnnotation(secondParamAnnotations[1], "AnnotationEnumElement", "enumval", "LSimpleEnum;Red");
  42. }
  43. if (m.getName().equals("main")) {
  44. RuntimeVisParamAnnos paramAnns =
  45. (RuntimeVisParamAnnos) findAttribute("RuntimeVisibleParameterAnnotations", m.getAttributes());
  46. assertTrue("main takes one parameter, not " + paramAnns.getParameterAnnotations().size(),
  47. paramAnns.getParameterAnnotations().size() == 1);
  48. AnnotationGen[] firstParamAnnotations = paramAnns.getAnnotationsOnParameter(0);
  49. checkAnnotation(firstParamAnnotations[0], "SimpleAnnotation", "id", "1");
  50. }
  51. }
  52. }
  53. public void testAccessingParameterAnnotationsThroughGetAnnotations() throws ClassNotFoundException {
  54. JavaClass clazz = getClassFromJar("AnnotatedParameters");
  55. Attribute[] rvaAttr = findAttribute("RuntimeVisibleParameterAnnotations",clazz);
  56. checkFooMethod(clazz);
  57. }
  58. public void testParameterAnnotationsReadWrite() throws ClassNotFoundException,IOException {
  59. JavaClass clazz = getClassFromJar("AnnotatedParameters");
  60. checkFooMethod(clazz);
  61. // Write it out
  62. File tfile = createTestdataFile("AnnotatedParameters.class");
  63. clazz.dump(tfile);
  64. SyntheticRepository repos2 = createRepos(".");
  65. JavaClass clazz2 = repos2.loadClass("AnnotatedParameters");
  66. checkFooMethod(clazz);
  67. assertTrue(tfile.delete());
  68. }
  69. public void checkFooMethod(JavaClass clazz) {
  70. Method[] methods = clazz.getMethods();
  71. for (Method m : methods) {
  72. if (m.getName().equals("foo")) {
  73. AnnotationGen[] firstParamAnnotations = m.getAnnotationsOnParameter(0);
  74. checkAnnotation(firstParamAnnotations[0], "SimpleAnnotation", "id", "2");
  75. AnnotationGen[] secondParamAnnotations = m.getAnnotationsOnParameter(1);
  76. checkAnnotation(secondParamAnnotations[0], "SimpleAnnotation", "id", "3");
  77. checkAnnotation(secondParamAnnotations[1], "AnnotationEnumElement", "enumval", "LSimpleEnum;Red");
  78. }
  79. }
  80. }
  81. private void checkAnnotation(AnnotationGen a,String name,String elementname,String elementvalue) {
  82. assertTrue("Expected annotation to have name "+name+" but it had name "+a.getTypeName(),
  83. a.getTypeName().equals(name));
  84. assertTrue("Expected annotation to have one element but it had "+a.getValues().size(),a.getValues().size()==1);
  85. NameValuePair envp = a.getValues().get(0);
  86. assertTrue("Expected element name "+elementname+" but was "+envp.getNameString(),
  87. elementname.equals(envp.getNameString()));
  88. assertTrue("Expected element value "+elementvalue+" but was "+envp.getValue().stringifyValue(),
  89. elementvalue.equals(envp.getValue().stringifyValue()));
  90. }
  91. // helper methods
  92. public void checkValue(AnnotationGen a,String name,String tostring) {
  93. for (NameValuePair element : a.getValues()) {
  94. if (element.getNameString().equals(name)) {
  95. if (!element.getValue().stringifyValue().equals(tostring)) {
  96. fail("Expected element " + name + " to have value " + tostring + " but it had value " + element.getValue().stringifyValue());
  97. }
  98. return;
  99. }
  100. }
  101. fail("Didnt find named element "+name);
  102. }
  103. protected void tearDown() throws Exception {
  104. super.tearDown();
  105. }
  106. }