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:
5506
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 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:
5506
diff
changeset
|
39 |
for(Class<?> clazz: List.of(TestClass1.class, TestClass2.class)) |
2 | 40 |
for(Constructor<?> ctor: clazz.getDeclaredConstructors()) { |
41 |
ExpectedGenericString egs = ctor.getAnnotation(ExpectedGenericString.class); |
|
42 |
String actual = ctor.toGenericString(); |
|
43 |
System.out.println(actual); |
|
39825
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
44 |
failures += checkForFailure(egs.value(), actual); |
2 | 45 |
|
46 |
if (ctor.isAnnotationPresent(ExpectedString.class)) { |
|
39825
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
47 |
failures += checkForFailure(ctor.getAnnotation(ExpectedString.class).value(), |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
48 |
ctor.toString()); |
2 | 49 |
} |
50 |
} |
|
51 |
||
52 |
if (failures > 0) { |
|
53 |
System.err.println("Test failed."); |
|
54 |
throw new RuntimeException(); |
|
55 |
} |
|
56 |
} |
|
39825
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
57 |
|
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
58 |
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
|
59 |
if (!expected.equals(actual)) { |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
60 |
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
|
61 |
expected, actual); |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
62 |
return 1; |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
63 |
} else |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
64 |
return 0; |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
65 |
} |
2 | 66 |
} |
67 |
||
68 |
class TestClass1 { |
|
69 |
@ExpectedGenericString( |
|
70 |
"TestClass1(int,double)") |
|
71 |
TestClass1(int x, double y) {} |
|
72 |
||
73 |
@ExpectedGenericString( |
|
74 |
"private TestClass1(int,int)") |
|
75 |
private TestClass1(int x, int param2) {} |
|
76 |
||
77 |
@ExpectedGenericString( |
|
78 |
"private TestClass1(java.lang.Object) throws java.lang.RuntimeException") |
|
39825
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
79 |
@ExpectedString( |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
80 |
"private TestClass1(java.lang.Object) throws java.lang.RuntimeException") |
2 | 81 |
private TestClass1(Object o) throws RuntimeException {} |
82 |
||
83 |
@ExpectedGenericString( |
|
84 |
"protected <S,T> TestClass1(S,T) throws java.lang.Exception") |
|
39825
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
85 |
@ExpectedString( |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
86 |
"protected TestClass1(java.lang.Object,java.lang.Object) throws java.lang.Exception") |
2 | 87 |
protected <S, T> TestClass1(S s, T t) throws Exception{} |
88 |
||
89 |
@ExpectedGenericString( |
|
39825
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
90 |
"<E> TestClass1() throws E") |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
91 |
@ExpectedString( |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
92 |
"TestClass1() throws java.lang.Exception") |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
93 |
<E extends Exception> TestClass1() throws E {} |
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
94 |
|
6441823cea46
8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents:
39731
diff
changeset
|
95 |
@ExpectedGenericString( |
2 | 96 |
"TestClass1(java.lang.Object...)") |
97 |
@ExpectedString( |
|
98 |
"TestClass1(java.lang.Object[])") |
|
99 |
TestClass1(Object... o){} |
|
100 |
} |
|
101 |
||
102 |
class TestClass2<E> { |
|
103 |
@ExpectedGenericString( |
|
104 |
"public <T> TestClass2(E,T)") |
|
105 |
public <T> TestClass2(E e, T t) {} |
|
106 |
} |
|
107 |
||
108 |
@Retention(RetentionPolicy.RUNTIME) |
|
109 |
@interface ExpectedGenericString { |
|
110 |
String value(); |
|
111 |
} |
|
112 |
||
113 |
@Retention(RetentionPolicy.RUNTIME) |
|
114 |
@interface ExpectedString { |
|
115 |
String value(); |
|
116 |
} |