Path: blob/master/test/jdk/java/io/ObjectInputStream/ResolveProxyClass.java
41149 views
/*1* Copyright (c) 1999, 2019, 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.io.*;3940public class ResolveProxyClass {4142/*43* This class is a dummy ObjectInputStream subclass that allows the44* test code to access ObjectInputStream's protected resolveProxyClass45* method directly.46*/47private static class TestObjectInputStream extends ObjectInputStream {4849TestObjectInputStream() throws IOException {50super();51}5253protected Class<?> resolveProxyClass(String[] interfaces)54throws IOException, ClassNotFoundException55{56return super.resolveProxyClass(interfaces);57}58}5960public static void main(String[] args) {6162System.err.println("\nRegression test for bug 4258644\n");6364try {6566/*67* Set this thread's context class loader to null, so that the68* resolveProxyClass implementation cannot cheat by guessing that69* the context class loader is the appropriate loader to pass to70* the Proxy.getProxyClass method.71*/72Thread.currentThread().setContextClassLoader(null);7374/*75* Expect the proxy class to be defined in the system class76* loader, because that is the defining loader of this test77* code, and it should be the first loader on the stack when78* ObjectInputStream.resolveProxyClass gets executed.79*/80ClassLoader expectedLoader = ResolveProxyClass.class.getClassLoader();8182TestObjectInputStream in = new TestObjectInputStream();83Class<?> proxyClass = in.resolveProxyClass(84new String[] { Runnable.class.getName() });85ClassLoader proxyLoader = proxyClass.getClassLoader();86System.err.println("proxy class \"" + proxyClass +87"\" defined in loader: " + proxyLoader);8889if (proxyLoader != expectedLoader) {90throw new RuntimeException(91"proxy class defined in loader: " + proxyLoader);92}9394System.err.println("\nTEST PASSED");9596} catch (Throwable e) {97System.err.println("\nTEST FAILED:");98e.printStackTrace();99throw new RuntimeException("TEST FAILED: " + e.toString());100}101}102}103104105