Path: blob/master/test/jdk/java/lang/invoke/8022701/MHIllegalAccess.java
41153 views
/*1* Copyright (c) 2013, 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 802270126* @summary Illegal access exceptions via methodhandle invocations threw wrong error.27* @modules java.base/jdk.internal.org.objectweb.asm28* @compile -XDignore.symbol.file BogoLoader.java InvokeSeveralWays.java MHIllegalAccess.java MethodSupplier.java29* @run main/othervm MHIllegalAccess30*/3132import java.lang.reflect.InvocationTargetException;33import java.util.HashMap;34import java.util.HashSet;35import jdk.internal.org.objectweb.asm.ClassWriter;36import jdk.internal.org.objectweb.asm.MethodVisitor;37import jdk.internal.org.objectweb.asm.ClassVisitor;38import jdk.internal.org.objectweb.asm.Opcodes;3940public class MHIllegalAccess implements Opcodes {4142public static void main(String args[]) throws Throwable {43System.out.println("Classpath is " + System.getProperty("java.class.path"));44System.out.println();4546/**47* Make method m be private to provoke an IllegalAccessError.48*/49BogoLoader.VisitorMaker privatize = new BogoLoader.VisitorMaker() {50public ClassVisitor make(ClassVisitor cv) {51return new ClassVisitor(Opcodes.ASM5, cv) {52public MethodVisitor visitMethod(int access, String name, String desc,53String signature, String[] exceptions) {54if (name.equals("m"))55access = (access | ACC_PRIVATE) & ~ (ACC_PUBLIC | ACC_PROTECTED);56return super.visitMethod(access, name, desc, signature, exceptions);57}58};59}60};6162/**63* Rename method m as nemo to provoke a NoSuchMethodError.64*/65BogoLoader.VisitorMaker changeName = new BogoLoader.VisitorMaker() {66public ClassVisitor make(ClassVisitor cv) {67return new ClassVisitor(Opcodes.ASM5, cv) {68public MethodVisitor visitMethod(int access, String name, String desc,69String signature, String[] exceptions) {70if (name.equals("m"))71name = "nemo";72return super.visitMethod(access, name, desc, signature, exceptions);73}74};75}76};7778int failures = 0;79failures += testOneError(privatize, args, IllegalAccessError.class);80failures += testOneError(changeName, args, NoSuchMethodError.class);81if (failures > 0) {82System.out.println("Saw " + failures + " failures, see standard out for details");83throw new Error("FAIL test");84}85}8687/**88*89* @param vm VisitorMaker, to be stored in a table and passed to a BogoLoader90* @param args A copy of the main args, to be passed on to InvokeSeveralWays.test91* @param expected The class of the exception that should be thrown after92* attempted invocation of MethodSupplier.m.93* @throws ClassNotFoundException94* @throws Throwable95*/96private static int testOneError(BogoLoader.VisitorMaker vm, String[] args, Class expected) throws ClassNotFoundException, Throwable {97HashMap<String, BogoLoader.VisitorMaker> replace = new HashMap<String, BogoLoader.VisitorMaker>();98replace.put("MethodSupplier", vm);99100HashSet<String> in_bogus = new HashSet<String>();101in_bogus.add("InvokeSeveralWays");102in_bogus.add("MyFunctionalInterface");103in_bogus.add("Invoker");104105BogoLoader bl = new BogoLoader(in_bogus, replace);106Class<?> isw = bl.loadClass("InvokeSeveralWays");107Object[] arg_for_args = new Object[2];108arg_for_args[0] = args;109arg_for_args[1] = expected;110try {111Object result = isw.getMethod("test", String[].class, Class.class).invoke(null, arg_for_args);112return (Integer)result;113} catch (InvocationTargetException e) {114Throwable th = e.getCause();115throw th == null ? e : th;116}117}118}119120121