Search in sources :

Example 16 with MethodType

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

the class ThrowExceptionsTest method makeStackOverflow.

private static MethodHandle makeStackOverflow() {
    MethodType cellType = methodType(void.class);
    // recursion point
    MethodHandle[] cell = { null };
    MethodHandle getCell = insertArguments(arrayElementGetter(cell.getClass()), 0, cell, 0);
    MethodHandle invokeCell = foldArguments(exactInvoker(cellType), getCell);
    assert (invokeCell.type() == cellType);
    cell[0] = invokeCell;
    // make it conformable to any type:
    invokeCell = dropArguments(invokeCell, 0, Object[].class).asVarargsCollector(Object[].class);
    return invokeCell;
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 17 with MethodType

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

the class VMAnonymousClass method test.

private static void test(String pkg) throws Throwable {
    byte[] bytes = dumpClass(pkg);
    // Define VM anonymous class in privileged context (on BCP).
    Class anonClass = unsafe.defineAnonymousClass(Object.class, bytes, null);
    MethodType t = MethodType.methodType(Object.class, int.class);
    MethodHandle target = MethodHandles.lookup().findStatic(anonClass, "get", t);
    // Wrap target into LF (convert) to get "target" referenced from LF
    MethodHandle wrappedMH = target.asType(MethodType.methodType(Object.class, Integer.class));
    // Invoke enough times to provoke LF compilation to bytecode.
    for (int i = 0; i < 100; i++) {
        Object r = wrappedMH.invokeExact((Integer) 1);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 18 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 19 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 20 with MethodType

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

the class RevealDirectTest method consistent.

static boolean consistent(UnreflectResult res, MethodHandleInfo info) {
    assert (res.mh != null);
    assertEquals(res.kind, info.getReferenceKind());
    assertEquals(res.mem.getModifiers(), info.getModifiers());
    assertEquals(res.mem.getDeclaringClass(), info.getDeclaringClass());
    String expectName = res.mem.getName();
    if (res.kind == REF_newInvokeSpecial)
        expectName = "<init>";
    assertEquals(expectName, info.getName());
    MethodType expectType = res.mh.type();
    if ((res.kind & 1) == (REF_getField & 1))
        expectType = expectType.dropParameterTypes(0, 1);
    if (res.kind == REF_newInvokeSpecial)
        expectType = expectType.changeReturnType(void.class);
    assertEquals(expectType, info.getMethodType());
    assertEquals(res.mh.isVarargsCollector(), isVarArgs(info));
    assertEquals(res.toInfoString(), info.toString());
    assertEquals(res.toInfoString(), MethodHandleInfo.toString(info.getReferenceKind(), info.getDeclaringClass(), info.getName(), info.getMethodType()));
    return true;
}
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