當前位置: 首頁>>代碼示例>>Java>>正文


Java MethodHandles.explicitCastArguments方法代碼示例

本文整理匯總了Java中java.lang.invoke.MethodHandles.explicitCastArguments方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodHandles.explicitCastArguments方法的具體用法?Java MethodHandles.explicitCastArguments怎麽用?Java MethodHandles.explicitCastArguments使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.lang.invoke.MethodHandles的用法示例。


在下文中一共展示了MethodHandles.explicitCastArguments方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: cast

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
/** Forces a cast to class A for target (only if types differ) */
public static MethodHandle cast(Class<?> classA, MethodHandle target) {
    MethodType newType = MethodType.methodType(classA).unwrap();
    MethodType targetType = MethodType.methodType(target.type().returnType()).unwrap();
    
    // don't do a conversion if types are the same. explicitCastArguments has this opto,
    // but we do it explicitly, to make the boolean check simpler
    if (newType.returnType() == targetType.returnType()) {
        return target;
    }
    
    // we don't allow the to/from boolean conversions of explicitCastArguments
    if (newType.returnType() == boolean.class || targetType.returnType() == boolean.class) {
        throw new ClassCastException("Cannot cast " + targetType.returnType() + " to " + newType.returnType());
    }
    
    // null return values are not possible for our arguments.
    return MethodHandles.explicitCastArguments(target, target.type().changeReturnType(newType.returnType()));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:20,代碼來源:DefMath.java

示例2: cast

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
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);
	}
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-systemtest,代碼行數:9,代碼來源:StaticAsTypeTestExplicit.java

示例3: cast

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
public 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);
	}
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-systemtest,代碼行數:9,代碼來源:AsTypeTest.java

示例4: zeroConstantFunction

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
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);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:33,代碼來源:ValueConversions.java

示例5: testVarargsCollector

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
/**
 * 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"}));
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:15,代碼來源:ExplicitCastArgumentsTest.java

示例6: checkForWrongMethodTypeException

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
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());
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:12,代碼來源:ExplicitCastArgumentsTest.java

示例7: testMultipleArgs

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
/**
 * 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);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:36,代碼來源:ExplicitCastArgumentsTest.java

示例8: tweak

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:LoopCombinatorTest.java

示例9: explicitCastArguments

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Override
public MethodHandle explicitCastArguments(final MethodHandle target, final MethodType type) {
    final MethodHandle mh = MethodHandles.explicitCastArguments(target, type);
    return debug(mh, "explicitCastArguments", target, type);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:6,代碼來源:MethodHandleFactory.java

示例10: testInterfaceCast

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
public void testInterfaceCast(MethodHandle mh, Class<?> ctype,
                                               boolean doret, boolean docast) throws Throwable {
    MethodHandle mh0 = mh;
    if (verbosity > 1)
        System.out.println("mh="+mh+", ctype="+ctype.getName()+", doret="+doret+", docast="+docast);
    String normalRetVal = "normal return value";
    MethodType mt = mh.type();
    MethodType mt0 = mt;
    if (doret)  mt = mt.changeReturnType(ctype);
    else        mt = mt.changeParameterType(0, ctype);
    if (docast) mh = MethodHandles.explicitCastArguments(mh, mt);
    else        mh = mh.asType(mt);
    assertEquals(mt, mh.type());
    MethodType mt1 = mt;
    // this bit is needed to make the interface types disappear for invokeWithArguments:
    mh = MethodHandles.explicitCastArguments(mh, mt.generic());
    Class<?>[] step = {
        mt1.parameterType(0),  // param as passed to mh at first
        mt0.parameterType(0),  // param after incoming cast
        mt0.returnType(),      // return value before cast
        mt1.returnType(),      // return value after outgoing cast
    };
    // where might a checkCast occur?
    boolean[] checkCast = new boolean[step.length];
    // the string value must pass each step without causing an exception
    if (!docast) {
        if (!doret) {
            if (step[0] != step[1])
                checkCast[1] = true;  // incoming value is cast
        } else {
            if (step[2] != step[3])
                checkCast[3] = true;  // outgoing value is cast
        }
    }
    boolean expectFail = false;
    for (int i = 0; i < step.length; i++) {
        Class<?> c = step[i];
        if (!checkCast[i])  c = i2o(c);
        if (!c.isInstance(normalRetVal)) {
            if (verbosity > 3)
                System.out.println("expect failure at step "+i+" in "+Arrays.toString(step)+Arrays.toString(checkCast));
            expectFail = true;
            break;
        }
    }
    countTest(!expectFail);
    if (verbosity > 2)
        System.out.println("expectFail="+expectFail+", mt="+mt);
    Object res;
    try {
        res = mh.invokeWithArguments(normalRetVal);
    } catch (Exception ex) {
        res = ex;
    }
    boolean sawFail = !(res instanceof String);
    if (sawFail != expectFail) {
        System.out.println("*** testInterfaceCast: mh0 = "+mh0);
        System.out.println("  retype using "+(docast ? "explicitCastArguments" : "asType")+" to "+mt+" => "+mh);
        System.out.println("  call returned "+res);
        System.out.println("  expected "+(expectFail ? "an exception" : normalRetVal));
    }
    if (!expectFail) {
        assertFalse(res.toString(), sawFail);
        assertEquals(normalRetVal, res);
    } else {
        assertTrue(res.toString(), sawFail);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:69,代碼來源:MethodHandlesTest.java


注:本文中的java.lang.invoke.MethodHandles.explicitCastArguments方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。