J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @bug 4765255 |
| 26 | * @summary Verify proper functioning of package equality checks used to |
| 27 | * determine accessibility of superclass constructor and inherited |
| 28 | * writeReplace/readResolve methods. |
| 29 | */ |
| 30 | |
| 31 | import java.io.*; |
| 32 | import java.net.*; |
| 33 | |
| 34 | public class Test { |
| 35 | |
| 36 | static Class bcl; |
| 37 | static Class dcl; |
| 38 | |
| 39 | public static void main(String[] args) throws Exception { |
| 40 | ClassLoader ldr = |
| 41 | new URLClassLoader(new URL[]{ new URL("file:foo.jar") }); |
| 42 | bcl = Class.forName("B", true, ldr); |
| 43 | dcl = Class.forName("D", true, ldr); |
| 44 | |
| 45 | Object b = bcl.newInstance(); |
| 46 | try { |
| 47 | swizzle(b); |
| 48 | throw new Error("expected InvalidClassException for class B"); |
| 49 | } catch (InvalidClassException e) { |
| 50 | System.out.println("caught " + e); |
| 51 | e.printStackTrace(); |
| 52 | } |
| 53 | if (A.packagePrivateConstructorInvoked) { |
| 54 | throw new Error("package private constructor of A invoked"); |
| 55 | } |
| 56 | |
| 57 | Object d = dcl.newInstance(); |
| 58 | swizzle(d); |
| 59 | } |
| 60 | |
| 61 | static void swizzle(Object obj) throws Exception { |
| 62 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 63 | ObjectOutputStream oout = new ObjectOutputStream(bout); |
| 64 | oout.writeObject(obj); |
| 65 | oout.close(); |
| 66 | ByteArrayInputStream bin = |
| 67 | new ByteArrayInputStream(bout.toByteArray()); |
| 68 | new TestObjectInputStream(bin).readObject(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | class TestObjectInputStream extends ObjectInputStream { |
| 73 | TestObjectInputStream(InputStream in) throws IOException { |
| 74 | super(in); |
| 75 | } |
| 76 | |
| 77 | protected Class resolveClass(ObjectStreamClass desc) |
| 78 | throws IOException, ClassNotFoundException |
| 79 | { |
| 80 | String n = desc.getName(); |
| 81 | if (n.equals("B")) { |
| 82 | return Test.bcl; |
| 83 | } else if (n.equals("D")) { |
| 84 | return Test.dcl; |
| 85 | } else { |
| 86 | return super.resolveClass(desc); |
| 87 | } |
| 88 | } |
| 89 | } |