36511
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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 |
* @summary Test SignatureParser
|
|
27 |
* @author Jean-Francois Denise
|
|
28 |
* @modules java.base/jdk.internal.jimage.decompressor
|
|
29 |
* @run main SignatureParserTest
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.util.Arrays;
|
|
33 |
import java.util.Objects;
|
|
34 |
|
|
35 |
import jdk.internal.jimage.decompressor.SignatureParser;
|
|
36 |
|
|
37 |
public class SignatureParserTest {
|
|
38 |
|
|
39 |
private int passed = 0;
|
|
40 |
private int failed = 0;
|
|
41 |
|
|
42 |
public static void main(String[] args) {
|
|
43 |
new SignatureParserTest().test();
|
|
44 |
}
|
|
45 |
|
|
46 |
private void test() {
|
|
47 |
test("[Ljava/lang/String;", "[L;", "java/lang/String");
|
|
48 |
test("[[[[[[[[[[Ljava/lang/String;", "[[[[[[[[[[L;", "java/lang/String");
|
|
49 |
test("<T:Ljava/lang/Object;:Ljava/lang/Comparable<-TT;>;>" +
|
|
50 |
"(Ljava/lang/String;Ljava/lang/Class<TT;>;TT;Ljava/lang/Comparable<-TT;>;" +
|
|
51 |
"Ljava/lang/Comparable<-TT;>;ZZ)V",
|
|
52 |
"<T:L;:L<-TT;>;>(L;L<TT;>;TT;L<-TT;>;L<-TT;>;ZZ)V",
|
|
53 |
"java/lang/Object", "java/lang/Comparable", "java/lang/String",
|
|
54 |
"java/lang/Class", "java/lang/Comparable", "java/lang/Comparable");
|
|
55 |
test("(Ljava/lang/String;ZLjava/util/EventListener;TTK;)V",
|
|
56 |
"(L;ZL;TTK;)V",
|
|
57 |
"java/lang/String", "java/util/EventListener");
|
|
58 |
test("<Y:Ljava/lang/String;>", "<Y:L;>", "java/lang/String");
|
|
59 |
test("<Y:Ljava/lang/String;Z::Ljava/util/EventListener;>",
|
|
60 |
"<Y:L;Z::L;>", "java/lang/String",
|
|
61 |
"java/util/EventListener");
|
|
62 |
test("<Y:Ljava/lang/String;Z::Ljava/util/EventListener;O::Ljava/lang/Comparable<Ljava/lang/String;>;>",
|
|
63 |
"<Y:L;Z::L;O::L<L;>;>",
|
|
64 |
"java/lang/String", "java/util/EventListener", "java/lang/Comparable", "java/lang/String");
|
|
65 |
test("<Y:Ljava/lang/String;O::Ljava/lang/Comparable<Ljava/lang/String;Ljava/lang/Float;>;>",
|
|
66 |
"<Y:L;O::L<L;L;>;>",
|
|
67 |
"java/lang/String", "java/lang/Comparable", "java/lang/String", "java/lang/Float");
|
|
68 |
test("<Y:Ljava/lang/String;O::Ljava/lang/Comparable<Ljava/lang/String;Ljava/lang/Float<Ljava/lang/Object;>;>;>",
|
|
69 |
"<Y:L;O::L<L;L<L;>;>;>",
|
|
70 |
"java/lang/String", "java/lang/Comparable", "java/lang/String", "java/lang/Float", "java/lang/Object");
|
|
71 |
test("Ljava/util/Set;", "L;", "java/util/Set");
|
|
72 |
test("Ljavaapplication20/Titi<[Ljava/lang/String;Ljava/lang/Integer;>;", "L<[L;L;>;",
|
|
73 |
"javaapplication20/Titi",
|
|
74 |
"java/lang/String", "java/lang/Integer");
|
|
75 |
test("Ljava/lang/Comparable<TK;>;", "L<TK;>;", "java/lang/Comparable");
|
|
76 |
test("Ljava/io/Serializable;Ljava/lang/Comparable<TK;>;", "L;L<TK;>;",
|
|
77 |
"java/io/Serializable", "java/lang/Comparable");
|
|
78 |
test("<Y:Ljava/lang/String;Z::Ljava/util/EventListener;K::Ljava/util/EventListener;O::"
|
|
79 |
+ "Ljava/lang/Comparable<Ljava/lang/String;>;>"
|
|
80 |
+ "Ljavaapplication20/Titi<[Ljava/lang/String;Ljava/lang/Integer;TZ;>;"
|
|
81 |
+ "Ljava/io/Serializable;Ljava/lang/Comparable<TK;>;",
|
|
82 |
"<Y:L;Z::L;K::L;O::L<L;>;>L<[L;L;TZ;>;L;L<TK;>;",
|
|
83 |
"java/lang/String", "java/util/EventListener", "java/util/EventListener", "java/lang/Comparable",
|
|
84 |
"java/lang/String", "javaapplication20/Titi", "java/lang/String", "java/lang/Integer",
|
|
85 |
"java/io/Serializable", "java/lang/Comparable");
|
|
86 |
test("<PO:Ljava/lang/Object;>(Ljava/lang/Integer;TPO;)Ljava/lang/Integer;",
|
|
87 |
"<PO:L;>(L;TPO;)L;",
|
|
88 |
"java/lang/Object", "java/lang/Integer", "java/lang/Integer");
|
|
89 |
test("<PO:Ljava/lang/Object;>(Ljava/lang/Integer;TPO;)TPO;", "<PO:L;>(L;TPO;)TPO;",
|
|
90 |
"java/lang/Object", "java/lang/Integer");
|
|
91 |
test("<T::Ljava/util/EventListener;>(Ljava/lang/Class<TT;>;)[TT;",
|
|
92 |
"<T::L;>(L<TT;>;)[TT;",
|
|
93 |
"java/util/EventListener", "java/lang/Class");
|
|
94 |
test("<PO:LTiti;>(Ljava/lang/Integer;ITPO;)Z", "<PO:L;>(L;ITPO;)Z",
|
|
95 |
"Titi", "java/lang/Integer");
|
|
96 |
test("<K:Ljava/lang/Object;V:Ljava/lang/Object;>Ljava/lang/Object;",
|
|
97 |
"<K:L;V:L;>L;",
|
|
98 |
"java/lang/Object", "java/lang/Object", "java/lang/Object");
|
|
99 |
test("Ljava/util/LinkedHashMap<TK;TV;>.LinkedHashIterator;Ljava/util/Iterator<TV;>;",
|
|
100 |
"L<TK;TV;>.L;L<TV;>;",
|
|
101 |
"java/util/LinkedHashMap",
|
|
102 |
"inkedHashIterator",
|
|
103 |
"java/util/Iterator");
|
|
104 |
test("LToto<Ljava/lang/String;>;", "L<L;>;", "Toto",
|
|
105 |
"java/lang/String");
|
|
106 |
test("Ljavaapplication20/Titi<[Ljava/lang/String;Ljava/lang/Integer<LToto;>;TZ;>;",
|
|
107 |
"L<[L;L<L;>;TZ;>;",
|
|
108 |
"javaapplication20/Titi", "java/lang/String", "java/lang/Integer", "Toto");
|
|
109 |
test("LX<[LQ;LW<LToto;>;TZ;>;", "L<[L;L<L;>;TZ;>;",
|
|
110 |
"X", "Q", "W", "Toto");
|
|
111 |
test("Ljava/lang/String<*>;", "L<*>;", "java/lang/String");
|
|
112 |
test("Ljava/util/List<[B>;", "L<[B>;", "java/util/List");
|
|
113 |
test("<T:Ljava/lang/Object;T_NODE::Ljava/util/stream/Node<TT;>;>Ljava/lang/Object;Ljava/util/stream/Node<TT;>;",
|
|
114 |
"<T:L;T_NODE::L<TT;>;>L;L<TT;>;",
|
|
115 |
"java/lang/Object", "java/util/stream/Node", "java/lang/Object", "java/util/stream/Node");
|
|
116 |
test("Ljavaapplication20/Titi<[Ljava/lang/String;>;", "L<[L;>;",
|
|
117 |
"javaapplication20/Titi", "java/lang/String");
|
|
118 |
test("<A::Ljava/lang/annotation/Annotation;"
|
|
119 |
+ "W::Lcom/sun/codemodel/internal/JAnnotationWriter<TA;>;>"
|
|
120 |
+ "Ljava/lang/Object;Ljava/lang/reflect/InvocationHandler;"
|
|
121 |
+ "Lcom/sun/codemodel/internal/JAnnotationWriter<TA;>;",
|
|
122 |
"<A::L;W::L<TA;>;>L;L;L<TA;>;",
|
|
123 |
"java/lang/annotation/Annotation", "com/sun/codemodel/internal/JAnnotationWriter",
|
|
124 |
"java/lang/Object", "java/lang/reflect/InvocationHandler", "com/sun/codemodel/internal/JAnnotationWriter");
|
|
125 |
test("<W::Lcom/sun/codemodel/internal/JAnnotationWriter<*>;>(Ljava/lang/Class<TW;>;" +
|
|
126 |
"Lcom/sun/codemodel/internal/JAnnotatable;)TW;",
|
|
127 |
"<W::L<*>;>(L<TW;>;L;)TW;",
|
|
128 |
"com/sun/codemodel/internal/JAnnotationWriter", "java/lang/Class", "com/sun/codemodel/internal/JAnnotatable");
|
|
129 |
test("Ljava/util/Set<Lcom/sun/tools/jdeps/JdepsTask$DotGraph<TT;>.Edge;>;",
|
|
130 |
"L<L<TT;>.Edge;>;",
|
|
131 |
"java/util/Set",
|
|
132 |
"com/sun/tools/jdeps/JdepsTask$DotGraph");
|
|
133 |
test("<E::Lcom/sun/xml/internal/rngom/ast/om/ParsedElementAnnotation;" +
|
|
134 |
"L::Lcom/sun/xml/internal/rngom/ast/om/Location;" +
|
|
135 |
"CL::Lcom/sun/xml/internal/rngom/ast/builder/CommentList<TL;>;>Ljava/lang/Object;",
|
|
136 |
"<E::L;L::L;CL::L<TL;>;>L;",
|
|
137 |
"com/sun/xml/internal/rngom/ast/om/ParsedElementAnnotation",
|
|
138 |
"",
|
|
139 |
"com/sun/xml/internal/rngom/ast/om/Location",
|
|
140 |
"",
|
|
141 |
"com/sun/xml/internal/rngom/ast/builder/CommentList",
|
|
142 |
"",
|
|
143 |
"java/lang/Object");
|
|
144 |
test("(Ljava/util/List<Lcom/sun/xml/internal/rngom/nc/NameClass;>;TL;TA;)" +
|
|
145 |
"Lcom/sun/xml/internal/rngom/nc/NameClass;",
|
|
146 |
"(L<L;>;TL;TA;)L;",
|
|
147 |
"java/util/List",
|
|
148 |
"com/sun/xml/internal/rngom/nc/NameClass",
|
|
149 |
"",
|
|
150 |
"com/sun/xml/internal/rngom/nc/NameClass");
|
|
151 |
test("[Ljava/util/List;", "[L;", "java/util/List");
|
|
152 |
test("[Ljava/util/List<+Lcom/sun/jdi/request/EventRequest;>;",
|
|
153 |
"[L<+L;>;",
|
|
154 |
"java/util/List", "com/sun/jdi/request/EventRequest");
|
|
155 |
test("Lcom/sun/xml/internal/bind/v2/util/QNameMap<TV;>.HashIterator" +
|
|
156 |
"<Lcom/sun/xml/internal/bind/v2/util/QNameMap$Entry<TV;>;>;",
|
|
157 |
"L<TV;>.HashIterator<L<TV;>;>;",
|
|
158 |
"com/sun/xml/internal/bind/v2/util/QNameMap", "com/sun/xml/internal/bind/v2/util/QNameMap$Entry");
|
|
159 |
test("[Ljava/lang/String;", "[L;", "java/lang/String");
|
|
160 |
test("[Ljava/lang/String<Ljava/lang/Toto<Ljava/lang/Titi;>;>;",
|
|
161 |
"[L<L<L;>;>;",
|
|
162 |
"java/lang/String", "java/lang/Toto", "java/lang/Titi");
|
|
163 |
test("<T::Ljava/util/EventListener;K:Ljava/util/BOO;>(ZCLjava/lang/Class<TT;>;IJS)[TT;",
|
|
164 |
"<T::L;K:L;>(ZCL<TT;>;IJS)[TT;",
|
|
165 |
"java/util/EventListener", "java/util/BOO", "java/lang/Class");
|
|
166 |
test("<T:Ljava/lang/Object;>(TT;ILjava/lang/Long;)TT;",
|
|
167 |
"<T:L;>(TT;IL;)TT;", "java/lang/Object", "java/lang/Long");
|
|
168 |
test("<T:Ljava/lang/Object;>(TT;ILjava/lang/Long;)TT;^TT;",
|
|
169 |
"<T:L;>(TT;IL;)TT;^TT;", "java/lang/Object", "java/lang/Long");
|
|
170 |
test("<T:Ljava/lang/Object;>(TT;ILjava/lang/Long;)TT;^TT;^Ljava/lang/Exception;",
|
|
171 |
"<T:L;>(TT;IL;)TT;^TT;^L;",
|
|
172 |
"java/lang/Object", "java/lang/Long", "java/lang/Exception");
|
|
173 |
if (passed + failed == 0) {
|
|
174 |
throw new AssertionError("No tests were run");
|
|
175 |
}
|
|
176 |
String message = String.format("Passed: %d, failed: %d, total: %d", passed, failed, passed + failed);
|
|
177 |
if (failed > 0) {
|
|
178 |
throw new AssertionError("Test failed: " + message);
|
|
179 |
} else {
|
|
180 |
System.err.println(message);
|
|
181 |
}
|
|
182 |
}
|
|
183 |
|
|
184 |
private void test(String type, String formatted, String...classNames) {
|
|
185 |
try {
|
|
186 |
SignatureParser.ParseResult result = SignatureParser.parseSignatureDescriptor(type);
|
|
187 |
String[] parsedNames = parse(classNames);
|
|
188 |
assertEquals(result.formatted, formatted, "Input: '" + type + "', checking 'formatted'");
|
|
189 |
assertEquals(result.types.size(), 2 * classNames.length,
|
|
190 |
"Input: '" + type + "', checking the length of 'types':" +
|
|
191 |
"\nexpected: " + Arrays.toString(parsedNames) +
|
|
192 |
"\n got: " + result.types);
|
|
193 |
for (int i = 0; i < result.types.size(); ++i) {
|
|
194 |
assertEquals(result.types.get(i), parsedNames[i],
|
|
195 |
"Input: '" + type + "', checking 'packageName' at index " + i / 2);
|
|
196 |
++i;
|
|
197 |
assertEquals(result.types.get(i), parsedNames[i],
|
|
198 |
"Input: '" + type + "', checking 'simpleName' at index " + i / 2);
|
|
199 |
}
|
|
200 |
String reconstructed = SignatureParser.reconstruct(result.formatted, result.types);
|
|
201 |
assertEquals(reconstructed, type, "Input: '" + type + "', checking reconstruction from: "
|
|
202 |
+ result.formatted + " " + result.types);
|
|
203 |
++passed;
|
|
204 |
} catch (Exception | AssertionError e) {
|
|
205 |
e.printStackTrace();
|
|
206 |
++failed;
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
private void assertEquals(Object actual, Object expected, String message) {
|
|
211 |
if (!Objects.equals(actual, expected)) {
|
|
212 |
throw new AssertionError(message + ": expected: " + expected + ", actual: " + actual);
|
|
213 |
}
|
|
214 |
}
|
|
215 |
|
|
216 |
private String[] parse(String[] classNames) {
|
|
217 |
String[] result = new String[2 * classNames.length];
|
|
218 |
for (int i = 0; i < classNames.length; ++i) {
|
|
219 |
int index = classNames[i].lastIndexOf("/");
|
|
220 |
result[2 * i] = index == -1 ? "" : classNames[i].substring(0, index);
|
|
221 |
result[2 *i + 1] = classNames[i].substring(index + 1);
|
|
222 |
}
|
|
223 |
return result;
|
|
224 |
}
|
|
225 |
}
|