Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdLineTests/proxyFieldAccess/src/defect/cmvc198986/ProxyFieldAccess.java
6007 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2018 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 defect.cmvc198986;
23
24
import java.lang.reflect.*;
25
import java.security.AccessControlException;
26
27
public class ProxyFieldAccess {
28
static final Class<?> signalClass = sun.misc.Signal.class;
29
30
public static void main(String[] argv) throws Exception {
31
new ProxyFieldAccess().test();
32
}
33
34
void test() {
35
try {
36
// create a proxy instance in a non-restricted package by
37
// implementing a non-public interface
38
Class<?> nonPublicIntf = Class.forName("java.util.Formatter$FormatString", false, null);
39
Class<?> restrictedIntf = sun.misc.SignalHandler.class;
40
Class<?>[] intfs = new Class<?>[] { nonPublicIntf, restrictedIntf };
41
Object proxy = Proxy.newProxyInstance(null, intfs, new InvocationHandler() {
42
public Object invoke(Object o, Method m, Object[] args) {
43
return null;
44
}
45
});
46
47
Method m = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
48
m.setAccessible(true);
49
ClassLoader cl = getClass().getClassLoader();
50
System.out.println(cl);
51
Object o = m.invoke(cl, "java.util.$Proxy0");
52
if (o != null) {
53
throw new Error("Test failed due to java.util.$Proxy0 already loaded.");
54
}
55
System.out.println(o);
56
57
System.setSecurityManager(new SecurityManager());
58
59
ProxyFieldAccess pfa = new ProxyFieldAccess();
60
pfa.fieldAccess(proxy);
61
pfa.methodAccess(proxy);
62
63
// System.setSecurityManager(null);
64
// m = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
65
// m.setAccessible(true);
66
o = m.invoke(getClass().getClassLoader(), "java.util.$Proxy0");
67
if (o == null) {
68
throw new Error("Test failed due to java.util.$Proxy0 NOT loaded.");
69
}
70
System.out.println(m.invoke(getClass().getClassLoader(), "java.util.$Proxy0"));
71
72
System.out.println("ProxyFieldAccess test passed");
73
} catch (ClassNotFoundException e) {
74
} catch (NoSuchMethodException e) {
75
e.printStackTrace();
76
} catch (SecurityException e) {
77
e.printStackTrace();
78
} catch (IllegalAccessException e) {
79
e.printStackTrace();
80
} catch (IllegalArgumentException e) {
81
e.printStackTrace();
82
} catch (InvocationTargetException e) {
83
e.printStackTrace();
84
} catch (Exception e) {
85
e.printStackTrace();
86
}
87
}
88
89
private void fieldAccess(Object proxy) throws Exception {
90
try {
91
Object o = ((java.util.$Proxy0)proxy).SIG_DFL;
92
throw new Error("Proxy field access via static linking test failed");
93
} catch (AccessControlException e) {
94
/* ok */
95
}
96
97
try {
98
Class<?> c = proxy.getClass();
99
Field f = c.getField("SIG_DFL");
100
throw new Error("Proxy field access via Class.getField test failed");
101
} catch (AccessControlException e) {
102
/* ok */
103
}
104
}
105
106
private void methodAccess(Object proxy) throws Exception {
107
try {
108
((java.util.$Proxy0)proxy).handle(null);
109
throw new Error("Proxy method access via static linking test failed");
110
} catch (AccessControlException e) {
111
/* ok */
112
}
113
114
try {
115
Class<?> c = proxy.getClass();
116
Method m = c.getMethod("handle", signalClass);
117
throw new Error("Proxy method access via Class.getMethod test failed");
118
} catch (AccessControlException e) {
119
/* ok */
120
}
121
}
122
}
123
124