Search in sources :

Example 1 with InvocationSite

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

the class NullAnnotationMatching method checkForContradictions.

/**
 * After a method has substituted type parameters, check if this resulted in any contradictory null annotations.
 * Problems are either reported directly (if scope != null) or by returning a ProblemMethodBinding.
 */
public static MethodBinding checkForContradictions(MethodBinding method, Object location, Scope scope) {
    int start = 0, end = 0;
    if (location instanceof InvocationSite) {
        start = ((InvocationSite) location).sourceStart();
        end = ((InvocationSite) location).sourceEnd();
    } else if (location instanceof ASTNode) {
        start = ((ASTNode) location).sourceStart;
        end = ((ASTNode) location).sourceEnd;
    }
    SearchContradictions searchContradiction = new SearchContradictions();
    TypeBindingVisitor.visit(searchContradiction, method.returnType);
    if (searchContradiction.typeWithContradiction != null) {
        if (scope == null)
            return new ProblemMethodBinding(method, method.selector, method.parameters, ProblemReasons.ContradictoryNullAnnotations);
        scope.problemReporter().contradictoryNullAnnotationsInferred(method, start, end, location instanceof FunctionalExpression);
        // note: if needed, we might want to update the method by removing the contradictory annotations??
        return method;
    }
    Expression[] arguments = null;
    if (location instanceof Invocation)
        arguments = ((Invocation) location).arguments();
    for (int i = 0; i < method.parameters.length; i++) {
        TypeBindingVisitor.visit(searchContradiction, method.parameters[i]);
        if (searchContradiction.typeWithContradiction != null) {
            if (scope == null)
                return new ProblemMethodBinding(method, method.selector, method.parameters, ProblemReasons.ContradictoryNullAnnotations);
            if (arguments != null && i < arguments.length)
                scope.problemReporter().contradictoryNullAnnotationsInferred(method, arguments[i]);
            else
                scope.problemReporter().contradictoryNullAnnotationsInferred(method, start, end, location instanceof FunctionalExpression);
            return method;
        }
    }
    return method;
}
Also used : ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) InvocationSite(org.eclipse.jdt.internal.compiler.lookup.InvocationSite)

Example 2 with InvocationSite

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

the class ProblemReporter method isClassPathCorrect.

public void isClassPathCorrect(char[][] wellKnownTypeName, CompilationUnitDeclaration compUnitDecl, Object location, boolean implicitAnnotationUse) {
    // ProblemReporter is not designed to be reentrant. Just in case, we discovered a build path problem while we are already
    // in the midst of reporting some other problem, save and restore reference context thereby mimicking a stack.
    // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=442755.
    ReferenceContext savedContext = this.referenceContext;
    this.referenceContext = compUnitDecl;
    String[] arguments = new String[] { CharOperation.toString(wellKnownTypeName) };
    int start = 0, end = 0;
    if (location != null) {
        if (location instanceof InvocationSite) {
            InvocationSite site = (InvocationSite) location;
            start = site.sourceStart();
            end = site.sourceEnd();
        } else if (location instanceof ASTNode) {
            ASTNode node = (ASTNode) location;
            start = node.sourceStart();
            end = node.sourceEnd();
        }
    }
    try {
        this.handle(implicitAnnotationUse ? IProblem.MissingNullAnnotationImplicitlyUsed : IProblem.IsClassPathCorrect, arguments, arguments, start, end);
    } finally {
        this.referenceContext = savedContext;
    }
}
Also used : InvocationSite(org.eclipse.jdt.internal.compiler.lookup.InvocationSite) ReferenceContext(org.eclipse.jdt.internal.compiler.impl.ReferenceContext) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode)

Example 3 with InvocationSite

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

the class ProblemReporter method conflictingPackageInModules.

public void conflictingPackageInModules(char[][] wellKnownTypeName, CompilationUnitDeclaration compUnitDecl, Object location, char[] packageName, char[] expectedModuleName, char[] conflictingModuleName) {
    ReferenceContext savedContext = this.referenceContext;
    this.referenceContext = compUnitDecl;
    String[] arguments = new String[] { CharOperation.toString(wellKnownTypeName), new String(packageName), new String(expectedModuleName), new String(conflictingModuleName) };
    int start = 0, end = 0;
    if (location != null) {
        if (location instanceof InvocationSite) {
            InvocationSite site = (InvocationSite) location;
            start = site.sourceStart();
            end = site.sourceEnd();
        } else if (location instanceof ASTNode) {
            ASTNode node = (ASTNode) location;
            start = node.sourceStart();
            end = node.sourceEnd();
        }
    }
    try {
        this.handle(IProblem.ConflictingPackageInModules, arguments, arguments, start, end);
    } finally {
        this.referenceContext = savedContext;
    }
}
Also used : InvocationSite(org.eclipse.jdt.internal.compiler.lookup.InvocationSite) ReferenceContext(org.eclipse.jdt.internal.compiler.impl.ReferenceContext) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode)

Example 4 with InvocationSite

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

the class CastExpression method checkAlternateBinding.

