Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/accessProtectedSuper/Test.java
47525 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 802271827* @summary Runtime accessibility checking: protected class, if extended, should be accessible from another package28*29* @compile -XDignore.symbol.file BogoLoader.java MethodInvoker.java Test.java anotherpkg/MethodSupplierOuter.java30* @run main/othervm Test31*/3233import java.lang.reflect.InvocationTargetException;34import java.lang.reflect.Method;35import java.util.HashMap;36import java.util.HashSet;37import jdk.internal.org.objectweb.asm.ClassWriter;38import jdk.internal.org.objectweb.asm.MethodVisitor;39import jdk.internal.org.objectweb.asm.ClassVisitor;40import jdk.internal.org.objectweb.asm.Opcodes;4142interface MyFunctionalInterface {4344void invokeMethodReference();45}4647class MakeProtected implements BogoLoader.VisitorMaker, Opcodes {4849final boolean whenVisitInner;5051MakeProtected(boolean when_visit_inner) {52super();53whenVisitInner = when_visit_inner;54}5556public ClassVisitor make(ClassVisitor cv) {57return new ClassVisitor(Opcodes.ASM5, cv) {5859@Override60public void visitInnerClass(String name, String outerName,61String innerName, int access) {62if (whenVisitInner) {63int access_ = (ACC_PROTECTED | access) & ~(ACC_PRIVATE | ACC_PUBLIC);64System.out.println("visitInnerClass: name = " + name65+ ", outerName = " + outerName66+ ", innerName = " + innerName67+ ", access original = 0x" + Integer.toHexString(access)68+ ", access modified to 0x" + Integer.toHexString(access_));69access = access_;70}71super.visitInnerClass(name, outerName, innerName, access);72}73};74}75};7677public class Test {7879public static void main(String argv[]) throws Exception, Throwable {80BogoLoader.VisitorMaker makeProtectedNop = new MakeProtected(false);81BogoLoader.VisitorMaker makeProtectedMod = new MakeProtected(true);8283int errors = 0;84errors += tryModifiedInvocation(makeProtectedNop);85errors += tryModifiedInvocation(makeProtectedMod);8687if (errors > 0) {88throw new Error("FAIL; there were errors");89}90}9192private static int tryModifiedInvocation(BogoLoader.VisitorMaker makeProtected)93throws Throwable, ClassNotFoundException {94HashMap<String, BogoLoader.VisitorMaker> replace95= new HashMap<String, BogoLoader.VisitorMaker>();96replace.put("anotherpkg.MethodSupplierOuter$MethodSupplier", makeProtected);97HashSet<String> in_bogus = new HashSet<String>();98in_bogus.add("MethodInvoker");99in_bogus.add("MyFunctionalInterface");100in_bogus.add("anotherpkg.MethodSupplierOuter"); // seems to be never loaded101in_bogus.add("anotherpkg.MethodSupplierOuter$MethodSupplier");102103BogoLoader bl = new BogoLoader(in_bogus, replace);104try {105Class<?> isw = bl.loadClass("MethodInvoker");106Method meth = isw.getMethod("invoke");107Object result = meth.invoke(null);108} catch (Throwable th) {109System.out.flush();110Thread.sleep(250); // Let Netbeans get its I/O sorted out.111th.printStackTrace();112System.err.flush();113Thread.sleep(250); // Let Netbeans get its I/O sorted out.114return 1;115}116return 0;117}118}119120121