1 32 package net.sf.retrotranslator.runtime.java.lang.reflect; 33 34 import java.lang.annotation.Annotation ; 35 import java.lang.reflect.*; 36 import java.text.SimpleDateFormat ; 37 import java.util.*; 38 import net.sf.retrotranslator.runtime.java.lang.*; 39 import net.sf.retrotranslator.tests.BaseTestCase; 40 41 44 public class _FieldTestCase extends BaseTestCase { 45 46 @MyFormatter( 47 pattern = "aabbcc", 48 format = SimpleDateFormat .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 message; 57 58 private Field field; 59 60 protected void setUp() throws Exception { 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 .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 [] 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 { 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 { 114 StringBuffer buffer = new StringBuffer (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 buffer, String 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 { 140 Annotation [] 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 { 147 assertTrue(Arrays.equals(field.getDeclaredAnnotations(), field.getAnnotations())); 148 assertEquals(0, getProxyField().getDeclaredAnnotations().length); 149 } 150 151 public void testGetGenericType() throws Exception { 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 <? super Integer >>.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 .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 .class, comparable.getRawType()); 182 assertNull(comparable.getOwnerType()); 183 184 WildcardType comparableParam = (WildcardType) singleton(comparable.getActualTypeArguments()); 185 assertEquals(Object .class, singleton(comparableParam.getUpperBounds())); 186 assertEquals(Integer .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 { 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 { 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 { 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 <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 { 221 String 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 .class).getDeclaredFields()[0]; 234 } 235 236 } | Popular Tags |