Search in sources :

Example 41 with MethodType

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

the class TestPrivateMemberPackageSibling method test.

public void test() throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodType mt = MethodType.methodType(void.class);
    try {
        Class<?> checkInittedHolder = TestPrivateMemberPackageSibling.class;
        // Original model:  checkInittedHolder = Class.class;
        // Not using Class.checkInitted because it could change without notice.
        MethodHandle mh = lookup.findStatic(checkInittedHolder, "checkInitted", mt);
        throw new RuntimeException("IllegalAccessException not thrown");
    } catch (IllegalAccessException e) {
        // okay
        System.out.println("Expected exception: " + e.getMessage());
    }
}
Also used : MethodHandles(java.lang.invoke.MethodHandles) MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 42 with MethodType

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

the class LambdaReturn method main.

public static void main(String[] args) throws Throwable {
    l = MethodHandles.lookup();
    MethodHandle hV = l.findStatic(LambdaReturn.class, "hereV", mt(void.class));
    MethodHandle hS = l.findStatic(LambdaReturn.class, "hereS", mt(String.class));
    List<String> errs = new ArrayList<>();
    MethodType V = mt(void.class);
    MethodType S = mt(String.class);
    MethodType O = mt(Object.class);
    MethodType I = mt(int.class);
    amf(errs, hS, S, S, O, true);
    amf(errs, hS, S, S, V, false);
    amf(errs, hS, S, S, I, false);
    amf(errs, hS, O, S, S, true);
    amf(errs, hS, V, S, S, false);
    amf(errs, hS, I, S, S, false);
    amf(errs, hS, O, O, S, false);
    amf(errs, hS, S, O, O, false);
    amf(errs, hV, V, V, O, false);
    amf(errs, hV, V, V, I, false);
    amf(errs, hV, V, V, S, false);
    amf(errs, hV, O, V, V, false);
    amf(errs, hV, I, V, V, false);
    amf(errs, hV, S, V, V, false);
    if (errs.size() > 0) {
        for (String err : errs) {
            System.err.println(err);
        }
        throw new AssertionError("Errors: " + errs.size());
    }
}
Also used : MethodType(java.lang.invoke.MethodType) ArrayList(java.util.ArrayList) MethodHandle(java.lang.invoke.MethodHandle)

Example 43 with MethodType

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

the class ValueConversions method convertPrimitive.

public static MethodHandle convertPrimitive(Wrapper wsrc, Wrapper wdst) {
    WrapperCache cache = CONVERT_PRIMITIVE_FUNCTIONS[wsrc.ordinal()];
    MethodHandle mh = cache.get(wdst);
    if (mh != null) {
        return mh;
    }
    // slow path
    Class<?> src = wsrc.primitiveType();
    Class<?> dst = wdst.primitiveType();
    MethodType type = MethodType.methodType(dst, src);
    if (wsrc == wdst) {
        mh = MethodHandles.identity(src);
    } else {
        assert (src.isPrimitive() && dst.isPrimitive());
        try {
            mh = IMPL_LOOKUP.findStatic(THIS_CLASS, src.getSimpleName() + "To" + capitalize(dst.getSimpleName()), type);
        } catch (ReflectiveOperationException ex) {
            mh = null;
        }
    }
    if (mh != null) {
        assert (mh.type() == type) : mh;
        return cache.put(wdst, mh);
    }
    throw new IllegalArgumentException("cannot find primitive conversion function for " + src.getSimpleName() + " -> " + dst.getSimpleName());
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 44 with MethodType

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

the class ValueConversions method unbox.

private static MethodHandle unbox(Wrapper wrap, int kind) {
    // kind 0 -> strongly typed with NPE
    // kind 1 -> strongly typed but zero for null,
    // kind 2 -> asType rules: accept multiple box types but only widening conversions with NPE
    // kind 3 -> explicitCastArguments rules: allow narrowing conversions, zero for null
    WrapperCache cache = UNBOX_CONVERSIONS[kind];
    MethodHandle mh = cache.get(wrap);
    if (mh != null) {
        return mh;
    }
    // slow path
    switch(wrap) {
        case OBJECT:
        case VOID:
            throw new IllegalArgumentException("unbox " + wrap);
    }
    // look up the method
    String name = "unbox" + wrap.wrapperSimpleName();
    MethodType type = unboxType(wrap, kind);
    try {
        mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
    } catch (ReflectiveOperationException ex) {
        mh = null;
    }
    if (mh != null) {
        if (kind > 0) {
            boolean cast = (kind != 2);
            mh = MethodHandles.insertArguments(mh, 1, cast);
        }
        if (kind == 1) {
            // casting but exact (null -> zero)
            mh = mh.asType(unboxType(wrap, 0));
        }
        return cache.put(wrap, mh);
    }
    throw new IllegalArgumentException("cannot find unbox adapter for " + wrap + (kind <= 1 ? " (exact)" : kind == 3 ? " (cast)" : ""));
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 45 with MethodType

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

the class AccessControlTest_sibling method testOneAccess.

private void testOneAccess(LookupCase sourceCase, Method method, String kind) {
    Class<?> targetClass = method.getDeclaringClass();
    String methodName = method.getName();
    MethodType methodType = methodType(method.getReturnType(), method.getParameterTypes());
    boolean willAccess = sourceCase.willAccess(method);
    boolean didAccess = false;
    ReflectiveOperationException accessError = null;
    try {
        switch(kind) {
            case "find":
                if ((method.getModifiers() & Modifier.STATIC) != 0)
                    sourceCase.lookup().findStatic(targetClass, methodName, methodType);
                else
                    sourceCase.lookup().findVirtual(targetClass, methodName, methodType);
                break;
            case "unreflect":
                sourceCase.lookup().unreflect(method);
                break;
            default:
                throw new AssertionError(kind);
        }
        didAccess = true;
    } catch (ReflectiveOperationException ex) {
        accessError = ex;
    }
    if (willAccess != didAccess) {
        System.out.println(sourceCase + " => " + targetClass.getSimpleName() + "." + methodName + methodType);
        System.out.println("fail on " + method + " ex=" + accessError);
        assertEquals(willAccess, didAccess);
    }
    testCount++;
    if (!didAccess)
        testCountFails++;
}
Also used : MethodType(java.lang.invoke.MethodType)

Aggregations

MethodType (java.lang.invoke.MethodType)103 MethodHandle (java.lang.invoke.MethodHandle)37 MethodHandles (java.lang.invoke.MethodHandles)6 Test (org.junit.Test)6 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 InitializeInvokerException (cn.moyada.dubbo.faker.core.exception.InitializeInvokerException)1 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