test/jdk/java/io/Serializable/packageAccess/PackageAccessTest.java
changeset 58565 baa5969ecf34
parent 51977 a8862960c19f
equal deleted inserted replaced
58564:218a1a642c6f 58565:baa5969ecf34
     1 /*
     1 /*
     2  * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    48 
    48 
    49 import jdk.test.lib.util.JarUtils;
    49 import jdk.test.lib.util.JarUtils;
    50 
    50 
    51 public class PackageAccessTest {
    51 public class PackageAccessTest {
    52 
    52 
    53     static Class bcl;
    53     static Class<?> bcl;
    54     static Class dcl;
    54     static Class<?> dcl;
    55 
    55 
    56     public static void main(String[] args) throws Exception {
    56     public static void main(String[] args) throws Exception {
    57         setup();
    57         setup();
    58 
    58 
    59         try (URLClassLoader ldr =
    59         try (URLClassLoader ldr =
    60             new URLClassLoader(new URL[]{ new URL("file:foo.jar") },
    60             new URLClassLoader(new URL[]{ new URL("file:foo.jar") },
    61                     PackageAccessTest.class.getClassLoader())) {
    61                     PackageAccessTest.class.getClassLoader())) {
    62             bcl = Class.forName("B", true, ldr);
    62             bcl = Class.forName("B", true, ldr);
    63             dcl = Class.forName("D", true, ldr);
    63             dcl = Class.forName("D", true, ldr);
    64 
    64 
    65             Object b = bcl.newInstance();
    65             Object b = bcl.getConstructor().newInstance();
    66             try {
    66             try {
    67                 swizzle(b);
    67                 swizzle(b);
    68                 throw new Error("expected InvalidClassException for class B");
    68                 throw new Error("expected InvalidClassException for class B");
    69             } catch (InvalidClassException e) {
    69             } catch (InvalidClassException e) {
    70                 System.out.println("caught " + e);
    70                 System.out.println("caught " + e);
    72             }
    72             }
    73             if (A.packagePrivateConstructorInvoked) {
    73             if (A.packagePrivateConstructorInvoked) {
    74                 throw new Error("package private constructor of A invoked");
    74                 throw new Error("package private constructor of A invoked");
    75             }
    75             }
    76 
    76 
    77             Object d = dcl.newInstance();
    77             Object d = dcl.getConstructor().newInstance();
    78             swizzle(d);
    78             swizzle(d);
    79         }
    79         }
    80     }
    80     }
    81 
    81 
    82     static void swizzle(Object obj) throws Exception {
    82     static void swizzle(Object obj) throws Exception {
   101 class TestObjectInputStream extends ObjectInputStream {
   101 class TestObjectInputStream extends ObjectInputStream {
   102     TestObjectInputStream(InputStream in) throws IOException {
   102     TestObjectInputStream(InputStream in) throws IOException {
   103         super(in);
   103         super(in);
   104     }
   104     }
   105 
   105 
   106     protected Class resolveClass(ObjectStreamClass desc)
   106     protected Class<?> resolveClass(ObjectStreamClass desc)
   107         throws IOException, ClassNotFoundException
   107         throws IOException, ClassNotFoundException
   108     {
   108     {
   109         String n = desc.getName();
   109         String n = desc.getName();
   110         if (n.equals("B")) {
   110         if (n.equals("B")) {
   111             return PackageAccessTest.bcl;
   111             return PackageAccessTest.bcl;