Java Code Examples for jdk.internal.dynalink.support.TypeUtilities#isMethodInvocationConvertible()

The following examples show how to use jdk.internal.dynalink.support.TypeUtilities#isMethodInvocationConvertible() . 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 TencentKona-8 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.isMethodInvocationConvertible(callSiteType.parameterType(i),
                methodType.parameterType(i))) {
            return false;
        }
    }
    return true;
}
 
Example 2
Source File: ApplicableOverloadedMethods.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
boolean isApplicable(MethodType callSiteType, 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.isMethodInvocationConvertible(callSiteType.parameterType(i),
                methodType.parameterType(i))) {
            return false;
        }
    }
    return true;
}
 
Example 3
Source File: ApplicableOverloadedMethods.java    From jdk8u60 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.isMethodInvocationConvertible(callSiteType.parameterType(i),
                methodType.parameterType(i))) {
            return false;
        }
    }
    return true;
}
 
Example 4
Source File: ApplicableOverloadedMethods.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
boolean isApplicable(MethodType callSiteType, 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.isMethodInvocationConvertible(callSiteType.parameterType(i),
                methodType.parameterType(i))) {
            return false;
        }
    }
    return true;
}
 
Example 5
Source File: ApplicableOverloadedMethods.java    From hottub 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.isMethodInvocationConvertible(callSiteType.parameterType(i),
                methodType.parameterType(i))) {
            return false;
        }
    }
    return true;
}
 
Example 6
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.isMethodInvocationConvertible(callSiteType.parameterType(i),
                methodType.parameterType(i))) {
            return false;
        }
    }
    return true;
}
 
Example 7
Source File: BeanLinker.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle convertArgToInt(MethodHandle mh, LinkerServices ls, CallSiteDescriptor desc) {
    final Class<?> sourceType = desc.getMethodType().parameterType(1);
    if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) {
        return mh;
    } else if(ls.canConvert(sourceType, Number.class)) {
        final MethodHandle converter = ls.getTypeConverter(sourceType, Number.class);
        return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType(
                mh.type().parameterType(1))));
    }
    return mh;
}
 
Example 8
Source File: ApplicableOverloadedMethods.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
boolean isApplicable(final MethodType callSiteType, final SingleDynamicMethod method) {
    if(!method.isVarArgs()) {
        return false;
    }
    final MethodType methodType = method.getMethodType();
    final int methodArity = methodType.parameterCount();
    final int fixArity = methodArity - 1;
    final int callSiteArity = callSiteType.parameterCount();
    if(fixArity > callSiteArity) {
        return false;
    }
    // 0th arg is receiver; it doesn't matter for overload
    // resolution.
    for(int i = 1; i < fixArity; ++i) {
        if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i),
                methodType.parameterType(i))) {
            return false;
        }
    }
    final Class<?> varArgType = methodType.parameterType(fixArity).getComponentType();
    for(int i = fixArity; i < callSiteArity; ++i) {
        if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i), varArgType)) {
            return false;
        }
    }
    return true;
}
 
Example 9
Source File: ApplicableOverloadedMethods.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
boolean isApplicable(MethodType callSiteType, SingleDynamicMethod method) {
    if(!method.isVarArgs()) {
        return false;
    }
    final MethodType methodType = method.getMethodType();
    final int methodArity = methodType.parameterCount();
    final int fixArity = methodArity - 1;
    final int callSiteArity = callSiteType.parameterCount();
    if(fixArity > callSiteArity) {
        return false;
    }
    // 0th arg is receiver; it doesn't matter for overload
    // resolution.
    for(int i = 1; i < fixArity; ++i) {
        if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i),
                methodType.parameterType(i))) {
            return false;
        }
    }
    final Class<?> varArgType = methodType.parameterType(fixArity).getComponentType();
    for(int i = fixArity; i < callSiteArity; ++i) {
        if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i), varArgType)) {
            return false;
        }
    }
    return true;
}
 
Example 10
Source File: BeanLinker.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle convertArgToInt(MethodHandle mh, LinkerServices ls, CallSiteDescriptor desc) {
    final Class<?> sourceType = desc.getMethodType().parameterType(1);
    if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) {
        return mh;
    } else if(ls.canConvert(sourceType, Number.class)) {
        final MethodHandle converter = ls.getTypeConverter(sourceType, Number.class);
        return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType(
                mh.type().parameterType(1))));
    }
    return mh;
}
 
