本文整理匯總了Java中java.lang.invoke.MethodHandle.isVarargsCollector方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodHandle.isVarargsCollector方法的具體用法?Java MethodHandle.isVarargsCollector怎麽用?Java MethodHandle.isVarargsCollector使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.invoke.MethodHandle
的用法示例。
在下文中一共展示了MethodHandle.isVarargsCollector方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: findTestMH
import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
static MethodHandle findTestMH(String name, int[] perm)
throws ReflectiveOperationException {
int arity = perm.length;
Lookup lookup = lookup();
for (Method m : lookup.lookupClass().getDeclaredMethods()) {
if (m.getName().equals(name) &&
Modifier.isStatic(m.getModifiers())) {
MethodHandle mh = lookup.unreflect(m);
int mhArity = mh.type().parameterCount();
if (mh.isVarargsCollector()) {
if (mhArity-1 <= arity)
return adjustArity(mh, arity);
} else if (mhArity == arity) {
return mh;
}
}
}
throw new RuntimeException("no such method for arity "+arity+": "+name);
}
示例2: 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;
}
示例3: OverloadedMethod
import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
OverloadedMethod(final List<MethodHandle> methodHandles, final OverloadedDynamicMethod parent, final MethodType callSiteType,
final LinkerServices linkerServices) {
this.parent = parent;
final Class<?> commonRetType = getCommonReturnType(methodHandles);
this.callSiteType = callSiteType.changeReturnType(commonRetType);
this.linkerServices = linkerServices;
fixArgMethods = new ArrayList<>(methodHandles.size());
varArgMethods = new ArrayList<>(methodHandles.size());
final int argNum = callSiteType.parameterCount();
for(MethodHandle mh: methodHandles) {
if(mh.isVarargsCollector()) {
final MethodHandle asFixed = mh.asFixedArity();
if(argNum == asFixed.type().parameterCount()) {
fixArgMethods.add(asFixed);
}
varArgMethods.add(mh);
} else {
fixArgMethods.add(mh);
}
}
fixArgMethods.trimToSize();
varArgMethods.trimToSize();
final MethodHandle bound = SELECT_METHOD.bindTo(this);
final MethodHandle collecting = SingleDynamicMethod.collectArguments(bound, argNum).asType(
callSiteType.changeReturnType(MethodHandle.class));
invoker = MethodHandles.foldArguments(MethodHandles.exactInvoker(this.callSiteType), collecting);
}
示例4: test
import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
static void test(String name, MethodHandle mh) throws Throwable {
if (VERBOSE)
System.out.println("mh = "+name+" : "+mh+" { "
+Arrays.toString(junkArgs(mh.type().parameterArray())));
int testCases0 = testCases;
if (!mh.isVarargsCollector()) {
// normal case
testPermutations(mh);
} else {
// varargs case; add params up to MAX_ARITY
MethodType mt = mh.type();
int posArgs = mt.parameterCount() - 1;
int arity0 = Math.max(3, posArgs);
for (int arity = arity0; arity <= MAX_ARITY; arity++) {
MethodHandle mh1;
try {
mh1 = adjustArity(mh, arity);
} catch (IllegalArgumentException ex) {
System.out.println("*** mh = "+name+" : "+mh+"; arity = "+arity+" => "+ex);
ex.printStackTrace(System.out);
break; // cannot get this arity for this type
}
test("("+arity+")"+name, mh1);
arity = jump(arity, arity0*2, MAX_ARITY);
}
}
if (VERBOSE)
System.out.println("ran "+(testCases - testCases0)+" test cases for "+name+" }");
}
示例5: OverloadedMethod
import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
OverloadedMethod(final List<MethodHandle> methodHandles,
final OverloadedDynamicMethod parent,
final ClassLoader callSiteClassLoader,
final MethodType callSiteType,
final LinkerServices linkerServices,
final SecureLookupSupplier lookupSupplier) {
this.parent = parent;
this.callSiteClassLoader = callSiteClassLoader;
final Class<?> commonRetType = getCommonReturnType(methodHandles);
this.callSiteType = callSiteType.changeReturnType(commonRetType);
this.linkerServices = linkerServices;
this.lookupSupplier = lookupSupplier;
fixArgMethods = new ArrayList<>(methodHandles.size());
varArgMethods = new ArrayList<>(methodHandles.size());
final int argNum = callSiteType.parameterCount();
for(final MethodHandle mh: methodHandles) {
if(mh.isVarargsCollector()) {
final MethodHandle asFixed = mh.asFixedArity();
if(argNum == asFixed.type().parameterCount()) {
fixArgMethods.add(asFixed);
}
varArgMethods.add(mh);
} else {
fixArgMethods.add(mh);
}
}
fixArgMethods.trimToSize();
varArgMethods.trimToSize();
final MethodHandle bound = SELECT_METHOD.bindTo(this);
final MethodHandle collecting = SingleDynamicMethod.collectArguments(bound, argNum).asType(
callSiteType.changeReturnType(MethodHandle.class));
invoker = linkerServices.asTypeLosslessReturn(MethodHandles.foldArguments(
MethodHandles.exactInvoker(this.callSiteType), collecting), callSiteType);
}
示例6: asSpreader
import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
private static MethodHandle asSpreader(MethodHandle mh) {
int paramCount = mh.type().parameterCount();
if (mh.isVarargsCollector()) {
if (paramCount == 1) {
return mh;
}
return mh.asSpreader(Object[].class, paramCount - 1);
}
return mh.asSpreader(Object[].class, paramCount);
}
示例7: matchesArgs
import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
private static boolean matchesArgs(Object[] args, MethodHandle mh) {
MethodType mt = mh.type();
if (mt.parameterCount() != args.length && !mh.isVarargsCollector()) {
return false;
}
Class<?>[] params = mt.parameterArray();
for (int i = 0; i < params.length; i++) {
if (i == params.length - 1 && mh.isVarargsCollector()) {
break;
}
Class<?> param = params[i];
Object arg = args[i];
if (!param.isInstance(arg)) {
if (arg instanceof Number && Util.NUMERIC_CLASSES.contains(param)) {
continue;
}
if (param.isPrimitive() && Util.WRAPPER_CLASSES.get(param).isInstance(arg)) {
continue;
}
if (arg instanceof String
&& (param == char.class || param == Character.class)
&& ((String) arg).length() == 1) {
continue;
}
if (param == Class.class && arg instanceof JavaType) {
continue;
}
if (!param.isPrimitive() && arg instanceof Null) {
continue;
}
return false;
}
}
return true;
}