-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathGenericsTest.java
344 lines (321 loc) · 13.8 KB
/
GenericsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 4421040
* @summary JPDA: Add support for JSR-014 Generics
*
* @author jjh
*
* @run build TestScaffold VMConnection TargetListener TargetAdapter
* @run compile -g GenericsTest.java
* @run driver GenericsTest
*/
import com.sun.jdi.*;
import com.sun.jdi.event.*;
import com.sun.jdi.request.*;
import java.util.*;
/********** target program **********/
class GenericsTarg {
static Gen1<String> genField = new Gen1<String>();;
static Sub1 sub1Field = new Sub1();
String[] strArray = null;
int intField = 0;
Object objField;
public static void main(String[] args){
//genField.print();
System.out.println("Goodbye from GenericsTarg!");
}
}
class Gen1<tt> {
tt field1;
Gen1() {
System.out.println("Gen1<tt> ctor called");
}
tt method1(tt p1) {
Gen1<String> xxx = null;
System.out.println("method1: param is " + p1);
return p1;
}
String method2() {
String str = "This local variable is not generic";
return str;
}
}
class Sub1 extends Gen1<String> {
String method1(String p1) {
System.out.println("method1 has been overridden: param is " + p1);
return "hi";
}
}
/********** test program **********/
public class GenericsTest extends TestScaffold {
ReferenceType targetClass;
ThreadReference mainThread;
static boolean useOld;
GenericsTest (String args[]) {
super(args);
}
public static void main(String[] args) throws Exception {
/*
* The 1.5 FE must be able to talk to a 1.4 BE, ie, JDWP version <= 1.4.
* This is hard to test since this test file must be compiled with
* -source 1.5 which will cause its class file to be version 49 which
* won't run on a pre 1.5 JDK. We can simulate this though
* by passing
* -xjdk <pathname>
* to this test which causes the debuggee to be run on that JDK.
* This should be a version of 1.5 that accepts classfile version 49,
* but which still contains the 1.4 version of JDWP.
* This trick verifies that the calls to genericSignature() methods
* in the test do not cause the generic JDWP commands to be issued.
* The value to use for this is currently:
* /java/re/jdk/1.5/promoted/all/b17/binaries/solaris-sparc
*/
if (args.length > 1 && args[0].equals("-xjdk")) {
System.setProperty("java.home", args[1]);
useOld = true;
// Delete this arg
String[] args1 = new String[args.length - 2];
for (int ii = 0; ii < args.length -2; ii++) {
args1[ii] = args[ii + 2];
}
args = args1;
}
new GenericsTest(args).startTests();
}
/********** test core **********/
protected void runTests() throws Exception {
/*
* Get to the top of main()
* to determine targetClass and mainThread
*/
BreakpointEvent bpe = startToMain("GenericsTarg");
targetClass = bpe.location().declaringType();
{
/*
* Prove that arrays aren't broken and that
* null is returned if there is no generic signature
*/
Field strArray = targetClass.fieldByName("strArray");
ReferenceType fieldType = (ReferenceType)(strArray.type());
String genSig = fieldType.genericSignature();
System.out.println("strArray name = " + strArray);
System.out.println(" type = " + fieldType);
System.out.println(" sig = " + fieldType.signature());
System.out.println(" genSig = " + genSig);
if (!useOld && genSig != null) {
failure("FAILED: Expected generic signature = null for "
+ fieldType.name() + ", received: " + genSig);
}
}
{
// prove that primitives aren't broken.
Field intField = targetClass.fieldByName("intField");
Type fieldType = (Type)(intField.type());
System.out.println("intField name = " + intField);
System.out.println(" type = " + fieldType);
System.out.println(" sig = " + fieldType.signature());
}
Field genField = targetClass.fieldByName("genField");
ReferenceType gen1Class = (ReferenceType)(genField.type());
String genSig;
String expected;
{
// Verify genericSignature for a class
expected = "<tt:Ljava/lang/Object;>Ljava/lang/Object;";
genSig = gen1Class.genericSignature();
System.out.println("genField name = " + genField);
System.out.println(" type = " + gen1Class);
System.out.println(" sig = " + gen1Class.signature());
System.out.println(" genSig = " + genSig);
if (!useOld && !expected.equals(genSig)) {
failure("FAILED: Expected generic signature for gen1: " +
expected + ", received: " + genSig);
}
}
{
// Verify genericSignature() for a field
List genFields = gen1Class.fields();
Field field1 = (Field)genFields.get(0);
// there is only one field
expected = "Ttt;";
genSig = field1.genericSignature();
System.out.println("field1 name = " + field1);
System.out.println(" type = " + gen1Class.signature());
System.out.println(" sig = " + field1.signature());
System.out.println(" gen sig = " + genSig);
if (!useOld && !expected.equals(genSig)) {
failure("FAILED: Expected generic signature for field1: " +
expected + ", received: " + genSig);
}
}
{
// Verify genericSignature() for a method
List genMethods = gen1Class.methodsByName("method1");
// There is only uno
Method method1 = (Method)genMethods.get(0);
expected = "(Ttt;)Ttt;";
genSig = method1.genericSignature();
System.out.println("method1 name = " + method1);
System.out.println(" type = " + gen1Class.signature());
System.out.println(" sig = " + method1.signature());
System.out.println(" gen sig = " + genSig);
System.out.println(" bridge = " + method1.isBridge());
if (!useOld && !expected.equals(genSig)) {
failure("FAILED: Expected generic signature for method1: " +
expected + ", received: " + genSig);
}
// Verify this is not a bridge method
if (method1.isBridge()) {
failure("FAILED: Expected gen1.method1 to not be a bridge"
+ " method but it is");
}
// Verify genericSignature for a local var
List localVars = method1.variables();
String[] expectedGenSigs = { "Ttt", "Gen1<String>" };
for ( int ii = 0 ; ii < localVars.size(); ii++) {
expected = expectedGenSigs[ii];
LocalVariable pp = (LocalVariable)localVars.get(ii);
genSig = pp.genericSignature();
System.out.println(" local var " + ii + " = " + pp.name());
System.out.println(" sig = " + pp.signature());
System.out.println(" gen sig = " + genSig);
//jjh Uncomment when generics for local vars are available from
//jjh javac and hotspot. See:
//jjh 4914602 LVT entries for classfile version > 49 must be converted
//jjh if (!useOld && !expected.equals(genSig)) {
//jjh failure("FAILED: Expected generic signature for local var: " +
//jjh expected + ", received: " + genSig);
//jjh }
}
}
{
// Verify genericSignature() for a method2
List genMethods = gen1Class.methodsByName("method2");
// There is only uno
Method method2 = (Method)genMethods.get(0);
expected = "null";
genSig = method2.genericSignature();
genSig = (genSig == null) ? "null" : genSig;
System.out.println("method2 name = " + method2);
System.out.println(" type = " + gen1Class.signature());
System.out.println(" sig = " + method2.signature());
System.out.println(" gen sig = " + genSig);
System.out.println(" bridge = " + method2.isBridge());
if (!useOld && !expected.equals(genSig)) {
failure("FAILED: Expected generic signature for method2: " +
expected + ", received: " + genSig);
}
// Verify this is not a bridge method
if (method2.isBridge()) {
failure("FAILED: Expected gen1.method2 to not be a bridge"
+ " method but it is");
}
// Verify genericSignature for a local var
List localVars = method2.variables();
expected = "null";
for ( int ii = 0 ; ii < localVars.size(); ii++) {
LocalVariable pp = (LocalVariable)localVars.get(ii);
genSig = pp.genericSignature();
genSig = (genSig == null) ? "null" : genSig;
System.out.println(" local var " + ii + " = " + pp.name());
System.out.println(" sig = " + pp.signature());
System.out.println(" gen sig = " + genSig);
if (!useOld && !expected.equals(genSig)) {
failure("FAILED: Expected generic signature for local var: " +
expected + ", received: " + genSig);
}
}
}
{
Field sub1Field = targetClass.fieldByName("sub1Field");
ReferenceType sub1Class = (ReferenceType)(sub1Field.type());
List<Method> sub1Methods = sub1Class.methodsByName("method1");
for (Method mm: sub1Methods) {
System.out.println("method is: " + mm);
}
/*
* There should be two methods - the first is the
* method1 defined in Sub1, and the 2nd is a javac generated
* bridge method.
*/
Method method1 = (Method)sub1Methods.get(1);
System.out.println("\nmethod1 name = " + method1);
System.out.println(" sig = " + method1.signature());
System.out.println(" bridge = " + method1.isBridge());
if (!useOld && !method1.isBridge()) {
failure("FAILED: Expected Sub1.method1 to be a bridge method"
+ " but it isn't");
}
}
{
// Verify genericSignature for a non generic class
genSig = targetClass.genericSignature();
if (genSig != null) {
failure("FAILED: Expected generic signature = null for "
+ targetClass.name() + ", received: " + genSig);
}
}
{
// Verify genericSignature for a non generic field
Field objField = targetClass.fieldByName("objField");
genSig = objField.genericSignature();
if (genSig != null) {
failure("FAILED: Expected generic signature = null for "
+ objField.name() + ", received: " + genSig);
}
}
{
// Verify genericSignature for a non generic method
List methods = targetClass.methodsByName("main");
Method main = (Method)methods.get(0);
genSig = main.genericSignature();
if (genSig != null) {
failure("FAILED: Expected generic signature = null for "
+ main.name() + ", received: " + genSig);
}
}
if (0 == 1) {
mainThread = bpe.thread();
EventRequestManager erm = vm().eventRequestManager();
StepRequest request = erm.createStepRequest(mainThread,
StepRequest.STEP_LINE,
StepRequest.STEP_INTO);
request.enable();
}
/*
* resume the target listening for events
*/
listenUntilVMDisconnect();
/*
* deal with results of test
* if anything has called failure("foo") testFailed will be true
*/
if (!testFailed) {
println("GenericsTest: passed");
} else {
throw new Exception("GenericsTest: failed");
}
}
}