1 /*
2 * Copyright (c) 2013, 2016, 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 6298888 6992705 8161500 6304578
27 * @summary Check Class.toGenericString()
28 * @author Joseph D. Darcy
29 */
30
31 import java.lang.reflect.*;
32 import java.lang.annotation.*;
33 import java.util.*;
34
35 @ExpectedGenericString("public class GenericStringTest")
36 public class GenericStringTest {
37 public Map<String, Integer>[] mixed = null;
38 public Map<String, Integer>[][] mixed2 = null;
39
40 public static void main(String... args) throws ReflectiveOperationException {
41 int failures = 0;
42
43 String[][] nested = {{""}};
44 int[][] intArray = {{1}};
45
46 Map<Class<?>, String> testCases =
47 Map.of(int.class, "int",
48 void.class, "void",
53 java.util.Map.class, "public abstract interface java.util.Map<K,V>",
54 java.util.EnumMap.class, "public class java.util.EnumMap<K extends java.lang.Enum<K>,V>",
55 java.util.EventListenerProxy.class, "public abstract class java.util.EventListenerProxy<T extends java.util.EventListener>");
56
57 for (Map.Entry<Class<?>, String> testCase : testCases.entrySet()) {
58 failures += checkToGenericString(testCase.getKey(), testCase.getValue());
59 }
60
61 Field f = GenericStringTest.class.getDeclaredField("mixed");
62 // The expected value includes "<K,V>" rather than
63 // "<...String,...Integer>" since the Class object rather than
64 // Type objects is being queried.
65 failures += checkToGenericString(f.getType(), "java.util.Map<K,V>[]");
66 f = GenericStringTest.class.getDeclaredField("mixed2");
67 failures += checkToGenericString(f.getType(), "java.util.Map<K,V>[][]");
68
69 for(Class<?> clazz : List.of(GenericStringTest.class,
70 AnInterface.class,
71 LocalMap.class,
72 AnEnum.class,
73 AnotherEnum.class)) {
74 failures += checkToGenericString(clazz, clazz.getAnnotation(ExpectedGenericString.class).value());
75 }
76
77 if (failures > 0) {
78 throw new RuntimeException();
79 }
80 }
81
82 private static int checkToGenericString(Class<?> clazz, String expected) {
83 String genericString = clazz.toGenericString();
84 if (!genericString.equals(expected)) {
85 System.err.printf("Unexpected Class.toGenericString output; expected %n\t'%s',%n got %n\t'%s'.%n",
86 expected,
87 genericString);
88 return 1;
89 } else
90 return 0;
91 }
92 }
93
94 @Retention(RetentionPolicy.RUNTIME)
95 @interface ExpectedGenericString {
96 String value();
97 }
98
99 @ExpectedGenericString("abstract interface AnInterface")
100 strictfp interface AnInterface {}
101
102 @ExpectedGenericString("abstract interface LocalMap<K,V>")
103 interface LocalMap<K,V> {}
104
105 @ExpectedGenericString("final enum AnEnum")
106 enum AnEnum {
107 FOO;
108 }
109
110 @ExpectedGenericString("enum AnotherEnum")
111 enum AnotherEnum {
112 BAR{};
113 }
|
1 /*
2 * Copyright (c) 2013, 2024, 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 6298888 6992705 8161500 6304578
27 * @summary Check Class.toGenericString()
28 * @author Joseph D. Darcy
29 * @enablePreview
30 * @compile GenericStringTest.java
31 * @run main/othervm GenericStringTest
32 */
33
34 import java.lang.reflect.*;
35 import java.lang.annotation.*;
36 import java.util.*;
37
38 @ExpectedGenericString("public class GenericStringTest")
39 public class GenericStringTest {
40 public Map<String, Integer>[] mixed = null;
41 public Map<String, Integer>[][] mixed2 = null;
42
43 public static void main(String... args) throws ReflectiveOperationException {
44 int failures = 0;
45
46 String[][] nested = {{""}};
47 int[][] intArray = {{1}};
48
49 Map<Class<?>, String> testCases =
50 Map.of(int.class, "int",
51 void.class, "void",
56 java.util.Map.class, "public abstract interface java.util.Map<K,V>",
57 java.util.EnumMap.class, "public class java.util.EnumMap<K extends java.lang.Enum<K>,V>",
58 java.util.EventListenerProxy.class, "public abstract class java.util.EventListenerProxy<T extends java.util.EventListener>");
59
60 for (Map.Entry<Class<?>, String> testCase : testCases.entrySet()) {
61 failures += checkToGenericString(testCase.getKey(), testCase.getValue());
62 }
63
64 Field f = GenericStringTest.class.getDeclaredField("mixed");
65 // The expected value includes "<K,V>" rather than
66 // "<...String,...Integer>" since the Class object rather than
67 // Type objects is being queried.
68 failures += checkToGenericString(f.getType(), "java.util.Map<K,V>[]");
69 f = GenericStringTest.class.getDeclaredField("mixed2");
70 failures += checkToGenericString(f.getType(), "java.util.Map<K,V>[][]");
71
72 for(Class<?> clazz : List.of(GenericStringTest.class,
73 AnInterface.class,
74 LocalMap.class,
75 AnEnum.class,
76 AnotherEnum.class,
77 AValueClass.class)) {
78 failures += checkToGenericString(clazz, clazz.getAnnotation(ExpectedGenericString.class).value());
79 }
80
81 if (failures > 0) {
82 throw new RuntimeException();
83 }
84 }
85
86 private static int checkToGenericString(Class<?> clazz, String expected) {
87 String genericString = clazz.toGenericString();
88 if (!genericString.equals(expected)) {
89 System.err.printf("Unexpected Class.toGenericString output; expected %n\t'%s',%n got %n\t'%s'.%n",
90 expected,
91 genericString);
92 return 1;
93 } else
94 return 0;
95 }
96 }
97
98 @Retention(RetentionPolicy.RUNTIME)
99 @interface ExpectedGenericString {
100 String value();
101 }
102
103 @ExpectedGenericString("abstract interface AnInterface")
104 strictfp interface AnInterface {}
105
106 @ExpectedGenericString("abstract interface LocalMap<K,V>")
107 interface LocalMap<K,V> {}
108
109 @ExpectedGenericString("final enum AnEnum")
110 enum AnEnum {
111 FOO;
112 }
113
114 @ExpectedGenericString("enum AnotherEnum")
115 enum AnotherEnum {
116 BAR{};
117 }
118
119 @ExpectedGenericString("final value class AValueClass<E>")
120 value class AValueClass<E> {}
|