Example 11
Source File: BeanLinker.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle convertArgToInt(final MethodHandle mh, final LinkerServices ls, final CallSiteDescriptor desc) {
    final Class<?> sourceType = desc.getMethodType().parameterType(1);
    if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) {
        return mh;
    } else if(ls.canConvert(sourceType, Number.class)) {
        final MethodHandle converter = ls.getTypeConverter(sourceType, Number.class);
        return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType(
                mh.type().parameterType(1))));
    }
    return mh;
}
 
Example 12
Source File: ApplicableOverloadedMethods.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
boolean isApplicable(final MethodType callSiteType, final SingleDynamicMethod method) {
    if(!method.isVarArgs()) {
        return false;
    }
    final MethodType methodType = method.getMethodType();
    final int methodArity = methodType.parameterCount();
    final int fixArity = methodArity - 1;
    final int callSiteArity = callSiteType.parameterCount();
    if(fixArity > callSiteArity) {
        return false;
    }
    // 0th arg is receiver; it doesn't matter for overload
    // resolution.
    for(int i = 1; i < fixArity; ++i) {
        if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i),
                methodType.parameterType(i))) {
            return false;
        }
    }
    final Class<?> varArgType = methodType.parameterType(fixArity).getComponentType();
    for(int i = fixArity; i < callSiteArity; ++i) {
        if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i), varArgType)) {
            return false;
        }
    }
    return true;
}
 
Example 13
Source File: ApplicableOverloadedMethods.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
boolean isApplicable(final MethodType callSiteType, final SingleDynamicMethod method) {
    if(!method.isVarArgs()) {
        return false;
    }
    final MethodType methodType = method.getMethodType();
    final int methodArity = methodType.parameterCount();
    final int fixArity = methodArity - 1;
    final int callSiteArity = callSiteType.parameterCount();
    if(fixArity > callSiteArity) {
        return false;
    }
    // 0th arg is receiver; it doesn't matter for overload
    // resolution.
    for(int i = 1; i < fixArity; ++i) {
        if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i),
                methodType.parameterType(i))) {
            return false;
        }
    }
    final Class<?> varArgType = methodType.parameterType(fixArity).getComponentType();
    for(int i = fixArity; i < callSiteArity; ++i) {
        if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i), varArgType)) {
            return false;
        }
    }
    return true;
}
 
Example 14
Source File: ApplicableOverloadedMethods.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
boolean isApplicable(final MethodType callSiteType, final SingleDynamicMethod method) {
    if(!method.isVarArgs()) {
        return false;
    }
    final MethodType methodType = method.getMethodType();
    final int methodArity = methodType.parameterCount();
    final int fixArity = methodArity - 1;
    final int callSiteArity = callSiteType.parameterCount();
    if(fixArity > callSiteArity) {
        return false;
    }
    // 0th arg is receiver; it doesn't matter for overload
    // resolution.
    for(int i = 1; i < fixArity; ++i) {
        if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i),
                methodType.parameterType(i))) {
            return false;
        }
    }
    final Class<?> varArgType = methodType.parameterType(fixArity).getComponentType();
    for(int i = fixArity; i < callSiteArity; ++i) {
        if(!TypeUtilities.isMethodInvocationConvertible(callSiteType.parameterType(i), varArgType)) {
            return false;
        }
    }
    return true;
}
 
Example 15
Source File: NashornPrimitiveLinker.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Implements the somewhat involved prioritization of JavaScript primitive types conversions. Instead of explaining
 * it here in prose, just follow the source code comments.
 * @param sourceType the source type to convert from
 * @param targetType1 one candidate target type
 * @param targetType2 another candidate target type
 * @return one of {@link jdk.internal.dynalink.linker.ConversionComparator.Comparison} values signifying which
 * target type should be favored for conversion.
 */
