Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Proxy/Basic1.java
38828 views
/*1* Copyright (c) 1999, 2013, 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 448767225* @summary This is a basic functional test of the dynamic proxy API (part 1).26* @author Peter Jones27*28* @build Basic129* @run main Basic130*/3132import java.lang.reflect.*;33import java.security.*;34import java.util.*;3536public class Basic1 {3738public static void main(String[] args) {3940System.err.println(41"\nBasic functional test of dynamic proxy API, part 1\n");4243try {44Class<?>[] interfaces =45new Class<?>[] { Runnable.class, Observer.class };4647ClassLoader loader = ClassLoader.getSystemClassLoader();4849/*50* Generate a proxy class.51*/52Class<?> proxyClass = Proxy.getProxyClass(loader, interfaces);53System.err.println("+ generated proxy class: " + proxyClass);5455/*56* Verify that it is public, final, and not abstract.57*/58int flags = proxyClass.getModifiers();59System.err.println(60"+ proxy class's modifiers: " + Modifier.toString(flags));61if (!Modifier.isPublic(flags)) {62throw new RuntimeException("proxy class in not public");63}64if (!Modifier.isFinal(flags)) {65throw new RuntimeException("proxy class in not final");66}67if (Modifier.isAbstract(flags)) {68throw new RuntimeException("proxy class in abstract");69}7071/*72* Verify that it is assignable to the proxy interfaces.73*/74for (Class<?> intf : interfaces) {75if (!intf.isAssignableFrom(proxyClass)) {76throw new RuntimeException(77"proxy class not assignable to proxy interface " +78intf.getName());79}80}8182/*83* Verify that it has the given permutation of interfaces.84*/85List<Class<?>> l1 = Arrays.asList(interfaces);86List<Class<?>> l2 = Arrays.asList(proxyClass.getInterfaces());87System.err.println("+ proxy class's interfaces: " + l2);88if (!l1.equals(l2)) {89throw new RuntimeException(90"proxy class interfaces are " + l2 +91" (expected " + l1 + ")");92}9394/*95* Verify that system agress that it is a proxy class.96*/97if (Proxy.isProxyClass(Object.class)) {98throw new RuntimeException(99"Proxy.isProxyClass returned true for java.lang.Object");100}101if (!Proxy.isProxyClass(proxyClass)) {102throw new RuntimeException(103"Proxy.isProxyClass returned false for proxy class");104}105106/*107* Verify that its protection domain is the bootstrap domain.108*/109ProtectionDomain pd = proxyClass.getProtectionDomain();110System.err.println("+ proxy class's protextion domain: " + pd);111if (!pd.implies(new AllPermission())) {112throw new RuntimeException(113"proxy class does not have AllPermission");114}115116/*117* Verify that it has a constructor that takes an118* InvocationHandler instance.119*/120Constructor<?> cons = proxyClass.getConstructor(InvocationHandler.class);121122/*123* Test constructor with null InvocationHandler124*/125try {126cons.newInstance(new Object[] { null });127throw new RuntimeException("Expected NullPointerException thrown");128} catch (InvocationTargetException e) {129Throwable t = e.getTargetException();130if (!(t instanceof NullPointerException)) {131throw t;132}133}134135/*136* Construct a proxy instance.137*/138Handler handler = new Handler();139Object proxy = cons.newInstance(handler);140handler.currentProxy = proxy;141142/*143* Invoke a method on a proxy instance.144*/145Method m = Runnable.class.getMethod("run", null);146((Runnable) proxy).run();147if (!handler.lastMethod.equals(m)) {148throw new RuntimeException(149"proxy method invocation failure (lastMethod = " +150handler.lastMethod + ")");151}152153System.err.println("\nTEST PASSED");154155} catch (Throwable e) {156System.err.println("\nTEST FAILED:");157e.printStackTrace();158throw new RuntimeException("TEST FAILED: " + e.toString());159}160}161162public static class Handler implements InvocationHandler {163164Object currentProxy;165Method lastMethod;166167public Object invoke(Object proxy, Method method, Object[] args)168throws Throwable169{170if (proxy != currentProxy) {171throw new RuntimeException(172"wrong proxy instance passed to invoke method");173}174lastMethod = method;175return null;176}177}178}179180181