本文整理匯總了Java中java.lang.invoke.MethodType.parameterCount方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodType.parameterCount方法的具體用法?Java MethodType.parameterCount怎麽用?Java MethodType.parameterCount使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.invoke.MethodType
的用法示例。
在下文中一共展示了MethodType.parameterCount方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: explicitParams
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
private MethodType explicitParams(final MethodType callSiteType) {
if (CompiledFunction.isVarArgsType(callSiteType)) {
return null;
}
final MethodType noCalleeThisType = callSiteType.dropParameterTypes(0, 2); // (callee, this) is always in call site type
final int callSiteParamCount = noCalleeThisType.parameterCount();
// Widen parameters of reference types to Object as we currently don't care for specialization among reference
// types. E.g. call site saying (ScriptFunction, Object, String) should still link to (ScriptFunction, Object, Object)
final Class<?>[] paramTypes = noCalleeThisType.parameterArray();
boolean changed = false;
for (int i = 0; i < paramTypes.length; ++i) {
final Class<?> paramType = paramTypes[i];
if (!(paramType.isPrimitive() || paramType == Object.class)) {
paramTypes[i] = Object.class;
changed = true;
}
}
final MethodType generalized = changed ? MethodType.methodType(noCalleeThisType.returnType(), paramTypes) : noCalleeThisType;
if (callSiteParamCount < getArity()) {
return generalized.appendParameterTypes(Collections.<Class<?>>nCopies(getArity() - callSiteParamCount, Object.class));
}
return generalized;
}
示例2: spreadGuardArguments
import java.lang.invoke.MethodType; //導入方法依賴的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: bindToNameIfNeeded
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
private static MethodHandle bindToNameIfNeeded(final MethodHandle methodHandle, final String bindName) {
if (bindName == null) {
return methodHandle;
}
// if it is vararg method, we need to extend argument array with
// a new zeroth element that is set to bindName value.
final MethodType methodType = methodHandle.type();
final int parameterCount = methodType.parameterCount();
final boolean isVarArg = parameterCount > 0 && methodType.parameterType(parameterCount - 1).isArray();
if (isVarArg) {
return MH.filterArguments(methodHandle, 1, MH.insertArguments(ADD_ZEROTH_ELEMENT, 1, bindName));
}
return MH.insertArguments(methodHandle, 1, bindName);
}
示例4: lookupExactApplyToCall
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
/**
* Used to find an apply to call version that fits this callsite.
* We cannot just, as in the normal matcher case, return e.g. (Object, Object, int)
* for (Object, Object, int, int, int) or we will destroy the semantics and get
* a function that, when padded with undefineds, behaves differently
* @param type actual call site type
* @return apply to call that perfectly fits this callsite or null if none found
*/
CompiledFunction lookupExactApplyToCall(final MethodType type) {
for (final CompiledFunction cf : code) {
if (!cf.isApplyToCall()) {
continue;
}
final MethodType cftype = cf.type();
if (cftype.parameterCount() != type.parameterCount()) {
continue;
}
if (widen(cftype).equals(widen(type))) {
return cf;
}
}
return null;
}
示例5: bindToNameIfNeeded
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
private static MethodHandle bindToNameIfNeeded(final MethodHandle methodHandle, final String bindName) {
if (bindName == null) {
return methodHandle;
}
// if it is vararg method, we need to extend argument array with
// a new zeroth element that is set to bindName value.
final MethodType methodType = methodHandle.type();
final int parameterCount = methodType.parameterCount();
if (parameterCount < 2) {
return methodHandle; // method does not have enough parameters
}
final boolean isVarArg = methodType.parameterType(parameterCount - 1).isArray();
if (isVarArg) {
return MH.filterArguments(methodHandle, 1, MH.insertArguments(ADD_ZEROTH_ELEMENT, 1, bindName));
}
return MH.insertArguments(methodHandle, 1, bindName);
}
示例6: testCountedLoopBodyParameters
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
@Test(dataProvider = "countedLoopBodyParameters")
public static void testCountedLoopBodyParameters(MethodType countType, MethodType initType, MethodType bodyType) throws Throwable {
MethodHandle loop = MethodHandles.countedLoop(
MethodHandles.empty(countType),
initType == null ? null : MethodHandles.empty(initType),
MethodHandles.empty(bodyType));
// The rule: If body takes the minimum number of parameters, then take what countType offers.
// The initType has to just roll with whatever the other two agree on.
int innerParams = (bodyType.returnType() == void.class ? 1 : 2);
MethodType expectType = bodyType.dropParameterTypes(0, innerParams);
if (expectType.parameterCount() == 0)
expectType = expectType.insertParameterTypes(0, countType.parameterList());
assertEquals(expectType, loop.type());
}
示例7: needsCallee
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
static boolean needsCallee(final MethodType type) {
final int length = type.parameterCount();
if (length == 0) {
return false;
}
final Class<?> param0 = type.parameterType(0);
return param0 == ScriptFunction.class || param0 == boolean.class && length > 1 && type.parameterType(1) == ScriptFunction.class;
}
示例8: getGuardedInvocation
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
final Object self = linkRequest.getReceiver();
final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
if (self instanceof ConsString) {
// In order to treat ConsString like a java.lang.String we need a link request with a string receiver.
final Object[] arguments = linkRequest.getArguments();
arguments[0] = "";
final LinkRequest forgedLinkRequest = linkRequest.replaceArguments(desc, arguments);
final GuardedInvocation invocation = getGuardedInvocation(beansLinker, forgedLinkRequest, linkerServices);
// If an invocation is found we add a filter that makes it work for both Strings and ConsStrings.
return invocation == null ? null : invocation.filterArguments(0, FILTER_CONSSTRING);
}
if (self != null && "call".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
// Support dyn:call on any object that supports some @FunctionalInterface
// annotated interface. This way Java method, constructor references or
// implementations of java.util.function.* interfaces can be called as though
// those are script functions.
final Method m = getFunctionalInterfaceMethod(self.getClass());
if (m != null) {
final MethodType callType = desc.getMethodType();
// 'callee' and 'thiz' passed from script + actual arguments
if (callType.parameterCount() != m.getParameterCount() + 2) {
throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
}
return new GuardedInvocation(
// drop 'thiz' passed from the script.
MH.dropArguments(desc.getLookup().unreflect(m), 1, callType.parameterType(1)),
Guards.getInstanceOfGuard(m.getDeclaringClass())).asTypeSafeReturn(
new NashornBeansLinkerServices(linkerServices), callType);
}
}
return getGuardedInvocation(beansLinker, linkRequest, linkerServices);
}
示例9: invoke
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
private void invoke(String method, Object logger, MethodType mt, Object... args) {
try {
final int last = mt.parameterCount()-1;
boolean isVarargs = mt.parameterType(last).isArray();
final MethodHandle handle = lookup.findVirtual(definingClass,
method, mt).bindTo(logger);
final StringBuilder builder = new StringBuilder();
builder.append(logger.getClass().getSimpleName()).append('.')
.append(method).append('(');
String sep = "";
int offset = 0;
Object[] params = args;
for (int i=0; (i-offset) < params.length; i++) {
if (isVarargs && i == last) {
offset = last;
params = (Object[])args[i];
if (params == null) break;
}
Object p = params[i - offset];
String quote = (p instanceof String) ? "\"" : "";
builder.append(sep).append(quote).append(p).append(quote);
sep = ", ";
}
builder.append(')');
if (verbose) {
System.out.println(builder);
}
handle.invokeWithArguments(args);
} catch (Throwable ex) {
throw new RuntimeException(ex);
}
}
示例10: test
import java.lang.invoke.MethodType; //導入方法依賴的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+" }");
}
示例11: toTypeWithoutCallee
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
private static Type[] toTypeWithoutCallee(final MethodType type, final int thisIndex) {
final int paramCount = type.parameterCount();
final Type[] t = new Type[paramCount - thisIndex];
for(int i = thisIndex; i < paramCount; ++i) {
t[i - thisIndex] = Type.typeFor(type.parameterType(i));
}
return t;
}
示例12: TypeMap
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
/**
* Constructor
* @param functionNodeId function node id
* @param type method type found at runtime corresponding to parameter guess
* @param needsCallee does the function using this type map need a callee
*/
public TypeMap(final int functionNodeId, final MethodType type, final boolean needsCallee) {
final Type[] types = new Type[type.parameterCount()];
int pos = 0;
for (final Class<?> p : type.parameterArray()) {
types[pos++] = Type.typeFor(p);
}
this.functionNodeId = functionNodeId;
this.paramTypes = types;
this.returnType = Type.typeFor(type.returnType());
this.needsCallee = needsCallee;
}
示例13: typeMatchesDescription
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
private static boolean typeMatchesDescription(final String paramTypes, final MethodType type) {
final StringTokenizer tok = new StringTokenizer(paramTypes, ", ");
for(int i = 1; i < type.parameterCount(); ++i) { // i = 1 as we ignore the receiver
if(!(tok.hasMoreTokens() && typeNameMatches(tok.nextToken(), type.parameterType(i)))) {
return false;
}
}
return !tok.hasMoreTokens();
}
示例14: TypeMap
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
/**
* Constructor
* @param functionNodeId function node id
* @param type method type found at runtime corresponding to parameter guess
* @param needsCallee does the function using this type map need a callee
*/
public TypeMap(final int functionNodeId, final MethodType type, final boolean needsCallee) {
final Type[] types = new Type[type.parameterCount()];
int pos = 0;
for (final Class<?> p : type.parameterArray()) {
types[pos++] = Type.typeFor(p);
}
paramTypeMap.put(functionNodeId, types);
returnTypeMap.put(functionNodeId, Type.typeFor(type.returnType()));
this.needsCallee = needsCallee;
}
示例15: isNullConversion
import java.lang.invoke.MethodType; //導入方法依賴的package包/類
/**
* True if a method handle can receive a call under a slightly different
* method type, without moving or reformatting any stack elements.
*
* @param call the type of call being made
* @param recv the type of the method handle receiving the call
* @return whether the retyping can be done without motion or reformatting
*/
public static boolean isNullConversion(MethodType call, MethodType recv, boolean keepInterfaces) {
if (call == recv) return true;
int len = call.parameterCount();
if (len != recv.parameterCount()) return false;
for (int i = 0; i < len; i++)
if (!isNullConversion(call.parameterType(i), recv.parameterType(i), keepInterfaces))
return false;
return isNullConversion(recv.returnType(), call.returnType(), keepInterfaces);
}