Java Code Examples for java.lang.invoke.MethodType#parameterCount()

The following examples show how to use java.lang.invoke.MethodType#parameterCount() . 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: ApplicableOverloadedMethods.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
boolean isApplicable(final MethodType callSiteType, final SingleDynamicMethod method) {
    final MethodType methodType = method.getMethodType();
    final int methodArity = methodType.parameterCount();
    if(methodArity != callSiteType.parameterCount()) {
        return false;
    }
    // 0th arg is receiver; it doesn't matter for overload
    // resolution.
    for(int i = 1; i < methodArity; ++i) {
        if(!TypeUtilities.isSubtype(callSiteType.parameterType(i), methodType.parameterType(i))) {
            return false;
        }
    }
    return true;
}
 
Example 2
Source File: ScriptFunction.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: ApplicableOverloadedMethods.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
boolean isApplicable(final MethodType callSiteType, final SingleDynamicMethod method) {
    final MethodType methodType = method.getMethodType();
    final int methodArity = methodType.parameterCount();
    if(methodArity != callSiteType.parameterCount()) {
        return false;
    }
    // 0th arg is receiver; it doesn't matter for overload
    // resolution.
    for(int i = 1; i < methodArity; ++i) {
        if(!TypeUtilities.isSubtype(callSiteType.parameterType(i), methodType.parameterType(i))) {
            return false;
        }
    }
    return true;
}
 
Example 4
Source File: ScriptFunction.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 5
Source File: OverloadedMethod.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
OverloadedMethod(List<MethodHandle> methodHandles, OverloadedDynamicMethod parent, MethodType callSiteType,
        LinkerServices linkerServices) {
    this.parent = parent;
    this.callSiteType = callSiteType;
    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(callSiteType), collecting);
}
 
Example 6
Source File: Properties.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle getComputedValueMethodHandle(Lookup lookup, Method method)
        throws IllegalAccessException {
    // check: (ExecutionContext) -> Object
    MethodHandle handle = lookup.unreflect(method);
    MethodType type = handle.type();
    if (type.parameterCount() != 1 || !ExecutionContext.class.equals(type.parameterType(0))) {
        throw new IllegalArgumentException(handle.toString());
    }
    if (!Object.class.equals(type.returnType())) {
        throw new IllegalArgumentException(handle.toString());
    }
    return handle;
}
 
Example 7
Source File: TypeMap.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: TypeMap.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 9
Source File: MethodTypeTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test of parameterCount method, of class MethodType.
 */
@Test
public void testParameterCount() {
    System.out.println("parameterCount");
    MethodType instance = mt_viS;
    int expResult = 2;
    int result = instance.parameterCount();
    assertEquals(expResult, result);
}
 
Example 10
Source File: TypeMap.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: MethodTypeTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test of parameterCount method, of class MethodType.
 */
@Test
public void testParameterCount() {
    System.out.println("parameterCount");
    MethodType instance = mt_viS;
    int expResult = 2;
    int result = instance.parameterCount();
    assertEquals(expResult, result);
}
 
Example 12
Source File: ScriptFunctionData.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: TypeMap.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: ScriptFunctionData.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 15
Source File: SingleDynamicMethod.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static boolean typeMatchesDescription(String paramTypes, 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();
}
 
Example 16
Source File: MethodTypeTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test of parameterCount method, of class MethodType.
 */
@Test
public void testParameterCount() {
    System.out.println("parameterCount");
    MethodType instance = mt_viS;
    int expResult = 2;
    int result = instance.parameterCount();
    assertEquals(expResult, result);
}
 
Example 17
Source File: SingleDynamicMethod.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 18
Source File: SingleDynamicMethod.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 19
Source File: OverloadedDynamicMethod.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isApplicableDynamically(final LinkerServices linkerServices, final MethodType callSiteType,
        final SingleDynamicMethod m) {
    final MethodType methodType = m.getMethodType();
    final boolean varArgs = m.isVarArgs();
    final int fixedArgLen = methodType.parameterCount() - (varArgs ? 1 : 0);
    final int callSiteArgLen = callSiteType.parameterCount();

    // Arity checks
    if(varArgs) {
        if(callSiteArgLen < fixedArgLen) {
            return false;
        }
    } else if(callSiteArgLen != fixedArgLen) {
        return false;
    }

    // Fixed arguments type checks, starting from 1, as receiver type doesn't participate
    for(int i = 1; i < fixedArgLen; ++i) {
        if(!isApplicableDynamically(linkerServices, callSiteType.parameterType(i), methodType.parameterType(i))) {
            return false;
        }
    }
    if(!varArgs) {
        // Not vararg; both arity and types matched.
        return true;
    }

    final Class<?> varArgArrayType = methodType.parameterType(fixedArgLen);
    final Class<?> varArgType = varArgArrayType.getComponentType();

    if(fixedArgLen == callSiteArgLen - 1) {
        // Exactly one vararg; check both array type matching and array component type matching.
        final Class<?> callSiteArgType = callSiteType.parameterType(fixedArgLen);
        return isApplicableDynamically(linkerServices, callSiteArgType, varArgArrayType)
                || isApplicableDynamically(linkerServices, callSiteArgType, varArgType);
    }

    // Either zero, or more than one vararg; check if all actual vararg types match the vararg array component type.
    for(int i = fixedArgLen; i < callSiteArgLen; ++i) {
        if(!isApplicableDynamically(linkerServices, callSiteType.parameterType(i), varArgType)) {
            return false;
        }
    }

    return true;
}
 
Example 20
Source File: ScriptFunctionData.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Heuristic to figure out if the method handle has a callee argument. If it's type is
 * {@code (ScriptFunction, ...)}, then we'll assume it has a callee argument. We need this as
 * the constructor above is not passed this information, and can't just blindly assume it's false
 * (notably, it's being invoked for creation of new scripts, and scripts have scopes, therefore
 * they also always receive a callee).
 *
 * @param mh the examined method handle
 *
 * @return true if the method handle expects a callee, false otherwise
 */
protected static boolean needsCallee(final MethodHandle mh) {
    final MethodType type = mh.type();
    return (type.parameterCount() > 0 && type.parameterType(0) == ScriptFunction.class);
}