Path: blob/master/test/functional/cmdLineTests/proxyFieldAccess/src/defect/cmvc198986/ProxyFieldAccess.java
6007 views
/*******************************************************************************1* Copyright (c) 2001, 2018 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/21package defect.cmvc198986;2223import java.lang.reflect.*;24import java.security.AccessControlException;2526public class ProxyFieldAccess {27static final Class<?> signalClass = sun.misc.Signal.class;2829public static void main(String[] argv) throws Exception {30new ProxyFieldAccess().test();31}3233void test() {34try {35// create a proxy instance in a non-restricted package by36// implementing a non-public interface37Class<?> nonPublicIntf = Class.forName("java.util.Formatter$FormatString", false, null);38Class<?> restrictedIntf = sun.misc.SignalHandler.class;39Class<?>[] intfs = new Class<?>[] { nonPublicIntf, restrictedIntf };40Object proxy = Proxy.newProxyInstance(null, intfs, new InvocationHandler() {41public Object invoke(Object o, Method m, Object[] args) {42return null;43}44});4546Method m = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);47m.setAccessible(true);48ClassLoader cl = getClass().getClassLoader();49System.out.println(cl);50Object o = m.invoke(cl, "java.util.$Proxy0");51if (o != null) {52throw new Error("Test failed due to java.util.$Proxy0 already loaded.");53}54System.out.println(o);5556System.setSecurityManager(new SecurityManager());5758ProxyFieldAccess pfa = new ProxyFieldAccess();59pfa.fieldAccess(proxy);60pfa.methodAccess(proxy);6162// System.setSecurityManager(null);63// m = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);64// m.setAccessible(true);65o = m.invoke(getClass().getClassLoader(), "java.util.$Proxy0");66if (o == null) {67throw new Error("Test failed due to java.util.$Proxy0 NOT loaded.");68}69System.out.println(m.invoke(getClass().getClassLoader(), "java.util.$Proxy0"));7071System.out.println("ProxyFieldAccess test passed");72} catch (ClassNotFoundException e) {73} catch (NoSuchMethodException e) {74e.printStackTrace();75} catch (SecurityException e) {76e.printStackTrace();77} catch (IllegalAccessException e) {78e.printStackTrace();79} catch (IllegalArgumentException e) {80e.printStackTrace();81} catch (InvocationTargetException e) {82e.printStackTrace();83} catch (Exception e) {84e.printStackTrace();85}86}8788private void fieldAccess(Object proxy) throws Exception {89try {90Object o = ((java.util.$Proxy0)proxy).SIG_DFL;91throw new Error("Proxy field access via static linking test failed");92} catch (AccessControlException e) {93/* ok */94}9596try {97Class<?> c = proxy.getClass();98Field f = c.getField("SIG_DFL");99throw new Error("Proxy field access via Class.getField test failed");100} catch (AccessControlException e) {101/* ok */102}103}104105private void methodAccess(Object proxy) throws Exception {106try {107((java.util.$Proxy0)proxy).handle(null);108throw new Error("Proxy method access via static linking test failed");109} catch (AccessControlException e) {110/* ok */111}112113try {114Class<?> c = proxy.getClass();115Method m = c.getMethod("handle", signalClass);116throw new Error("Proxy method access via Class.getMethod test failed");117} catch (AccessControlException e) {118/* ok */119}120}121}122123124