Java Code Examples for java.lang.invoke.MethodHandles#explicitCastArguments()

The following examples show how to use java.lang.invoke.MethodHandles#explicitCastArguments() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndyArrayAccess.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static MethodHandle buildSetter(Class<?> arrayClass) {
    MethodHandle set = MethodHandles.arrayElementSetter(arrayClass);
    MethodHandle fallback = MethodHandles.explicitCastArguments(set, set.type().changeParameterType(0, Object.class));

    fallback = MethodHandles.dropArguments(fallback, 3, int.class);
    MethodType reorderType = fallback.type().
            insertParameterTypes(0, int.class).
            dropParameterTypes(4, 5);
    fallback = MethodHandles.permuteArguments(fallback, reorderType, 1, 0, 3, 0);

    fallback = MethodHandles.foldArguments(fallback, normalizeIndex);
    fallback = MethodHandles.explicitCastArguments(fallback, set.type());

    MethodHandle guard = MethodHandles.dropArguments(notNegative, 0, arrayClass);
    MethodHandle handle = MethodHandles.guardWithTest(guard, set, fallback);
    return handle;
}
 
Example 2
Source File: Selector.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * setting a call site target consists of the following steps:
 * # get the meta class
 * # select a method/constructor/property from it, if it is a MetaClassImpl
 * # make a handle out of the selection
 * # if nothing could be selected select a path through the given MetaClass or the GroovyObject
 * # apply transformations for vargs, implicit null argument, coercion, wrapping, null receiver and spreading
 */
@Override
public void setCallSiteTarget() {
    if (!setNullForSafeNavigation() && !setInterceptor()) {
        getMetaClass();
        if (LOG_ENABLED) LOG.info("meta class is " + mc);
        setSelectionBase();
        MetaClassImpl mci = getMetaClassImpl(mc, callType != CallType.GET);
        chooseMeta(mci);
        setHandleForMetaMethod();
        setMetaClassCallHandleIfNeeded(mci != null);
        correctParameterLength();
        correctCoerce();
        correctWrapping();
        correctNullReceiver();
        correctSpreading();

        if (LOG_ENABLED) LOG.info("casting explicit from " + handle.type() + " to " + targetType);
        handle = MethodHandles.explicitCastArguments(handle, targetType);

        addExceptionHandler();
    }
    setGuards(args[0]);
    doCallSiteTargetSet();
}
 
Example 3
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static MethodHandle tweak(MethodHandle mh, int argPos, Class<?> type) {
    MethodType mt = mh.type();
    if (argPos == -1)
        mt = mt.changeReturnType(type);
    else
        mt = mt.changeParameterType(argPos, type);
    return MethodHandles.explicitCastArguments(mh, mt);
}
 
Example 4
Source File: StaticAsTypeTestImplicit.java    From openjdk-systemtest with Apache License 2.0 5 votes vote down vote up
public static MethodHandle cast(MethodHandle mh, MethodType mt) throws Throwable {
	println("calling " + mh.type() + " as " + mt);
	if (explicit) {
		return MethodHandles.explicitCastArguments(mh, mt);
	} else {
		return mh.asType(mt);
	}
}
 
Example 5
Source File: StaticAsTypeTestExplicit.java    From openjdk-systemtest with Apache License 2.0 5 votes vote down vote up
public static MethodHandle cast(MethodHandle mh, MethodType mt) throws Throwable {
	println("calling " + mh.type() + " as " + mt);
	if (explicit) {
		return MethodHandles.explicitCastArguments(mh, mt);
	} else {
		return mh.asType(mt);
	}
}
 
Example 6
Source File: ValueConversions.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static MethodHandle zeroConstantFunction(Wrapper wrap) {
    WrapperCache cache = CONSTANT_FUNCTIONS[0];
    MethodHandle mh = cache.get(wrap);
    if (mh != null) {
        return mh;
    }
    // slow path
    MethodType type = MethodType.methodType(wrap.primitiveType());
    switch (wrap) {
        case VOID:
            mh = EMPTY;
            break;
        case OBJECT:
        case INT: case LONG: case FLOAT: case DOUBLE:
            try {
                mh = IMPL_LOOKUP.findStatic(THIS_CLASS, "zero"+wrap.wrapperSimpleName(), type);
            } catch (ReflectiveOperationException ex) {
                mh = null;
            }
            break;
    }
    if (mh != null) {
        return cache.put(wrap, mh);
    }

    // use zeroInt and cast the result
    if (wrap.isSubwordOrInt() && wrap != Wrapper.INT) {
        mh = MethodHandles.explicitCastArguments(zeroConstantFunction(Wrapper.INT), type);
        return cache.put(wrap, mh);
    }
    throw new IllegalArgumentException("cannot find zero constant for " + wrap);
}
 
Example 7
Source File: ExplicitCastArgumentsTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that MHs.explicitCastArguments does incorrect type checks for
 * VarargsCollector. Bug 8066746.
 *
 * @throws java.lang.Throwable
 */
