Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/ObjectInputStream/ResolveProxyClass.java
38811 views
/*1* Copyright (c) 1999, 2010, 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 425864425* @summary ObjectInputStream's default implementation of its protected26* resolveProxyClass method is specified to pass the first non-null class27* loader up the execution stack to the Proxy.getProxyClass method when28* it creates the specified proxy class; this test makes sure that it does29* that in situations where it hadn't in the past, such as if the defining30* loaders of the interfaces were all strict ancestors of the first31* non-null loader up the stack.32* @author Peter Jones33*34* @build ResolveProxyClass35* @run main ResolveProxyClass36*/3738import java.lang.reflect.*;39import java.io.*;4041public class ResolveProxyClass {4243/*44* This class is a dummy ObjectInputStream subclass that allows the45* test code to access ObjectInputStream's protected resolveProxyClass46* method directly.47*/48private static class TestObjectInputStream extends ObjectInputStream {4950TestObjectInputStream() throws IOException {51super();52}5354protected Class resolveProxyClass(String[] interfaces)55throws IOException, ClassNotFoundException56{57return super.resolveProxyClass(interfaces);58}59}6061public static void main(String[] args) {6263System.err.println("\nRegression test for bug 4258644\n");6465try {6667/*68* Set this thread's context class loader to null, so that the69* resolveProxyClass implementation cannot cheat by guessing that70* the context class loader is the appropriate loader to pass to71* the Proxy.getProxyClass method.72*/73Thread.currentThread().setContextClassLoader(null);7475/*76* Expect the proxy class to be defined in the system class77* loader, because that is the defining loader of this test78* code, and it should be the first loader on the stack when79* ObjectInputStream.resolveProxyClass gets executed.80*/81ClassLoader expectedLoader = ResolveProxyClass.class.getClassLoader();8283TestObjectInputStream in = new TestObjectInputStream();84Class proxyClass = in.resolveProxyClass(85new String[] { Runnable.class.getName() });86ClassLoader proxyLoader = proxyClass.getClassLoader();87System.err.println("proxy class \"" + proxyClass +88"\" defined in loader: " + proxyLoader);8990if (proxyLoader != expectedLoader) {91throw new RuntimeException(92"proxy class defined in loader: " + proxyLoader);93}9495System.err.println("\nTEST PASSED");9697} catch (Throwable e) {98System.err.println("\nTEST FAILED:");99e.printStackTrace();100throw new RuntimeException("TEST FAILED: " + e.toString());101}102}103}104105106