summaryrefslogtreecommitdiffstats
path: root/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/RuntimeVisibleParameterAnnotationAttributeTest.java
blob: 424d5352f0b141d7af89a9ac2241deb2b56970bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* *******************************************************************
 * Copyright (c) 2004 IBM
 * All rights reserved. 
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *     Andy Clement -     initial implementation 
 * ******************************************************************/

package org.aspectj.apache.bcel.classfile.tests;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.aspectj.apache.bcel.classfile.Attribute;
import org.aspectj.apache.bcel.classfile.JavaClass;
import org.aspectj.apache.bcel.classfile.Method;
import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
import org.aspectj.apache.bcel.classfile.annotation.ElementNameValuePairGen;
import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleParameterAnnotations;
import org.aspectj.apache.bcel.util.SyntheticRepository;


public class RuntimeVisibleParameterAnnotationAttributeTest extends BcelTestCase {
	

	protected void setUp() throws Exception {
		super.setUp();
	}
	

	public void testAccessingRuntimeVisibleParameterAnnotations() throws ClassNotFoundException {
		JavaClass clazz = getClassFromJar("AnnotatedParameters");
		Attribute[] rvaAttr = findAttribute("RuntimeVisibleParameterAnnotations",clazz);
		Method[] methods = clazz.getMethods();

		for (int i = 0; i < methods.length; i++) {
			Method m = methods[i];
			if (m.getName().equals("foo")) {
				RuntimeVisibleParameterAnnotations paramAnns = 
					(RuntimeVisibleParameterAnnotations) findAttribute("RuntimeVisibleParameterAnnotations",m.getAttributes());
				assertTrue("foo takes two parameters, not "+paramAnns.getParameterAnnotations().size(),
						paramAnns.getParameterAnnotations().size()==2);

				AnnotationGen[] firstParamAnnotations = paramAnns.getAnnotationsOnParameter(0);
				checkAnnotation(firstParamAnnotations[0],"SimpleAnnotation","id","2");

				AnnotationGen[] secondParamAnnotations = paramAnns.getAnnotationsOnParameter(1);
				checkAnnotation(secondParamAnnotations[0],"SimpleAnnotation","id","3");
				checkAnnotation(secondParamAnnotations[1],"AnnotationEnumElement","enumval","LSimpleEnum;Red");
				
			}
			if (m.getName().equals("main")) {
				RuntimeVisibleParameterAnnotations paramAnns = 
					(RuntimeVisibleParameterAnnotations) findAttribute("RuntimeVisibleParameterAnnotations",m.getAttributes());
				assertTrue("main takes one parameter, not "+paramAnns.getParameterAnnotations().size(),
						paramAnns.getParameterAnnotations().size()==1);

				AnnotationGen[] firstParamAnnotations = paramAnns.getAnnotationsOnParameter(0);
				checkAnnotation(firstParamAnnotations[0],"SimpleAnnotation","id","1");
			}
		}
	}
	
	public void testAccessingParameterAnnotationsThroughGetAnnotations() throws ClassNotFoundException {
		JavaClass clazz = getClassFromJar("AnnotatedParameters");
		Attribute[] rvaAttr = findAttribute("RuntimeVisibleParameterAnnotations",clazz);
		
		checkFooMethod(clazz);
	}
	
	public void testParameterAnnotationsReadWrite() throws ClassNotFoundException,IOException {
		JavaClass clazz = getClassFromJar("AnnotatedParameters");
		
		checkFooMethod(clazz);

		//	 Write it out
		File tfile = createTestdataFile("AnnotatedParameters.class");
		clazz.dump(tfile);
		
		SyntheticRepository repos2 = createRepos(".");
		JavaClass           clazz2 = repos2.loadClass("AnnotatedParameters");
		
		checkFooMethod(clazz);

		assertTrue(tfile.delete());
	}
		
		
	public void checkFooMethod(JavaClass clazz) {
		Method[] methods = clazz.getMethods();

		for (int i = 0; i < methods.length; i++) {
			Method m = methods[i];
			if (m.getName().equals("foo")) {

				AnnotationGen[] firstParamAnnotations = m.getAnnotationsOnParameter(0);
				checkAnnotation(firstParamAnnotations[0],"SimpleAnnotation","id","2");

				AnnotationGen[] secondParamAnnotations = m.getAnnotationsOnParameter(1);
				checkAnnotation(secondParamAnnotations[0],"SimpleAnnotation","id","3");
				checkAnnotation(secondParamAnnotations[1],"AnnotationEnumElement","enumval","LSimpleEnum;Red");
				
			}
		}
	}
	
	private void checkAnnotation(AnnotationGen a,String name,String elementname,String elementvalue) {
		assertTrue("Expected annotation to have name "+name+" but it had name "+a.getTypeName(),
				a.getTypeName().equals(name));
		assertTrue("Expected annotation to have one element but it had "+a.getValues().size(),a.getValues().size()==1);
		ElementNameValuePairGen envp = (ElementNameValuePairGen)a.getValues().get(0);
		assertTrue("Expected element name "+elementname+" but was "+envp.getNameString(),
				elementname.equals(envp.getNameString()));
		assertTrue("Expected element value "+elementvalue+" but was "+envp.getValue().stringifyValue(),
				elementvalue.equals(envp.getValue().stringifyValue()));
	}
	

	// helper methods
	
	public void checkValue(AnnotationGen a,String name,String tostring) {
		for (Iterator i = a.getValues().iterator(); i.hasNext();) {
			ElementNameValuePairGen element = (ElementNameValuePairGen) i.next();
			if (element.getNameString().equals(name)) {
				if (!element.getValue().stringifyValue().equals(tostring)) {
					fail("Expected element "+name+" to have value "+tostring+" but it had value "+element.getValue().stringifyValue());
				}
				return;
			}
		}
		fail("Didnt find named element "+name);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
}