public static void testVarargsCollector() throws Throwable {
    MethodType mt = MethodType.methodType(String[].class, String[].class);
    MethodHandle mh = MethodHandles.publicLookup()
            .findStatic(THIS_CLASS, "f", mt);
    mh = MethodHandles.explicitCastArguments(mh,
            MethodType.methodType(Object.class, Object.class));
    mh.invokeWithArguments((Object) (new String[]{"str1", "str2"}));
}
 
Example 8
Source File: ValueConversions.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static MethodHandle zeroConstantFunction(Wrapper wrap) {
    WrapperCache cache = CONSTANT_FUNCTIONS[0];
    MethodHandle mh = cache.get(wrap);
    if (mh != null) {
        return mh;
    }
    // slow path
    MethodType type = MethodType.methodType(wrap.primitiveType());
    switch (wrap) {
        case VOID:
            mh = EMPTY;
            break;
        case OBJECT:
        case INT: case LONG: case FLOAT: case DOUBLE:
            try {
                mh = IMPL_LOOKUP.findStatic(THIS_CLASS, "zero"+wrap.wrapperSimpleName(), type);
            } catch (ReflectiveOperationException ex) {
                mh = null;
            }
            break;
    }
    if (mh != null) {
        return cache.put(wrap, mh);
    }

    // use zeroInt and cast the result
    if (wrap.isSubwordOrInt() && wrap != Wrapper.INT) {
        mh = MethodHandles.explicitCastArguments(zeroConstantFunction(Wrapper.INT), type);
        return cache.put(wrap, mh);
    }
    throw new IllegalArgumentException("cannot find zero constant for " + wrap);
}
 
Example 9
Source File: ExplicitCastArgumentsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that MHs.eCA method works correctly with MHs with multiple arguments.
 * @throws Throwable
 */
public static void testMultipleArgs() throws Throwable {
    int arity = 1 + RNG.nextInt(Helper.MAX_ARITY / 2 - 2);
    int arityMinus = RNG.nextInt(arity);
    int arityPlus = arity + RNG.nextInt(Helper.MAX_ARITY / 2 - arity) + 1;
    MethodType mType = Helper.randomMethodTypeGenerator(arity);
    MethodType mTypeNew = Helper.randomMethodTypeGenerator(arity);
    MethodType mTypeNewMinus = Helper.randomMethodTypeGenerator(arityMinus);
    MethodType mTypeNewPlus = Helper.randomMethodTypeGenerator(arityPlus);
    Class<?> rType = mType.returnType();
    MethodHandle original;
    if (rType.equals(void.class)) {
        MethodType mt = MethodType.methodType(void.class);
        original = MethodHandles.publicLookup()
                .findStatic(THIS_CLASS, "retVoid", mt);
    } else {
        Object rValue = Helper.castToWrapper(1, rType);
        original = MethodHandles.constant(rType, rValue);
    }
    original = Helper.addTrailingArgs(original, arity, mType.parameterList());
    MethodHandle target = MethodHandles
                .explicitCastArguments(original, mTypeNew);
    Object[] parList = Helper.randomArgs(mTypeNew.parameterList());
    for (int i = 0; i < parList.length; i++) {
        if (parList[i] instanceof String) {
            parList[i] = null; //getting rid of Stings produced by randomArgs
        }
    }
    target.invokeWithArguments(parList);
    checkForWrongMethodTypeException(original, mTypeNewMinus);
    checkForWrongMethodTypeException(original, mTypeNewPlus);
}
 
Example 10
Source File: ValueConversions.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static MethodHandle zeroConstantFunction(Wrapper wrap) {
    WrapperCache cache = CONSTANT_FUNCTIONS[0];
    MethodHandle mh = cache.get(wrap);
    if (mh != null) {
        return mh;
    }
    // slow path
    MethodType type = MethodType.methodType(wrap.primitiveType());
    switch (wrap) {
        case VOID:
            mh = EMPTY;
            break;
        case OBJECT:
        case INT: case LONG: case FLOAT: case DOUBLE:
            try {
                mh = IMPL_LOOKUP.findStatic(THIS_CLASS, "zero"+wrap.wrapperSimpleName(), type);
            } catch (ReflectiveOperationException ex) {
                mh = null;
            }
            break;
    }
    if (mh != null) {
        return cache.put(wrap, mh);
    }

    // use zeroInt and cast the result
    if (wrap.isSubwordOrInt() && wrap != Wrapper.INT) {
        mh = MethodHandles.explicitCastArguments(zeroConstantFunction(Wrapper.INT), type);
        return cache.put(wrap, mh);
    }
    throw new IllegalArgumentException("cannot find zero constant for " + wrap);
}
 
