Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/8022701/MHIllegalAccess.java
48283 views
1
/*
2
* Copyright (c) 2013, 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
/**
26
* @test
27
* @bug 8022701
28
* @summary Illegal access exceptions via methodhandle invocations threw wrong error.
29
*
30
* @compile -XDignore.symbol.file BogoLoader.java InvokeSeveralWays.java MHIllegalAccess.java MethodSupplier.java
31
* @run main/othervm MHIllegalAccess
32
*/
33
34
import java.lang.reflect.InvocationTargetException;
35
import java.util.HashMap;
36
import java.util.HashSet;
37
import jdk.internal.org.objectweb.asm.ClassWriter;
38
import jdk.internal.org.objectweb.asm.MethodVisitor;
39
import jdk.internal.org.objectweb.asm.ClassVisitor;
40
import jdk.internal.org.objectweb.asm.Opcodes;
41
42
public class MHIllegalAccess implements Opcodes {
43
44
public static void main(String args[]) throws Throwable {
45
System.out.println("Classpath is " + System.getProperty("java.class.path"));
46
System.out.println();
47
48
/**
49
* Make method m be private to provoke an IllegalAccessError.
50
*/
51
BogoLoader.VisitorMaker privatize = new BogoLoader.VisitorMaker() {
52
public ClassVisitor make(ClassVisitor cv) {
53
return new ClassVisitor(Opcodes.ASM5, cv) {
54
public MethodVisitor visitMethod(int access, String name, String desc,
55
String signature, String[] exceptions) {
56
if (name.equals("m"))
57
access = (access | ACC_PRIVATE) & ~ (ACC_PUBLIC | ACC_PROTECTED);
58
return super.visitMethod(access, name, desc, signature, exceptions);
59
}
60
};
61
}
62
};
63
64
/**
65
* Rename method m as nemo to provoke a NoSuchMethodError.
66
*/
67
BogoLoader.VisitorMaker changeName = new BogoLoader.VisitorMaker() {
68
public ClassVisitor make(ClassVisitor cv) {
69
return new ClassVisitor(Opcodes.ASM5, cv) {
70
public MethodVisitor visitMethod(int access, String name, String desc,
71
String signature, String[] exceptions) {
72
if (name.equals("m"))
73
name = "nemo";
74
return super.visitMethod(access, name, desc, signature, exceptions);
75
}
76
};
77
}
78
};
79
80
int failures = 0;
81
failures += testOneError(privatize, args, IllegalAccessError.class);
82
failures += testOneError(changeName, args, NoSuchMethodError.class);
83
if (failures > 0) {
84
System.out.println("Saw " + failures + " failures, see standard out for details");
85
throw new Error("FAIL test");
86
}
87
}
88
89
/**
90
*
91
* @param vm VisitorMaker, to be stored in a table and passed to a BogoLoader
92
* @param args A copy of the main args, to be passed on to InvokeSeveralWays.test
93
* @param expected The class of the exception that should be thrown after
94
* attempted invocation of MethodSupplier.m.
95
* @throws ClassNotFoundException
96
* @throws Throwable
97
*/
98
private static int testOneError(BogoLoader.VisitorMaker vm, String[] args, Class expected) throws ClassNotFoundException, Throwable {
99
HashMap<String, BogoLoader.VisitorMaker> replace = new HashMap<String, BogoLoader.VisitorMaker>();
100
replace.put("MethodSupplier", vm);
101
102
HashSet<String> in_bogus = new HashSet<String>();
103
in_bogus.add("InvokeSeveralWays");
104
in_bogus.add("MyFunctionalInterface");
105
in_bogus.add("Invoker");
106
107
BogoLoader bl = new BogoLoader(in_bogus, replace);
108
Class<?> isw = bl.loadClass("InvokeSeveralWays");
109
Object[] arg_for_args = new Object[2];
110
arg_for_args[0] = args;
111
arg_for_args[1] = expected;
112
try {
113
Object result = isw.getMethod("test", String[].class, Class.class).invoke(null, arg_for_args);
114
return (Integer)result;
115
} catch (InvocationTargetException e) {
116
Throwable th = e.getCause();
117
throw th == null ? e : th;
118
}
119
}
120
}
121
122