001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.lang.reflect;
032:
033: import java.lang.annotation.Annotation;
034: import java.lang.reflect.*;
035: import net.sf.retrotranslator.runtime.java.lang.*;
036: import net.sf.retrotranslator.tests.TestCaseBase;
037:
038: /**
039: * @author Taras Puchko
040: */
041: public class _MethodTestCase extends TestCaseBase {
042:
043: private static final String NAME = "doAction";
044:
045: private static Method PROXY_METHOD = getProxyMethod();
046:
047: private static Method getProxyMethod() {
048: try {
049: return Proxy.getProxyClass(
050: _MethodTestCase.class.getClassLoader(),
051: Comparable.class).getMethod("compareTo",
052: Object.class);
053: } catch (NoSuchMethodException e) {
054: throw new Error(e);
055: }
056: }
057:
058: @MyStyle("bold")
059: public void doAction() {
060: }
061:
062: @MyStyle("italic")
063: public void doAction(String string) {
064: }
065:
066: public void doAction(@MyStyle("glass")
067: String string, int i) {
068: }
069:
070: public void testGetAnnotationForAbsent() throws Exception {
071: assertNull(PROXY_METHOD.getAnnotation(MyStyle.class));
072: }
073:
074: public void testGetAnnotationForNoParam() throws Exception {
075: Method noParamMethod = _MethodTestCase.class.getMethod(NAME);
076: assertEquals("bold", noParamMethod.getAnnotation(MyStyle.class)
077: .value());
078: }
079:
080: public void testGetAnnotationForOneParam() throws Exception {
081: Method oneParamMethod = _MethodTestCase.class.getMethod(NAME,
082: String.class);
083: assertEquals("italic", oneParamMethod.getAnnotation(
084: MyStyle.class).value());
085: }
086:
087: public void testGetAnnotationForTwoParam() throws Exception {
088: Method twoParamMethod = _MethodTestCase.class.getMethod(NAME,
089: String.class, int.class);
090: assertNull(twoParamMethod.getAnnotation(MyStyle.class));
091: }
092:
093: public void testGetAnnotations() throws Exception {
094: Method noParamMethod = _MethodTestCase.class.getMethod(NAME);
095: assertEquals("bold",
096: ((MyStyle) noParamMethod.getAnnotations()[0]).value());
097: assertEquals(0, PROXY_METHOD.getAnnotations().length);
098: }
099:
100: public void testGetDeclaredAnnotations() throws Exception {
101: Method oneParamMethod = _MethodTestCase.class.getMethod(NAME,
102: String.class);
103: assertEquals("italic", ((MyStyle) oneParamMethod
104: .getDeclaredAnnotations()[0]).value());
105: assertEquals(0, PROXY_METHOD.getDeclaredAnnotations().length);
106: }
107:
108: public void testGetDefaultValue() throws Exception {
109: assertEquals(MyColor.RED, MyFormatter.class.getMethod(
110: "backgroundColor").getDefaultValue());
111: assertNull(MyStyle.class.getMethod("value").getDefaultValue());
112: assertNull(_MethodTestCase.class.getMethod(NAME)
113: .getDefaultValue());
114: }
115:
116: public void testGetGenericExceptionTypes() throws Exception {
117: class Test<T extends RuntimeException> {
118: public <X extends Throwable> void m() throws T, X {
119: }
120: }
121: Type[] types = Test.class.getMethod("m")
122: .getGenericExceptionTypes();
123:
124: TypeVariable variableT = (TypeVariable) types[0];
125: assertEquals("T", variableT.getName());
126: assertEquals(Test.class, variableT.getGenericDeclaration());
127: assertEquals(RuntimeException.class, singleton(variableT
128: .getBounds()));
129:
130: TypeVariable variableX = (TypeVariable) types[1];
131: assertEquals("X", variableX.getName());
132: assertEquals(Test.class.getMethod("m"), variableX
133: .getGenericDeclaration());
134: assertEquals(Throwable.class, singleton(variableX.getBounds()));
135: }
136:
137: public void testGetGenericExceptionTypes_NotGeneric()
138: throws Exception {
139: class Test<T extends RuntimeException> {
140: public void m() throws RuntimeException {
141: }
142: }
143: assertEquals(RuntimeException.class, singleton(Test.class
144: .getMethod("m").getGenericExceptionTypes()));
145: assertEqualElements(PROXY_METHOD.getExceptionTypes(),
146: (Object[]) PROXY_METHOD.getGenericExceptionTypes());
147: }
148:
149: public void testGetGenericParameterTypes() throws Exception {
150: class Test<T extends RuntimeException> {
151: public <E> void m(T t, E e, Comparable<? super Integer> c,
152: String s) throws RuntimeException {
153: }
154: }
155: Method method = Test.class.getMethod("m",
156: RuntimeException.class, Object.class, Comparable.class,
157: String.class);
158: Type[] types = method.getGenericParameterTypes();
159: assertEquals(4, types.length);
160: TypeVariable t = (TypeVariable) types[0];
161: TypeVariable e = (TypeVariable) types[1];
162: ParameterizedType c = (ParameterizedType) types[2];
163: Class s = (Class) types[3];
164:
165: assertEquals("T", t.getName());
166: assertEquals(Test.class, t.getGenericDeclaration());
167: assertEquals(RuntimeException.class, singleton(t.getBounds()));
168:
169: assertEquals("E", e.getName());
170: assertEquals(method, e.getGenericDeclaration());
171: assertEquals(Object.class, singleton(e.getBounds()));
172:
173: assertEquals(Comparable.class, c.getRawType());
174: assertNull(c.getOwnerType());
175: WildcardType argument = (WildcardType) singleton(c
176: .getActualTypeArguments());
177: assertEquals(Integer.class,
178: singleton(argument.getLowerBounds()));
179: assertEquals(Object.class, singleton(argument.getUpperBounds()));
180:
181: assertEquals(String.class, s);
182: assertEqualElements(PROXY_METHOD.getParameterTypes(),
183: (Object[]) PROXY_METHOD.getGenericParameterTypes());
184: }
185:
186: public void testGetGenericReturnType() throws Exception {
187: class Test<T extends RuntimeException> {
188: class Inner {
189: public T m() {
190: return null;
191: }
192: }
193: }
194: TypeVariable variable = (TypeVariable) Test.Inner.class
195: .getMethod("m").getGenericReturnType();
196: assertEquals("T", variable.getName());
197: assertEquals(Test.class, variable.getGenericDeclaration());
198: assertEquals(RuntimeException.class, singleton(variable
199: .getBounds()));
200: assertEquals(PROXY_METHOD.getReturnType(), PROXY_METHOD
201: .getGenericReturnType());
202: }
203:
204: public void testGetParameterAnnotationsForTwoParam()
205: throws Exception {
206: Method twoParamMethod = _MethodTestCase.class.getMethod(NAME,
207: String.class, int.class);
208: Annotation[][] annotations = twoParamMethod
209: .getParameterAnnotations();
210: assertEquals(2, annotations.length);
211: assertEquals(1, annotations[0].length);
212: assertEquals(0, annotations[1].length);
213: assertEquals("glass", ((MyStyle) annotations[0][0]).value());
214: if (!System.getProperty("java.vm.version")
215: .startsWith("R26.0.0")) {
216: assertEquals(0, singleton(PROXY_METHOD
217: .getParameterAnnotations()).length);
218: }
219: }
220:
221: public void testGetTypeParameters() throws Exception {
222: class Test<T extends String> {
223: public <E extends Number> void m(T t, E e)
224: throws RuntimeException {
225: }
226: }
227: Method method = Test.class.getMethod("m", String.class,
228: Number.class);
229: TypeVariable<Method> variable = singleton(method
230: .getTypeParameters());
231: assertEquals("E", variable.getName());
232: assertEquals(Number.class, singleton(variable.getBounds()));
233: assertEquals(method, variable.getGenericDeclaration());
234: assertEquals(0, PROXY_METHOD.getTypeParameters().length);
235: }
236:
237: public void testIsAnnotationPresent() throws Exception {
238: assertTrue(_MethodTestCase.class.getMethod(NAME)
239: .isAnnotationPresent(MyStyle.class));
240: assertFalse(_MethodTestCase.class.getMethod(NAME)
241: .isAnnotationPresent(MyFormatter.class));
242: assertFalse(PROXY_METHOD.isAnnotationPresent(MyFormatter.class));
243: }
244:
245: public void testIsBridge() throws Exception {
246: class Test implements Comparable<String> {
247: public int compareTo(String o) {
248: return 0;
249: }
250: }
251: assertTrue(Test.class.getDeclaredMethod("compareTo",
252: Object.class).isBridge());
253: assertFalse(Test.class.getDeclaredMethod("compareTo",
254: String.class).isBridge());
255: assertFalse(PROXY_METHOD.isBridge());
256: }
257:
258: public void testIsSynthetic() throws Exception {
259: class Test implements Comparable<String> {
260: public int compareTo(String o) {
261: return 0;
262: }
263: }
264: assertTrue(Test.class.getMethod("compareTo", Object.class)
265: .isSynthetic());
266: assertFalse(Test.class.getMethod("compareTo", String.class)
267: .isSynthetic());
268: assertFalse(PROXY_METHOD.isSynthetic());
269: }
270:
271: interface VarargsInterface {
272: void m1(String... s);
273:
274: void m2(String s);
275: }
276:
277: public void testIsVarArgs() throws Exception {
278: abstract class Test {
279: public void m1(String... s) {
280: }
281:
282: public void m2(String s) {
283: }
284: }
285: assertTrue(Test.class.getMethod("m1", String[].class)
286: .isVarArgs());
287: assertFalse(Test.class.getMethod("m2", String.class)
288: .isVarArgs());
289:
290: assertTrue(VarargsInterface.class.getMethod("m1",
291: String[].class).isVarArgs());
292: assertFalse(VarargsInterface.class
293: .getMethod("m2", String.class).isVarArgs());
294:
295: assertFalse(PROXY_METHOD.isVarArgs());
296: }
297:
298: class Test<T extends String, RE extends RuntimeException> {
299: public <E extends Number> void m(T t, E e, String[] strings)
300: throws RE, ClassNotFoundException {
301: }
302: }
303:
304: public void testGetGenericString() throws Exception {
305: assertEquals("public <E> void " + this .getClass().getName()
306: + "$Test.m(T,E,java.lang.String[])"
307: + " throws RE,java.lang.ClassNotFoundException",
308: Test.class.getDeclaredMethods()[0].toGenericString());
309: assertEquals(PROXY_METHOD.toString(), PROXY_METHOD
310: .toGenericString());
311: }
312: }
|