KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
36 import java.text.SimpleDateFormat JavaDoc;
37 import java.util.*;
38 import net.sf.retrotranslator.runtime.java.lang.*;
39 import net.sf.retrotranslator.tests.BaseTestCase;
40
41 /**
42  * @author Taras Puchko
43  */

44 public class _FieldTestCase extends BaseTestCase {
45
46     @MyFormatter(
47             pattern = "aabbcc",
48             format = SimpleDateFormat JavaDoc.class,
49             backgroundColor = MyColor.BLUE,
50             backgroundStyle = @MyStyle("italic"),
51             tabPositions = {10, 20, 30},
52             keywords = {"my", "formatter"},
53             colors = {MyColor.RED, MyColor.GREEN},
54             styles = {@MyStyle("bold"), @MyStyle("small")}
55     )
56     protected String JavaDoc message;
57
58     private Field field;
59
60     protected void setUp() throws Exception JavaDoc {
61         super.setUp();
62         field = getClass().getDeclaredField("message");
63     }
64
65     private MyFormatter getAnnotation() {
66         return field.getAnnotation(MyFormatter.class);
67     }
68
69     public void testGetAnnotation() {
70         MyFormatter formatter = getAnnotation();
71         assertEquals("aabbcc", formatter.pattern());
72         assertEquals("en", formatter.lang());
73         assertEquals(SimpleDateFormat JavaDoc.class, formatter.format());
74         assertEquals(MyColor.BLUE, formatter.backgroundColor());
75         assertEquals("italic", formatter.backgroundStyle().value());
76
77         int[] tabPositions = formatter.tabPositions();
78         assertEquals(3, tabPositions.length);
79         assertEquals(10, tabPositions[0]);
80         assertEquals(20, tabPositions[1]);
81         assertEquals(30, tabPositions[2]);
82
83         String JavaDoc[] keywords = formatter.keywords();
84         assertEquals(2, keywords.length);
85         assertEquals("my", keywords[0]);
86         assertEquals("formatter", keywords[1]);
87
88         MyColor[] colors = formatter.colors();
89         assertEquals(2, colors.length);
90         assertSame(MyColor.RED, colors[0]);
91         assertSame(MyColor.GREEN, colors[1]);
92
93         MyStyle[] styles = formatter.styles();
94         assertEquals(2, styles.length);
95         assertEquals("bold", styles[0].value());
96         assertEquals("small", styles[1].value());
97         assertNull(getProxyField().getAnnotation(MyFormatter.class));
98     }
99
100     public void testGetAnnotation_Equals() throws Exception JavaDoc {
101         MyFormatter first = getAnnotation();
102         MyFormatter second = getAnnotation();
103         assertEquals(first, second);
104         assertEquals(getAnnotation(), pump(getAnnotation()));
105     }
106
107     public void testGetAnnotation_HashCode() {
108         int first = getAnnotation().hashCode();
109         int second = getAnnotation().hashCode();
110         assertEquals(first, second);
111     }
112
113     public void testGetAnnotation_ToString() throws Exception JavaDoc {
114         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(getAnnotation().toString());
115         delete(buffer, "pattern=aabbcc");
116         delete(buffer, "lang=en");
117         delete(buffer, "format=class java.text.SimpleDateFormat");
118         delete(buffer, "backgroundColor=BLUE");
119         delete(buffer, "backgroundStyle=@" + MyStyle.class.getName() + "(value=italic)");
120         delete(buffer, "tabPositions=[10, 20, 30]");
121         delete(buffer, "keywords=[my, formatter]");
122         delete(buffer, "colors=[RED, GREEN]");
123         delete(buffer, "styles=[@" + MyStyle.class.getName() + "(value=bold), @" + MyStyle.class.getName() + "(value=small)]");
124         delete(buffer, "numbers=[1, 2, 3]");
125         delete(buffer, "isPlain=false");
126         assertEquals("@" + MyFormatter.class.getName() + "(, , , , , , , , , , )", buffer.toString());
127     }
128
129     private static void delete(StringBuffer JavaDoc buffer, String JavaDoc substring) {
130         int index = buffer.indexOf(substring);
131         if (index == -1) {
132             substring = substring.replaceAll("\\s", "");
133             index = buffer.indexOf(substring);
134         }
135         assertFalse("Cannot find: " + substring + " in " + buffer, index == -1);
136         buffer.delete(index, index + substring.length());
137     }
138
139     public void testGetAnnotations() throws Exception JavaDoc {
140         Annotation JavaDoc[] annotations = field.getAnnotations();
141         assertEquals(1, annotations.length);
142         assertEquals(getAnnotation(), annotations[0]);
143         assertEquals(0, getProxyField().getAnnotations().length);
144     }
145
146     public void testGetDeclaredAnnotations() throws Exception JavaDoc {
147         assertTrue(Arrays.equals(field.getDeclaredAnnotations(), field.getAnnotations()));
148         assertEquals(0, getProxyField().getDeclaredAnnotations().length);
149     }
150
151     public void testGetGenericType() throws Exception JavaDoc {
152         class Outer {
153             class Top<A> {
154                 class Middle<B> {
155                     class Bottom {
156                     }
157                 }
158             }
159         }
160
161         class Test<T> {
162             public Outer.Top<Comparable JavaDoc<? super Integer JavaDoc>>.Middle<List<?>>.Bottom f;
163         }
164         Field field = Test.class.getField("f");
165         ParameterizedType bottom = (ParameterizedType) field.getGenericType();
166         assertEquals(Outer.Top.Middle.Bottom.class, bottom.getRawType());
167         assertEquals(0, bottom.getActualTypeArguments().length);
168         ParameterizedType middle = (ParameterizedType) bottom.getOwnerType();
169         assertEquals(Outer.Top.Middle.class, middle.getRawType());
170
171         ParameterizedType list = (ParameterizedType) singleton(middle.getActualTypeArguments());
172         assertEquals(List.class, list.getRawType());
173         assertNull(list.getOwnerType());
174         WildcardType listParam = (WildcardType) singleton(list.getActualTypeArguments());
175         assertEquals(Object JavaDoc.class, singleton(listParam.getUpperBounds()));
176         assertEquals(0, listParam.getLowerBounds().length);
177
178         ParameterizedType top = (ParameterizedType) middle.getOwnerType();
179         assertEquals(Outer.Top.class, top.getRawType());
180         ParameterizedType comparable = (ParameterizedType) singleton(top.getActualTypeArguments());
181         assertEquals(Comparable JavaDoc.class, comparable.getRawType());
182         assertNull(comparable.getOwnerType());
183
184         WildcardType comparableParam = (WildcardType) singleton(comparable.getActualTypeArguments());
185         assertEquals(Object JavaDoc.class, singleton(comparableParam.getUpperBounds()));
186         assertEquals(Integer JavaDoc.class, singleton(comparableParam.getLowerBounds()));
187         assertEquals(Outer.class, top.getOwnerType());
188         Field proxyField = getProxyField();
189         assertEquals(proxyField.getType(), proxyField.getGenericType());
190     }
191
192     public void testIsAnnotationPresent() throws Exception JavaDoc {
193         assertTrue(field.isAnnotationPresent(MyFormatter.class));
194         assertFalse(field.isAnnotationPresent(MyStyle.class));
195         assertFalse(getProxyField().isAnnotationPresent(MyStyle.class));
196     }
197
198     public void testIsEnumConstant() throws Exception JavaDoc {
199         assertTrue(MyColor.class.getField(MyColor.BLUE.name()).isEnumConstant());
200         assertFalse(field.isEnumConstant());
201         assertFalse(getProxyField().isEnumConstant());
202     }
203
204     public void testIsSynthetic() throws Exception JavaDoc {
205         class Test {
206         }
207         assertTrue(Test.class.getDeclaredFields()[0].isSynthetic());
208         assertFalse(getProxyField().isSynthetic());
209     }
210
211     static class Test<T extends Map> {
212         public Comparable JavaDoc<T>[] c;
213         public static boolean b;
214         public static Test<HashMap>.Inner i;
215
216         public class Inner {
217         }
218     }
219
220     public void testToGenericString() throws Exception JavaDoc {
221         String JavaDoc name = this.getClass().getName();
222         assertEquals("public java.lang.Comparable<T>[] " + name + "$Test.c",
223                 Test.class.getField("c").toGenericString());
224         assertEquals("public static boolean " + name + "$Test.b",
225                 Test.class.getField("b").toGenericString());
226         assertEquals("public static " + name + "." + name + "$Test<java.util.HashMap>.Inner " + name + "$Test.i",
227                 Test.class.getField("i").toGenericString());
228         Field proxyField = getProxyField();
229         assertEquals(proxyField.toString(), proxyField.toGenericString());
230     }
231
232     private Field getProxyField() {
233         return Proxy.getProxyClass(getClass().getClassLoader(), Comparable JavaDoc.class).getDeclaredFields()[0];
234     }
235
236 }
Popular Tags