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.

RuntimeVisibleParameterAnnotationAttributeTest.java 5.2KB

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