@Override
public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) {
    final Class<?> wrapper1 = getWrapperTypeOrSelf(targetType1);
    if (sourceType == wrapper1) {
        // Source type exactly matches target 1
        return Comparison.TYPE_1_BETTER;
    }
    final Class<?> wrapper2 = getWrapperTypeOrSelf(targetType2);
    if (sourceType == wrapper2) {
        // Source type exactly matches target 2
        return Comparison.TYPE_2_BETTER;
    }

    if (Number.class.isAssignableFrom(sourceType)) {
        // If exactly one of the targets is a number, pick it.
        if (Number.class.isAssignableFrom(wrapper1)) {
            if (!Number.class.isAssignableFrom(wrapper2)) {
                return Comparison.TYPE_1_BETTER;
            }
        } else if (Number.class.isAssignableFrom(wrapper2)) {
            return Comparison.TYPE_2_BETTER;
        }

        // If exactly one of the targets is a character, pick it. Numbers can be reasonably converted to chars using
        // the UTF-16 values.
        if (Character.class == wrapper1) {
            return Comparison.TYPE_1_BETTER;
        } else if (Character.class == wrapper2) {
            return Comparison.TYPE_2_BETTER;
        }

        // For all other cases, we fall through to the next if statement - not that we repeat the condition in it
        // too so if we entered this branch, we'll enter the below if statement too.
    }

    if (sourceType == String.class || sourceType == Boolean.class || Number.class.isAssignableFrom(sourceType)) {
        // Treat wrappers as primitives.
        final Class<?> primitiveType1 = getPrimitiveTypeOrSelf(targetType1);
        final Class<?> primitiveType2 = getPrimitiveTypeOrSelf(targetType2);
        // Basically, choose the widest possible primitive type. (First "if" returning TYPE_2_BETTER is correct;
        // when faced with a choice between double and int, choose double).
        if (TypeUtilities.isMethodInvocationConvertible(primitiveType1, primitiveType2)) {
            return Comparison.TYPE_2_BETTER;
        } else if (TypeUtilities.isMethodInvocationConvertible(primitiveType2, primitiveType1)) {
            return Comparison.TYPE_1_BETTER;
        }
        // Ok, at this point we're out of possible number conversions, so try strings. A String can represent any
        // value without loss, so if one of the potential targets is string, go for it.
        if (targetType1 == String.class) {
            return Comparison.TYPE_1_BETTER;
        }
        if (targetType2 == String.class) {
            return Comparison.TYPE_2_BETTER;
        }
    }

    return Comparison.INDETERMINATE;
}
 
Example 16
Source File: NashornPrimitiveLinker.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Implements the somewhat involved prioritization of JavaScript primitive types conversions. Instead of explaining
 * it here in prose, just follow the source code comments.
 * @param sourceType the source type to convert from
 * @param targetType1 one candidate target type
 * @param targetType2 another candidate target type
 * @return one of {@link jdk.internal.dynalink.linker.ConversionComparator.Comparison} values signifying which
 * target type should be favored for conversion.
 */
@Override
public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) {
    final Class<?> wrapper1 = getWrapperTypeOrSelf(targetType1);
    if (sourceType == wrapper1) {
        // Source type exactly matches target 1
        return Comparison.TYPE_1_BETTER;
    }
    final Class<?> wrapper2 = getWrapperTypeOrSelf(targetType2);
    if (sourceType == wrapper2) {
        // Source type exactly matches target 2
        return Comparison.TYPE_2_BETTER;
    }

    if (Number.class.isAssignableFrom(sourceType)) {
        // If exactly one of the targets is a number, pick it.
        if (Number.class.isAssignableFrom(wrapper1)) {
            if (!Number.class.isAssignableFrom(wrapper2)) {
                return Comparison.TYPE_1_BETTER;
            }
        } else if (Number.class.isAssignableFrom(wrapper2)) {
            return Comparison.TYPE_2_BETTER;
        }

        // If exactly one of the targets is a character, pick it. Numbers can be reasonably converted to chars using
        // the UTF-16 values.
        if (Character.class == wrapper1) {
            return Comparison.TYPE_1_BETTER;
        } else if (Character.class == wrapper2) {
            return Comparison.TYPE_2_BETTER;
        }

        // For all other cases, we fall through to the next if statement - not that we repeat the condition in it
        // too so if we entered this branch, we'll enter the below if statement too.
    }

    if (sourceType == String.class || sourceType == Boolean.class || Number.class.isAssignableFrom(sourceType)) {
        // Treat wrappers as primitives.
        final Class<?> primitiveType1 = getPrimitiveTypeOrSelf(targetType1);
        final Class<?> primitiveType2 = getPrimitiveTypeOrSelf(targetType2);
        // Basically, choose the widest possible primitive type. (First "if" returning TYPE_2_BETTER is correct;
        // when faced with a choice between double and int, choose double).
        if (TypeUtilities.isMethodInvocationConvertible(primitiveType1, primitiveType2)) {
            return Comparison.TYPE_2_BETTER;
        } else if (TypeUtilities.isMethodInvocationConvertible(primitiveType2, primitiveType1)) {
            return Comparison.TYPE_1_BETTER;
        }
        // Ok, at this point we're out of possible number conversions, so try strings. A String can represent any
        // value without loss, so if one of the potential targets is string, go for it.
        if (targetType1 == String.class) {
            return Comparison.TYPE_1_BETTER;
        }
        if (targetType2 == String.class) {
            return Comparison.TYPE_2_BETTER;
        }
    }

    return Comparison.INDETERMINATE;
}
 
