Search in sources :

Example 86 with MethodType

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

the class RicochetTest method assertEqualFunction.

private static void assertEqualFunction(MethodHandle x, MethodHandle y) throws Throwable {
    //??
    assertEquals(x.type(), y.type());
    MethodType t = x.type();
    if (t.parameterCount() == 0) {
        assertEqualFunctionAt(null, x, y);
        return;
    }
    Class<?> ptype = t.parameterType(0);
    if (ptype == long.class || ptype == Long.class) {
        for (long i = -10; i <= 10; i++) {
            assertEqualFunctionAt(i, x, y);
        }
    } else {
        for (int i = -10; i <= 10; i++) {
            assertEqualFunctionAt(i, x, y);
        }
    }
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 87 with MethodType

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

the class PermuteArgsTest method adjustArity.

static MethodHandle adjustArity(MethodHandle mh, int arity) {
    MethodType mt = mh.type();
    int posArgs = mt.parameterCount() - 1;
    Class<?> reptype = mt.parameterType(posArgs).getComponentType();
    MethodType mt1 = mt.dropParameterTypes(posArgs, posArgs + 1);
    while (mt1.parameterCount() < arity) {
        Class<?> pt = reptype;
        if (pt == Object.class && posArgs > 0)
            // repeat types cyclically if possible:
            pt = mt1.parameterType(mt1.parameterCount() - posArgs);
        mt1 = mt1.appendParameterTypes(pt);
    }
    try {
        return mh.asType(mt1);
    } catch (WrongMethodTypeException | IllegalArgumentException ex) {
        throw new IllegalArgumentException("cannot convert to type " + mt1 + " from " + mh, ex);
    }
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 88 with MethodType

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

the class ValueConversionsTest method testConvert.

static void testConvert(Wrapper src, Wrapper dst, long tval) throws Throwable {
    // must have prims
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)
        return;
    // must have values
    if (dst == Wrapper.VOID || src == Wrapper.VOID)
        return;
    boolean testSingleCase = (tval != 0);
    final long tvalInit = tval;
    MethodHandle conv = ValueConversions.convertPrimitive(src, dst);
    MethodType convType = MethodType.methodType(dst.primitiveType(), src.primitiveType());
    assertEquals(convType, conv.type());
    MethodHandle converter = conv.asType(conv.type().changeReturnType(Object.class));
    for (; ; ) {
        long n = tval;
        Object testValue = src.wrap(n);
        Object expResult = dst.cast(testValue, dst.primitiveType());
        Object result;
        switch(src) {
            case INT:
                result = converter.invokeExact((int) n);
                break;
            case LONG:
                result = converter.invokeExact(/*long*/
                n);
                break;
            case FLOAT:
                result = converter.invokeExact((float) n);
                break;
            case DOUBLE:
                result = converter.invokeExact((double) n);
                break;
            case CHAR:
                result = converter.invokeExact((char) n);
                break;
            case BYTE:
                result = converter.invokeExact((byte) n);
                break;
            case SHORT:
                result = converter.invokeExact((short) n);
                break;
            case BOOLEAN:
                result = converter.invokeExact((n & 1) != 0);
                break;
            default:
                throw new AssertionError();
        }
        assertEquals("(src,dst,n,testValue)=" + Arrays.asList(src, dst, "0x" + Long.toHexString(n), testValue), expResult, result);
        if (testSingleCase)
            break;
        // next test value:
        tval = nextTestValue(tval);
        // repeat
        if (tval == tvalInit)
            break;
    }
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 89 with MethodType

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

the class MethodTypeTest method testParameterType.

/**
     * Test of parameterType method, of class MethodType.
     */
@Test
public void testParameterType() {
    System.out.println("parameterType");
    for (int num = 0; num < ptypes.length; num++) {
        MethodType instance = mt_viS;
        Class<?> expResult = ptypes[num];
        Class<?> result = instance.parameterType(num);
        assertSame(expResult, result);
    }
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 90 with MethodType

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

the class MethodTypeTest method testMake_Class_List.

/**
     * Test of make method, of class MethodType.
     */
@Test
public void testMake_Class_List() {
    System.out.println("make (from type list)");
    MethodType result = MethodType.methodType(rtype, Arrays.asList(ptypes));
    assertSame(mt_viS, result);
}
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