Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Method/invoke/IllegalAccessInInvoke.java
38889 views
/*1* Copyright (c) 1998, 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 410928925@summary Turning off access checks now enables illegal reflection.26@author Anand Palaniswamy27*/2829import java.lang.reflect.Method;3031/**32* Try to call a private method with Method.invoke(). If that doesn't33* throw a IllegalAccessException, then access checks are disabled,34* which is a bad idea.35*/36public class IllegalAccessInInvoke {37public static void main(String[] argv) {38Class[] argTypes = new Class[0];39Object[] args = new Object[0];40Method pm = null;4142try {43pm = Foo.class.getDeclaredMethod("privateMethod", argTypes);44} catch (NoSuchMethodException nsme) {45throw new46RuntimeException("Bizzare: privateMethod *must* be there");47}4849boolean ethrown = false;50try {51pm.invoke(new Foo(), args);52} catch (IllegalAccessException iae) {53ethrown = true;54} catch (Exception e) {55throw new RuntimeException("Unexpected " + e.toString());56}5758if (!ethrown) {59throw new60RuntimeException("Reflection access checks are disabled");61}62}63}6465class Foo {66private void privateMethod() {67}68}697071