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.Constructor;
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 _ConstructorTestCase extends TestCaseBase {
042:
043: @MyStyle("bold")
044: public _ConstructorTestCase() {
045: }
046:
047: @MyStyle("italic")
048: public _ConstructorTestCase(String string) {
049: super (string);
050: }
051:
052: public _ConstructorTestCase(String string, @MyStyle("glass")
053: int i) throws RuntimeException {
054: super (string + i);
055: }
056:
057: public _ConstructorTestCase(String string, int... numbers) {
058: super (string + numbers.length);
059: }
060:
061: public void testGetAnnotationForNoParam() throws Exception {
062: Constructor<_ConstructorTestCase> noParamConstructor = _ConstructorTestCase.class
063: .getConstructor();
064: assertEquals("bold", noParamConstructor.getAnnotation(
065: MyStyle.class).value());
066: }
067:
068: public void testGetAnnotationForOneParam() throws Exception {
069: Constructor<_ConstructorTestCase> oneParamConstructor = _ConstructorTestCase.class
070: .getConstructor(String.class);
071: assertEquals("italic", oneParamConstructor.getAnnotation(
072: MyStyle.class).value());
073: }
074:
075: public void testGetAnnotationForTwoParam() throws Exception {
076: Constructor<_ConstructorTestCase> twoParamConstructor = _ConstructorTestCase.class
077: .getConstructor(String.class, int.class);
078: assertNull(twoParamConstructor.getAnnotation(MyStyle.class));
079: }
080:
081: public void testGetDeclaredAnnotations() throws Exception {
082: Constructor<_ConstructorTestCase> constructor = _ConstructorTestCase.class
083: .getConstructor();
084: assertEqualElements(constructor.getAnnotations(),
085: (Object[]) constructor.getDeclaredAnnotations());
086: }
087:
088: public void testGetGenericExceptionTypes() throws Exception {
089: Constructor<_ConstructorTestCase> constructor = _ConstructorTestCase.class
090: .getConstructor(String.class, int.class);
091: assertEquals(RuntimeException.class, singleton(constructor
092: .getGenericExceptionTypes()));
093: }
094:
095: public void testGetGenericParameterTypes() throws Exception {
096: Constructor<_ConstructorTestCase> constructor = _ConstructorTestCase.class
097: .getConstructor(String.class);
098: assertEquals(String.class, constructor
099: .getGenericParameterTypes()[0]);
100: }
101:
102: public void testGetParameterAnnotationsForTwoParam()
103: throws Exception {
104: Constructor<_ConstructorTestCase> twoParamConstructor = _ConstructorTestCase.class
105: .getConstructor(String.class, int.class);
106: Annotation[][] annotations = twoParamConstructor
107: .getParameterAnnotations();
108: assertEquals(2, annotations.length);
109: assertEquals(0, annotations[0].length);
110: assertEquals(1, annotations[1].length);
111: assertEquals("glass", ((MyStyle) annotations[1][0]).value());
112: }
113:
114: public void testGetTypeParameters() throws Exception {
115: Constructor<_ConstructorTestCase> constructor = _ConstructorTestCase.class
116: .getConstructor(String.class, int.class);
117: assertEquals(0, constructor.getTypeParameters().length);
118: }
119:
120: public void testIsAnnotationPresent() throws Exception {
121: Constructor<_ConstructorTestCase> constructor = _ConstructorTestCase.class
122: .getConstructor();
123: assertTrue(constructor.isAnnotationPresent(MyStyle.class));
124: assertFalse(constructor.isAnnotationPresent(MyFormatter.class));
125: }
126:
127: public static class Test {
128: private Test() {
129: }
130: }
131:
132: public void testIsSynthetic() throws Exception {
133: new Test();
134: Constructor[] constructors = Test.class
135: .getDeclaredConstructors();
136: assertEquals(2, constructors.length);
137: Constructor syntheticConstructor = null;
138: for (Constructor constructor : constructors) {
139: if (constructor.isSynthetic())
140: syntheticConstructor = constructor;
141: }
142: assertNotNull(syntheticConstructor);
143: assertFalse(Test.class.isSynthetic());
144: assertFalse(_ConstructorTestCase.class.isSynthetic());
145: }
146:
147: public void testIsVarArgs() throws Exception {
148: assertTrue(_ConstructorTestCase.class.getConstructor(
149: String.class, int[].class).isVarArgs());
150: assertFalse(_ConstructorTestCase.class.getConstructor(
151: String.class, int.class).isVarArgs());
152: }
153:
154: static class Test2<T extends String, RE extends RuntimeException> {
155: public <E extends Number> Test2(T t, E e, String[] strings)
156: throws RE, ClassNotFoundException {
157: }
158: }
159:
160: public void testGetGenericString() throws Exception {
161: assertEquals("public <E> " + this .getClass().getName()
162: + "$Test2(T,E,java.lang.String[])"
163: + " throws RE,java.lang.ClassNotFoundException",
164: Test2.class.getConstructors()[0].toGenericString());
165: }
166: }
|