Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Proxy/ClassRestrictions.java
38828 views
/*1* Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4227192 800492825* @summary This is a test of the restrictions on the parameters that may26* be passed to the Proxy.getProxyClass method.27* @author Peter Jones28*29* @build ClassRestrictions30* @run main ClassRestrictions31*/3233import java.lang.reflect.Modifier;34import java.lang.reflect.Proxy;35import java.net.URLClassLoader;3637public class ClassRestrictions {3839public interface Bar {40int foo();41}4243public interface Baz {44long foo();45}4647interface Bashful {48void foo();49}5051public static final String nonPublicIntrfaceName = "java.util.zip.ZipConstants";5253public static void main(String[] args) {5455System.err.println(56"\nTest of restrictions on parameters to Proxy.getProxyClass\n");5758try {59ClassLoader loader = ClassRestrictions.class.getClassLoader();60Class<?>[] interfaces;61Class<?> proxyClass;6263/*64* All of the Class objects in the interfaces array must represent65* interfaces, not classes or primitive types.66*/67try {68interfaces = new Class<?>[] { Object.class };69proxyClass = Proxy.getProxyClass(loader, interfaces);70throw new Error(71"proxy class created with java.lang.Object as interface");72} catch (IllegalArgumentException e) {73e.printStackTrace();74System.err.println();75// assume exception is for intended failure76}77try {78interfaces = new Class<?>[] { Integer.TYPE };79proxyClass = Proxy.getProxyClass(loader, interfaces);80throw new Error(81"proxy class created with int.class as interface");82} catch (IllegalArgumentException e) {83e.printStackTrace();84System.err.println();85// assume exception is for intended failure86}8788/*89* No two elements in the interfaces array may refer to identical90* Class objects.91*/92try {93interfaces = new Class<?>[] { Bar.class, Bar.class };94proxyClass = Proxy.getProxyClass(loader, interfaces);95throw new Error(96"proxy class created with repeated interfaces");97} catch (IllegalArgumentException e) {98e.printStackTrace();99System.err.println();100// assume exception is for intended failure101}102103/*104* All of the interfaces types must be visible by name though the105* specified class loader.106*/107ClassLoader altLoader = new URLClassLoader(108((URLClassLoader) loader).getURLs(), null);109Class altBarClass;110altBarClass = Class.forName(Bar.class.getName(), false, altLoader);111try {112interfaces = new Class<?>[] { altBarClass };113proxyClass = Proxy.getProxyClass(loader, interfaces);114throw new Error(115"proxy class created with interface " +116"not visible to class loader");117} catch (IllegalArgumentException e) {118e.printStackTrace();119System.err.println();120// assume exception is for intended failure121}122123/*124* All non-public interfaces must be in the same package.125*/126Class<?> nonPublic1 = Bashful.class;127Class<?> nonPublic2 = Class.forName(nonPublicIntrfaceName);128if (Modifier.isPublic(nonPublic2.getModifiers())) {129throw new Error(130"Interface " + nonPublicIntrfaceName +131" is public and need to be changed!");132}133try {134interfaces = new Class<?>[] { nonPublic1, nonPublic2 };135proxyClass = Proxy.getProxyClass(loader, interfaces);136throw new Error(137"proxy class created with two non-public interfaces " +138"in different packages");139} catch (IllegalArgumentException e) {140e.printStackTrace();141System.err.println();142// assume exception is for intended failure143}144145/*146* No two interfaces may each have a method with the same name and147* parameter signature but different return type.148*/149try {150interfaces = new Class<?>[] { Bar.class, Baz.class };151proxyClass = Proxy.getProxyClass(loader, interfaces);152throw new Error(153"proxy class created with conflicting methods");154} catch (IllegalArgumentException e) {155e.printStackTrace();156System.err.println();157// assume exception is for intended failure158}159160/*161* All components of this test have passed.162*/163System.err.println("\nTEST PASSED");164165} catch (Throwable e) {166System.err.println("\nTEST FAILED:");167e.printStackTrace();168throw new Error("TEST FAILED: ", e);169}170}171}172173174