Example usage for org.eclipse.jdt.internal.compiler.lookup InvocationSite genericTypeArguments

List of usage examples for org.eclipse.jdt.internal.compiler.lookup InvocationSite genericTypeArguments

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup InvocationSite genericTypeArguments.

Prototype

TypeBinding[] genericTypeArguments();

Source Link

Usage

From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java

License:Open Source License

/**
 * Internal use only/*from  w  w  w. java2s  . c om*/
 * Given a method, returns null if arguments cannot be converted to parameters.
 * Will answer a substituted method in case the method was generic and type inference got triggered;
 * in case the method was originally compatible, then simply answer it back.
 */
protected final MethodBinding computeCompatibleMethod(MethodBinding method, TypeBinding[] arguments,
        InvocationSite invocationSite) {
    TypeBinding[] genericTypeArguments = invocationSite.genericTypeArguments();
    TypeBinding[] parameters = method.parameters;
    TypeVariableBinding[] typeVariables = method.typeVariables;
    if (parameters == arguments && (method.returnType.tagBits & TagBits.HasTypeVariable) == 0
            && genericTypeArguments == null && typeVariables == Binding.NO_TYPE_VARIABLES)
        return method;

    int argLength = arguments.length;
    int paramLength = parameters.length;
    boolean isVarArgs = method.isVarargs();
    if (argLength != paramLength)
        if (!isVarArgs || argLength < paramLength - 1)
            return null; // incompatible

    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=330435, inference should kick in only at source 1.5+
    if (typeVariables != Binding.NO_TYPE_VARIABLES
            && compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5) { // generic method
        TypeBinding[] newArgs = null;
        for (int i = 0; i < argLength; i++) {
            TypeBinding param = i < paramLength ? parameters[i] : parameters[paramLength - 1];
            if (arguments[i].isBaseType() != param.isBaseType()) {
                if (newArgs == null) {
                    newArgs = new TypeBinding[argLength];
                    System.arraycopy(arguments, 0, newArgs, 0, argLength);
                }
                newArgs[i] = environment().computeBoxingType(arguments[i]);
            }
        }
        if (newArgs != null)
            arguments = newArgs;
        method = ParameterizedGenericMethodBinding.computeCompatibleMethod(method, arguments, this,
                invocationSite);
        if (method == null)
            return null; // incompatible
        if (!method.isValidBinding())
            return method; // bound check issue is taking precedence
    } else if (genericTypeArguments != null && compilerOptions().complianceLevel < ClassFileConstants.JDK1_7) {
        if (method instanceof ParameterizedGenericMethodBinding) {
            if (!((ParameterizedGenericMethodBinding) method).wasInferred)
                // attempt to invoke generic method of raw type with type hints <String>foo()
                return new ProblemMethodBinding(method, method.selector, genericTypeArguments,
                        ProblemReasons.TypeArgumentsForRawGenericMethod);
        } else if (!method.isOverriding() || !isOverriddenMethodGeneric(method)) {
            return new ProblemMethodBinding(method, method.selector, genericTypeArguments,
                    ProblemReasons.TypeParameterArityMismatch);
        }
    }

    if (parameterCompatibilityLevel(method, arguments) > NOT_COMPATIBLE) {
        if ((method.tagBits & TagBits.AnnotationPolymorphicSignature) != 0) {
            // generate polymorphic method
            return this.environment().createPolymorphicMethod(method, arguments);
        }
        return method;
    }
    // if method is generic and type arguments have been supplied, only then answer a problem 
    // of ParameterizedMethodTypeMismatch, else a non-generic method was invoked using type arguments
    // in which case this problem category will be bogus
    if (genericTypeArguments != null && typeVariables != Binding.NO_TYPE_VARIABLES)
        return new ProblemMethodBinding(method, method.selector, arguments,
                ProblemReasons.ParameterizedMethodTypeMismatch);
    return null; // incompatible
}

From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java

License:Open Source License

