author | darcy |
Tue, 26 Jul 2016 12:56:26 -0700 | |
changeset 39825 | 6441823cea46 |
parent 39731 | 7a4bc90065bd |
permissions | -rw-r--r-- |
2 | 1 |
/* |
39731
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
2 |
* Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
39825
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
26 |
* @bug 5033583 6316717 6470106 8004979 8161500 8162539 |
2 | 27 |
* @summary Check toGenericString() and toString() methods |
28 |
* @author Joseph D. Darcy |
|
29 |
*/ |
|
30 |
||
31 |
import java.lang.reflect.*; |
|
32 |
import java.lang.annotation.*; |
|
33 |
import java.util.*; |
|
34 |
||
35 |
public class GenericStringTest { |
|
36 |
public static void main(String argv[]) throws Exception { |
|
37 |
int failures = 0; |
|
38 |
||
39731
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
39 |
for(Class<?> clazz: List.of(TestClass1.class, TestClass2.class, |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
40 |
Roebling.class, TestInterface1.class)) |
2 | 41 |
for(Method method: clazz.getDeclaredMethods()) { |
42 |
ExpectedGenericString egs = method.getAnnotation(ExpectedGenericString.class); |
|
43 |
if (egs != null) { |
|
44 |
String actual = method.toGenericString(); |
|
45 |
System.out.println(actual); |
|
19387
1054a32f3f02
8015780: java/lang/reflect/Method/GenericStringTest.java failing
vromero
parents:
16729
diff
changeset
|
46 |
if (method.isBridge()) { |
39825
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
47 |
failures += checkForFailure(egs.bridgeValue(), actual); |
19387
1054a32f3f02
8015780: java/lang/reflect/Method/GenericStringTest.java failing
vromero
parents:
16729
diff
changeset
|
48 |
} else { |
39825
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
49 |
failures += checkForFailure(egs.value(), actual); |
2 | 50 |
} |
51 |
} |
|
52 |
||
53 |
if (method.isAnnotationPresent(ExpectedString.class)) { |
|
54 |
ExpectedString es = method.getAnnotation(ExpectedString.class); |
|
55 |
String actual = method.toString(); |
|
39825
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
56 |
failures += checkForFailure(es.value(), actual); |
2 | 57 |
} |
58 |
||
59 |
} |
|
60 |
||
61 |
// Bridge Test; no "volatile" methods |
|
62 |
for(Method method: Roebling.class.getDeclaredMethods()) { |
|
63 |
String s1 = method.toGenericString(); |
|
64 |
String s2 = method.toString(); |
|
65 |
System.out.println("Generic: " + s1); |
|
66 |
System.out.println("Regular: " + s2); |
|
67 |
if (s1.indexOf("volatile") != -1 || |
|
68 |
s2.indexOf("volatile") != -1) { |
|
69 |
failures++; |
|
70 |
System.err.println("ERROR: Bad string; unexpected ``volatile''"); |
|
71 |
} |
|
72 |
} |
|
73 |
||
74 |
if (failures > 0) { |
|
75 |
System.err.println("Test failed."); |
|
76 |
throw new RuntimeException(); |
|
77 |
} |
|
78 |
} |
|
39825
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
79 |
|
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
80 |
private static int checkForFailure(String expected, String actual) { |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
81 |
if (!expected.equals(actual)) { |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
82 |
System.err.printf("ERROR: Expected ''%s'';%ngot ''%s''.\n", |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
83 |
expected, actual); |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
84 |
return 1; |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
85 |
} else |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
86 |
return 0; |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
87 |
} |
2 | 88 |
} |
89 |
||
90 |
class TestClass1 { |
|
91 |
@ExpectedGenericString( |
|
92 |
"void TestClass1.method1(int,double)") |
|
93 |
void method1(int x, double y) {} |
|
94 |
||
95 |
@ExpectedGenericString( |
|
96 |
"private static java.lang.String TestClass1.method2(int,int)") |
|
97 |
private static String method2(int x, int param2) {return null;} |
|
98 |
||
99 |
@ExpectedGenericString( |
|
100 |
"static void TestClass1.method3() throws java.lang.RuntimeException") |
|
101 |
static void method3() throws RuntimeException {return;} |
|
102 |
||
103 |
@ExpectedGenericString( |
|
104 |
"protected <S,T> S TestClass1.method4(S,T) throws java.lang.Exception") |
|
105 |
protected <S, T> S method4(S s, T t) throws Exception {return null;} |
|
106 |
} |
|
107 |
||
108 |
class TestClass2<E, F extends Exception> { |
|
109 |
@ExpectedGenericString( |
|
110 |
"public <T> T TestClass2.method1(E,T)") |
|
111 |
public <T> T method1(E e, T t) {return null;} |
|
112 |
||
113 |
@ExpectedGenericString( |
|
114 |
"public void TestClass2.method2() throws F") |
|
39825
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
115 |
@ExpectedString( |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
116 |
"public void TestClass2.method2() throws java.lang.Exception") |
2 | 117 |
public void method2() throws F {return;} |
39731
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
118 |
|
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
119 |
@ExpectedGenericString( |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
120 |
"public E[] TestClass2.method3()") |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
121 |
public E[] method3() {return null;} |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
122 |
|
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
123 |
@ExpectedGenericString( |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
124 |
"public E[][] TestClass2.method4()") |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
125 |
public E[][] method4() {return null;} |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
126 |
|
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
127 |
@ExpectedGenericString( |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
128 |
"public java.util.List<E[]> TestClass2.method5()") |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
129 |
public List<E[]> method5() {return null;} |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
130 |
|
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
131 |
@ExpectedGenericString( |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
132 |
"public java.util.List<?> TestClass2.method6()") |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
133 |
public List<?> method6() {return null;} |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
134 |
|
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
135 |
@ExpectedGenericString( |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
136 |
"public java.util.List<?>[] TestClass2.method7()") |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
137 |
public List<?>[] method7() {return null;} |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
138 |
|
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
139 |
@ExpectedGenericString( |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
140 |
"public <K,V> java.util.Map<K, V> TestClass2.method8()") |
7a4bc90065bd
8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents:
19387
diff
changeset
|
141 |
public <K, V> Map<K, V> method8() {return null;} |
2 | 142 |
} |
143 |
||
144 |
class Roebling implements Comparable<Roebling> { |
|
145 |
@ExpectedGenericString( |
|
19387
1054a32f3f02
8015780: java/lang/reflect/Method/GenericStringTest.java failing
vromero
parents:
16729
diff
changeset
|
146 |
value="public int Roebling.compareTo(Roebling)", |
1054a32f3f02
8015780: java/lang/reflect/Method/GenericStringTest.java failing
vromero
parents:
16729
diff
changeset
|
147 |
bridgeValue="public int Roebling.compareTo(java.lang.Object)") |
2 | 148 |
public int compareTo(Roebling r) { |
149 |
throw new IllegalArgumentException(); |
|
150 |
} |
|
151 |
||
152 |
// Not a transient method, (transient var-arg overlap) |
|
153 |
@ExpectedGenericString( |
|
154 |
"void Roebling.varArg(java.lang.Object...)") |
|
155 |
@ExpectedString( |
|
156 |
"void Roebling.varArg(java.lang.Object[])") |
|
157 |
void varArg(Object ... arg) {} |
|
158 |
} |
|
159 |
||
16729
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
160 |
interface TestInterface1 { |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
161 |
@ExpectedGenericString( |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
162 |
"public default void TestInterface1.foo()") |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
163 |
@ExpectedString( |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
164 |
"public default void TestInterface1.foo()") |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
165 |
public default void foo(){;} |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
166 |
|
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
167 |
@ExpectedString( |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
168 |
"public default java.lang.Object TestInterface1.bar()") |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
169 |
@ExpectedGenericString( |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
170 |
"public default <A> A TestInterface1.bar()") |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
171 |
default <A> A bar(){return null;} |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
172 |
|
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
173 |
@ExpectedString( |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
174 |
"public default strictfp double TestInterface1.quux()") |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
175 |
@ExpectedGenericString( |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
176 |
"public default strictfp double TestInterface1.quux()") |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
177 |
strictfp default double quux(){return 1.0;} |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
178 |
} |
3b26e313ad81
8004979: java.lang.reflect.Modifier.toString should include "default"
darcy
parents:
5506
diff
changeset
|
179 |
|
2 | 180 |
@Retention(RetentionPolicy.RUNTIME) |
181 |
@interface ExpectedGenericString { |
|
182 |
String value(); |
|
19387
1054a32f3f02
8015780: java/lang/reflect/Method/GenericStringTest.java failing
vromero
parents:
16729
diff
changeset
|
183 |
String bridgeValue() default ""; |
2 | 184 |
} |
185 |
||
186 |
@Retention(RetentionPolicy.RUNTIME) |
|
187 |
@interface ExpectedString { |
|
188 |
String value(); |
|
189 |
} |
|
19387
1054a32f3f02
8015780: java/lang/reflect/Method/GenericStringTest.java failing
vromero
parents:
16729
diff
changeset
|
190 |