Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/8022701/MHIllegalAccess.java
48283 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*22*/2324/**25* @test26* @bug 802270127* @summary Illegal access exceptions via methodhandle invocations threw wrong error.28*29* @compile -XDignore.symbol.file BogoLoader.java InvokeSeveralWays.java MHIllegalAccess.java MethodSupplier.java30* @run main/othervm MHIllegalAccess31*/3233import java.lang.reflect.InvocationTargetException;34import java.util.HashMap;35import java.util.HashSet;36import jdk.internal.org.objectweb.asm.ClassWriter;37import jdk.internal.org.objectweb.asm.MethodVisitor;38import jdk.internal.org.objectweb.asm.ClassVisitor;39import jdk.internal.org.objectweb.asm.Opcodes;4041public class MHIllegalAccess implements Opcodes {4243public static void main(String args[]) throws Throwable {44System.out.println("Classpath is " + System.getProperty("java.class.path"));45System.out.println();4647/**48* Make method m be private to provoke an IllegalAccessError.49*/50BogoLoader.VisitorMaker privatize = new BogoLoader.VisitorMaker() {51public ClassVisitor make(ClassVisitor cv) {52return new ClassVisitor(Opcodes.ASM5, cv) {53public MethodVisitor visitMethod(int access, String name, String desc,54String signature, String[] exceptions) {55if (name.equals("m"))56access = (access | ACC_PRIVATE) & ~ (ACC_PUBLIC | ACC_PROTECTED);57return super.visitMethod(access, name, desc, signature, exceptions);58}59};60}61};6263/**64* Rename method m as nemo to provoke a NoSuchMethodError.65*/66BogoLoader.VisitorMaker changeName = new BogoLoader.VisitorMaker() {67public ClassVisitor make(ClassVisitor cv) {68return new ClassVisitor(Opcodes.ASM5, cv) {69public MethodVisitor visitMethod(int access, String name, String desc,70String signature, String[] exceptions) {71if (name.equals("m"))72name = "nemo";73return super.visitMethod(access, name, desc, signature, exceptions);74}75};76}77};7879int failures = 0;80failures += testOneError(privatize, args, IllegalAccessError.class);81failures += testOneError(changeName, args, NoSuchMethodError.class);82if (failures > 0) {83System.out.println("Saw " + failures + " failures, see standard out for details");84throw new Error("FAIL test");85}86}8788/**89*90* @param vm VisitorMaker, to be stored in a table and passed to a BogoLoader91* @param args A copy of the main args, to be passed on to InvokeSeveralWays.test92* @param expected The class of the exception that should be thrown after93* attempted invocation of MethodSupplier.m.94* @throws ClassNotFoundException95* @throws Throwable96*/97private static int testOneError(BogoLoader.VisitorMaker vm, String[] args, Class expected) throws ClassNotFoundException, Throwable {98HashMap<String, BogoLoader.VisitorMaker> replace = new HashMap<String, BogoLoader.VisitorMaker>();99replace.put("MethodSupplier", vm);100101HashSet<String> in_bogus = new HashSet<String>();102in_bogus.add("InvokeSeveralWays");103in_bogus.add("MyFunctionalInterface");104in_bogus.add("Invoker");105106BogoLoader bl = new BogoLoader(in_bogus, replace);107Class<?> isw = bl.loadClass("InvokeSeveralWays");108Object[] arg_for_args = new Object[2];109arg_for_args[0] = args;110arg_for_args[1] = expected;111try {112Object result = isw.getMethod("test", String[].class, Class.class).invoke(null, arg_for_args);113return (Integer)result;114} catch (InvocationTargetException e) {115Throwable th = e.getCause();116throw th == null ? e : th;117}118}119}120121122