public MethodBinding findExactMethod(ReferenceBinding receiverType, char[] selector,
        TypeBinding[] argumentTypes, InvocationSite invocationSite) {
    CompilationUnitScope unitScope = compilationUnitScope();
    unitScope.recordTypeReferences(argumentTypes);
    MethodBinding exactMethod = receiverType.getExactMethod(selector, argumentTypes, unitScope);
    if (exactMethod != null && exactMethod.typeVariables == Binding.NO_TYPE_VARIABLES
            && !exactMethod.isBridge()) {
        // in >= 1.5 mode, ensure the exactMatch did not match raw types
        if (compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5)
            for (int i = argumentTypes.length; --i >= 0;)
                if (isPossibleSubtypeOfRawType(argumentTypes[i]))
                    return null;
        // must find both methods for this case: <S extends A> void foo() {}  and  <N extends B> N foo() { return null; }
        // or find an inherited method when the exact match is to a bridge method
        unitScope.recordTypeReferences(exactMethod.thrownExceptions);
        if (exactMethod.isAbstract() && exactMethod.thrownExceptions != Binding.NO_EXCEPTIONS)
            return null; // may need to merge exceptions with interface method
        // special treatment for Object.getClass() in 1.5 mode (substitute parameterized return type)
        if (receiverType.isInterface() || exactMethod.canBeSeenBy(receiverType, invocationSite, this)) {
            if (argumentTypes == Binding.NO_PARAMETERS && CharOperation.equals(selector, TypeConstants.GETCLASS)
                    && exactMethod.returnType.isParameterizedType()/*1.5*/) {
                return environment().createGetClassMethod(receiverType, exactMethod, this);
            }/*from   w w  w.  j a v a2s  . c o m*/
            // targeting a generic method could find an exact match with variable return type
            if (invocationSite.genericTypeArguments() != null) {
                exactMethod = computeCompatibleMethod(exactMethod, argumentTypes, invocationSite);
            }
            return exactMethod;
        }
    }
    return null;
}

From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java

License:Open Source License

public MethodBinding getConstructor(ReferenceBinding receiverType, TypeBinding[] argumentTypes,
        InvocationSite invocationSite) {
    CompilationUnitScope unitScope = compilationUnitScope();
    LookupEnvironment env = unitScope.environment;
    try {/*from   w  ww .  ja v  a2  s . c om*/
        env.missingClassFileLocation = invocationSite;
        unitScope.recordTypeReference(receiverType);
        unitScope.recordTypeReferences(argumentTypes);
        MethodBinding methodBinding = receiverType.getExactConstructor(argumentTypes);
        if (methodBinding != null && methodBinding.canBeSeenBy(invocationSite, this)) {
            // targeting a non generic constructor with type arguments ?
            if (invocationSite.genericTypeArguments() != null)
                methodBinding = computeCompatibleMethod(methodBinding, argumentTypes, invocationSite);
            return methodBinding;
        }
        MethodBinding[] methods = receiverType.getMethods(TypeConstants.INIT, argumentTypes.length);
        if (methods == Binding.NO_METHODS)
            return new ProblemMethodBinding(TypeConstants.INIT, argumentTypes, ProblemReasons.NotFound);

        MethodBinding[] compatible = new MethodBinding[methods.length];
        int compatibleIndex = 0;
        MethodBinding problemMethod = null;
        for (int i = 0, length = methods.length; i < length; i++) {
            MethodBinding compatibleMethod = computeCompatibleMethod(methods[i], argumentTypes, invocationSite);
            if (compatibleMethod != null) {
                if (compatibleMethod.isValidBinding())
                    compatible[compatibleIndex++] = compatibleMethod;
                else if (problemMethod == null)
                    problemMethod = compatibleMethod;
            }
        }
        if (compatibleIndex == 0) {
            if (problemMethod == null)
                return new ProblemMethodBinding(methods[0], TypeConstants.INIT, argumentTypes,
                        ProblemReasons.NotFound);
            return problemMethod;
        }
        // need a more descriptive error... cannot convert from X to Y

        MethodBinding[] visible = new MethodBinding[compatibleIndex];
        int visibleIndex = 0;
        for (int i = 0; i < compatibleIndex; i++) {
            MethodBinding method = compatible[i];
            if (method.canBeSeenBy(invocationSite, this))
                visible[visibleIndex++] = method;
        }
        if (visibleIndex == 1)
            return visible[0];
        if (visibleIndex == 0)
            return new ProblemMethodBinding(compatible[0], TypeConstants.INIT, compatible[0].parameters,
                    ProblemReasons.NotVisible);
        // all of visible are from the same declaringClass, even before 1.4 we can call this method instead of mostSpecificClassMethodBinding
        return mostSpecificMethodBinding(visible, visibleIndex, argumentTypes, invocationSite, receiverType);
    } catch (AbortCompilation e) {
        e.updateContext(invocationSite, referenceCompilationUnit().compilationResult);
        throw e;
    } finally {
        env.missingClassFileLocation = null;
    }
}