Path: blob/master/test/hotspot/jtreg/compiler/exceptions/ExceptionInInit.java
41152 views
/*1* Copyright (c) 2018, 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/*24* @test25* @bug 416597326* @summary Attempt to read inaccessible property can produce27* exception of the wrong type.28* @author Tom Rodriguez29*30* @modules java.rmi31* @run main/othervm -Djava.security.manager=allow compiler.exceptions.ExceptionInInit32*/3334package compiler.exceptions;3536import java.security.AccessController;37import java.security.PrivilegedAction;3839public class ExceptionInInit {4041public static void main(String[] args) {4243Test test = null;4445try {46System.setSecurityManager(new java.rmi.RMISecurityManager());47Test.showTest();48} catch (ExceptionInInitializerError e) {49}50}5152public static class FooBar {53static String test = "test";54FooBar(String test) {55this.test = test;56}57}5859public static class Test extends FooBar {6061/*62* An AccessControlException is thrown in the static initializer of the63* class FooBar. This exception should produce an ExceptionInInitializer64* error. Instead it causes a more cryptic ClassNotFound error.65*66* The following is an excerpt from the output from java.security.debug=all67*68* access: access denied (java.util.PropertyPermission test.src read)69* java.lang.Exception: Stack trace70* at java.lang.Thread.dumpStack(Thread.java:938)71* at java.security.AccessControlContext.checkPermission(AccessControlContext.java:184)72* at java.security.AccessController.checkPermission(AccessController.java:402)73* at java.lang.SecurityManager.checkPermission(SecurityManager.java:516)74* at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1035)75* at java.lang.System.getProperty(System.java:441)76* at sun.security.action.GetPropertyAction.run(GetPropertyAction.java:73)77* at java.security.AccessController.doPrivileged(Native Method)78* at ExceptionInInit$Test.<clinit>(ExceptionInInit.java:33)79* at ExceptionInInit.main(ExceptionInInit.java:18)80* access: domain that failed ProtectionDomain (file:/tmp/exceptionInInit/<no certificates>)81*82* The following exception is occurring when this test program tries83* to access the test.src property.84*/85private static String test =86AccessController.doPrivileged((PrivilegedAction<String>)() -> System.getProperty("test.src", "."));8788Test(String test) {89super(test);90}91public static void showTest() {92System.err.println(test);93}94}95}969798