Search in sources :

Example 1 with InferenceVariable

use of org.eclipse.jdt.internal.compiler.lookup.InferenceVariable in project bazel-jdt-java-toolchain by salesforce.

the class MessageSend method resolveType.

@Override
public TypeBinding resolveType(BlockScope scope) {
    // Base type promotion
    if (this.constant != Constant.NotAConstant) {
        this.constant = Constant.NotAConstant;
        long sourceLevel = scope.compilerOptions().sourceLevel;
        boolean receiverCast = false;
        if (this.receiver instanceof CastExpression) {
            // will check later on
            this.receiver.bits |= ASTNode.DisableUnnecessaryCastCheck;
            receiverCast = true;
        }
        this.actualReceiverType = this.receiver.resolveType(scope);
        if (this.actualReceiverType instanceof InferenceVariable) {
            // not yet ready for resolving
            return null;
        }
        this.receiverIsType = this.receiver.isType();
        if (receiverCast && this.actualReceiverType != null) {
            // due to change of declaring class with receiver type, only identity cast should be notified
            TypeBinding resolvedType2 = ((CastExpression) this.receiver).expression.resolvedType;
            if (TypeBinding.equalsEquals(resolvedType2, this.actualReceiverType)) {
                if (!scope.environment().usesNullTypeAnnotations() || !NullAnnotationMatching.analyse(this.actualReceiverType, resolvedType2, -1).isAnyMismatch()) {
                    scope.problemReporter().unnecessaryCast((CastExpression) this.receiver);
                }
            }
        }
        // resolve type arguments (for generic constructor call)
        if (this.typeArguments != null) {
            int length = this.typeArguments.length;
            // typeChecks all arguments
            this.argumentsHaveErrors = sourceLevel < ClassFileConstants.JDK1_5;
            this.genericTypeArguments = new TypeBinding[length];
            for (int i = 0; i < length; i++) {
                TypeReference typeReference = this.typeArguments[i];
                if ((this.genericTypeArguments[i] = typeReference.resolveType(scope, true, /* check bounds*/
                Binding.DefaultLocationTypeArgument)) == null) {
                    this.argumentsHaveErrors = true;
                }
                if (this.argumentsHaveErrors && typeReference instanceof Wildcard) {
                    scope.problemReporter().illegalUsageOfWildcard(typeReference);
                }
            }
            if (this.argumentsHaveErrors) {
                if (this.arguments != null) {
                    // still attempt to resolve arguments
                    for (int i = 0, max = this.arguments.length; i < max; i++) {
                        this.arguments[i].resolveType(scope);
                    }
                }
                return null;
            }
        }
        // will check for null after args are resolved
        if (this.arguments != null) {
            // typeChecks all arguments
            this.argumentsHaveErrors = false;
            int length = this.arguments.length;
            this.argumentTypes = new TypeBinding[length];
            for (int i = 0; i < length; i++) {
                Expression argument = this.arguments[i];
                if (this.arguments[i].resolvedType != null)
                    // $NON-NLS-1$
                    scope.problemReporter().genericInferenceError("Argument was unexpectedly found resolved", this);
                if (argument instanceof CastExpression) {
                    // will check later on
                    argument.bits |= ASTNode.DisableUnnecessaryCastCheck;
                    this.argsContainCast = true;
                }
                argument.setExpressionContext(INVOCATION_CONTEXT);
                if ((this.argumentTypes[i] = argument.resolveType(scope)) == null) {
                    this.argumentsHaveErrors = true;
                }
            }
            if (this.argumentsHaveErrors) {
                if (this.actualReceiverType instanceof ReferenceBinding) {
                    // record a best guess, for clients who need hint about possible method match
                    TypeBinding[] pseudoArgs = new TypeBinding[length];
                    for (int i = length; --i >= 0; ) // replace args with errors with null type
                    pseudoArgs[i] = this.argumentTypes[i] == null ? TypeBinding.NULL : this.argumentTypes[i];
                    this.binding = this.receiver.isImplicitThis() ? scope.getImplicitMethod(this.selector, pseudoArgs, this) : scope.findMethod((ReferenceBinding) this.actualReceiverType, this.selector, pseudoArgs, this, false);
                    if (this.binding != null && !this.binding.isValidBinding()) {
                        MethodBinding closestMatch = ((ProblemMethodBinding) this.binding).closestMatch;
                        // record the closest match, for clients who may still need hint about possible method match
                        if (closestMatch != null) {
                            if (closestMatch.original().typeVariables != Binding.NO_TYPE_VARIABLES) {
                                // generic method
                                // shouldn't return generic method outside its context, rather convert it to raw method (175409)
                                closestMatch = scope.environment().createParameterizedGenericMethod(closestMatch.original(), (RawTypeBinding) null);
                            }
                            this.binding = closestMatch;
                            MethodBinding closestMatchOriginal = closestMatch.original();
                            if (closestMatchOriginal.isOrEnclosedByPrivateType() && !scope.isDefinedInMethod(closestMatchOriginal)) {
                                // ignore cases where method is used from within inside itself (e.g. direct recursions)
                                closestMatchOriginal.modifiers |= ExtraCompilerModifiers.AccLocallyUsed;
                            }
                        }
                    }
                }
                return null;
            }
        }
        if (this.actualReceiverType == null) {
            return null;
        }
        // base type cannot receive any message
        if (this.actualReceiverType.isBaseType()) {
            scope.problemReporter().errorNoMethodFor(this, this.actualReceiverType, this.argumentTypes);
            return null;
        }
    }
    TypeBinding methodType = findMethodBinding(scope);
    if (methodType != null && methodType.isPolyType()) {
        this.resolvedType = this.binding.returnType.capture(scope, this.sourceStart, this.sourceEnd);
        return methodType;
    }
    if (!this.binding.isValidBinding()) {
        if (this.binding.declaringClass == null) {
            if (this.actualReceiverType instanceof ReferenceBinding) {
                this.binding.declaringClass = (ReferenceBinding) this.actualReceiverType;
            } else {
                scope.problemReporter().errorNoMethodFor(this, this.actualReceiverType, this.argumentTypes);
                return null;
            }
        }
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=245007 avoid secondary errors in case of
        // missing super type for anonymous classes ...
        ReferenceBinding declaringClass = this.binding.declaringClass;
        boolean avoidSecondary = declaringClass != null && declaringClass.isAnonymousType() && declaringClass.superclass() instanceof MissingTypeBinding;
        if (!avoidSecondary)
            scope.problemReporter().invalidMethod(this, this.binding, scope);
        MethodBinding closestMatch = ((ProblemMethodBinding) this.binding).closestMatch;
        switch(this.binding.problemId()) {
            case ProblemReasons.Ambiguous:
                // no resilience on ambiguous
                break;
            case ProblemReasons.InferredApplicableMethodInapplicable:
            case ProblemReasons.InvocationTypeInferenceFailure:
                // Grabbing the closest match improves error reporting in nested invocation contexts
                if (this.expressionContext != INVOCATION_CONTEXT)
                    break;
            // $FALL-THROUGH$
            case ProblemReasons.NotVisible:
            case ProblemReasons.NonStaticReferenceInConstructorInvocation:
            case ProblemReasons.NonStaticReferenceInStaticContext:
            case ProblemReasons.ReceiverTypeNotVisible:
            case ProblemReasons.ParameterBoundMismatch:
                // only steal returnType in cases listed above
                if (closestMatch != null)
                    this.resolvedType = closestMatch.returnType;
                break;
            case ProblemReasons.ContradictoryNullAnnotations:
                if (closestMatch != null && closestMatch.returnType != null)
                    this.resolvedType = closestMatch.returnType.withoutToplevelNullAnnotation();
                break;
        }
        // record the closest match, for clients who may still need hint about possible method match
        if (closestMatch != null) {
            this.binding = closestMatch;
            MethodBinding closestMatchOriginal = closestMatch.original();
            if (closestMatchOriginal.isOrEnclosedByPrivateType() && !scope.isDefinedInMethod(closestMatchOriginal)) {
                // ignore cases where method is used from within inside itself (e.g. direct recursions)
                closestMatchOriginal.modifiers |= ExtraCompilerModifiers.AccLocallyUsed;
            }
        }
        return (this.resolvedType != null && (this.resolvedType.tagBits & TagBits.HasMissingType) == 0) ? this.resolvedType : null;
    }
    final CompilerOptions compilerOptions = scope.compilerOptions();
    if (compilerOptions.complianceLevel <= ClassFileConstants.JDK1_6 && this.binding.isPolymorphic()) {
        scope.problemReporter().polymorphicMethodNotBelow17(this);
        return null;
    }
    if (compilerOptions.isAnnotationBasedNullAnalysisEnabled) {
        ImplicitNullAnnotationVerifier.ensureNullnessIsKnown(this.binding, scope);
        if (compilerOptions.sourceLevel >= ClassFileConstants.JDK1_8) {
            if (this.binding instanceof ParameterizedGenericMethodBinding && this.typeArguments != null) {
                TypeVariableBinding[] typeVariables = this.binding.original().typeVariables();
                for (int i = 0; i < this.typeArguments.length; i++) this.typeArguments[i].checkNullConstraints(scope, (ParameterizedGenericMethodBinding) this.binding, typeVariables, i);
            }
        }
    }
    if (this.binding.isPolymorphic()) {
        boolean resultDetermined = compilerOptions.sourceLevel >= ClassFileConstants.JDK9 && (this.binding.returnType == TypeBinding.VOID || this.binding.returnType.id != TypeIds.T_JavaLangObject);
        if (!resultDetermined && ((this.bits & ASTNode.InsideExpressionStatement) != 0)) {
            // we only set the return type to be void if this method invocation is used inside an expression statement
            this.binding = scope.environment().updatePolymorphicMethodReturnType((PolymorphicMethodBinding) this.binding, TypeBinding.VOID);
        }
    }
    if ((this.binding.tagBits & TagBits.HasMissingType) != 0) {
        scope.problemReporter().missingTypeInMethod(this, this.binding);
    }
    if (!this.binding.isStatic()) {
        // the "receiver" must not be a type
        if (this.receiverIsType) {
            scope.problemReporter().mustUseAStaticMethod(this, this.binding);
            if (this.actualReceiverType.isRawType() && (this.receiver.bits & ASTNode.IgnoreRawTypeCheck) == 0 && compilerOptions.getSeverity(CompilerOptions.RawTypeReference) != ProblemSeverities.Ignore) {
                scope.problemReporter().rawTypeReference(this.receiver, this.actualReceiverType);
            }
        } else {
            // handle indirect inheritance thru variable secondary bound
            // receiver may receive generic cast, as part of implicit conversion
            TypeBinding oldReceiverType = this.actualReceiverType;
            this.actualReceiverType = this.actualReceiverType.getErasureCompatibleType(this.binding.declaringClass);
            this.receiver.computeConversion(scope, this.actualReceiverType, this.actualReceiverType);
            if (TypeBinding.notEquals(this.actualReceiverType, oldReceiverType) && TypeBinding.notEquals(this.receiver.postConversionType(scope), this.actualReceiverType)) {
                // record need for explicit cast at codegen since receiver could not handle it
                this.bits |= NeedReceiverGenericCast;
            }
        }
    } else {
        // static message invoked through receiver? legal but unoptimal (optional warning).
        if (this.binding.declaringClass.isInterface() && !((isTypeAccess() || this.receiver.isImplicitThis()) && TypeBinding.equalsEquals(this.binding.declaringClass, this.actualReceiverType))) {
            scope.problemReporter().nonStaticOrAlienTypeReceiver(this, this.binding);
        } else if (!(this.receiver.isImplicitThis() || this.receiver.isSuper() || this.receiverIsType)) {
            scope.problemReporter().nonStaticAccessToStaticMethod(this, this.binding);
        }
        if (!this.receiver.isImplicitThis() && TypeBinding.notEquals(this.binding.declaringClass, this.actualReceiverType)) {
            scope.problemReporter().indirectAccessToStaticMethod(this, this.binding);
        }
    }
    if (checkInvocationArguments(scope, this.receiver, this.actualReceiverType, this.binding, this.arguments, this.argumentTypes, this.argsContainCast, this)) {
        this.bits |= ASTNode.Unchecked;
    }
    // -------message send that are known to fail at compile time-----------
    if (this.binding.isAbstract()) {
        if (this.receiver.isSuper()) {
            scope.problemReporter().cannotDireclyInvokeAbstractMethod(this, this.binding);
        }
    // abstract private methods cannot occur nor abstract static............
    }
    if (isMethodUseDeprecated(this.binding, scope, true, this))
        scope.problemReporter().deprecatedMethod(this.binding, this);
    TypeBinding returnType;
    if ((this.bits & ASTNode.Unchecked) != 0 && this.genericTypeArguments == null) {
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=277643, align with javac on JLS 15.12.2.6
        returnType = this.binding.returnType;
        if (returnType != null) {
            returnType = scope.environment().convertToRawType(returnType.erasure(), true);
        }
    } else {
        returnType = this.binding.returnType;
        if (returnType != null) {
            returnType = returnType.capture(scope, this.sourceStart, this.sourceEnd);
        }
    }
    this.resolvedType = returnType;
    if (this.receiver.isSuper() && compilerOptions.getSeverity(CompilerOptions.OverridingMethodWithoutSuperInvocation) != ProblemSeverities.Ignore) {
        final ReferenceContext referenceContext = scope.methodScope().referenceContext;
        if (referenceContext instanceof AbstractMethodDeclaration) {
            final AbstractMethodDeclaration abstractMethodDeclaration = (AbstractMethodDeclaration) referenceContext;
            MethodBinding enclosingMethodBinding = abstractMethodDeclaration.binding;
            if (enclosingMethodBinding.isOverriding() && CharOperation.equals(this.binding.selector, enclosingMethodBinding.selector) && this.binding.areParametersEqual(enclosingMethodBinding)) {
                abstractMethodDeclaration.bits |= ASTNode.OverridingMethodWithSupercall;
            }
        }
    }
    if (this.receiver.isSuper() && this.actualReceiverType.isInterface()) {
        // 15.12.3 (Java 8)
        scope.checkAppropriateMethodAgainstSupers(this.selector, this.binding, this.argumentTypes, this);
    }
    if (this.typeArguments != null && this.binding.original().typeVariables == Binding.NO_TYPE_VARIABLES) {
        scope.problemReporter().unnecessaryTypeArgumentsForMethodInvocation(this.binding, this.genericTypeArguments, this.typeArguments);
    }
    return (this.resolvedType.tagBits & TagBits.HasMissingType) == 0 ? this.resolvedType : null;
}
Also used : TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) PolyTypeBinding(org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding) RawTypeBinding(org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding) PolymorphicMethodBinding(org.eclipse.jdt.internal.compiler.lookup.PolymorphicMethodBinding) PolyParameterizedGenericMethodBinding(org.eclipse.jdt.internal.compiler.lookup.PolyParameterizedGenericMethodBinding) ParameterizedGenericMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedGenericMethodBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) ProblemReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding) InferenceVariable(org.eclipse.jdt.internal.compiler.lookup.InferenceVariable) RawTypeBinding(org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) ReferenceContext(org.eclipse.jdt.internal.compiler.impl.ReferenceContext) PolymorphicMethodBinding(org.eclipse.jdt.internal.compiler.lookup.PolymorphicMethodBinding) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) ParameterizedMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedMethodBinding) PolyParameterizedGenericMethodBinding(org.eclipse.jdt.internal.compiler.lookup.PolyParameterizedGenericMethodBinding) ParameterizedGenericMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedGenericMethodBinding)

Aggregations

CompilerOptions (org.eclipse.jdt.internal.compiler.impl.CompilerOptions)1 ReferenceContext (org.eclipse.jdt.internal.compiler.impl.ReferenceContext)1 InferenceVariable (org.eclipse.jdt.internal.compiler.lookup.InferenceVariable)1 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)1 MissingTypeBinding (org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding)1 ParameterizedGenericMethodBinding (org.eclipse.jdt.internal.compiler.lookup.ParameterizedGenericMethodBinding)1 ParameterizedMethodBinding (org.eclipse.jdt.internal.compiler.lookup.ParameterizedMethodBinding)1 PolyParameterizedGenericMethodBinding (org.eclipse.jdt.internal.compiler.lookup.PolyParameterizedGenericMethodBinding)1 PolyTypeBinding (org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding)1 PolymorphicMethodBinding (org.eclipse.jdt.internal.compiler.lookup.PolymorphicMethodBinding)1 ProblemMethodBinding (org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding)1 ProblemReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding)1 RawTypeBinding (org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding)1 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)1 SourceTypeBinding (org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding)1 TypeBinding (org.eclipse.jdt.internal.compiler.lookup.TypeBinding)1 TypeVariableBinding (org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding)1