1 /* *******************************************************************
2 * Copyright (c) 2004 IBM
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
10 * Andy Clement - initial implementation
11 * ******************************************************************/
13 package org.aspectj.apache.bcel.classfile.tests;
16 import java.io.IOException;
17 import java.util.Iterator;
19 import org.aspectj.apache.bcel.classfile.Attribute;
20 import org.aspectj.apache.bcel.classfile.JavaClass;
21 import org.aspectj.apache.bcel.classfile.Method;
22 import org.aspectj.apache.bcel.classfile.annotation.Annotation;
23 import org.aspectj.apache.bcel.classfile.annotation.ElementNameValuePair;
24 import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleParameterAnnotations;
25 import org.aspectj.apache.bcel.util.SyntheticRepository;
28 public class RuntimeVisibleParameterAnnotationAttributeTest extends BcelTestCase {
31 protected void setUp() throws Exception {
36 public void testAccessingRuntimeVisibleParameterAnnotations() throws ClassNotFoundException {
37 JavaClass clazz = getClassFromJar("AnnotatedParameters");
38 Attribute[] rvaAttr = findAttribute("RuntimeVisibleParameterAnnotations",clazz);
39 Method[] methods = clazz.getMethods();
41 for (int i = 0; i < methods.length; i++) {
42 Method m = methods[i];
43 if (m.getName().equals("foo")) {
44 RuntimeVisibleParameterAnnotations paramAnns =
45 (RuntimeVisibleParameterAnnotations) findAttribute("RuntimeVisibleParameterAnnotations",m.getAttributes());
46 assertTrue("foo takes two parameters, not "+paramAnns.getParameterAnnotations().size(),
47 paramAnns.getParameterAnnotations().size()==2);
49 Annotation[] firstParamAnnotations = paramAnns.getAnnotationsOnParameter(0);
50 checkAnnotation(firstParamAnnotations[0],"SimpleAnnotation","id","2");
52 Annotation[] secondParamAnnotations = paramAnns.getAnnotationsOnParameter(1);
53 checkAnnotation(secondParamAnnotations[0],"SimpleAnnotation","id","3");
54 checkAnnotation(secondParamAnnotations[1],"AnnotationEnumElement","enumval","Red");
57 if (m.getName().equals("main")) {
58 RuntimeVisibleParameterAnnotations paramAnns =
59 (RuntimeVisibleParameterAnnotations) findAttribute("RuntimeVisibleParameterAnnotations",m.getAttributes());
60 assertTrue("main takes one parameter, not "+paramAnns.getParameterAnnotations().size(),
61 paramAnns.getParameterAnnotations().size()==1);
63 Annotation[] firstParamAnnotations = paramAnns.getAnnotationsOnParameter(0);
64 checkAnnotation(firstParamAnnotations[0],"SimpleAnnotation","id","1");
69 public void testAccessingParameterAnnotationsThroughGetAnnotations() throws ClassNotFoundException {
70 JavaClass clazz = getClassFromJar("AnnotatedParameters");
71 Attribute[] rvaAttr = findAttribute("RuntimeVisibleParameterAnnotations",clazz);
73 checkFooMethod(clazz);
76 public void testParameterAnnotationsReadWrite() throws ClassNotFoundException,IOException {
77 JavaClass clazz = getClassFromJar("AnnotatedParameters");
79 checkFooMethod(clazz);
82 File tfile = createTestdataFile("AnnotatedParameters.class");
85 SyntheticRepository repos2 = createRepos(".");
86 JavaClass clazz2 = repos2.loadClass("AnnotatedParameters");
88 checkFooMethod(clazz);
90 assertTrue(tfile.delete());
94 public void checkFooMethod(JavaClass clazz) {
95 Method[] methods = clazz.getMethods();
97 for (int i = 0; i < methods.length; i++) {
98 Method m = methods[i];
99 if (m.getName().equals("foo")) {
101 Annotation[] firstParamAnnotations = m.getAnnotationsOnParameter(0);
102 checkAnnotation(firstParamAnnotations[0],"SimpleAnnotation","id","2");
104 Annotation[] secondParamAnnotations = m.getAnnotationsOnParameter(1);
105 checkAnnotation(secondParamAnnotations[0],"SimpleAnnotation","id","3");
106 checkAnnotation(secondParamAnnotations[1],"AnnotationEnumElement","enumval","Red");
112 private void checkAnnotation(Annotation a,String name,String elementname,String elementvalue) {
113 assertTrue("Expected annotation to have name "+name+" but it had name "+a.getTypeName(),
114 a.getTypeName().equals(name));
115 assertTrue("Expected annotation to have one element but it had "+a.getValues().size(),a.getValues().size()==1);
116 ElementNameValuePair envp = (ElementNameValuePair)a.getValues().get(0);
117 assertTrue("Expected element name "+elementname+" but was "+envp.getNameString(),
118 elementname.equals(envp.getNameString()));
119 assertTrue("Expected element value "+elementvalue+" but was "+envp.getValue().stringifyValue(),
120 elementvalue.equals(envp.getValue().stringifyValue()));
126 public void checkValue(Annotation a,String name,String tostring) {
127 for (Iterator i = a.getValues().iterator(); i.hasNext();) {
128 ElementNameValuePair element = (ElementNameValuePair) i.next();
129 if (element.getNameString().equals(name)) {
130 if (!element.getValue().stringifyValue().equals(tostring)) {
131 fail("Expected element "+name+" to have value "+tostring+" but it had value "+element.getValue().stringifyValue());
136 fail("Didnt find named element "+name);
139 protected void tearDown() throws Exception {