1/* 2 * Copyright (c) 2015, 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 24package jdk.test; 25 26import jdk.test.internal.*; 27import jdk.test.internal.foo.*; 28import p.two.Bar; 29import p.three.P; 30 31import java.lang.reflect.InvocationHandler; 32import java.lang.reflect.Method; 33import java.lang.reflect.Proxy; 34import java.net.URL; 35import java.net.URLClassLoader; 36 37/** 38 * Test proxy class to have access to types referenced in the public methods. 39 */ 40public class ProxyClassAccess { 41 public static void main(String... args) throws Exception { 42 testImplClass(); 43 testProxyClass1(); 44 testProxyClass2(); 45 testNonPublicProxy(); 46 } 47 48 /* 49 * Invoke methods from implementation class 50 */ 51 static void testImplClass() { 52 R impl = new RImpl(); 53 impl.foo(); 54 Bar[][] bars = new Bar[0][0]; 55 impl.setBarArray(bars); 56 try { 57 impl.throwException(); 58 throw new RuntimeException("FooException not thrown"); 59 } catch (FooException e) { } 60 } 61 62 /* 63 * Invoke methods via proxy 64 */ 65 static void testProxyClass1() { 66 R proxy = (R) Proxy.newProxyInstance(R.class.getClassLoader(), 67 new Class<?>[] { R.class }, handler); 68 proxy.foo(); 69 Bar[][] bars = new Bar[0][0]; 70 proxy.setBarArray(bars); 71 } 72 73 /* 74 * Invoke methods via proxy defined with a custom class loader 75 */ 76 static void testProxyClass2() { 77 URLClassLoader loader = new URLClassLoader(new URL[0]); 78 P proxy = (P) Proxy.newProxyInstance(loader, 79 new Class<?>[] { R.class, P.class }, handler); 80 proxy.bar(); 81 proxy.barArrays(); 82 } 83 84 static void testNonPublicProxy() { 85 NP proxy = (NP) Proxy.newProxyInstance(NP.class.getClassLoader(), 86 new Class<?>[]{NP.class}, handler); 87 proxy.test(); 88 89 try { 90 URLClassLoader loader = new URLClassLoader(new URL[0]); 91 proxy = (NP) Proxy.newProxyInstance(loader, 92 new Class<?>[]{NP.class}, handler); 93 throw new RuntimeException("IllegalArgumentException not thrown"); 94 } catch (IllegalArgumentException e) { 95 } 96 } 97 98 static InvocationHandler handler = new InvocationHandler() { 99 final R impl = new RImpl(); 100 @Override 101 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 102 if (method.getDeclaringClass() == R.class) { 103 return method.invoke(impl, args); 104 } else { 105 return null; 106 } 107 } 108 }; 109} 110