1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8058202 8212081 27 * @summary Test java.lang.Object methods on AnnotatedType objects. 28 */ 29 30 import java.lang.annotation.*; 31 import java.lang.reflect.*; 32 import java.util.*; 33 import java.util.regex.*; 34 35 /** 36 * Test toString, equals, and hashCode on various AnnotatedType objects. 37 */ 38 39 public class TestObjectMethods { 40 private static int errors = 0; 41 42 /* 43 * There are various subtypes of AnnotatedType implementations: 44 * 45 * AnnotatedType 46 * AnnotatedArrayType 56 * well. 57 */ 58 public static void main(String... args) { 59 Class<?>[] testClasses = {TypeHost.class, AnnotatedTypeHost.class}; 60 61 for (Class<?> clazz : testClasses) { 62 testEqualsReflexivity(clazz); 63 testEquals(clazz); 64 } 65 66 testToString(TypeHost.class); 67 testToString(AnnotatedTypeHost.class); 68 69 testAnnotationsMatterForEquals(TypeHost.class, AnnotatedTypeHost.class); 70 71 testGetAnnotations(TypeHost.class, false); 72 testGetAnnotations(AnnotatedTypeHost.class, true); 73 74 testWildcards(); 75 76 if (errors > 0) { 77 throw new RuntimeException(errors + " errors"); 78 } 79 } 80 81 /* 82 * For non-array types, verify toString version of the annotated 83 * type ends with the same string as the generic type. 84 */ 85 static void testToString(Class<?> clazz) { 86 System.err.println("Testing toString on methods of class " + clazz.getName()); 87 Method[] methods = clazz.getDeclaredMethods(); 88 for (Method m : methods) { 89 // Expected information about the type annotations stored 90 // in a *declaration* annotation. 91 AnnotTypeInfo annotTypeInfo = m.getAnnotation(AnnotTypeInfo.class); 92 int expectedAnnotCount = annotTypeInfo.count(); 93 Relation relation = annotTypeInfo.relation(); 94 95 AnnotatedType annotType = m.getAnnotatedReturnType(); 286 System.err.println("Testing wildcards"); 287 // public @AnnotType(10) Set<? extends Number> fooNumberSet() {return null;} 288 // public @AnnotType(11) Set<@AnnotType(13) ? extends Number> fooNumberSet2() {return null;} 289 AnnotatedWildcardType awt1 = extractWildcard("fooNumberSet"); 290 AnnotatedWildcardType awt2 = extractWildcard("fooNumberSet2"); 291 292 if (!awt1.equals(extractWildcard("fooNumberSet")) || 293 !awt2.equals(extractWildcard("fooNumberSet2"))) { 294 errors++; 295 System.err.println("Bad equality comparison on wildcards."); 296 } 297 298 checkTypesForEquality(awt1, awt2, false); 299 300 if (awt2.getAnnotations().length == 0) { 301 errors++; 302 System.err.println("Expected annotations not found."); 303 } 304 } 305 306 private static AnnotatedWildcardType extractWildcard(String methodName) { 307 try { 308 return (AnnotatedWildcardType) 309 (((AnnotatedParameterizedType)(AnnotatedTypeHost.class.getMethod(methodName). 310 getAnnotatedReturnType())). 311 getAnnotatedActualTypeArguments()[0] ); 312 } catch (Exception e) { 313 throw new RuntimeException(e); 314 } 315 } 316 317 // The TypeHost and AnnotatedTypeHost classes declare methods with 318 // the same name and signatures but with the AnnotatedTypeHost 319 // methods having annotations on their return type, where 320 // possible. 321 322 static class TypeHost<E, F extends Number> { 323 @AnnotTypeInfo 324 public void fooVoid() {return;} 325 326 @AnnotTypeInfo 327 public int foo() {return 0;} 328 329 @AnnotTypeInfo 330 public String fooString() {return null;} 331 332 @AnnotTypeInfo 333 public int[] fooIntArray() {return null;} | 1 /* 2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8058202 8212081 8224012 27 * @summary Test java.lang.Object methods on AnnotatedType objects. 28 */ 29 30 import java.lang.annotation.*; 31 import java.lang.reflect.*; 32 import java.util.*; 33 import java.util.regex.*; 34 35 /** 36 * Test toString, equals, and hashCode on various AnnotatedType objects. 37 */ 38 39 public class TestObjectMethods { 40 private static int errors = 0; 41 42 /* 43 * There are various subtypes of AnnotatedType implementations: 44 * 45 * AnnotatedType 46 * AnnotatedArrayType 56 * well. 57 */ 58 public static void main(String... args) { 59 Class<?>[] testClasses = {TypeHost.class, AnnotatedTypeHost.class}; 60 61 for (Class<?> clazz : testClasses) { 62 testEqualsReflexivity(clazz); 63 testEquals(clazz); 64 } 65 66 testToString(TypeHost.class); 67 testToString(AnnotatedTypeHost.class); 68 69 testAnnotationsMatterForEquals(TypeHost.class, AnnotatedTypeHost.class); 70 71 testGetAnnotations(TypeHost.class, false); 72 testGetAnnotations(AnnotatedTypeHost.class, true); 73 74 testWildcards(); 75 76 testFbounds(); 77 78 if (errors > 0) { 79 throw new RuntimeException(errors + " errors"); 80 } 81 } 82 83 /* 84 * For non-array types, verify toString version of the annotated 85 * type ends with the same string as the generic type. 86 */ 87 static void testToString(Class<?> clazz) { 88 System.err.println("Testing toString on methods of class " + clazz.getName()); 89 Method[] methods = clazz.getDeclaredMethods(); 90 for (Method m : methods) { 91 // Expected information about the type annotations stored 92 // in a *declaration* annotation. 93 AnnotTypeInfo annotTypeInfo = m.getAnnotation(AnnotTypeInfo.class); 94 int expectedAnnotCount = annotTypeInfo.count(); 95 Relation relation = annotTypeInfo.relation(); 96 97 AnnotatedType annotType = m.getAnnotatedReturnType(); 288 System.err.println("Testing wildcards"); 289 // public @AnnotType(10) Set<? extends Number> fooNumberSet() {return null;} 290 // public @AnnotType(11) Set<@AnnotType(13) ? extends Number> fooNumberSet2() {return null;} 291 AnnotatedWildcardType awt1 = extractWildcard("fooNumberSet"); 292 AnnotatedWildcardType awt2 = extractWildcard("fooNumberSet2"); 293 294 if (!awt1.equals(extractWildcard("fooNumberSet")) || 295 !awt2.equals(extractWildcard("fooNumberSet2"))) { 296 errors++; 297 System.err.println("Bad equality comparison on wildcards."); 298 } 299 300 checkTypesForEquality(awt1, awt2, false); 301 302 if (awt2.getAnnotations().length == 0) { 303 errors++; 304 System.err.println("Expected annotations not found."); 305 } 306 } 307 308 309 private static AnnotatedWildcardType extractWildcard(String methodName) { 310 try { 311 return (AnnotatedWildcardType) 312 (((AnnotatedParameterizedType)(AnnotatedTypeHost.class.getMethod(methodName). 313 getAnnotatedReturnType())). 314 getAnnotatedActualTypeArguments()[0] ); 315 } catch (Exception e) { 316 throw new RuntimeException(e); 317 } 318 } 319 320 static void testFbounds() { 321 // Make sure equals and hashCode work fine for a type 322 // involving an F-bound, in particular Comparable<E> in 323 // java.lang.Enum: 324 // 325 // class Enum<E extends Enum<E>> 326 // implements Constable, Comparable<E>, Serializable 327 328 AnnotatedType[] types = Enum.class.getAnnotatedInterfaces(); 329 330 for (int i = 0; i < types.length; i ++) { 331 for (int j = 0; j < types.length; j ++) { 332 checkTypesForEquality(types[i], types[j], i == j); 333 } 334 } 335 } 336 337 // The TypeHost and AnnotatedTypeHost classes declare methods with 338 // the same name and signatures but with the AnnotatedTypeHost 339 // methods having annotations on their return type, where 340 // possible. 341 342 static class TypeHost<E, F extends Number> { 343 @AnnotTypeInfo 344 public void fooVoid() {return;} 345 346 @AnnotTypeInfo 347 public int foo() {return 0;} 348 349 @AnnotTypeInfo 350 public String fooString() {return null;} 351 352 @AnnotTypeInfo 353 public int[] fooIntArray() {return null;} |