Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/invoke/MethodHandles/ensureInitialized/Main.java
41154 views
1
/*
2
* Copyright (c) 2020, 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
import java.lang.invoke.MethodHandles;
25
import java.lang.reflect.Method;
26
27
import org.testng.annotations.BeforeClass;
28
import org.testng.annotations.Test;
29
import static org.testng.Assert.*;
30
31
/**
32
* @test
33
* @bug 8235521
34
* @summary Tests for Lookup::ensureClassInitialized
35
* @build java.base/* m1/* m2/* Main
36
* @run testng/othervm --add-modules m1 Main
37
*/
38
39
public class Main {
40
// Test access to public java.lang class
41
@Test
42
public void testPublic() throws Exception {
43
assertFalse(Helper.isInitialized(PublicInit.class));
44
MethodHandles.lookup().ensureInitialized(PublicInit.class);
45
assertTrue(Helper.isInitialized(PublicInit.class));
46
// no-op if already initialized
47
MethodHandles.lookup().ensureInitialized(PublicInit.class);
48
}
49
50
// access denied to package-private java.lang class
51
@Test(expectedExceptions = { IllegalAccessException.class })
52
public void testPackagePrivate() throws Exception {
53
Class<?> c = Class.forName("java.lang.DefaultInit", false, null);
54
assertFalse(Helper.isInitialized(c));
55
// access denied
56
MethodHandles.lookup().ensureInitialized(c);
57
}
58
59
// access denied to public class in a non-exported package
60
@Test(expectedExceptions = { IllegalAccessException.class })
61
public void testNonExportedPackage() throws Exception {
62
Class<?> c = Class.forName("jdk.internal.misc.VM", false, null);
63
// access denied
64
MethodHandles.lookup().ensureInitialized(c);
65
}
66
67
// invoke p1.Test::test to test module boundary access
68
@Test
69
public void testModuleAccess() throws Exception {
70
Class<?> c = Class.forName("p1.Test");
71
Method m = c.getMethod("test");
72
m.invoke(null);
73
}
74
75
@Test(expectedExceptions = { IllegalArgumentException.class })
76
public void testArrayType() throws Exception {
77
Class<?> arrayType = PublicInit.class.arrayType();
78
MethodHandles.lookup().ensureInitialized(arrayType);
79
}
80
}
81
82