Search in sources :

Example 21 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class RevealDirectTest method unreflectMemberOrThrow.

static UnreflectResult unreflectMemberOrThrow(Lookup lookup, Member mem, int variation, byte[] refKind) throws ReflectiveOperationException {
    Class<?> cls = lookup.lookupClass();
    Class<?> defc = mem.getDeclaringClass();
    String name = mem.getName();
    int mods = mem.getModifiers();
    boolean isStatic = Modifier.isStatic(mods);
    MethodHandle mh = null;
    byte kind = 0;
    if (mem instanceof Method) {
        Method m = (Method) mem;
        MethodType type = methodType(m.getReturnType(), m.getParameterTypes());
        boolean canBeSpecial = (!isStatic && (lookup.lookupModes() & Modifier.PRIVATE) != 0 && defc.isAssignableFrom(cls) && (!defc.isInterface() || Arrays.asList(cls.getInterfaces()).contains(defc)));
        if (variation >= 2)
            kind = REF_invokeSpecial;
        else if (isStatic)
            kind = REF_invokeStatic;
        else if (defc.isInterface())
            kind = REF_invokeInterface;
        else
            kind = REF_invokeVirtual;
        refKind[0] = kind;
        switch(variation) {
            case 0:
                mh = lookup.unreflect(m);
                break;
            case 1:
                if (defc == MethodHandle.class && !isStatic && m.isVarArgs() && Modifier.isFinal(mods) && Modifier.isNative(mods)) {
                    break;
                }
                if (isStatic)
                    mh = lookup.findStatic(defc, name, type);
                else
                    mh = lookup.findVirtual(defc, name, type);
                break;
            case 2:
                if (!canBeSpecial)
                    break;
                mh = lookup.unreflectSpecial(m, lookup.lookupClass());
                break;
            case 3:
                if (!canBeSpecial)
                    break;
                mh = lookup.findSpecial(defc, name, type, lookup.lookupClass());
                break;
        }
    } else if (mem instanceof SignaturePolymorphicMethod) {
        SignaturePolymorphicMethod m = (SignaturePolymorphicMethod) mem;
        MethodType type = methodType(m.getReturnType(), m.getParameterTypes());
        kind = REF_invokeVirtual;
        refKind[0] = kind;
        switch(variation) {
            case 0:
                mh = lookup.findVirtual(defc, name, type);
                break;
        }
    } else if (mem instanceof Constructor) {
        // not used
        name = "<init>";
        Constructor<?> m = (Constructor<?>) mem;
        MethodType type = methodType(void.class, m.getParameterTypes());
        kind = REF_newInvokeSpecial;
        refKind[0] = kind;
        switch(variation) {
            case 0:
                mh = lookup.unreflectConstructor(m);
                break;
            case 1:
                mh = lookup.findConstructor(defc, type);
                break;
        }
    } else if (mem instanceof Field) {
        Field m = (Field) mem;
        Class<?> type = m.getType();
        boolean canHaveSetter = !Modifier.isFinal(mods);
        if (variation >= 2)
            kind = (byte) (isStatic ? REF_putStatic : REF_putField);
        else
            kind = (byte) (isStatic ? REF_getStatic : REF_getField);
        refKind[0] = kind;
        switch(variation) {
            case 0:
                mh = lookup.unreflectGetter(m);
                break;
            case 1:
                if (isStatic)
                    mh = lookup.findStaticGetter(defc, name, type);
                else
                    mh = lookup.findGetter(defc, name, type);
                break;
            case 3:
                if (!canHaveSetter)
                    break;
                mh = lookup.unreflectSetter(m);
                break;
            case 2:
                if (!canHaveSetter)
                    break;
                if (isStatic)
                    mh = lookup.findStaticSetter(defc, name, type);
                else
                    mh = lookup.findSetter(defc, name, type);
                break;
        }
    } else {
        throw new IllegalArgumentException(String.valueOf(mem));
    }
    if (mh == null)
        // ran out of valid variations; return null to caller
        return null;
    return new UnreflectResult(mh, kind, mem, variation);
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 22 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class PrivateInvokeTest method testInvokeDirect.

void testInvokeDirect(Method m, int refKind) {
    if (verbose >= 1)
        System.out.println("testInvoke m=" + m + " : " + REF_KIND_NAMES[refKind]);
    final MethodHandle mh = unreflect(m);
    Object[] args = makeArguments(mh.type());
    Object res1 = invokeWithArguments(m, args);
    // res1 comes from java.lang.reflect.Method::invoke
    if (verbose >= 1)
        System.out.println("m" + Arrays.asList(args) + " => " + res1);
    // res2 comes from java.lang.invoke.MethodHandle::invoke
    Object res2 = invokeWithArguments(mh, args);
    assertEquals(res1, res2);
    MethodType mtype = mh.type();
    testInvokeVia("DMH invoker", refKind, directInvoker(refKind, mtype), mh, res1, args);
    MethodType etype = mtype.erase();
    if (etype != mtype) {
        // Try a detuned invoker.
        testInvokeVia("erased DMH invoker", refKind, directInvoker(refKind, etype), mh, res1, args);
    }
    MethodType btype = basicType(mtype);
    if (btype != mtype && btype != etype) {
        // Try a detuned invoker.
        testInvokeVia("basic DMH invoker", refKind, directInvoker(refKind, btype), mh, res1, args);
    }
    if (false) {
        // this can crash the JVM
        testInvokeVia("generic DMH invoker", refKind, directInvoker(refKind, mtype.generic()), mh, res1, args);
    }
    refKindTestCounts[refKind] += 1;
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 23 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class ObjectMethodInInterfaceTest method main.

public static void main(String[] args) throws Throwable {
    MethodHandle mh = MethodHandles.lookup().findVirtual(CharSequence.class, "toString", MethodType.methodType(String.class));
    MethodType mt = MethodType.methodType(Object.class, CharSequence.class);
    mh = mh.asType(mt);
    Object res = mh.invokeExact((CharSequence) "123");
    System.out.println("TEST PASSED");
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 24 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class PermuteArgsTest method test.

static void test(String name, MethodHandle mh) throws Throwable {
    if (VERBOSE)
        System.out.println("mh = " + name + " : " + mh + " { " + Arrays.toString(junkArgs(mh.type().parameterArray())));
    int testCases0 = testCases;
    if (!mh.isVarargsCollector()) {
        // normal case
        testPermutations(mh);
    } else {
        // varargs case; add params up to MAX_ARITY
        MethodType mt = mh.type();
        int posArgs = mt.parameterCount() - 1;
        int arity0 = Math.max(3, posArgs);
        for (int arity = arity0; arity <= MAX_ARITY; arity++) {
            MethodHandle mh1;
            try {
                mh1 = adjustArity(mh, arity);
            } catch (IllegalArgumentException ex) {
                System.out.println("*** mh = " + name + " : " + mh + "; arity = " + arity + " => " + ex);
                ex.printStackTrace(System.out);
                // cannot get this arity for this type
                break;
            }
            test("(" + arity + ")" + name, mh1);
            arity = jump(arity, arity0 * 2, MAX_ARITY);
        }
    }
    if (VERBOSE)
        System.out.println("ran " + (testCases - testCases0) + " test cases for " + name + " }");
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 25 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class MethodTypeTest method testMakeGeneric.

@Test
public void testMakeGeneric() {
    System.out.println("makeGeneric");
    int objectArgCount = 2;
    MethodType expResult = mt_OO2;
    MethodType result = MethodType.genericMethodType(objectArgCount);
    assertSame(expResult, result);
}
Also used : MethodType(java.lang.invoke.MethodType)

Aggregations

MethodType (java.lang.invoke.MethodType)102 MethodHandle (java.lang.invoke.MethodHandle)36 Test (org.junit.Test)6 MethodHandles (java.lang.invoke.MethodHandles)5 ArrayList (java.util.ArrayList)4 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)3 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)3 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)3 CallSite (java.lang.invoke.CallSite)3 LambdaReceiver_A (LambdaReceiver_anotherpkg.LambdaReceiver_A)2 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)2 ConnectorSession (com.facebook.presto.spi.ConnectorSession)2 PrestoException (com.facebook.presto.spi.PrestoException)2 Map (java.util.Map)2 Session (com.facebook.presto.Session)1 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)1 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)1 Parameter (com.facebook.presto.bytecode.Parameter)1 Scope (com.facebook.presto.bytecode.Scope)1 Variable (com.facebook.presto.bytecode.Variable)1