Example 17
Source File: ClassString.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static boolean canConvert(final LinkerServices ls, final Class<?> from, final Class<?> to) {
    if(from == NULL_CLASS) {
        return !to.isPrimitive();
    }
    return ls == null ? TypeUtilities.isMethodInvocationConvertible(from, to) : ls.canConvert(from, to);
}
 
Example 18
Source File: NashornPrimitiveLinker.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Implements the somewhat involved prioritization of JavaScript primitive types conversions. Instead of explaining
 * it here in prose, just follow the source code comments.
 * @param sourceType the source type to convert from
 * @param targetType1 one candidate target type
 * @param targetType2 another candidate target type
 * @return one of {@link jdk.internal.dynalink.linker.ConversionComparator.Comparison} values signifying which
 * target type should be favored for conversion.
 */
@Override
public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) {
    final Class<?> wrapper1 = getWrapperTypeOrSelf(targetType1);
    if (sourceType == wrapper1) {
        // Source type exactly matches target 1
        return Comparison.TYPE_1_BETTER;
    }
    final Class<?> wrapper2 = getWrapperTypeOrSelf(targetType2);
    if (sourceType == wrapper2) {
        // Source type exactly matches target 2
        return Comparison.TYPE_2_BETTER;
    }

    if (Number.class.isAssignableFrom(sourceType)) {
        // If exactly one of the targets is a number, pick it.
        if (Number.class.isAssignableFrom(wrapper1)) {
            if (!Number.class.isAssignableFrom(wrapper2)) {
                return Comparison.TYPE_1_BETTER;
            }
        } else if (Number.class.isAssignableFrom(wrapper2)) {
            return Comparison.TYPE_2_BETTER;
        }

        // If exactly one of the targets is a character, pick it. Numbers can be reasonably converted to chars using
        // the UTF-16 values.
        if (Character.class == wrapper1) {
            return Comparison.TYPE_1_BETTER;
        } else if (Character.class == wrapper2) {
            return Comparison.TYPE_2_BETTER;
        }

        // For all other cases, we fall through to the next if statement - not that we repeat the condition in it
        // too so if we entered this branch, we'll enter the below if statement too.
    }

    if (sourceType == String.class || sourceType == Boolean.class || Number.class.isAssignableFrom(sourceType)) {
        // Treat wrappers as primitives.
        final Class<?> primitiveType1 = getPrimitiveTypeOrSelf(targetType1);
        final Class<?> primitiveType2 = getPrimitiveTypeOrSelf(targetType2);
        // Basically, choose the widest possible primitive type. (First "if" returning TYPE_2_BETTER is correct;
        // when faced with a choice between double and int, choose double).
        if (TypeUtilities.isMethodInvocationConvertible(primitiveType1, primitiveType2)) {
            return Comparison.TYPE_2_BETTER;
        } else if (TypeUtilities.isMethodInvocationConvertible(primitiveType2, primitiveType1)) {
            return Comparison.TYPE_1_BETTER;
        }
        // Ok, at this point we're out of possible number conversions, so try strings. A String can represent any
        // value without loss, so if one of the potential targets is string, go for it.
        if (targetType1 == String.class) {
            return Comparison.TYPE_1_BETTER;
        }
        if (targetType2 == String.class) {
            return Comparison.TYPE_2_BETTER;
        }
    }

    return Comparison.INDETERMINATE;
}
 
Example 19
Source File: ClassString.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
private static boolean canConvert(LinkerServices ls, Class<?> from, Class<?> to) {
    if(from == NULL_CLASS) {
        return !to.isPrimitive();
    }
    return ls == null ? TypeUtilities.isMethodInvocationConvertible(from, to) : ls.canConvert(from, to);
}
 
Example 20
Source File: ClassString.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static boolean canConvert(final LinkerServices ls, final Class<?> from, final Class<?> to) {
    if(from == NULL_CLASS) {
        return !to.isPrimitive();
    }
    return ls == null ? TypeUtilities.isMethodInvocationConvertible(from, to) : ls.canConvert(from, to);
}