Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/invoke/AccessControlTest.java
41149 views
1
/*
2
* Copyright (c) 2012, 2019, 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
/* @test
25
* @summary test access checking by java.lang.invoke.MethodHandles.Lookup
26
* @compile AccessControlTest.java AccessControlTest_subpkg/Acquaintance_remote.java
27
* @run testng/othervm test.java.lang.invoke.AccessControlTest
28
*/
29
30
package test.java.lang.invoke;
31
32
import java.lang.invoke.*;
33
import java.lang.reflect.*;
34
import java.lang.reflect.Modifier;
35
import java.util.*;
36
import org.testng.annotations.*;
37
38
import static java.lang.invoke.MethodHandles.*;
39
import static java.lang.invoke.MethodHandles.Lookup.*;
40
import static java.lang.invoke.MethodType.*;
41
import static org.testng.Assert.*;
42
43
import test.java.lang.invoke.AccessControlTest_subpkg.Acquaintance_remote;
44
45
46
/**
47
* Test many combinations of Lookup access and cross-class lookupStatic.
48
* @author jrose
49
*/
50
public class AccessControlTest {
51
static final Class<?> THIS_CLASS = AccessControlTest.class;
52
// How much output?
53
static int verbosity = 0;
54
static {
55
String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbosity");
56
if (vstr == null)
57
vstr = System.getProperty(THIS_CLASS.getName()+".verbosity");
58
if (vstr != null) verbosity = Integer.parseInt(vstr);
59
}
60
61
private class LookupCase implements Comparable<LookupCase> {
62
final Lookup lookup;
63
final Class<?> lookupClass;
64
final Class<?> prevLookupClass;
65
final int lookupModes;
66
public LookupCase(Lookup lookup) {
67
this.lookup = lookup;
68
this.lookupClass = lookup.lookupClass();
69
this.prevLookupClass = lookup.previousLookupClass();
70
this.lookupModes = lookup.lookupModes();
71
72
assert(lookupString().equals(lookup.toString()));
73
numberOf(lookupClass().getClassLoader()); // assign CL#
74
}
75
public LookupCase(Class<?> lookupClass, Class<?> prevLookupClass, int lookupModes) {
76
this.lookup = null;
77
this.lookupClass = lookupClass;
78
this.prevLookupClass = prevLookupClass;
79
this.lookupModes = lookupModes;
80
numberOf(lookupClass().getClassLoader()); // assign CL#
81
}
82
83
public final Class<?> lookupClass() { return lookupClass; }
84
public final Class<?> prevLookupClass() { return prevLookupClass; }
85
public final int lookupModes() { return lookupModes; }
86
87
public Lookup lookup() { lookup.getClass(); return lookup; }
88
89
@Override
90
public int compareTo(LookupCase that) {
91
Class<?> c1 = this.lookupClass();
92
Class<?> c2 = that.lookupClass();
93
Class<?> p1 = this.prevLookupClass();
94
Class<?> p2 = that.prevLookupClass();
95
if (c1 != c2) {
96
int cmp = c1.getName().compareTo(c2.getName());
97
if (cmp != 0) return cmp;
98
cmp = numberOf(c1.getClassLoader()) - numberOf(c2.getClassLoader());
99
assert(cmp != 0);
100
return cmp;
101
} else if (p1 != p2){
102
if (p1 == null)
103
return 1;
104
else if (p2 == null)
105
return -1;
106
int cmp = p1.getName().compareTo(p2.getName());
107
if (cmp != 0) return cmp;
108
cmp = numberOf(p1.getClassLoader()) - numberOf(p2.getClassLoader());
109
assert(cmp != 0);
110
return cmp;
111
}
112
return -(this.lookupModes() - that.lookupModes());
113
}
114
115
@Override
116
public boolean equals(Object that) {
117
return (that instanceof LookupCase && equals((LookupCase)that));
118
}
119
public boolean equals(LookupCase that) {
120
return (this.lookupClass() == that.lookupClass() &&
121
this.prevLookupClass() == that.prevLookupClass() &&
122
this.lookupModes() == that.lookupModes());
123
}
124
125
@Override
126
public int hashCode() {
127
return lookupClass().hashCode() + (lookupModes() * 31);
128
}
129
130
/** Simulate all assertions in the spec. for Lookup.toString. */
131
private String lookupString() {
132
String name = lookupClass.getName();
133
if (prevLookupClass != null)
134
name += "/" + prevLookupClass.getName();
135
String suffix = "";
136
if (lookupModes == 0)
137
suffix = "/noaccess";
138
else if (lookupModes == PUBLIC)
139
suffix = "/public";
140
else if (lookupModes == UNCONDITIONAL)
141
suffix = "/publicLookup";
142
else if (lookupModes == (PUBLIC|MODULE))
143
suffix = "/module";
144
else if (lookupModes == (PUBLIC|PACKAGE)
145
|| lookupModes == (PUBLIC|MODULE|PACKAGE))
146
suffix = "/package";
147
else if (lookupModes == (PUBLIC|PACKAGE|PRIVATE)
148
|| lookupModes == (PUBLIC|MODULE|PACKAGE|PRIVATE))
149
suffix = "/private";
150
else if (lookupModes == (PUBLIC|PACKAGE|PRIVATE|PROTECTED)
151
|| lookupModes == (PUBLIC|MODULE|PACKAGE|PRIVATE|PROTECTED)
152
|| lookupModes == (PUBLIC|MODULE|PACKAGE|PRIVATE|PROTECTED|ORIGINAL))
153
suffix = "";
154
else
155
suffix = "/#"+Integer.toHexString(lookupModes);
156
return name+suffix;
157
}
158
159
/** Simulate all assertions from the spec. for Lookup.in:
160
* <hr>
161
* Creates a lookup on the specified new lookup class.
162
* [A1] The resulting object will report the specified
163
* class as its own {@link #lookupClass lookupClass}.
164
* [A1-a] However, the resulting {@code Lookup} object is guaranteed
165
* to have no more access capabilities than the original.
166
* In particular, access capabilities can be lost as follows:<ul>
167
* [A2] If the new lookup class is not the same as the old lookup class,
168
* then {@link #ORIGINAL ORIGINAL} access is lost.
169
* [A3] If the new lookup class is in a different module from the old one,
170
* i.e. {@link #MODULE MODULE} access is lost.
171
* [A4] If the new lookup class is in a different package
172
* than the old one, protected and default (package) members will not be accessible,
173
* i.e. {@link #PROTECTED PROTECTED} and {@link #PACKAGE PACKAGE} access are lost.
174
* [A5] If the new lookup class is not within the same package member
175
* as the old one, private members will not be accessible, and protected members
176
* will not be accessible by virtue of inheritance,
177
* i.e. {@link #PRIVATE PRIVATE} access is lost.
178
* (Protected members may continue to be accessible because of package sharing.)
179
* [A6] If the new lookup class is not
180
* {@linkplain #accessClass(Class) accessible} to this lookup,
181
* then no members, not even public members, will be accessible
182
* i.e. all access modes are lost.
183
* [A7] If the new lookup class, the old lookup class and the previous lookup class
184
* are all in different modules i.e. teleporting to a third module,
185
* all access modes are lost.
186
* <p>
187
* The new previous lookup class is chosen as follows:
188
* [A8] If the new lookup object has {@link #UNCONDITIONAL UNCONDITIONAL} bit,
189
* the new previous lookup class is {@code null}.
190
* [A9] If the new lookup class is in the same module as the old lookup class,
191
* the new previous lookup class is the old previous lookup class.
192
* [A10] If the new lookup class is in a different module from the old lookup class,
193
* the new previous lookup class is the the old lookup class.
194
*
195
* Other than the above cases, the new lookup will have the same
196
* access capabilities as the original. [A11]
197
* <hr>
198
*/
199
public LookupCase in(Class<?> c2) {
200
Class<?> c1 = lookupClass();
201
Module m1 = c1.getModule();
202
Module m2 = c2.getModule();
203
Module m0 = prevLookupClass() != null ? prevLookupClass.getModule() : c1.getModule();
204
int modes1 = lookupModes();
205
int changed = 0;
206
// for the purposes of access control then treat classes in different unnamed
207
// modules as being in the same module.
208
boolean sameModule = (m1 == m2) ||
209
(!m1.isNamed() && !m2.isNamed());
210
boolean samePackage = (c1.getClassLoader() == c2.getClassLoader() &&
211
c1.getPackageName().equals(c2.getPackageName()));
212
boolean sameTopLevel = (topLevelClass(c1) == topLevelClass(c2));
213
boolean sameClass = (c1 == c2);
214
assert(samePackage || !sameTopLevel);
215
assert(sameTopLevel || !sameClass);
216
boolean accessible = sameClass;
217
218
if ((modes1 & PACKAGE) != 0) accessible |= samePackage;
219
if ((modes1 & PUBLIC ) != 0) {
220
if (isModuleAccessible(c2))
221
accessible |= (c2.getModifiers() & PUBLIC) != 0;
222
else
223
accessible = false;
224
}
225
if ((modes1 & UNCONDITIONAL) != 0) {
226
if (m2.isExported(c2.getPackageName()))
227
accessible |= (c2.getModifiers() & PUBLIC) != 0;
228
else
229
accessible = false;
230
}
231
if (!accessible) {
232
// no access to c2; lose all access.
233
changed |= (PUBLIC|MODULE|PACKAGE|PRIVATE|PROTECTED|UNCONDITIONAL); // [A7]
234
}
235
if (!sameClass) {
236
changed |= ORIGINAL; // [A2]
237
}
238
if (m2 != m1 && m0 != m1) {
239
// hop to a third module; lose all access
240
changed |= (PUBLIC|MODULE|PACKAGE|PRIVATE|PROTECTED); // [A8]
241
}
242
if (!sameModule) {
243
changed |= MODULE; // [A3]
244
}
245
if (!samePackage) {
246
// Different package; loose PACKAGE and lower access.
247
changed |= (PACKAGE|PRIVATE|PROTECTED); // [A4]
248
}
249
if (!sameTopLevel) {
250
// Different top-level class. Lose PRIVATE and PROTECTED access.
251
changed |= (PRIVATE|PROTECTED); // [A5]
252
}
253
if (sameClass) {
254
assert(changed == 0); // [A11] (no deprivation if same class)
255
}
256
257
if (accessible) assert((changed & PUBLIC) == 0);
258
int modes2 = modes1 & ~changed;
259
Class<?> plc = (m1 == m2) ? prevLookupClass() : c1; // [A9] [A10]
260
if ((modes1 & UNCONDITIONAL) != 0) plc = null; // [A8]
261
LookupCase l2 = new LookupCase(c2, plc, modes2);
262
assert(l2.lookupClass() == c2); // [A1]
263
assert((modes1 | modes2) == modes1); // [A1-a] (no elevation of access)
264
assert(l2.prevLookupClass() == null || (modes2 & MODULE) == 0);
265
return l2;
266
}
267
268
LookupCase dropLookupMode(int modeToDrop) {
269
int oldModes = lookupModes();
270
int newModes = oldModes & ~(modeToDrop | PROTECTED | ORIGINAL);
271
switch (modeToDrop) {
272
case PUBLIC: newModes &= ~(MODULE|PACKAGE|PROTECTED|PRIVATE); break;
273
case MODULE: newModes &= ~(PACKAGE|PRIVATE); break;
274
case PACKAGE: newModes &= ~(PRIVATE); break;
275
case PROTECTED:
276
case PRIVATE:
277
case ORIGINAL:
278
case UNCONDITIONAL: break;
279
default: throw new IllegalArgumentException(modeToDrop + " is not a valid mode to drop");
280
}
281
if (newModes == oldModes) return this; // return self if no change
282
LookupCase l2 = new LookupCase(lookupClass(), prevLookupClass(), newModes);
283
assert((oldModes | newModes) == oldModes); // [A2] (no elevation of access)
284
assert(l2.prevLookupClass() == null || (newModes & MODULE) == 0);
285
return l2;
286
}
287
288
boolean isModuleAccessible(Class<?> c) {
289
Module m1 = lookupClass().getModule();
290
Module m2 = c.getModule();
291
Module m0 = prevLookupClass() != null ? prevLookupClass.getModule() : m1;
292
String pn = c.getPackageName();
293
boolean accessible = m1.canRead(m2) && m2.isExported(pn, m1);
294
if (m1 != m0) {
295
accessible = accessible && m0.canRead(m2) && m2.isExported(pn, m0);
296
}
297
return accessible;
298
}
299
300
@Override
301
public String toString() {
302
String s = lookupClass().getSimpleName();
303
String lstr = lookupString();
304
int sl = lstr.indexOf('/');
305
if (sl >= 0) s += lstr.substring(sl);
306
ClassLoader cld = lookupClass().getClassLoader();
307
if (cld != THIS_LOADER) s += "/loader#"+numberOf(cld);
308
return s;
309
}
310
311
/** Predict the success or failure of accessing this method. */
312
public boolean willAccess(Method m) {
313
Class<?> c1 = lookupClass();
314
Class<?> c2 = m.getDeclaringClass();
315
Module m1 = c1.getModule();
316
Module m2 = c2.getModule();
317
Module m0 = prevLookupClass != null ? prevLookupClass.getModule() : m1;
318
// unconditional has access to all public types/members of types that is in a package
319
// are unconditionally exported
320
if ((lookupModes & UNCONDITIONAL) != 0) {
321
return m2.isExported(c2.getPackageName())
322
&& Modifier.isPublic(c2.getModifiers())
323
&& Modifier.isPublic(m.getModifiers());
324
}
325
326
// c1 and c2 are in different module
327
if (m1 != m2 || m0 != m2) {
328
return (lookupModes & PUBLIC) != 0
329
&& isModuleAccessible(c2)
330
&& Modifier.isPublic(c2.getModifiers())
331
&& Modifier.isPublic(m.getModifiers());
332
}
333
334
assert(m1 == m2 && prevLookupClass == null);
335
336
if (!willAccessClass(c2, false))
337
return false;
338
339
LookupCase lc = this.in(c2);
340
int modes1 = lc.lookupModes();
341
int modes2 = fixMods(m.getModifiers());
342
// allow private lookup on nestmates. Otherwise, privacy is strictly enforced
343
if (c1 != c2 && ((modes2 & PRIVATE) == 0 || !c1.isNestmateOf(c2))) {
344
modes1 &= ~PRIVATE;
345
}
346
// protected access is sometimes allowed
347
if ((modes2 & PROTECTED) != 0) {
348
int prev = modes2;
349
modes2 |= PACKAGE; // it acts like a package method also
350
if ((lookupModes() & PROTECTED) != 0 &&
351
c2.isAssignableFrom(c1))
352
modes2 |= PUBLIC; // from a subclass, it acts like a public method also
353
}
354
if (verbosity >= 2)
355
System.out.format("%s willAccess %s modes1=0x%h modes2=0x%h => %s%n", lookupString(), lc.lookupString(), modes1, modes2, (modes2 & modes1) != 0);
356
return (modes2 & modes1) != 0;
357
}
358
359
/** Predict the success or failure of accessing this class. */
360
public boolean willAccessClass(Class<?> c2, boolean load) {
361
Class<?> c1 = lookupClass();
362
if (load && c2.getClassLoader() != null) {
363
if (c1.getClassLoader() == null) {
364
// not visible
365
return false;
366
}
367
}
368
369
Module m1 = c1.getModule();
370
Module m2 = c2.getModule();
371
Module m0 = prevLookupClass != null ? prevLookupClass.getModule() : m1;
372
// unconditional has access to all public types that is in an unconditionally exported package
373
if ((lookupModes & UNCONDITIONAL) != 0) {
374
return m2.isExported(c2.getPackageName()) && Modifier.isPublic(c2.getModifiers());
375
}
376
// c1 and c2 are in different module
377
if (m1 != m2 || m0 != m2) {
378
return (lookupModes & PUBLIC) != 0
379
&& isModuleAccessible(c2)
380
&& Modifier.isPublic(c2.getModifiers());
381
}
382
383
assert(m1 == m2 && prevLookupClass == null);
384
385
LookupCase lc = this.in(c2);
386
int modes1 = lc.lookupModes();
387
boolean r = false;
388
if (modes1 == 0) {
389
r = false;
390
} else {
391
if (Modifier.isPublic(c2.getModifiers())) {
392
if ((modes1 & MODULE) != 0)
393
r = true;
394
else if ((modes1 & PUBLIC) != 0)
395
r = m1.isExported(c2.getPackageName());
396
} else {
397
if ((modes1 & PACKAGE) != 0 && c1.getPackage() == c2.getPackage())
398
r = true;
399
}
400
}
401
if (verbosity >= 2) {
402
System.out.println(this+" willAccessClass "+lc+" c1="+c1+" c2="+c2+" => "+r);
403
}
404
return r;
405
}
406
}
407
408
private static Class<?> topLevelClass(Class<?> cls) {
409
Class<?> c = cls;
410
for (Class<?> ec; (ec = c.getEnclosingClass()) != null; )
411
c = ec;
412
assert(c.getEnclosingClass() == null);
413
assert(c == cls || cls.getEnclosingClass() != null);
414
return c;
415
}
416
417
private final TreeSet<LookupCase> CASES = new TreeSet<>();
418
private final TreeMap<LookupCase,TreeSet<LookupCase>> CASE_EDGES = new TreeMap<>();
419
private final ArrayList<ClassLoader> LOADERS = new ArrayList<>();
420
private final ClassLoader THIS_LOADER = this.getClass().getClassLoader();
421
{ if (THIS_LOADER != null) LOADERS.add(THIS_LOADER); } // #1
422
423
private LookupCase lookupCase(String name) {
424
for (LookupCase lc : CASES) {
425
if (lc.toString().equals(name))
426
return lc;
427
}
428
throw new AssertionError(name);
429
}
430
431
private int numberOf(ClassLoader cl) {
432
if (cl == null) return 0;
433
int i = LOADERS.indexOf(cl);
434
if (i < 0) {
435
i = LOADERS.size();
436
LOADERS.add(cl);
437
}
438
return i+1;
439
}
440
441
private void addLookupEdge(LookupCase l1, Class<?> c2, LookupCase l2, int dropAccess) {
442
TreeSet<LookupCase> edges = CASE_EDGES.get(l2);
443
if (edges == null) CASE_EDGES.put(l2, edges = new TreeSet<>());
444
if (edges.add(l1)) {
445
Class<?> c1 = l1.lookupClass();
446
assert(l2.lookupClass() == c2); // [A1]
447
int m1 = l1.lookupModes();
448
int m2 = l2.lookupModes();
449
assert((m1 | m2) == m1); // [A2] (no elevation of access)
450
LookupCase expect = dropAccess == 0 ? l1.in(c2) : l1.in(c2).dropLookupMode(dropAccess);
451
if (!expect.equals(l2))
452
System.out.println("*** expect "+l1+" => "+expect+" but got "+l2);
453
assertEquals(l2, expect);
454
}
455
}
456
457
private void makeCases(Lookup[] originalLookups) {
458
// make initial set of lookup test cases
459
CASES.clear(); LOADERS.clear(); CASE_EDGES.clear();
460
ArrayList<Class<?>> classes = new ArrayList<>();
461
for (Lookup l : originalLookups) {
462
CASES.add(new LookupCase(l));
463
classes.remove(l.lookupClass()); // no dups please
464
classes.add(l.lookupClass());
465
}
466
System.out.println("loaders = "+LOADERS);
467
int rounds = 0;
468
for (int lastCount = -1; lastCount != CASES.size(); ) {
469
lastCount = CASES.size(); // if CASES grow in the loop we go round again
470
for (LookupCase lc1 : CASES.toArray(new LookupCase[0])) {
471
for (int mode : ACCESS_CASES) {
472
LookupCase lc2 = new LookupCase(lc1.lookup().dropLookupMode(mode));
473
addLookupEdge(lc1, lc1.lookupClass(), lc2, mode);
474
CASES.add(lc2);
475
}
476
for (Class<?> c2 : classes) {
477
LookupCase lc2 = new LookupCase(lc1.lookup().in(c2));
478
addLookupEdge(lc1, c2, lc2, 0);
479
CASES.add(lc2);
480
}
481
}
482
rounds++;
483
}
484
System.out.println("filled in "+CASES.size()+" cases from "+originalLookups.length+" original cases in "+rounds+" rounds");
485
if (false) {
486
System.out.println("CASES: {");
487
for (LookupCase lc : CASES) {
488
System.out.println(lc);
489
Set<LookupCase> edges = CASE_EDGES.get(lc);
490
if (edges != null)
491
for (LookupCase prev : edges) {
492
System.out.println("\t"+prev);
493
}
494
}
495
System.out.println("}");
496
}
497
}
498
499
@Test public void test() {
500
makeCases(lookups());
501
if (verbosity > 0) {
502
verbosity += 9;
503
Method pro_in_self = targetMethod(THIS_CLASS, PROTECTED, methodType(void.class));
504
testOneAccess(lookupCase("AccessControlTest/module"), pro_in_self, "find");
505
testOneAccess(lookupCase("Remote_subclass/module"), pro_in_self, "find");
506
testOneAccess(lookupCase("Remote_subclass"), pro_in_self, "find");
507
verbosity -= 9;
508
}
509
Set<Class<?>> targetClassesDone = new HashSet<>();
510
for (LookupCase targetCase : CASES) {
511
Class<?> targetClass = targetCase.lookupClass();
512
if (!targetClassesDone.add(targetClass)) continue; // already saw this one
513
String targetPlace = placeName(targetClass);
514
if (targetPlace == null) continue; // Object, String, not a target
515
for (int targetAccess : ACCESS_CASES) {
516
if (targetAccess == MODULE || targetAccess == UNCONDITIONAL)
517
continue;
518
MethodType methodType = methodType(void.class);
519
Method method = targetMethod(targetClass, targetAccess, methodType);
520
// Try to access target method from various contexts.
521
for (LookupCase sourceCase : CASES) {
522
testOneAccess(sourceCase, method, "findClass");
523
testOneAccess(sourceCase, method, "accessClass");
524
testOneAccess(sourceCase, method, "find");
525
testOneAccess(sourceCase, method, "unreflect");
526
}
527
}
528
}
529
System.out.println("tested "+testCount+" access scenarios; "+testCountFails+" accesses were denied");
530
}
531
532
private int testCount, testCountFails;
533
534
private void testOneAccess(LookupCase sourceCase, Method method, String kind) {
535
Class<?> targetClass = method.getDeclaringClass();
536
String methodName = method.getName();
537
MethodType methodType = methodType(method.getReturnType(), method.getParameterTypes());
538
boolean isFindOrAccessClass = "findClass".equals(kind) || "accessClass".equals(kind);
539
boolean willAccess = isFindOrAccessClass ?
540
sourceCase.willAccessClass(targetClass, "findClass".equals(kind)) : sourceCase.willAccess(method);
541
boolean didAccess = false;
542
ReflectiveOperationException accessError = null;
543
try {
544
switch (kind) {
545
case "accessClass":
546
sourceCase.lookup().accessClass(targetClass);
547
break;
548
case "findClass":
549
sourceCase.lookup().findClass(targetClass.getName());
550
break;
551
case "find":
552
if ((method.getModifiers() & Modifier.STATIC) != 0)
553
sourceCase.lookup().findStatic(targetClass, methodName, methodType);
554
else
555
sourceCase.lookup().findVirtual(targetClass, methodName, methodType);
556
break;
557
case "unreflect":
558
sourceCase.lookup().unreflect(method);
559
break;
560
default:
561
throw new AssertionError(kind);
562
}
563
didAccess = true;
564
} catch (ReflectiveOperationException ex) {
565
accessError = ex;
566
}
567
if (willAccess != didAccess) {
568
System.out.println(sourceCase+" => "+targetClass.getSimpleName()+(isFindOrAccessClass?"":"."+methodName+methodType));
569
System.out.println("fail "+(isFindOrAccessClass?kind:"on "+method)+" ex="+accessError);
570
assertEquals(willAccess, didAccess);
571
}
572
testCount++;
573
if (!didAccess) testCountFails++;
574
}
575
576
static Method targetMethod(Class<?> targetClass, int targetAccess, MethodType methodType) {
577
String methodName = accessName(targetAccess)+placeName(targetClass);
578
if (verbosity >= 2)
579
System.out.println(targetClass.getSimpleName()+"."+methodName+methodType);
580
try {
581
Method method = targetClass.getDeclaredMethod(methodName, methodType.parameterArray());
582
assertEquals(method.getReturnType(), methodType.returnType());
583
int haveMods = method.getModifiers();
584
assert(Modifier.isStatic(haveMods));
585
assert(targetAccess == fixMods(haveMods));
586
return method;
587
} catch (NoSuchMethodException ex) {
588
throw new AssertionError(methodName, ex);
589
}
590
}
591
592
static String placeName(Class<?> cls) {
593
// return "self", "sibling", "nestmate", etc.
594
if (cls == AccessControlTest.class) return "self";
595
String cln = cls.getSimpleName();
596
int under = cln.lastIndexOf('_');
597
if (under < 0) return null;
598
return cln.substring(under+1);
599
}
600
static String accessName(int acc) {
601
switch (acc) {
602
case PUBLIC: return "pub_in_";
603
case PROTECTED: return "pro_in_";
604
case PACKAGE: return "pkg_in_";
605
case PRIVATE: return "pri_in_";
606
}
607
assert(false);
608
return "?";
609
}
610
private static final int[] ACCESS_CASES = {
611
PUBLIC, PACKAGE, PRIVATE, PROTECTED, MODULE, UNCONDITIONAL
612
};
613
/*
614
* Adjust PUBLIC => PUBLIC|MODULE|UNCONDITIONAL
615
* Adjust 0 => PACKAGE
616
*/
617
/** Return one of the ACCESS_CASES. */
618
static int fixMods(int mods) {
619
mods &= (PUBLIC|PRIVATE|PROTECTED);
620
switch (mods) {
621
case PUBLIC: case PRIVATE: case PROTECTED: return mods;
622
case 0: return PACKAGE;
623
}
624
throw new AssertionError(mods);
625
}
626
627
static Lookup[] lookups() {
628
ArrayList<Lookup> tem = new ArrayList<>();
629
Collections.addAll(tem,
630
AccessControlTest.lookup_in_self(),
631
Inner_nestmate.lookup_in_nestmate(),
632
AccessControlTest_sibling.lookup_in_sibling());
633
if (true) {
634
Collections.addAll(tem,Acquaintance_remote.lookups());
635
} else {
636
try {
637
Class<?> remc = Class.forName("test.java.lang.invoke.AccessControlTest_subpkg.Acquaintance_remote");
638
Lookup[] remls = (Lookup[]) remc.getMethod("lookups").invoke(null);
639
Collections.addAll(tem, remls);
640
} catch (ReflectiveOperationException ex) {
641
throw new LinkageError("reflection failed", ex);
642
}
643
}
644
tem.add(publicLookup());
645
tem.add(publicLookup().in(String.class));
646
tem.add(publicLookup().in(List.class));
647
return tem.toArray(new Lookup[0]);
648
}
649
650
static Lookup lookup_in_self() {
651
return MethodHandles.lookup();
652
}
653
public static void pub_in_self() { }
654
protected static void pro_in_self() { }
655
static /*package*/ void pkg_in_self() { }
656
private static void pri_in_self() { }
657
658
static class Inner_nestmate {
659
static Lookup lookup_in_nestmate() {
660
return MethodHandles.lookup();
661
}
662
public static void pub_in_nestmate() { }
663
protected static void pro_in_nestmate() { }
664
static /*package*/ void pkg_in_nestmate() { }
665
private static void pri_in_nestmate() { }
666
}
667
}
668
class AccessControlTest_sibling {
669
static Lookup lookup_in_sibling() {
670
return MethodHandles.lookup();
671
}
672
public static void pub_in_sibling() { }
673
protected static void pro_in_sibling() { }
674
static /*package*/ void pkg_in_sibling() { }
675
private static void pri_in_sibling() { }
676
}
677
678
// This guy tests access from outside the package:
679
/*
680
package test.java.lang.invoke.AccessControlTest_subpkg;
681
public class Acquaintance_remote {
682
public static Lookup[] lookups() { ...
683
}
684
...
685
}
686
*/
687
688