KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > lang > reflect > _ConstructorTestCase


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.java.lang.reflect;
33
34 import java.lang.annotation.Annotation JavaDoc;
35 import java.lang.reflect.Constructor JavaDoc;
36 import net.sf.retrotranslator.runtime.java.lang.*;
37 import net.sf.retrotranslator.tests.BaseTestCase;
38
39 /**
40  * @author Taras Puchko
41  */

42 public class _ConstructorTestCase extends BaseTestCase {
43
44     @MyStyle("bold")
45     public _ConstructorTestCase() {
46     }
47
48     @MyStyle("italic")
49     public _ConstructorTestCase(String JavaDoc string) {
50         super(string);
51     }
52
53     public _ConstructorTestCase(String JavaDoc string, @MyStyle("glass") int i) throws RuntimeException JavaDoc {
54         super(string + i);
55     }
56
57     public _ConstructorTestCase(String JavaDoc string, int... numbers) {
58         super(string + numbers.length);
59     }
60
61     public void testGetAnnotationForNoParam() throws Exception JavaDoc {
62         Constructor JavaDoc<_ConstructorTestCase> noParamConstructor = _ConstructorTestCase.class.getConstructor();
63         assertEquals("bold", noParamConstructor.getAnnotation(MyStyle.class).value());
64     }
65
66     public void testGetAnnotationForOneParam() throws Exception JavaDoc {
67         Constructor JavaDoc<_ConstructorTestCase> oneParamConstructor = _ConstructorTestCase.class.getConstructor(String JavaDoc.class);
68         assertEquals("italic", oneParamConstructor.getAnnotation(MyStyle.class).value());
69     }
70
71     public void testGetAnnotationForTwoParam() throws Exception JavaDoc {
72         Constructor JavaDoc<_ConstructorTestCase> twoParamConstructor = _ConstructorTestCase.class.getConstructor(String JavaDoc.class, int.class);
73         assertNull(twoParamConstructor.getAnnotation(MyStyle.class));
74     }
75
76     public void testGetDeclaredAnnotations() throws Exception JavaDoc {
77         Constructor JavaDoc<_ConstructorTestCase> constructor = _ConstructorTestCase.class.getConstructor();
78         assertEqualElements(constructor.getAnnotations(), (Object JavaDoc[]) constructor.getDeclaredAnnotations());
79     }
80
81     public void testGetGenericExceptionTypes() throws Exception JavaDoc {
82         Constructor JavaDoc<_ConstructorTestCase> constructor = _ConstructorTestCase.class.getConstructor(String JavaDoc.class, int.class);
83         assertEquals(RuntimeException JavaDoc.class, singleton(constructor.getGenericExceptionTypes()));
84     }
85
86     public void testGetGenericParameterTypes() throws Exception JavaDoc {
87         Constructor JavaDoc<_ConstructorTestCase> constructor = _ConstructorTestCase.class.getConstructor(String JavaDoc.class);
88         assertEquals(String JavaDoc.class, constructor.getGenericParameterTypes()[0]);
89     }
90
91     public void testGetParameterAnnotationsForTwoParam() throws Exception JavaDoc {
92         Constructor JavaDoc<_ConstructorTestCase> twoParamConstructor = _ConstructorTestCase.class.getConstructor(String JavaDoc.class, int.class);
93         Annotation JavaDoc[][] annotations = twoParamConstructor.getParameterAnnotations();
94         assertEquals(2, annotations.length);
95         assertEquals(0, annotations[0].length);
96         assertEquals(1, annotations[1].length);
97         assertEquals("glass", ((MyStyle) annotations[1][0]).value());
98     }
99
100     public void testGetTypeParameters() throws Exception JavaDoc {
101         Constructor JavaDoc<_ConstructorTestCase> constructor = _ConstructorTestCase.class.getConstructor(String JavaDoc.class, int.class);
102         assertEquals(0, constructor.getTypeParameters().length);
103     }
104
105     public void testIsAnnotationPresent() throws Exception JavaDoc {
106         Constructor JavaDoc<_ConstructorTestCase> constructor = _ConstructorTestCase.class.getConstructor();
107         assertTrue(constructor.isAnnotationPresent(MyStyle.class));
108         assertFalse(constructor.isAnnotationPresent(MyFormatter.class));
109     }
110
111     private static class Test {
112         private Test() {
113         }
114     }
115
116     public void testIsSynthetic() throws Exception JavaDoc {
117         new Test();
118         Constructor JavaDoc[] constructors = Test.class.getDeclaredConstructors();
119         assertEquals(2, constructors.length);
120         Constructor JavaDoc syntheticConstructor = null;
121         for (Constructor JavaDoc constructor : constructors) {
122             if (constructor.isSynthetic()) syntheticConstructor = constructor;
123         }
124         assertNotNull(syntheticConstructor);
125         Class JavaDoc parameterType = syntheticConstructor.getParameterTypes()[0];
126         assertTrue(parameterType.isSynthetic());
127         assertFalse(Test.class.isSynthetic());
128         assertFalse(_ConstructorTestCase.class.isSynthetic());
129     }
130
131     public void testIsVarArgs() throws Exception JavaDoc {
132         assertTrue(_ConstructorTestCase.class.getConstructor(String JavaDoc.class, int[].class).isVarArgs());
133         assertFalse(_ConstructorTestCase.class.getConstructor(String JavaDoc.class, int.class).isVarArgs());
134     }
135
136     public void testGetGenericString() throws Exception JavaDoc {
137         class Test<T extends String JavaDoc, RE extends RuntimeException JavaDoc> {
138             public <E extends Number JavaDoc> Test(T t, E e, String JavaDoc[] strings) throws RE, ClassNotFoundException JavaDoc {
139             }
140         }
141
142         assertEquals("public <E> " +
143                 this.getClass().getName() +
144                 "$1Test(T,E,java.lang.String[])" +
145                 " throws RE,java.lang.ClassNotFoundException",
146                 Test.class.getConstructors()[0].toGenericString());
147     }
148 }
Popular Tags