]> source.dussan.org Git - aspectj.git/blob
fad1dad2c4c3078716e05c0b86448b8374502a1f
[aspectj.git] /
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
13 package org.aspectj.apache.bcel.classfile.tests;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.Iterator;
18
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;
26
27
28 public class RuntimeVisibleParameterAnnotationAttributeTest extends BcelTestCase {
29         
30
31         protected void setUp() throws Exception {
32                 super.setUp();
33         }
34         
35
36         public void testAccessingRuntimeVisibleParameterAnnotations() throws ClassNotFoundException {
37                 JavaClass clazz = getClassFromJar("AnnotatedParameters");
38                 Attribute[] rvaAttr = findAttribute("RuntimeVisibleParameterAnnotations",clazz);
39                 Method[] methods = clazz.getMethods();
40
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);
48
49                                 Annotation[] firstParamAnnotations = paramAnns.getAnnotationsOnParameter(0);
50                                 checkAnnotation(firstParamAnnotations[0],"SimpleAnnotation","id","2");
51
52                                 Annotation[] secondParamAnnotations = paramAnns.getAnnotationsOnParameter(1);
53                                 checkAnnotation(secondParamAnnotations[0],"SimpleAnnotation","id","3");
54                                 checkAnnotation(secondParamAnnotations[1],"AnnotationEnumElement","enumval","Red");
55                                 
56                         }
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);
62
63                                 Annotation[] firstParamAnnotations = paramAnns.getAnnotationsOnParameter(0);
64                                 checkAnnotation(firstParamAnnotations[0],"SimpleAnnotation","id","1");
65                         }
66                 }
67         }
68         
69         public void testAccessingParameterAnnotationsThroughGetAnnotations() throws ClassNotFoundException {
70                 JavaClass clazz = getClassFromJar("AnnotatedParameters");
71                 Attribute[] rvaAttr = findAttribute("RuntimeVisibleParameterAnnotations",clazz);
72                 
73                 checkFooMethod(clazz);
74         }
75         
76         public void testParameterAnnotationsReadWrite() throws ClassNotFoundException,IOException {
77                 JavaClass clazz = getClassFromJar("AnnotatedParameters");
78                 
79                 checkFooMethod(clazz);
80
81                 //       Write it out
82                 File tfile = createTestdataFile("AnnotatedParameters.class");
83                 clazz.dump(tfile);
84                 
85                 SyntheticRepository repos2 = createRepos(".");
86                 JavaClass           clazz2 = repos2.loadClass("AnnotatedParameters");
87                 
88                 checkFooMethod(clazz);
89
90                 assertTrue(tfile.delete());
91         }
92                 
93                 
94         public void checkFooMethod(JavaClass clazz) {
95                 Method[] methods = clazz.getMethods();
96
97                 for (int i = 0; i < methods.length; i++) {
98                         Method m = methods[i];
99                         if (m.getName().equals("foo")) {
100
101                                 Annotation[] firstParamAnnotations = m.getAnnotationsOnParameter(0);
102                                 checkAnnotation(firstParamAnnotations[0],"SimpleAnnotation","id","2");
103
104                                 Annotation[] secondParamAnnotations = m.getAnnotationsOnParameter(1);
105                                 checkAnnotation(secondParamAnnotations[0],"SimpleAnnotation","id","3");
106                                 checkAnnotation(secondParamAnnotations[1],"AnnotationEnumElement","enumval","Red");
107                                 
108                         }
109                 }
110         }
111         
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()));
121         }
122         
123
124         // helper methods
125         
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());
132                                 }
133                                 return;
134                         }
135                 }
136                 fail("Didnt find named element "+name);
137         }
138
139         protected void tearDown() throws Exception {
140                 super.tearDown();
141         }
142         
143 }