jdk/test/java/io/Serializable/packageAccess/Test.java
changeset 45319 faf6c48f1f71
parent 45318 50e95c11aa99
parent 45314 d50641964075
child 45320 eb60a37a5b98
equal deleted inserted replaced
45318:50e95c11aa99 45319:faf6c48f1f71
     1 /*
       
     2  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * 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                                Test.class.getClassLoader());
       
    43         bcl = Class.forName("B", true, ldr);
       
    44         dcl = Class.forName("D", true, ldr);
       
    45 
       
    46         Object b = bcl.newInstance();
       
    47         try {
       
    48             swizzle(b);
       
    49             throw new Error("expected InvalidClassException for class B");
       
    50         } catch (InvalidClassException e) {
       
    51             System.out.println("caught " + e);
       
    52             e.printStackTrace();
       
    53         }
       
    54         if (A.packagePrivateConstructorInvoked) {
       
    55             throw new Error("package private constructor of A invoked");
       
    56         }
       
    57 
       
    58         Object d = dcl.newInstance();
       
    59         swizzle(d);
       
    60     }
       
    61 
       
    62     static void swizzle(Object obj) throws Exception {
       
    63         ByteArrayOutputStream bout = new ByteArrayOutputStream();
       
    64         ObjectOutputStream oout = new ObjectOutputStream(bout);
       
    65         oout.writeObject(obj);
       
    66         oout.close();
       
    67         ByteArrayInputStream bin =
       
    68             new ByteArrayInputStream(bout.toByteArray());
       
    69         new TestObjectInputStream(bin).readObject();
       
    70     }
       
    71 }
       
    72 
       
    73 class TestObjectInputStream extends ObjectInputStream {
       
    74     TestObjectInputStream(InputStream in) throws IOException {
       
    75         super(in);
       
    76     }
       
    77 
       
    78     protected Class resolveClass(ObjectStreamClass desc)
       
    79         throws IOException, ClassNotFoundException
       
    80     {
       
    81         String n = desc.getName();
       
    82         if (n.equals("B")) {
       
    83             return Test.bcl;
       
    84         } else if (n.equals("D")) {
       
    85             return Test.dcl;
       
    86         } else {
       
    87             return super.resolveClass(desc);
       
    88         }
       
    89     }
       
    90 }