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.3KB

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