Example 11
Source File: ValueConversions.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static MethodHandle zeroConstantFunction(Wrapper wrap) {
    EnumMap<Wrapper, MethodHandle> cache = CONSTANT_FUNCTIONS[0];
    MethodHandle mh = cache.get(wrap);
    if (mh != null) {
        return mh;
    }
    // slow path
    MethodType type = MethodType.methodType(wrap.primitiveType());
    switch (wrap) {
        case VOID:
            mh = EMPTY;
            break;
        case OBJECT:
        case INT: case LONG: case FLOAT: case DOUBLE:
            try {
                mh = IMPL_LOOKUP.findStatic(THIS_CLASS, "zero"+wrap.wrapperSimpleName(), type);
            } catch (ReflectiveOperationException ex) {
                mh = null;
            }
            break;
    }
    if (mh != null) {
        cache.put(wrap, mh);
        return mh;
    }

    // use zeroInt and cast the result
    if (wrap.isSubwordOrInt() && wrap != Wrapper.INT) {
        mh = MethodHandles.explicitCastArguments(zeroConstantFunction(Wrapper.INT), type);
        cache.put(wrap, mh);
        return mh;
    }
    throw new IllegalArgumentException("cannot find zero constant for " + wrap);
}
 
Example 12
Source File: ExplicitCastArgumentsTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static void checkForWrongMethodTypeException(MethodHandle mh, MethodType mt) {
    try {
        MethodHandles.explicitCastArguments(mh, mt);
        throw new AssertionError("Expected WrongMethodTypeException is not thrown");
    } catch (WrongMethodTypeException wmte) {
        if (VERBOSE) {
            System.out.printf("Expected exception %s: %s\n",
                    wmte.getClass(), wmte.getMessage());
        }
    }
}
 
Example 13
Source File: ExplicitCastArgumentsTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that MHs.explicitCastArguments does incorrect type checks for
 * VarargsCollector. Bug 8066746.
 *
 * @throws java.lang.Throwable
 */
public static void testVarargsCollector() throws Throwable {
    MethodType mt = MethodType.methodType(String[].class, String[].class);
    MethodHandle mh = MethodHandles.publicLookup()
            .findStatic(THIS_CLASS, "f", mt);
    mh = MethodHandles.explicitCastArguments(mh,
            MethodType.methodType(Object.class, Object.class));
    mh.invokeWithArguments((Object) (new String[]{"str1", "str2"}));
}
 
Example 14
Source File: IndyArrayAccess.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static MethodHandle arraySet(MethodType type) {
    Class<?> key = type.parameterType(0);
    MethodHandle res = setterMap.get(key);
    if (res != null) return res;
    res = buildSetter(key);
    res = MethodHandles.explicitCastArguments(res, type);
    return res;
}
 
Example 15
Source File: ExplicitCastArgumentsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void checkForWrongMethodTypeException(MethodHandle mh, MethodType mt) {
    try {
        MethodHandles.explicitCastArguments(mh, mt);
        throw new AssertionError("Expected WrongMethodTypeException is not thrown");
    } catch (WrongMethodTypeException wmte) {
        if (VERBOSE) {
            System.out.printf("Expected exception %s: %s\n",
                    wmte.getClass(), wmte.getMessage());
        }
    }
}
 
Example 16
Source File: ValueConversions.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static MethodHandle zeroConstantFunction(Wrapper wrap) {
    WrapperCache cache = CONSTANT_FUNCTIONS[0];
    MethodHandle mh = cache.get(wrap);
    if (mh != null) {
        return mh;
    }
    // slow path
    MethodType type = MethodType.methodType(wrap.primitiveType());
    switch (wrap) {
        case VOID:
            mh = EMPTY;
            break;
        case OBJECT:
        case INT: case LONG: case FLOAT: case DOUBLE:
            try {
                mh = IMPL_LOOKUP.findStatic(THIS_CLASS, "zero"+wrap.wrapperSimpleName(), type);
            } catch (ReflectiveOperationException ex) {
                mh = null;
            }
            break;
    }
    if (mh != null) {
        return cache.put(wrap, mh);
    }

    // use zeroInt and cast the result
    if (wrap.isSubwordOrInt() && wrap != Wrapper.INT) {
        mh = MethodHandles.explicitCastArguments(zeroConstantFunction(Wrapper.INT), type);
        return cache.put(wrap, mh);
    }
    throw new IllegalArgumentException("cannot find zero constant for " + wrap);
}
 
Example 17
Source File: MethodHandleFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public MethodHandle explicitCastArguments(final MethodHandle target, final MethodType type) {
    final MethodHandle mh = MethodHandles.explicitCastArguments(target, type);
    return debug(mh, "explicitCastArguments", target, type);
}
 
Example 18
Source File: MethodHandleFactory.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public MethodHandle explicitCastArguments(final MethodHandle target, final MethodType type) {
    return MethodHandles.explicitCastArguments(target, type);
}
 
Example 19
Source File: Selector.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void castAndSetGuards() {
    handle = MethodHandles.explicitCastArguments(handle, targetType);
    setGuards(args[0]);
    doCallSiteTargetSet();
}
 
Example 20
Source File: MethodHandleFactory.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public MethodHandle explicitCastArguments(final MethodHandle target, final MethodType type) {
    return MethodHandles.explicitCastArguments(target, type);
}