Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/Jsr292/src/com/ibm/j9/jsr292/LookupInTests.java
6007 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2019 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.j9.jsr292;
23
import org.testng.annotations.Test;
24
import org.testng.Assert;
25
26
import java.lang.invoke.MethodHandles;
27
import static java.lang.invoke.MethodHandles.Lookup;
28
import static java.lang.invoke.MethodHandles.lookup;
29
import static java.lang.invoke.MethodHandles.publicLookup;
30
31
import com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass;
32
import com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2;
33
import com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass2;
34
import com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass2.SamePackageInnerClass2_Nested_Level2;
35
import com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass_Protected;
36
import com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass_Static;
37
38
import examples.PackageExamples;
39
40
/**
41
*This test case verifies lookup modes being permitted by the Lookup factory object for various Lookup classes (callers)
42
*using test scenarios involving Lookup classes(callers) candidates of various access restriction enforcement.
43
*/
44
public class LookupInTests {
45
46
static final int NO_ACCESS = 0;
47
static final int PUBLIC_PACKAGE_MODE = MethodHandles.Lookup.PUBLIC | MethodHandles.Lookup.PACKAGE; // 9
48
static final int PUBLIC_PACKAGE_PRIVATE_MODE = MethodHandles.Lookup.PUBLIC | MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PACKAGE; //11
49
static final int PUBLIC_PACKAGE_PROTECTED_MODE = MethodHandles.Lookup.PUBLIC | MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PROTECTED; //13
50
static final int FULL_ACCESS_MODE = MethodHandles.Lookup.PUBLIC | MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED | MethodHandles.Lookup.PACKAGE; //15
51
52
final Lookup localLookup = lookup();
53
final Lookup localPublicLookup = publicLookup();
54
final Lookup packageExamplesLookup = PackageExamples.getLookup();
55
final Lookup samePackageExampleLookup = SamePackageExample.getLookup();
56
final Lookup samePackageExample2Lookup = SamePackageExample2.getLookup();
57
final Lookup samePackageExampleSubclassLookup = SamePackageExampleSubclass.getLookup();
58
59
SamePackageExample spe = new SamePackageExample();
60
SamePackageInnerClass innerClassD = spe.new SamePackageInnerClass();
61
final Lookup samePackageInnerClassLookup = spe.new SamePackageInnerClass().getLookup();
62
final Lookup samePackageInnerClass_Nested_Level2Lookup = innerClassD.new SamePackageInnerClass_Nested_Level2().getLookup();
63
64
/******************************************
65
* Basic Sanity tests for Lookup modes
66
* ****************************************/
67
68
/**
69
* Validates public access mode of a Lookup factory created by a call to MethodHandles.publicLookup()
70
* @throws Throwable
71
*/
72
@Test(groups = { "level.extended" })
73
public void testPublicLookup() throws Throwable {
74
assertClassAndMode(localPublicLookup, Object.class, MethodHandles.Lookup.PUBLIC);
75
Assert.assertEquals(localPublicLookup.lookupModes(), MethodHandles.Lookup.PUBLIC);
76
77
Lookup newLookup = localPublicLookup.in(int[].class);
78
assertClassAndMode(newLookup, int[].class, localPublicLookup.lookupModes());
79
}
80
81
/**
82
* Validates access restriction stored in a Lookup factory object that are applied to its own lookup class.
83
* @throws Throwable
84
*/
85
@Test(groups = { "level.extended" })
86
public void testLookup_inObject_Self() throws Throwable {
87
Lookup newLookup = localLookup.in(LookupInTests.class);
88
assertClassAndMode(newLookup, LookupInTests.class, FULL_ACCESS_MODE);
89
}
90
91
/**
92
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
93
* where the new lookup class is java.lang.Object.
94
* @throws Throwable
95
*/
96
@Test(groups = { "level.extended" })
97
public void testLookup_inObject() throws Throwable {
98
Lookup inObject = packageExamplesLookup.in(Object.class);
99
assertClassAndMode(inObject, Object.class, MethodHandles.Lookup.PUBLIC);
100
}
101
102
/**
103
* Using an old lookup object that was created from a call to MethodHandles.publicLookup(),
104
* validates access restrictions stored in a new Lookup object created from an old Lookup object
105
* where the new lookup class is java.lang.Object.
106
* @throws Throwable
107
*/
108
@Test(groups = { "level.extended" })
109
public void testLookup_inObject_Using_publicLookup() throws Throwable {
110
Lookup lookup = PackageExamples.getPublicLookup();
111
Lookup inObject = lookup.in(int.class);
112
113
/*NOTE:
114
* This is equivalent of the failing test case above, but this one works.
115
* The only difference is here we are using MethodHandles.publicLookup()
116
* instead of MethodHandles.lookup() to obtain the original Lookup object.
117
* */
118
assertClassAndMode(inObject, int.class, MethodHandles.Lookup.PUBLIC);
119
}
120
121
/**
122
* Validates access restrictions stored in a new Lookup object created from the Lookup object of this class (LookupInTests)
123
* where the new lookup class is junit.framework.Assert.
124
* @throws Throwable
125
*/
126
@Test(groups = { "level.extended" })
127
public void testLookup_in_Assert() throws Throwable {
128
Lookup newLookup = localLookup.in(Assert.class);
129
assertClassAndMode(newLookup, Assert.class, MethodHandles.Lookup.PUBLIC);
130
}
131
132
/*******************************************************************************
133
* Tests involving same package, cross package, super class, subclass lookups
134
* *****************************************************************************/
135
136
/**
137
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
138
* where the new lookup class is a different class but under the same package as the lookup class
139
* of the original Lookup object.
140
* @throws Throwable
141
*/
142
@Test(groups = { "level.extended" })
143
public void testLookup_SamePackageLookup() throws Throwable {
144
Lookup inObject = samePackageExampleLookup.in(SamePackageSingleMethodInterfaceExample.class);
145
assertClassAndMode(inObject, SamePackageSingleMethodInterfaceExample.class, PUBLIC_PACKAGE_MODE);
146
}
147
148
/**
149
* Validates that, if a new lookup class differs from the old one, protected members will not be
150
* accessible by virtue of inheritance. The test creates a new Lookup object from an old Lookup object
151
* where the new Lookup class is a subclass of the original lookup class.
152
* @throws Throwable
153
*/
154
@Test(groups = { "level.extended" })
155
public void testLookup_SamePackageLookup_Subclass() throws Throwable {
156
Lookup inObject = samePackageExampleLookup.in(SamePackageExampleSubclass.class);
157
assertClassAndMode(inObject, SamePackageExampleSubclass.class, PUBLIC_PACKAGE_MODE);
158
}
159
160
/**
161
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
162
* where the new lookup class is the super-class of the original Lookup class.
163
* @throws Throwable
164
*/
165
@Test(groups = { "level.extended" })
166
public void testLookup_SamePackageLookup_Superclass() throws Throwable {
167
Lookup inObject = samePackageExampleSubclassLookup.in(SamePackageExample.class);
168
assertClassAndMode(inObject, SamePackageExample.class, FULL_ACCESS_MODE);
169
}
170
171
/**
172
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
173
* where the new lookup class is in a different package than the old lookup class.
174
* @throws Throwable
175
*/
176
@Test(groups = { "level.extended" })
177
public void testLookup_CrossPackageLookup() throws Throwable {
178
Lookup inObject = samePackageExampleLookup.in(PackageExamples.class);
179
assertClassAndMode(inObject, PackageExamples.class, MethodHandles.Lookup.PUBLIC);
180
}
181
182
/**
183
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
184
* where the new lookup class is a subclass of the original lookup class but is residing in a
185
* different package than the original lookup class.
186
* @throws Throwable
187
*/
188
@Test(groups = { "level.extended" })
189
public void testLookup_CrossPackageLookup_Subclass() throws Throwable {
190
Lookup inObject = packageExamplesLookup.in(CrossPackageExampleSubclass.class);
191
assertClassAndMode(inObject, CrossPackageExampleSubclass.class, MethodHandles.Lookup.PUBLIC);
192
}
193
194
/**
195
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
196
* where the new lookup class is the superclass of the old lookup class but is residing in a
197
* different package than the old lookup class.
198
* @throws Throwable
199
*/
200
@Test(groups = { "level.extended" })
201
public void testLookup_CrossPackageLookup_Superclass() throws Throwable {
202
Lookup lookup = CrossPackageExampleSubclass.getLookup();
203
Lookup inObject = lookup.in(PackageExamples.class);
204
assertClassAndMode(inObject, PackageExamples.class, Lookup.PUBLIC);
205
}
206
207
/**
208
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
209
* where the new lookup class is a non-public outer class in the same Java source file as the old lookup class
210
* @throws Throwable
211
*/
212
@Test(groups = { "level.extended" })
213
public void testLookup_PrivateOuterClassLookup() throws Throwable {
214
Lookup inObj = localLookup.in(PackageClass.class);
215
assertClassAndMode(inObj, PackageClass.class, PUBLIC_PACKAGE_PRIVATE_MODE);
216
}
217
218
/**
219
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
220
* where the new lookup class is a non-public outer class belonging to the same package as the old lookup class.
221
* @throws Throwable
222
*/
223
@Test(groups = { "level.extended" })
224
public void testLookup_SamePackageLookup_PrivateOuterClass() throws Throwable {
225
Lookup inObj = samePackageExampleLookup.in(PackageClass.class);
226
assertClassAndMode(inObj, PackageClass.class, PUBLIC_PACKAGE_MODE);
227
}
228
229
/**
230
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
231
* where the new lookup class is a non-public outer class belonging to a different package than the old lookup class.
232
* @throws Throwable
233
*/
234
@Test(groups = { "level.extended" })
235
public void testLookup_CrossPackageLookup_PrivateOuterClass() throws Throwable {
236
Lookup inObj = packageExamplesLookup.in(PackageClass.class);
237
assertClassAndMode(inObj, PackageClass.class, NO_ACCESS);
238
}
239
240
/***************************************************
241
* Tests involving public inner classes
242
* ************************************************/
243
244
/**
245
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
246
* where the new lookup class is a public inner class under the old lookup class.
247
* @throws Throwable
248
*/
249
@Test(groups = { "level.extended" })
250
public void testLookup_PublicInnerClassLookup() throws Throwable {
251
Lookup inObj = samePackageExampleLookup.in(SamePackageInnerClass.class);
252
assertClassAndMode(inObj, SamePackageInnerClass.class, PUBLIC_PACKAGE_PRIVATE_MODE);
253
}
254
255
/**
256
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
257
* where the new lookup class is the outer class of the old lookup class.
258
* @throws Throwable
259
*/
260
@Test(groups = { "level.extended" })
261
public void testLookup_PublicOuterClassLookup() throws Throwable {
262
Lookup inObj = samePackageInnerClassLookup.in(SamePackageExample.class);
263
assertClassAndMode(inObj, SamePackageExample.class, PUBLIC_PACKAGE_PRIVATE_MODE);
264
}
265
266
/**
267
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
268
* where the new lookup class is a public inner class under the super-class of the old lookup class.
269
* @throws Throwable
270
*/
271
@Test(groups = { "level.extended" })
272
public void testLookup_PublicInnerClassLookup_Subclass() throws Throwable {
273
Lookup inObj = samePackageExampleSubclassLookup.in(SamePackageInnerClass.class);
274
assertClassAndMode(inObj, SamePackageInnerClass.class, PUBLIC_PACKAGE_PRIVATE_MODE);
275
}
276
277
/**
278
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
279
* where the new lookup class is a public inner class inside a top level class belonging to the same
280
* package as the old lookup class.
281
* @throws Throwable
282
*/
283
@Test(groups = { "level.extended" })
284
public void testLookup_PublicInnerClassLookup_SamePackage() throws Throwable {
285
Lookup inObj = samePackageExample2Lookup.in(SamePackageInnerClass.class);
286
assertClassAndMode(inObj, SamePackageInnerClass.class, PUBLIC_PACKAGE_MODE);
287
}
288
289
/**
290
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
291
* where the new lookup class is a public inner class inside a top level class belonging to a different
292
* package than the old lookup class.
293
* @throws Throwable
294
*/
295
@Test(groups = { "level.extended" })
296
public void testLookup_PublicInnerClassLookup_CrossPackage() throws Throwable {
297
Lookup inObj = packageExamplesLookup.in(SamePackageInnerClass.class);
298
assertClassAndMode(inObj, SamePackageInnerClass.class, MethodHandles.Lookup.PUBLIC);
299
}
300
301
/***************************************************
302
* Tests involving protected inner classes
303
* ************************************************/
304
305
/**
306
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
307
* where the new lookup class is a protected inner class under the old lookup class.
308
* @throws Throwable
309
*/
310
@Test(groups = { "level.extended" })
311
public void testLookup_ProtectedInnerClassLookup() throws Throwable {
312
Lookup inObj = samePackageExampleLookup.in(SamePackageInnerClass_Protected.class);
313
assertClassAndMode(inObj, SamePackageInnerClass_Protected.class, PUBLIC_PACKAGE_PRIVATE_MODE);
314
}
315
316
/**
317
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
318
* where the new lookup class is the outer class of the original lookup class.
319
* @throws Throwable
320
*/
321
@Test(groups = { "level.extended" })
322
public void testLookup_ProtectedOuterClassLookup() throws Throwable {
323
SamePackageExample spe = new SamePackageExample();
324
Lookup lookup = spe.new SamePackageInnerClass_Protected().getLookup();
325
Lookup inObj = lookup.in(SamePackageExample.class);
326
assertClassAndMode(inObj, SamePackageExample.class, PUBLIC_PACKAGE_PRIVATE_MODE);
327
}
328
329
/**
330
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
331
* where the new lookup class is a protected inner class under the super-class of the old lookup class.
332
* @throws Throwable
333
*/
334
@Test(groups = { "level.extended" })
335
public void testLookup_ProtectedInnerClassLookup_Subclass() throws Throwable {
336
Lookup inObj = samePackageExampleSubclassLookup.in(SamePackageInnerClass_Protected.class);
337
assertClassAndMode(inObj, SamePackageInnerClass_Protected.class, PUBLIC_PACKAGE_PRIVATE_MODE);
338
}
339
340
/**
341
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
342
* where the new lookup class is a protected inner class inside a top level class belonging to the same
343
* package as the old lookup class.
344
* @throws Throwable
345
*/
346
@Test(groups = { "level.extended" })
347
public void testLookup_ProtectedInnerClassLookup_SamePackage() throws Throwable {
348
Lookup inObj = samePackageExample2Lookup.in(SamePackageInnerClass_Protected.class);
349
assertClassAndMode(inObj, SamePackageInnerClass_Protected.class, PUBLIC_PACKAGE_MODE);
350
}
351
352
/**
353
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
354
* where the new lookup class is a protected inner class inside a top level class belonging to a different
355
* package than the old lookup class.
356
* Note: the access flag of a protected class is set to public when compiled to a class file.
357
* @throws Throwable
358
*/
359
@Test(groups = { "level.extended" })
360
public void testLookup_ProtectedInnerClassLookup_CrossPackage() throws Throwable {
361
Lookup inObj = packageExamplesLookup.in(SamePackageInnerClass_Protected.class);
362
assertClassAndMode(inObj, SamePackageInnerClass_Protected.class, MethodHandles.Lookup.PUBLIC);
363
}
364
365
/***************************************************
366
* Tests involving static inner classes
367
* ************************************************/
368
369
/**
370
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
371
* where the new lookup class is a static inner class under the old lookup class.
372
* @throws Throwable
373
*/
374
@Test(groups = { "level.extended" })
375
public void testLookup_StaticInnerClassLookup() throws Throwable {
376
Lookup inObj = samePackageExampleLookup.in(SamePackageInnerClass_Static.class);
377
assertClassAndMode(inObj, SamePackageInnerClass_Static.class, PUBLIC_PACKAGE_PRIVATE_MODE);
378
}
379
380
/**
381
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
382
* where the new lookup class is the outer class of the original lookup class.
383
* @throws Throwable
384
*/
385
@Test(groups = { "level.extended" })
386
public void testLookup_StaticdOuterClassLookup() throws Throwable {
387
Lookup lookup = SamePackageExample.SamePackageInnerClass_Static.getLookup();
388
Lookup inObj = lookup.in(SamePackageExample.class);
389
assertClassAndMode(inObj, SamePackageExample.class, PUBLIC_PACKAGE_PRIVATE_MODE);
390
}
391
392
/**
393
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
394
* where the new lookup class is a static inner class under the super-class of the old lookup class.
395
* @throws Throwable
396
*/
397
@Test(groups = { "level.extended" })
398
public void testLookup_StaticInnerClassLookup_Subclass() throws Throwable {
399
Lookup inObj = samePackageExampleSubclassLookup.in(SamePackageInnerClass_Static.class);
400
assertClassAndMode(inObj, SamePackageInnerClass_Static.class, PUBLIC_PACKAGE_PRIVATE_MODE);
401
}
402
403
/**
404
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
405
* where the new lookup class is a static inner class inside a top level class belonging to the same
406
* package as the old lookup class.
407
* @throws Throwable
408
*/
409
@Test(groups = { "level.extended" })
410
public void testLookup_StaticInnerClassLookup_SamePackage() throws Throwable {
411
Lookup inObj = samePackageExample2Lookup.in(SamePackageInnerClass_Static.class);
412
assertClassAndMode(inObj, SamePackageInnerClass_Static.class, PUBLIC_PACKAGE_MODE);
413
}
414
415
/**
416
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
417
* where the new lookup class is a static inner class inside a top level class belonging to a different
418
* package than the old lookup class.
419
* @throws Throwable
420
*/
421
@Test(groups = { "level.extended" })
422
public void testLookup_StaticInnerClassLookup_CrossPackage() throws Throwable {
423
Lookup inObj = packageExamplesLookup.in(SamePackageInnerClass_Static.class);
424
assertClassAndMode(inObj, SamePackageInnerClass_Static.class, NO_ACCESS);
425
}
426
427
/***************************************************
428
* Tests involving nested public inner classes
429
* ************************************************/
430
431
/**
432
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
433
* where the new lookup class is a second level inner class under the old lookup class which is a first level inner class.
434
* Basically we validate that a nested class C.D can access private members within another nested class C.D.E.
435
* @throws Throwable
436
*/
437
@Test(groups = { "level.extended" })
438
public void testLookup_PublicNestedInnerClassLookup_Level1() throws Throwable {
439
Lookup inObj = samePackageInnerClassLookup.in(SamePackageInnerClass_Nested_Level2.class);
440
assertClassAndMode(inObj, SamePackageInnerClass_Nested_Level2.class, PUBLIC_PACKAGE_PRIVATE_MODE);
441
}
442
443
/**
444
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
445
* where the new lookup class is a second level inner class under the old lookup class which is the top level outer class.
446
* Basically we validate that a top level class C can access private members within a nested class C.D.E.
447
* @throws Throwable
448
*/
449
@Test(groups = { "level.extended" })
450
public void testLookup_PublicNestedInnerClassLookup_Level2() throws Throwable {
451
Lookup inObj = samePackageExampleLookup.in(SamePackageInnerClass_Nested_Level2.class);
452
assertClassAndMode(inObj, SamePackageInnerClass_Nested_Level2.class, PUBLIC_PACKAGE_PRIVATE_MODE);
453
}
454
455
/**
456
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
457
* where the new lookup class is an inner class that shares the same outer class as the old lookup class which is
458
* another inner class. Basically we validate that a nested class C.D can access private members within another
459
* nested class C.B.
460
* @throws Throwable
461
*/
462
@Test(groups = { "level.extended" })
463
public void testLookup_PublicInnerClassLookup_ParallelInnerClasses_Level1() throws Throwable {
464
Lookup inObj = samePackageInnerClassLookup.in(SamePackageInnerClass2.class);
465
assertClassAndMode(inObj, SamePackageInnerClass2.class, PUBLIC_PACKAGE_PRIVATE_MODE);
466
}
467
468
/**
469
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
470
* where the new lookup class is an second level inner class that shares the same top level outer class as the old lookup class which is
471
* another second level inner class. Basically we validate that a nested class C.D.E can access private members within another
472
* nested class C.B.F
473
* @throws Throwable
474
*/
475
@Test(groups = { "level.extended" })
476
public void testLookup_PublicInnerClassLookup_ParallelInnerClasses_Level2() throws Throwable {
477
SamePackageExample spe = new SamePackageExample();
478
SamePackageInnerClass spei_level1 = spe.new SamePackageInnerClass();
479
SamePackageInnerClass_Nested_Level2 spei_level2 = spei_level1.new SamePackageInnerClass_Nested_Level2();
480
481
Lookup lookup = spei_level2.getLookup();
482
Lookup inObj = lookup.in(SamePackageInnerClass2_Nested_Level2.class);
483
assertClassAndMode(inObj, SamePackageInnerClass2_Nested_Level2.class, PUBLIC_PACKAGE_PRIVATE_MODE);
484
}
485
486
/**
487
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
488
* where the new lookup class is a first level inner class on top of the old lookup class which is the second
489
* level inner class. Basically we validate that a nested class C.D.E can access private members within
490
* another nested class C.D.
491
* @throws Throwable
492
*/
493
@Test(groups = { "level.extended" })
494
public void testLookup_PublicNestedOuterClassLookup_Level2() throws Throwable {
495
Lookup inObj = samePackageInnerClass_Nested_Level2Lookup.in(SamePackageInnerClass.class);
496
assertClassAndMode(inObj, SamePackageInnerClass.class, PUBLIC_PACKAGE_PRIVATE_MODE);
497
}
498
499
/**
500
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
501
* where the new lookup class is the top level outer class on top of the old lookup class which is the second
502
* level inner class. Basically we validate that a nested class C.D.E can access private members within
503
* the top level outer class C.
504
* @throws Throwable
505
*/
506
@Test(groups = { "level.extended" })
507
public void testLookup_PublicNestedOuterClassLookup_Level1() throws Throwable {
508
Lookup inObj = samePackageInnerClass_Nested_Level2Lookup.in(SamePackageExample.class);
509
assertClassAndMode(inObj, SamePackageExample.class, PUBLIC_PACKAGE_PRIVATE_MODE);
510
}
511
512
513
/****************************************************
514
* Tests involving cross class loaders
515
* ***************************************************/
516
517
/**
518
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
519
* where the new lookup class was loaded using a custom class loader unrelated to the class loader
520
* that loaded the original lookup class.
521
* @throws Throwable
522
*/
523
@Test(groups = { "level.extended" })
524
public void testLookup_UnrelatedClassLoaders() throws Throwable {
525
ParentCustomClassLoader unrelatedClassLoader = new ParentCustomClassLoader(LookupInTests.class.getClassLoader());
526
Object customLoadedClass = unrelatedClassLoader.loadClass("com.ibm.j9.jsr292.CustomLoadedClass1" ).newInstance();
527
528
Lookup inObject = samePackageExampleLookup.in(customLoadedClass.getClass());
529
assertClassAndMode(inObject, customLoadedClass.getClass(), MethodHandles.Lookup.PUBLIC);
530
}
531
532
/**
533
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
534
* where the new lookup class was loaded using a custom class loader which is the parent class loader
535
* of the class loader that loaded the original lookup class.
536
* @throws Throwable
537
*/
538
@Test(groups = { "level.extended" })
539
public void testLookup_RelatedClassLoaders_ChildLookupInParent() throws Throwable {
540
ParentCustomClassLoader parentCustomCL = new ParentCustomClassLoader(LookupInTests.class.getClassLoader());
541
Object customLoadedClass1 = parentCustomCL.loadClass("com.ibm.j9.jsr292.CustomLoadedClass1" ).newInstance();
542
543
Assert.assertTrue(customLoadedClass1.getClass().getClassLoader() instanceof ParentCustomClassLoader);
544
545
ChildCustomClassLoader childCustomCL = new ChildCustomClassLoader(parentCustomCL);
546
ICustomLoadedClass customLoadedClass2 = (ICustomLoadedClass) childCustomCL.loadClass("com.ibm.j9.jsr292.CustomLoadedClass2" ).newInstance();
547
548
Assert.assertTrue(customLoadedClass2.getClass().getClassLoader() instanceof ChildCustomClassLoader);
549
550
Lookup lookup = customLoadedClass2.getLookup();
551
Lookup inObject = lookup.in(customLoadedClass1.getClass());
552
553
assertClassAndMode(inObject, customLoadedClass1.getClass(), MethodHandles.Lookup.PUBLIC);
554
}
555
556
/**
557
* Validates access restrictions stored in a new Lookup object created from an old Lookup object
558
* where the new lookup class was loaded using a custom class loader which is the child class loader
559
* of the class loader that loaded the original lookup class.
560
* @throws Throwable
561
*/
562
@Test(groups = { "level.extended" })
563
public void testLookup_RelatedClassLoaders_ParentLookupInChild() throws Throwable {
564
ParentCustomClassLoader parentCustomCL = new ParentCustomClassLoader(LookupInTests.class.getClassLoader());
565
ICustomLoadedClass customLoadedClass1 = (ICustomLoadedClass) parentCustomCL.loadClass("com.ibm.j9.jsr292.CustomLoadedClass1" ).newInstance();
566
567
Assert.assertTrue(customLoadedClass1.getClass().getClassLoader() instanceof ParentCustomClassLoader);
568
569
ChildCustomClassLoader childCustomCL = new ChildCustomClassLoader(parentCustomCL);
570
ICustomLoadedClass customLoadedClass2 = (ICustomLoadedClass) childCustomCL.loadClass("com.ibm.j9.jsr292.CustomLoadedClass2" ).newInstance();
571
572
Assert.assertTrue(customLoadedClass2.getClass().getClassLoader() instanceof ChildCustomClassLoader);
573
574
Lookup lookup = customLoadedClass1.getLookup();
575
Lookup inObject = lookup.in(customLoadedClass2.getClass());
576
577
assertClassAndMode(inObject, customLoadedClass2.getClass(), MethodHandles.Lookup.PUBLIC);
578
}
579
580
/**
581
*Test for MethodHandles.Lookup.toString() class where we check output depending on various access modes.
582
*/
583
@Test(groups = { "level.extended" })
584
public void test_Lookup_toString() {
585
Lookup publicLookup = MethodHandles.publicLookup();
586
Assert.assertEquals(publicLookup.toString(), "java.lang.Object/public");
587
588
Lookup lookup = PackageExamples.getLookup();
589
Lookup inObject = lookup.in(Object.class);
590
Assert.assertEquals(inObject.toString(), "java.lang.Object/public");
591
592
lookup = MethodHandles.lookup();
593
Lookup inAssertObject = lookup.in(Assert.class);
594
Assert.assertEquals(inAssertObject.toString(), "org.testng.Assert/public");
595
596
lookup = SamePackageExample.getLookup();
597
Lookup inCrossPackageClass = lookup.in(PackageExamples.class);
598
Assert.assertEquals(inCrossPackageClass.toString(), "examples.PackageExamples/public");
599
600
lookup = SamePackageExample.getLookup();
601
Lookup inSamePackageClass = lookup.in(SamePackageSingleMethodInterfaceExample.class);
602
Assert.assertEquals(inSamePackageClass.toString(), "com.ibm.j9.jsr292.SamePackageSingleMethodInterfaceExample/package");
603
604
lookup = MethodHandles.lookup();
605
Lookup inPrivateClass = lookup.in(PackageClass.class);
606
Assert.assertEquals(inPrivateClass.toString(), "com.ibm.j9.jsr292.LookupInTests$PackageClass/private");
607
608
lookup = MethodHandles.publicLookup();
609
Lookup inNoAccess = lookup.in(PackageClass.class);
610
Assert.assertEquals(inNoAccess.toString(), "com.ibm.j9.jsr292.LookupInTests$PackageClass/noaccess");
611
}
612
613
/**
614
*Non-public outer class used in tests
615
*/
616
class PackageClass { }
617
618
/**
619
* Helper validation method. Validates the lookup class and lookup modes of the Lookup object being tested.
620
* @param lookup - Lookup object being tested
621
* @param c - Expected Lookup class
622
* @param mode - Expected Lookup Modes
623
*/
624
protected void assertClassAndMode(Lookup lookup, Class<?> c, int mode) {
625
Assert.assertTrue(lookup.lookupClass().equals(c), "Lookup class mismatch. Expected: " + c + ", found: " + lookup.lookupClass());
626
Assert.assertTrue(lookup.lookupModes() == mode, "Lookup mode mismatch. Expected: " + mode + ", found: " + lookup.lookupModes());
627
}
628
}
629
630