aboutsummaryrefslogtreecommitdiffstats
path: root/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/VarargsTest.java
blob: 52590bf71e9964e3d4d9615ab43855561183cad3 (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
/* *******************************************************************
 * 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.JavaClass;
import org.aspectj.apache.bcel.classfile.Method;
import org.aspectj.apache.bcel.classfile.annotation.Annotation;
import org.aspectj.apache.bcel.classfile.annotation.ElementNameValuePair;
import org.aspectj.apache.bcel.util.SyntheticRepository;


public class VarargsTest extends BcelTestCase {
	

	protected void setUp() throws Exception {
		super.setUp();
	}
	
	
	public void testVarargs() throws ClassNotFoundException {
		JavaClass clazz = getClassFromJar("VarargsClass");
		
		checkMarkedVarargs(clazz,"foo",true);
		checkMarkedVarargs(clazz,"goo",true);
		checkMarkedVarargs(clazz,"hoo",false);
	}
	
	public void testVarargsReadWrite() throws ClassNotFoundException,IOException {
		JavaClass clazz = getClassFromJar("VarargsClass");
		
		checkMarkedVarargs(clazz,"foo",true);
		checkMarkedVarargs(clazz,"goo",true);
		checkMarkedVarargs(clazz,"hoo",false);
		
		//	 Write it out
		File tfile = createTestdataFile("VarargsClass.class");
		clazz.dump(tfile);
		
		SyntheticRepository repos2 = createRepos(".");
		JavaClass           clazz2 = repos2.loadClass("VarargsClass");
		
		checkMarkedVarargs(clazz,"foo",true);
		checkMarkedVarargs(clazz,"goo",true);
		checkMarkedVarargs(clazz,"hoo",false);

		assertTrue(tfile.delete());
	}
	
	// helper methods
		
	public void checkMarkedVarargs(JavaClass clazz,String methodname,boolean shouldBeMarked) {
		Method[] methods = clazz.getMethods();

		for (int i = 0; i < methods.length; i++) {
			Method m = methods[i];
			if (m.getName().equals(methodname)) {
				assertTrue("Method '"+methodname+"' should answer varargs="+shouldBeMarked,
						m.isVarargs()==shouldBeMarked);
			}
		}
	}
	

	// helper methods
	
	public void checkValue(Annotation a,String name,String tostring) {
		for (Iterator i = a.getValues().iterator(); i.hasNext();) {
			ElementNameValuePair element = (ElementNameValuePair) 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();
	}
	
}