Path: blob/master/test/jdk/java/lang/Class/GenericStringTest.java
41149 views
/*1* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6298888 6992705 8161500 630457826* @summary Check Class.toGenericString()27* @author Joseph D. Darcy28*/2930import java.lang.reflect.*;31import java.lang.annotation.*;32import java.util.*;3334@ExpectedGenericString("public class GenericStringTest")35public class GenericStringTest {36public Map<String, Integer>[] mixed = null;37public Map<String, Integer>[][] mixed2 = null;3839public static void main(String... args) throws ReflectiveOperationException {40int failures = 0;4142String[][] nested = {{""}};43int[][] intArray = {{1}};4445Map<Class<?>, String> testCases =46Map.of(int.class, "int",47void.class, "void",48args.getClass(), "java.lang.String[]",49nested.getClass(), "java.lang.String[][]",50intArray.getClass(), "int[][]",51java.lang.Enum.class, "public abstract class java.lang.Enum<E extends java.lang.Enum<E>>",52java.util.Map.class, "public abstract interface java.util.Map<K,V>",53java.util.EnumMap.class, "public class java.util.EnumMap<K extends java.lang.Enum<K>,V>",54java.util.EventListenerProxy.class, "public abstract class java.util.EventListenerProxy<T extends java.util.EventListener>");5556for (Map.Entry<Class<?>, String> testCase : testCases.entrySet()) {57failures += checkToGenericString(testCase.getKey(), testCase.getValue());58}5960Field f = GenericStringTest.class.getDeclaredField("mixed");61// The expected value includes "<K,V>" rather than62// "<...String,...Integer>" since the Class object rather than63// Type objects is being queried.64failures += checkToGenericString(f.getType(), "java.util.Map<K,V>[]");65f = GenericStringTest.class.getDeclaredField("mixed2");66failures += checkToGenericString(f.getType(), "java.util.Map<K,V>[][]");6768for(Class<?> clazz : List.of(GenericStringTest.class,69AnInterface.class,70LocalMap.class,71AnEnum.class,72AnotherEnum.class)) {73failures += checkToGenericString(clazz, clazz.getAnnotation(ExpectedGenericString.class).value());74}7576if (failures > 0) {77throw new RuntimeException();78}79}8081private static int checkToGenericString(Class<?> clazz, String expected) {82String genericString = clazz.toGenericString();83if (!genericString.equals(expected)) {84System.err.printf("Unexpected Class.toGenericString output; expected %n\t'%s',%n got %n\t'%s'.%n",85expected,86genericString);87return 1;88} else89return 0;90}91}9293@Retention(RetentionPolicy.RUNTIME)94@interface ExpectedGenericString {95String value();96}9798@ExpectedGenericString("abstract interface AnInterface")99strictfp interface AnInterface {}100101@ExpectedGenericString("abstract interface LocalMap<K,V>")102interface LocalMap<K,V> {}103104@ExpectedGenericString("final enum AnEnum")105enum AnEnum {106FOO;107}108109@ExpectedGenericString("enum AnotherEnum")110enum AnotherEnum {111BAR{};112}113114115