private static void checkAlternateBinding(BlockScope scope, Expression receiver, TypeBinding receiverType, MethodBinding binding, Expression[] arguments, TypeBinding[] originalArgumentTypes, TypeBinding[] alternateArgumentTypes, final InvocationSite invocationSite) {
    InvocationSite fakeInvocationSite = new InvocationSite() {

        @Override
        public TypeBinding[] genericTypeArguments() {
            return null;
        }

        @Override
        public boolean isSuperAccess() {
            return invocationSite.isSuperAccess();
        }

        @Override
        public boolean isTypeAccess() {
            return invocationSite.isTypeAccess();
        }

        @Override
        public void setActualReceiverType(ReferenceBinding actualReceiverType) {
        /* ignore */
        }

        @Override
        public void setDepth(int depth) {
        /* ignore */
        }

        @Override
        public void setFieldIndex(int depth) {
        /* ignore */
        }

        @Override
        public int sourceStart() {
            return 0;
        }

        @Override
        public int sourceEnd() {
            return 0;
        }

        @Override
        public TypeBinding invocationTargetType() {
            return invocationSite.invocationTargetType();
        }

        @Override
        public boolean receiverIsImplicitThis() {
            return invocationSite.receiverIsImplicitThis();
        }

        @Override
        public InferenceContext18 freshInferenceContext(Scope someScope) {
            return invocationSite.freshInferenceContext(someScope);
        }

        @Override
        public ExpressionContext getExpressionContext() {
            return invocationSite.getExpressionContext();
        }

        @Override
        public boolean isQualifiedSuper() {
            return invocationSite.isQualifiedSuper();
        }

        @Override
        public boolean checkingPotentialCompatibility() {
            return false;
        }

        @Override
        public void acceptPotentiallyCompatibleMethods(MethodBinding[] methods) {
        /* ignore */
        }
    };
    MethodBinding bindingIfNoCast;
    if (binding.isConstructor()) {
        bindingIfNoCast = scope.getConstructor((ReferenceBinding) receiverType, alternateArgumentTypes, fakeInvocationSite);
    } else {
        bindingIfNoCast = receiver.isImplicitThis() ? scope.getImplicitMethod(binding.selector, alternateArgumentTypes, fakeInvocationSite) : scope.getMethod(receiverType, binding.selector, alternateArgumentTypes, fakeInvocationSite);
    }
    if (bindingIfNoCast == binding) {
        int argumentLength = originalArgumentTypes.length;
        if (binding.isVarargs()) {
            int paramLength = binding.parameters.length;
            if (paramLength == argumentLength) {
                int varargsIndex = paramLength - 1;
                ArrayBinding varargsType = (ArrayBinding) binding.parameters[varargsIndex];
                TypeBinding lastArgType = alternateArgumentTypes[varargsIndex];
                // to clarify between varargs/non-varargs call
                if (varargsType.dimensions != lastArgType.dimensions()) {
                    return;
                }
                if (lastArgType.isCompatibleWith(varargsType.elementsType()) && lastArgType.isCompatibleWith(varargsType)) {
                    return;
                }
            }
        }
        for (int i = 0; i < argumentLength; i++) {
            if (TypeBinding.notEquals(originalArgumentTypes[i], alternateArgumentTypes[i])) /*&& !originalArgumentTypes[i].needsUncheckedConversion(alternateArgumentTypes[i])*/
            {
                if (!preventsUnlikelyTypeWarning(originalArgumentTypes[i], alternateArgumentTypes[i], receiverType, binding, scope))
                    scope.problemReporter().unnecessaryCast((CastExpression) arguments[i]);
            }
        }
    }
}
Also used : BlockScope(org.eclipse.jdt.internal.compiler.lookup.BlockScope) Scope(org.eclipse.jdt.internal.compiler.lookup.Scope) ArrayBinding(org.eclipse.jdt.internal.compiler.lookup.ArrayBinding) InvocationSite(org.eclipse.jdt.internal.compiler.lookup.InvocationSite) ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) PolymorphicMethodBinding(org.eclipse.jdt.internal.compiler.lookup.PolymorphicMethodBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) ParameterizedGenericMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedGenericMethodBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)

Aggregations

InvocationSite (org.eclipse.jdt.internal.compiler.lookup.InvocationSite)4 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)2 ReferenceContext (org.eclipse.jdt.internal.compiler.impl.ReferenceContext)2 ArrayBinding (org.eclipse.jdt.internal.compiler.lookup.ArrayBinding)1 BlockScope (org.eclipse.jdt.internal.compiler.lookup.BlockScope)1 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)1 ParameterizedGenericMethodBinding (org.eclipse.jdt.internal.compiler.lookup.ParameterizedGenericMethodBinding)1 ParameterizedTypeBinding (org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding)1 PolymorphicMethodBinding (org.eclipse.jdt.internal.compiler.lookup.PolymorphicMethodBinding)1 ProblemMethodBinding (org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding)1 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)1 Scope (org.eclipse.jdt.internal.compiler.lookup.Scope)1 TypeBinding (org.eclipse.jdt.internal.compiler.lookup.TypeBinding)1