Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Method/invoke/TestPrivateInterfaceMethodReflect.java
38889 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 802621326* @summary Reflection support for private methods in interfaces27* @author Robert Field28* @run main TestPrivateInterfaceMethodReflect29*/3031import java.lang.reflect.*;3233import jdk.internal.org.objectweb.asm.ClassWriter;34import jdk.internal.org.objectweb.asm.MethodVisitor;35import jdk.internal.org.objectweb.asm.Opcodes;3637public class TestPrivateInterfaceMethodReflect {3839static final String INTERFACE_NAME = "PrivateInterfaceMethodReflectTest_Interface";40static final String CLASS_NAME = "PrivateInterfaceMethodReflectTest_Class";41static final int EXPECTED = 1234;4243static class TestClassLoader extends ClassLoader implements Opcodes {4445@Override46public Class findClass(String name) throws ClassNotFoundException {47byte[] b;48try {49b = loadClassData(name);50} catch (Throwable th) {51// th.printStackTrace();52throw new ClassNotFoundException("Loading error", th);53}54return defineClass(name, b, 0, b.length);55}5657private byte[] loadClassData(String name) throws Exception {58ClassWriter cw = new ClassWriter(0);59MethodVisitor mv;60switch (name) {61case INTERFACE_NAME:62cw.visit(V1_8, ACC_ABSTRACT | ACC_INTERFACE | ACC_PUBLIC, INTERFACE_NAME, null, "java/lang/Object", null);63{64mv = cw.visitMethod(ACC_PRIVATE, "privInstance", "()I", null, null);65mv.visitCode();66mv.visitLdcInsn(EXPECTED);67mv.visitInsn(IRETURN);68mv.visitMaxs(1, 1);69mv.visitEnd();70}71break;72case CLASS_NAME:73cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME, null, "java/lang/Object", new String[]{INTERFACE_NAME});74{75mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);76mv.visitCode();77mv.visitVarInsn(ALOAD, 0);78mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");79mv.visitInsn(RETURN);80mv.visitMaxs(1, 1);81mv.visitEnd();82}83break;84default:85break;86}87cw.visitEnd();8889return cw.toByteArray();90}91}9293public static void main(String[] args) throws Exception {94TestClassLoader tcl = new TestClassLoader();95Class<?> itf = tcl.loadClass(INTERFACE_NAME);96Class<?> k = tcl.loadClass(CLASS_NAME);97Object inst = k.newInstance();98Method[] meths = itf.getDeclaredMethods();99if (meths.length != 1) {100throw new Exception("Expected one method in " + INTERFACE_NAME + " instead " + meths.length);101}102103Method m = meths[0];104int mod = m.getModifiers();105if ((mod & Modifier.PRIVATE) == 0) {106throw new Exception("Expected " + m + " to be private");107}108if ((mod & Modifier.STATIC) != 0) {109throw new Exception("Expected " + m + " to be instance method");110}111112m.setAccessible(true);113for (int i = 1; i < 200; i++) {114if (!m.invoke(inst).equals(EXPECTED)) {115throw new Exception("Expected " + EXPECTED + " from " + m);116}117}118119System.out.println("Passed.");120}121}122123124