本文整理汇总了Java中java.lang.invoke.MethodHandle.type方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandle.type方法的具体用法?Java MethodHandle.type怎么用?Java MethodHandle.type使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandle
的用法示例。
在下文中一共展示了MethodHandle.type方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAsCollector
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
public void testAsCollector(Class<?> argType, int pos, int nargs) throws Throwable {
countTest();
// fake up a MH with the same type as the desired adapter:
MethodHandle fake = varargsArray(nargs);
fake = changeArgTypes(fake, argType);
MethodType newType = fake.type();
Object[] args = randomArgs(newType.parameterArray());
// here is what should happen:
Object[] collectedArgs = Arrays.copyOfRange(args, 0, pos+1);
collectedArgs[pos] = Arrays.copyOfRange(args, pos, args.length);
// here is the MH which will witness the collected argument tail:
MethodHandle target = varargsArray(pos+1);
target = changeArgTypes(target, 0, pos, argType);
target = changeArgTypes(target, pos, pos+1, Object[].class);
if (verbosity >= 3)
System.out.println("collect from "+Arrays.asList(args)+" ["+pos+".."+nargs+"]");
MethodHandle result = target.asCollector(Object[].class, nargs-pos).asType(newType);
Object[] returnValue = (Object[]) result.invokeWithArguments(args);
// assertTrue(returnValue.length == pos+1 && returnValue[pos] instanceof Object[]);
// returnValue[pos] = Arrays.asList((Object[]) returnValue[pos]);
// collectedArgs[pos] = Arrays.asList((Object[]) collectedArgs[pos]);
assertArrayEquals(collectedArgs, returnValue);
}
示例2: spreadGuardArguments
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private static MethodHandle spreadGuardArguments(final MethodHandle guard, final MethodType descType) {
final MethodType guardType = guard.type();
final int guardParamCount = guardType.parameterCount();
final int descParamCount = descType.parameterCount();
final int spreadCount = guardParamCount - descParamCount + 1;
if (spreadCount <= 0) {
// Guard doesn't dip into the varargs
return guard;
}
final MethodHandle arrayConvertingGuard;
// If the last parameter type of the guard is an array, then it is already itself a guard for a vararg apply
// invocation. We must filter the last argument with toApplyArgs otherwise deeper levels of nesting will fail
// with ClassCastException of NativeArray to Object[].
if (guardType.parameterType(guardParamCount - 1).isArray()) {
arrayConvertingGuard = MH.filterArguments(guard, guardParamCount - 1, NativeFunction.TO_APPLY_ARGS);
} else {
arrayConvertingGuard = guard;
}
return ScriptObject.adaptHandleToVarArgCallSite(arrayConvertingGuard, descParamCount);
}
示例3: spreadGuardArguments
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private static MethodHandle spreadGuardArguments(final MethodHandle guard, final MethodType descType) {
final MethodType guardType = guard.type();
final int guardParamCount = guardType.parameterCount();
final int descParamCount = descType.parameterCount();
final int spreadCount = guardParamCount - descParamCount + 1;
if (spreadCount <= 0) {
// Guard doesn't dip into the varargs
return guard;
}
final MethodHandle arrayConvertingGuard;
// If the last parameter type of the guard is an array, then it is already itself a guard for a vararg apply
// invocation. We must filter the last argument with toApplyArgs otherwise deeper levels of nesting will fail
// with ClassCastException of NativeArray to Object[].
if(guardType.parameterType(guardParamCount - 1).isArray()) {
arrayConvertingGuard = MH.filterArguments(guard, guardParamCount - 1, NativeFunction.TO_APPLY_ARGS);
} else {
arrayConvertingGuard = guard;
}
return ScriptObject.adaptHandleToVarArgCallSite(arrayConvertingGuard, descParamCount);
}
示例4: CatchExceptionTest
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
public CatchExceptionTest(TestCase testCase, final boolean isVararg, final int argsCount,
final int catchDrops) {
this.testCase = testCase;
this.dropped = catchDrops;
MethodHandle thrower = testCase.thrower;
int throwerLen = thrower.type().parameterCount();
List<Class<?>> classes;
int extra = Math.max(0, argsCount - throwerLen);
classes = getThrowerParams(isVararg, extra);
this.argsCount = throwerLen + classes.size();
thrower = Helper.addTrailingArgs(thrower, this.argsCount, classes);
if (isVararg && argsCount > throwerLen) {
MethodType mt = thrower.type();
Class<?> lastParam = mt.parameterType(mt.parameterCount() - 1);
thrower = thrower.asVarargsCollector(lastParam);
}
this.thrower = thrower;
this.dropped = Math.min(this.argsCount, catchDrops);
catcher = testCase.getCatcher(getCatcherParams());
nargs = Math.max(2, this.argsCount);
}
示例5: CatchExceptionTest
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
public CatchExceptionTest(TestCase testCase, final boolean isVararg,
final int argsCount, final int catchDrops) {
this.testCase = testCase;
this.dropped = catchDrops;
MethodHandle thrower = testCase.thrower;
int throwerLen = thrower.type().parameterCount();
List<Class<?>> classes;
int extra = Math.max(0, argsCount - throwerLen);
classes = getThrowerParams(isVararg, extra);
this.argsCount = throwerLen + classes.size();
thrower = Helper.addTrailingArgs(thrower, this.argsCount, classes);
if (isVararg && argsCount > throwerLen) {
MethodType mt = thrower.type();
Class<?> lastParam = mt.parameterType(mt.parameterCount() - 1);
thrower = thrower.asVarargsCollector(lastParam);
}
this.thrower = thrower;
this.dropped = Math.min(this.argsCount, catchDrops);
catcher = testCase.getCatcher(getCatcherParams());
nargs = Math.max(2, this.argsCount);
}
示例6: getFallbackLoggingRelink
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private MethodHandle getFallbackLoggingRelink(final MethodHandle relink) {
if (!getNashornDescriptor().isTraceMisses()) {
// If we aren't tracing misses, just return relink as-is
return relink;
}
final MethodType type = relink.type();
return MH.foldArguments(relink, MH.asType(MH.asCollector(MH.insertArguments(TRACEMISS, 0, this, "MISS " + getScriptLocation() + " "), Object[].class, type.parameterCount()), type.changeReturnType(void.class)));
}
示例7: filterOptimisticReturnValue
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
/**
* Given a method handle and an expected return type, perform return value filtering
* according to the optimistic type coercion rules
* @param mh method handle
* @param expectedReturnType expected return type
* @param programPoint program point
* @return filtered method
*/
public static MethodHandle filterOptimisticReturnValue(final MethodHandle mh, final Class<?> expectedReturnType, final int programPoint) {
if(!isValid(programPoint)) {
return mh;
}
final MethodType type = mh.type();
final Class<?> actualReturnType = type.returnType();
if(TypeUtilities.isConvertibleWithoutLoss(actualReturnType, expectedReturnType)) {
return mh;
}
final MethodHandle guard = getOptimisticTypeGuard(expectedReturnType, actualReturnType);
return guard == null ? mh : MH.filterReturnValue(mh, MH.insertArguments(guard, guard.type().parameterCount() - 1, programPoint));
}
示例8: matchReturnTypes
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
static MethodPair matchReturnTypes(final MethodHandle m1, final MethodHandle m2) {
final MethodType type1 = m1.type();
final MethodType type2 = m2.type();
final Class<?> commonRetType = InternalTypeUtilities.getCommonLosslessConversionType(type1.returnType(),
type2.returnType());
return new MethodPair(
m1.asType(type1.changeReturnType(commonRetType)),
m2.asType(type2.changeReturnType(commonRetType)));
}
示例9: createConstructorFromInvoker
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
/**
* Compose a constructor from an invoker.
*
* @param invoker invoker
* @return the composed constructor
*/
private static MethodHandle createConstructorFromInvoker(final MethodHandle invoker) {
final boolean needsCallee = ScriptFunctionData.needsCallee(invoker);
// If it was (callee, this, args...), permute it to (this, callee, args...). We're doing this because having
// "this" in the first argument position is what allows the elegant folded composition of
// (newFilter x constructor x allocator) further down below in the code. Also, ensure the composite constructor
// always returns Object.
final MethodHandle swapped = needsCallee ? swapCalleeAndThis(invoker) : invoker;
final MethodHandle returnsObject = MH.asType(swapped, swapped.type().changeReturnType(Object.class));
final MethodType ctorType = returnsObject.type();
// Construct a dropping type list for NEWFILTER, but don't include constructor "this" into it, so it's actually
// captured as "allocation" parameter of NEWFILTER after we fold the constructor into it.
// (this, [callee, ]args...) => ([callee, ]args...)
final Class<?>[] ctorArgs = ctorType.dropParameterTypes(0, 1).parameterArray();
// Fold constructor into newFilter that replaces the return value from the constructor with the originally
// allocated value when the originally allocated value is a JS primitive (String, Boolean, Number).
// (result, this, [callee, ]args...) x (this, [callee, ]args...) => (this, [callee, ]args...)
final MethodHandle filtered = MH.foldArguments(MH.dropArguments(NEWFILTER, 2, ctorArgs), returnsObject);
// allocate() takes a ScriptFunction and returns a newly allocated ScriptObject...
if (needsCallee) {
// ...we either fold it into the previous composition, if we need both the ScriptFunction callee object and
// the newly allocated object in the arguments, so (this, callee, args...) x (callee) => (callee, args...),
// or...
return MH.foldArguments(filtered, ScriptFunction.ALLOCATE);
}
// ...replace the ScriptFunction argument with the newly allocated object, if it doesn't need the callee
// (this, args...) filter (callee) => (callee, args...)
return MH.filterArguments(filtered, 0, ScriptFunction.ALLOCATE);
}
示例10: createComposableInvoker
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private MethodHandle createComposableInvoker(final boolean isConstructor) {
final MethodHandle handle = getInvokerOrConstructor(isConstructor);
// If compiled function is not optimistic, it can't ever change its invoker/constructor, so just return them
// directly.
if(!canBeDeoptimized()) {
return handle;
}
// Otherwise, we need a new level of indirection; need to introduce a mutable call site that can relink itslef
// to the compiled function's changed target whenever the optimistic assumptions are invalidated.
final CallSite cs = new MutableCallSite(handle.type());
relinkComposableInvoker(cs, this, isConstructor);
return cs.dynamicInvoker();
}
示例11: fixExpressionCallSite
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private static GuardedInvocation fixExpressionCallSite(final NashornCallSiteDescriptor desc, final GuardedInvocation link) {
// If it's not a getMethod, just add an expression filter that converts WithObject in "this" position to its
// expression.
if (!"getMethod".equals(desc.getFirstOperator())) {
return fixReceiverType(link, WITHEXPRESSIONFILTER).filterArguments(0, WITHEXPRESSIONFILTER);
}
final MethodHandle linkInvocation = link.getInvocation();
final MethodType linkType = linkInvocation.type();
final boolean linkReturnsFunction = ScriptFunction.class.isAssignableFrom(linkType.returnType());
return link.replaceMethods(
// Make sure getMethod will bind the script functions it receives to WithObject.expression
MH.foldArguments(
linkReturnsFunction ?
BIND_TO_EXPRESSION_FN :
BIND_TO_EXPRESSION_OBJ,
filterReceiver(
linkInvocation.asType(
linkType.changeReturnType(
linkReturnsFunction ?
ScriptFunction.class :
Object.class).
changeParameterType(
0,
Object.class)),
WITHEXPRESSIONFILTER)),
filterGuardReceiver(link, WITHEXPRESSIONFILTER));
// No clever things for the guard -- it is still identically filtered.
}
示例12: dropReceiver
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private static MethodHandle dropReceiver(final MethodHandle mh, final Class<?> receiverClass) {
MethodHandle newHandle = MethodHandles.dropArguments(mh, 0, receiverClass);
// NOTE: this is a workaround for the fact that dropArguments doesn't preserve vararg collector state.
if(mh.isVarargsCollector() && !newHandle.isVarargsCollector()) {
final MethodType type = mh.type();
newHandle = newHandle.asVarargsCollector(type.parameterType(type.parameterCount() - 1));
}
return newHandle;
}
示例13: matchReturnTypes
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
static MethodPair matchReturnTypes(final MethodHandle m1, final MethodHandle m2) {
final MethodType type1 = m1.type();
final MethodType type2 = m2.type();
final Class<?> commonRetType = TypeUtilities.getCommonLosslessConversionType(type1.returnType(),
type2.returnType());
return new MethodPair(
m1.asType(type1.changeReturnType(commonRetType)),
m2.asType(type2.changeReturnType(commonRetType)));
}
示例14: testPermutations
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
static void testPermutations(MethodHandle mh) throws Throwable {
HashSet<String> done = new HashSet<>();
MethodType mt = mh.type();
int[] perm = nullPerm(mt.parameterCount());
final int MARGIN = (perm.length <= 10 ? 2 : 0);
int testCases0 = testCases;
for (int j = 0; j <= 1; j++) {
int maxStart = perm.length-1;
if (j != 0) maxStart /= 2;
for (int start = 0; start <= maxStart; start++) {
int maxOmit = (maxStart - start) / 2;
if (start != 0) maxOmit = 2;
if (j != 0) maxOmit = 1;
for (int omit = 0; omit <= maxOmit; omit++) {
int end = perm.length - omit;
if (end - start >= 2) {
//System.out.println("testPermutations"+Arrays.asList(start, end)+(j == 0 ? "" : " (reverse)"));
testPermutations(mh, perm, start, end, done);
}
omit = jump(omit, (start == 0 && j == 0 ? MARGIN : 0), maxOmit);
}
start = jump(start, (j == 0 ? MARGIN : 0), maxStart);
}
// do everything in reverse:
reverse(perm, 0, perm.length);
}
switch (perm.length) {
case 2: assert(testCases - testCases0 == 2); break;
case 3: assert(testCases - testCases0 == 6); break;
case 4: assert(testCases - testCases0 == 24); break;
case 5: assert(testCases - testCases0 == 120); break;
case 6: assert(testCases - testCases0 > 720/3); break;
}
}
示例15: assertType
import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
private static void assertType(final MethodHandle mh, final MethodType type) {
if(!mh.type().equals(type)) {
throw new WrongMethodTypeException("Expected type: " + type + " actual type: " + mh.type());
}
}