5459N/A/*
5459N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
5459N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5459N/A *
5459N/A * This code is free software; you can redistribute it and/or modify it
5459N/A * under the terms of the GNU General Public License version 2 only, as
5459N/A * published by the Free Software Foundation. Oracle designates this
5459N/A * particular file as subject to the "Classpath" exception as provided
5459N/A * by Oracle in the LICENSE file that accompanied this code.
5459N/A *
5459N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5459N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5459N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5459N/A * version 2 for more details (a copy is included in the LICENSE file that
5459N/A * accompanied this code).
5459N/A *
5459N/A * You should have received a copy of the GNU General Public License version
5459N/A * 2 along with this work; if not, write to the Free Software Foundation,
5459N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5459N/A *
5459N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5459N/A * or visit www.oracle.com if you need additional information or have any
5459N/A * questions.
5459N/A */
5459N/A
5459N/A/* @test
5459N/A * @summary white-box testing of method handle sub-primitives
5459N/A * @run junit test.java.lang.invoke.PrivateInvokeTest
5459N/A */
5459N/A
5459N/Apackage test.java.lang.invoke;
5459N/A
5459N/Aimport java.lang.invoke.*;
5459N/Aimport static java.lang.invoke.MethodHandles.*;
5459N/Aimport static java.lang.invoke.MethodType.*;
5459N/Aimport java.lang.reflect.*;
5459N/Aimport java.util.ArrayList;
5459N/Aimport java.util.Arrays;
5459N/Aimport java.util.logging.Level;
5459N/Aimport java.util.logging.Logger;
5459N/Aimport org.junit.*;
5459N/Aimport static org.junit.Assert.*;
5459N/A
5459N/Apublic class PrivateInvokeTest {
5459N/A // Utility functions
5459N/A private static final Lookup LOOKUP = lookup();
5459N/A private static final Class<?> THIS_CLASS = PrivateInvokeTest.class;
5459N/A private static final int
5459N/A REF_NONE = 0, // null value
5459N/A REF_getField = 1,
5459N/A REF_getStatic = 2,
5459N/A REF_putField = 3,
5459N/A REF_putStatic = 4,
5459N/A REF_invokeVirtual = 5,
5459N/A REF_invokeStatic = 6,
5459N/A REF_invokeSpecial = 7,
5459N/A REF_newInvokeSpecial = 8,
5459N/A REF_invokeInterface = 9,
5459N/A REF_LIMIT = 10,
5459N/A REF_MH_invokeBasic = REF_NONE;;
5459N/A private static final String[] REF_KIND_NAMES = {
5459N/A "MH::invokeBasic",
5459N/A "REF_getField", "REF_getStatic", "REF_putField", "REF_putStatic",
5459N/A "REF_invokeVirtual", "REF_invokeStatic", "REF_invokeSpecial",
5459N/A "REF_newInvokeSpecial", "REF_invokeInterface"
5459N/A };
5459N/A private int verbose;
5459N/A //{ verbose = 99; } // for debugging
5459N/A {
5459N/A String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbose");
5459N/A if (vstr == null)
5459N/A vstr = System.getProperty(THIS_CLASS.getName()+".verbose");
5459N/A if (vstr == null)
5459N/A vstr = System.getProperty("test.verbose");
5459N/A if (vstr != null) verbose = Integer.parseInt(vstr);
5459N/A }
5459N/A private static int referenceKind(Method m) {
5459N/A if (Modifier.isStatic(m.getModifiers()))
5459N/A return REF_invokeStatic;
5459N/A else if (m.getDeclaringClass().isInterface())
5459N/A return REF_invokeInterface;
5459N/A else if (Modifier.isFinal(m.getModifiers()) ||
5459N/A Modifier.isFinal(m.getDeclaringClass().getModifiers()))
5459N/A return REF_invokeSpecial;
5459N/A else
5459N/A return REF_invokeVirtual;
5459N/A }
5459N/A private static MethodType basicType(MethodType mtype) {
5459N/A MethodType btype = mtype.erase();
5459N/A if (btype.hasPrimitives()) {
5459N/A for (int i = -1; i < mtype.parameterCount(); i++) {
5459N/A Class<?> type = (i < 0 ? mtype.returnType() : mtype.parameterType(i));
5459N/A if (type == boolean.class ||
5459N/A type == byte.class ||
5459N/A type == char.class ||
5459N/A type == short.class) {
5459N/A type = int.class;
5459N/A if (i < 0)
5459N/A btype = btype.changeReturnType(type);
5459N/A else
5459N/A btype = btype.changeParameterType(i, type);
5459N/A }
5459N/A }
5459N/A }
5459N/A return btype;
5459N/A }
5459N/A private static Method getMethod(Class<?> defc, String name, Class<?>... ptypes) {
5459N/A try {
5459N/A return defc.getDeclaredMethod(name, ptypes);
5459N/A } catch (NoSuchMethodException ex) {
5459N/A }
5459N/A try {
5459N/A return defc.getMethod(name, ptypes);
5459N/A } catch (NoSuchMethodException ex) {
5459N/A throw new IllegalArgumentException(ex);
5459N/A }
5459N/A }
5459N/A private static MethodHandle unreflect(Method m) {
5459N/A try {
5459N/A MethodHandle mh = LOOKUP.unreflect(m);
5459N/A if (Modifier.isTransient(m.getModifiers()))
5459N/A mh = mh.asFixedArity(); // remove varargs wrapper
5459N/A return mh;
5459N/A } catch (IllegalAccessException ex) {
5459N/A throw new IllegalArgumentException(ex);
5459N/A }
5459N/A }
5459N/A private static final Lookup DIRECT_INVOKER_LOOKUP;
5459N/A private static final Class<?> MEMBER_NAME_CLASS;
5459N/A private static final MethodHandle MH_INTERNAL_MEMBER_NAME;
5459N/A private static final MethodHandle MH_DEBUG_STRING;
5459N/A static {
5459N/A try {
5459N/A // This is white box testing. Use reflection to grab private implementation bits.
5459N/A String magicName = "IMPL_LOOKUP";
5459N/A Field magicLookup = MethodHandles.Lookup.class.getDeclaredField(magicName);
5459N/A // This unit test will fail if a security manager is installed.
5459N/A magicLookup.setAccessible(true);
5459N/A // Forbidden fruit...
5459N/A DIRECT_INVOKER_LOOKUP = (Lookup) magicLookup.get(null);
5459N/A MEMBER_NAME_CLASS = Class.forName("java.lang.invoke.MemberName", false, MethodHandle.class.getClassLoader());
5459N/A MH_INTERNAL_MEMBER_NAME = DIRECT_INVOKER_LOOKUP
5459N/A .findVirtual(MethodHandle.class, "internalMemberName", methodType(MEMBER_NAME_CLASS))
5459N/A .asType(methodType(Object.class, MethodHandle.class));
5459N/A MH_DEBUG_STRING = DIRECT_INVOKER_LOOKUP
5459N/A .findVirtual(MethodHandle.class, "debugString", methodType(String.class));
5459N/A } catch (ReflectiveOperationException ex) {
5465N/A throw new Error(ex);
5459N/A }
5459N/A }
5459N/A private Object internalMemberName(MethodHandle mh) {
5459N/A try {
5459N/A return MH_INTERNAL_MEMBER_NAME.invokeExact(mh);
5459N/A } catch (Throwable ex) {
5465N/A throw new Error(ex);
5459N/A }
5459N/A }
5459N/A private String debugString(MethodHandle mh) {
5459N/A try {
5459N/A return (String) MH_DEBUG_STRING.invokeExact(mh);
5459N/A } catch (Throwable ex) {
5465N/A throw new Error(ex);
5459N/A }
5459N/A }
5459N/A private static MethodHandle directInvoker(int refKind, MethodType mtype) {
5459N/A return directInvoker(REF_KIND_NAMES[refKind], mtype);
5459N/A }
5459N/A private static MethodHandle directInvoker(String name, MethodType mtype) {
5459N/A boolean isStatic;
5459N/A mtype = mtype.erase();
5459N/A if (name.startsWith("MH::")) {
5459N/A isStatic = false;
5459N/A name = strip("MH::", name);
5459N/A } else if (name.startsWith("REF_")) {
5459N/A isStatic = true;
5459N/A name = strip("REF_", name);
5459N/A if (name.startsWith("invoke"))
5459N/A name = "linkTo"+strip("invoke", name);
5459N/A mtype = mtype.appendParameterTypes(MEMBER_NAME_CLASS);
5459N/A } else {
5459N/A throw new AssertionError("name="+name);
5459N/A }
5459N/A //System.out.println("directInvoker = "+name+mtype);
5459N/A try {
5459N/A if (isStatic)
5459N/A return DIRECT_INVOKER_LOOKUP
5459N/A .findStatic(MethodHandle.class, name, mtype);
5459N/A else
5459N/A return DIRECT_INVOKER_LOOKUP
5459N/A .findVirtual(MethodHandle.class, name, mtype);
5459N/A } catch (ReflectiveOperationException ex) {
5459N/A throw new IllegalArgumentException(ex);
5459N/A }
5459N/A }
5459N/A private Object invokeWithArguments(Method m, Object... args) {
5459N/A Object recv = null;
5459N/A if (!Modifier.isStatic(m.getModifiers())) {
5459N/A recv = args[0];
5459N/A args = pop(1, args);
5459N/A }
5459N/A try {
5459N/A return m.invoke(recv, args);
5459N/A } catch (IllegalAccessException|IllegalArgumentException|InvocationTargetException ex) {
5459N/A throw new IllegalArgumentException(ex);
5459N/A }
5459N/A }
5459N/A private Object invokeWithArguments(MethodHandle mh, Object... args) {
5459N/A try {
5459N/A return mh.invokeWithArguments(args);
5459N/A } catch (Throwable ex) {
5459N/A throw new IllegalArgumentException(ex);
5459N/A }
5459N/A }
5459N/A private int counter;
5459N/A private Object makeArgument(Class<?> type) {
5459N/A final String cname = type.getSimpleName();
5459N/A final int n = ++counter;
5459N/A final int nn = (n << 10) + 13;
5459N/A if (type.isAssignableFrom(String.class)) {
5459N/A return "<"+cname+"#"+nn+">";
5459N/A }
5459N/A if (type == THIS_CLASS) return this.withCounter(nn);
5459N/A if (type == Integer.class || type == int.class) return nn;
5459N/A if (type == Character.class || type == char.class) return (char)(n % 100+' ');
5459N/A if (type == Byte.class || type == byte.class) return (byte)-(n % 100);
5459N/A if (type == Long.class || type == long.class) return (long)nn;
5459N/A throw new IllegalArgumentException("don't know how to make argument of type: "+type);
5459N/A }
5459N/A private Object[] makeArguments(Class<?>... ptypes) {
5459N/A Object[] args = new Object[ptypes.length];
5459N/A for (int i = 0; i < args.length; i++)
5459N/A args[i] = makeArgument(ptypes[i]);
5459N/A return args;
5459N/A }
5459N/A private Object[] makeArguments(MethodType mtype) {
5459N/A return makeArguments(mtype.parameterArray());
5459N/A }
5459N/A private Object[] pop(int n, Object[] args) {
5459N/A if (n >= 0)
5459N/A return Arrays.copyOfRange(args, n, args.length);
5459N/A else
5459N/A return Arrays.copyOfRange(args, 0, args.length+n);
5459N/A }
5459N/A private Object[] pushAtFront(Object arg1, Object[] args) {
5459N/A Object[] res = new Object[1+args.length];
5459N/A res[0] = arg1;
5459N/A System.arraycopy(args, 0, res, 1, args.length);
5459N/A return res;
5459N/A }
5459N/A private Object[] pushAtBack(Object[] args, Object argN) {
5459N/A Object[] res = new Object[1+args.length];
5459N/A System.arraycopy(args, 0, res, 0, args.length);
5459N/A res[args.length] = argN;
5459N/A return res;
5459N/A }
5459N/A private static String strip(String prefix, String s) {
5459N/A assert(s.startsWith(prefix));
5459N/A return s.substring(prefix.length());
5459N/A }
5459N/A
5459N/A private final int[] refKindTestCounts = new int[REF_KIND_NAMES.length];
5459N/A @After
5459N/A public void printCounts() {
5459N/A ArrayList<String> zeroes = new ArrayList<>();
5459N/A for (int i = 0; i < refKindTestCounts.length; i++) {
5459N/A final int count = refKindTestCounts[i];
5459N/A final String name = REF_KIND_NAMES[i];
5459N/A if (count == 0) {
5459N/A if (name != null) zeroes.add(name);
5459N/A continue;
5459N/A }
5459N/A if (verbose >= 0)
5459N/A System.out.println("test count for "+name+" : "+count);
5459N/A else if (name != null)
5459N/A zeroes.add(name);
5459N/A }
5459N/A if (verbose >= 0)
5459N/A System.out.println("test counts zero for "+zeroes);
5459N/A }
5459N/A
5459N/A // Test subjects
5459N/A public static String makeString(Object x) { return "makeString("+x+")"; }
5459N/A public static String dupString(String x) { return "("+x+"+"+x+")"; }
5459N/A public static String intString(int x) { return "intString("+x+")"; }
5459N/A public static String byteString(byte x) { return "byteString("+x+")"; }
5459N/A public static String longString(String x, long y, String z) { return "longString("+x+y+z+")"; }
5459N/A
5459N/A public final String toString() {
5459N/A return "<"+getClass().getSimpleName()+"#"+counter+">";
5459N/A }
5459N/A public final String hello() { return "hello from "+this; }
5459N/A private PrivateInvokeTest withCounter(int counter) {
5459N/A PrivateInvokeTest res = new PrivateInvokeTest();
5459N/A res.counter = counter;
5459N/A return res;
5459N/A }
5459N/A
5459N/A public static void main(String... av) throws Throwable {
5459N/A new PrivateInvokeTest().run();
5459N/A }
5459N/A public void run() throws Throwable {
5459N/A testFirst();
5459N/A testInvokeDirect();
5459N/A }
5459N/A
5459N/A @Test
5459N/A public void testFirst() throws Throwable {
5459N/A if (true) return; // nothing here
5459N/A try {
5459N/A System.out.println("start of testFirst");
5459N/A } finally {
5459N/A System.out.println("end of testFirst");
5459N/A }
5459N/A }
5459N/A
5459N/A @Test
5459N/A public void testInvokeDirect() {
5459N/A testInvokeDirect(getMethod(THIS_CLASS, "hello"));
5459N/A testInvokeDirect(getMethod(Object.class, "toString"));
5459N/A testInvokeDirect(getMethod(Comparable.class, "compareTo", Object.class));
5459N/A testInvokeDirect(getMethod(THIS_CLASS, "makeString", Object.class));
5459N/A testInvokeDirect(getMethod(THIS_CLASS, "dupString", String.class));
5459N/A testInvokeDirect(getMethod(THIS_CLASS, "intString", int.class));
5459N/A testInvokeDirect(getMethod(THIS_CLASS, "byteString", byte.class));
5459N/A testInvokeDirect(getMethod(THIS_CLASS, "longString", String.class, long.class, String.class));
5459N/A }
5459N/A
5459N/A void testInvokeDirect(Method m) {
5459N/A final int refKind = referenceKind(m);
5459N/A testInvokeDirect(m, refKind);
5459N/A testInvokeDirect(m, REF_MH_invokeBasic);
5459N/A }
5459N/A void testInvokeDirect(Method m, int refKind) {
5459N/A if (verbose >= 1)
5459N/A System.out.println("testInvoke m="+m+" : "+REF_KIND_NAMES[refKind]);
5459N/A final MethodHandle mh = unreflect(m);
5459N/A Object[] args = makeArguments(mh.type());
5459N/A Object res1 = invokeWithArguments(m, args);
5459N/A // res1 comes from java.lang.reflect.Method::invoke
5459N/A if (verbose >= 1)
5459N/A System.out.println("m"+Arrays.asList(args)+" => "+res1);
5459N/A // res2 comes from java.lang.invoke.MethodHandle::invoke
5459N/A Object res2 = invokeWithArguments(mh, args);
5459N/A assertEquals(res1, res2);
5459N/A MethodType mtype = mh.type();
5459N/A testInvokeVia("DMH invoker", refKind, directInvoker(refKind, mtype), mh, res1, args);
5459N/A MethodType etype = mtype.erase();
5459N/A if (etype != mtype) {
5459N/A // Try a detuned invoker.
5459N/A testInvokeVia("erased DMH invoker", refKind, directInvoker(refKind, etype), mh, res1, args);
5459N/A }
5459N/A MethodType btype = basicType(mtype);
5459N/A if (btype != mtype && btype != etype) {
5459N/A // Try a detuned invoker.
5459N/A testInvokeVia("basic DMH invoker", refKind, directInvoker(refKind, btype), mh, res1, args);
5459N/A }
5459N/A if (false) {
5459N/A // this can crash the JVM
5459N/A testInvokeVia("generic DMH invoker", refKind, directInvoker(refKind, mtype.generic()), mh, res1, args);
5459N/A }
5459N/A refKindTestCounts[refKind] += 1;
5459N/A }
5459N/A
5459N/A void testInvokeVia(String kind, int refKind, MethodHandle invoker, MethodHandle mh, Object res1, Object... args) {
5459N/A Object[] args1;
5459N/A if (refKind == REF_MH_invokeBasic)
5459N/A args1 = pushAtFront(mh, args);
5459N/A else
5459N/A args1 = pushAtBack(args, internalMemberName(mh));
5459N/A if (verbose >= 2) {
5459N/A System.out.println(kind+" invoker="+invoker+" mh="+debugString(mh)+" args="+Arrays.asList(args1));
5459N/A }
5459N/A Object res3 = invokeWithArguments(invoker, args1);
5459N/A assertEquals(res1, res3);
5459N/A }
5459N/A}