Search in sources :

Example 1 with Reference

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

the class LoopingFlowContext method recordFinalAssignment.

@Override
protected boolean recordFinalAssignment(VariableBinding binding, Reference finalAssignment) {
    // do not consider variables which are defined inside this loop
    if (binding instanceof LocalVariableBinding) {
        Scope scope = ((LocalVariableBinding) binding).declaringScope;
        while ((scope = scope.parent) != null) {
            if (scope == this.associatedScope)
                return false;
        }
    }
    if (this.assignCount == 0) {
        this.finalAssignments = new Reference[5];
        this.finalVariables = new VariableBinding[5];
    } else {
        if (this.assignCount == this.finalAssignments.length)
            System.arraycopy(this.finalAssignments, 0, (this.finalAssignments = new Reference[this.assignCount * 2]), 0, this.assignCount);
        System.arraycopy(this.finalVariables, 0, (this.finalVariables = new VariableBinding[this.assignCount * 2]), 0, this.assignCount);
    }
    this.finalAssignments[this.assignCount] = finalAssignment;
    this.finalVariables[this.assignCount++] = binding;
    return true;
}
Also used : Scope(org.eclipse.jdt.internal.compiler.lookup.Scope) BlockScope(org.eclipse.jdt.internal.compiler.lookup.BlockScope) Reference(org.eclipse.jdt.internal.compiler.ast.Reference) LocalVariableBinding(org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding)

Example 2 with Reference

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

the class ProblemReporter method expressionNonNullComparison.

/**
 * @param expr expression being compared for null or nonnull
 * @param checkForNull true if checking for null, false if checking for nonnull
 */
public boolean expressionNonNullComparison(Expression expr, boolean checkForNull) {
    int problemId = 0;
    Binding binding = null;
    String[] arguments = null;
    int start = 0, end = 0;
    Expression location = expr;
    if (expr.resolvedType != null) {
        long tagBits = expr.resolvedType.tagBits & TagBits.AnnotationNullMASK;
        if (tagBits == TagBits.AnnotationNonNull) {
            problemId = IProblem.RedundantNullCheckAgainstNonNullType;
            arguments = new String[] { String.valueOf(expr.resolvedType.nullAnnotatedReadableName(this.options, true)) };
            start = nodeSourceStart(location);
            end = nodeSourceEnd(location);
            handle(problemId, arguments, arguments, start, end);
            return true;
        }
    }
    // unwrap uninteresting nodes:
    while (true) {
        if (expr instanceof Assignment)
            // don't report against the assignment, but the variable
            return false;
        else if (expr instanceof CastExpression)
            expr = ((CastExpression) expr).expression;
        else
            break;
    }
    // check all those kinds of expressions that can possible answer NON_NULL from nullStatus():
    if (expr instanceof MessageSend) {
        problemId = checkForNull ? IProblem.NonNullMessageSendComparisonYieldsFalse : IProblem.RedundantNullCheckOnNonNullMessageSend;
        MethodBinding method = ((MessageSend) expr).binding;
        binding = method;
        arguments = new String[] { new String(method.shortReadableName()) };
        start = location.sourceStart;
        end = location.sourceEnd;
    } else if (expr instanceof Reference && !(expr instanceof ThisReference) && !(expr instanceof ArrayReference)) {
        FieldBinding field = ((Reference) expr).lastFieldBinding();
        if (field == null) {
            return false;
        }
        if (field.isNonNull()) {
            problemId = checkForNull ? IProblem.NonNullSpecdFieldComparisonYieldsFalse : IProblem.RedundantNullCheckOnNonNullSpecdField;
            char[][] nonNullName = this.options.nonNullAnnotationName;
            arguments = new String[] { new String(field.name), new String(nonNullName[nonNullName.length - 1]) };
        } else if (field.constant() != Constant.NotAConstant) {
            problemId = checkForNull ? IProblem.ConstNonNullFieldComparisonYieldsFalse : IProblem.RedundantNullCheckOnConstNonNullField;
            char[][] nonNullName = this.options.nonNullAnnotationName;
            arguments = new String[] { new String(field.name), new String(nonNullName[nonNullName.length - 1]) };
        } else {
            // signaling redundancy based on syntactic analysis:
            problemId = checkForNull ? IProblem.FieldComparisonYieldsFalse : IProblem.RedundantNullCheckOnField;
            arguments = new String[] { String.valueOf(field.name) };
        }
        binding = field;
        start = nodeSourceStart(binding, location);
        end = nodeSourceEnd(binding, location);
    } else if (expr instanceof AllocationExpression || expr instanceof ArrayAllocationExpression || expr instanceof ArrayInitializer || expr instanceof ClassLiteralAccess || expr instanceof ThisReference) {
    // fall through to bottom
    } else if (expr instanceof Literal || expr instanceof ConditionalExpression || expr instanceof SwitchExpression) {
        if (expr instanceof NullLiteral) {
            // reported as nonnull??
            needImplementation(location);
            return false;
        }
        if (expr.resolvedType != null && expr.resolvedType.isBaseType()) {
            // false alarm, auto(un)boxing is involved
            return false;
        }
    // fall through to bottom
    } else if (expr instanceof BinaryExpression) {
        if ((expr.bits & ASTNode.ReturnTypeIDMASK) != TypeIds.T_JavaLangString) {
            // false alarm, primitive types involved, must be auto(un)boxing?
            return false;
        }
    // fall through to bottom
    } else {
        // want to see if we get here
        needImplementation(expr);
        return false;
    }
    if (problemId == 0) {
        // standard case, fill in details now
        problemId = checkForNull ? IProblem.NonNullExpressionComparisonYieldsFalse : IProblem.RedundantNullCheckOnNonNullExpression;
        start = location.sourceStart;
        end = location.sourceEnd;
        arguments = NoArgument;
    }
    this.handle(problemId, arguments, arguments, start, end);
    return true;
}
Also used : LocalVariableBinding(org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding) FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) ArrayBinding(org.eclipse.jdt.internal.compiler.lookup.ArrayBinding) AnnotationBinding(org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) WildcardBinding(org.eclipse.jdt.internal.compiler.lookup.WildcardBinding) SyntheticArgumentBinding(org.eclipse.jdt.internal.compiler.lookup.SyntheticArgumentBinding) Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) ProblemReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding) PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding) ParameterizedGenericMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedGenericMethodBinding) RecordComponentBinding(org.eclipse.jdt.internal.compiler.lookup.RecordComponentBinding) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding) ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) VariableBinding(org.eclipse.jdt.internal.compiler.lookup.VariableBinding) SplitPackageBinding(org.eclipse.jdt.internal.compiler.lookup.SplitPackageBinding) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) CaptureBinding(org.eclipse.jdt.internal.compiler.lookup.CaptureBinding) TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) ArrayAllocationExpression(org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression) ArrayReference(org.eclipse.jdt.internal.compiler.ast.ArrayReference) SwitchExpression(org.eclipse.jdt.internal.compiler.ast.SwitchExpression) NameReference(org.eclipse.jdt.internal.compiler.ast.NameReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) Reference(org.eclipse.jdt.internal.compiler.ast.Reference) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference) ModuleReference(org.eclipse.jdt.internal.compiler.ast.ModuleReference) QualifiedSuperReference(org.eclipse.jdt.internal.compiler.ast.QualifiedSuperReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) ArrayReference(org.eclipse.jdt.internal.compiler.ast.ArrayReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) ImportReference(org.eclipse.jdt.internal.compiler.ast.ImportReference) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) CompoundAssignment(org.eclipse.jdt.internal.compiler.ast.CompoundAssignment) Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) BinaryExpression(org.eclipse.jdt.internal.compiler.ast.BinaryExpression) BinaryExpression(org.eclipse.jdt.internal.compiler.ast.BinaryExpression) InstanceOfExpression(org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression) LambdaExpression(org.eclipse.jdt.internal.compiler.ast.LambdaExpression) SwitchExpression(org.eclipse.jdt.internal.compiler.ast.SwitchExpression) FunctionalExpression(org.eclipse.jdt.internal.compiler.ast.FunctionalExpression) ReferenceExpression(org.eclipse.jdt.internal.compiler.ast.ReferenceExpression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) CastExpression(org.eclipse.jdt.internal.compiler.ast.CastExpression) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) ArrayAllocationExpression(org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) UnaryExpression(org.eclipse.jdt.internal.compiler.ast.UnaryExpression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) ArrayAllocationExpression(org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral) Literal(org.eclipse.jdt.internal.compiler.ast.Literal) FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) ParameterizedGenericMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedGenericMethodBinding) CastExpression(org.eclipse.jdt.internal.compiler.ast.CastExpression) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral) ClassLiteralAccess(org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)

Example 3 with Reference

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

the class ProblemReporter method nullityMismatch.

public void nullityMismatch(Expression expression, TypeBinding providedType, TypeBinding requiredType, int nullStatus, char[][] annotationName) {
    if ((nullStatus & FlowInfo.NULL) != 0) {
        nullityMismatchIsNull(expression, requiredType);
        return;
    }
    if (expression instanceof MessageSend) {
        if ((((MessageSend) expression).binding.tagBits & TagBits.AnnotationNullable) != 0) {
            nullityMismatchSpecdNullable(expression, requiredType, this.options.nonNullAnnotationName);
            return;
        }
    }
    if ((nullStatus & FlowInfo.POTENTIALLY_NULL) != 0) {
        VariableBinding var = expression.localVariableBinding();
        if (var == null && expression instanceof Reference) {
            var = ((Reference) expression).lastFieldBinding();
        }
        if (var != null && var.type.isFreeTypeVariable()) {
            nullityMismatchVariableIsFreeTypeVariable(var, expression);
            return;
        }
        if (var != null && var.isNullable()) {
            nullityMismatchSpecdNullable(expression, requiredType, annotationName);
            return;
        }
        if (expression instanceof ArrayReference && expression.resolvedType.isFreeTypeVariable()) {
            nullityMismatchingTypeAnnotation(expression, providedType, requiredType, NullAnnotationMatching.NULL_ANNOTATIONS_MISMATCH);
            return;
        }
        nullityMismatchPotentiallyNull(expression, requiredType, annotationName);
        return;
    }
    if (this.options.usesNullTypeAnnotations())
        nullityMismatchingTypeAnnotation(expression, providedType, requiredType, NullAnnotationMatching.NULL_ANNOTATIONS_UNCHECKED);
    else
        nullityMismatchIsUnknown(expression, providedType, requiredType, annotationName);
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) ArrayReference(org.eclipse.jdt.internal.compiler.ast.ArrayReference) NameReference(org.eclipse.jdt.internal.compiler.ast.NameReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) Reference(org.eclipse.jdt.internal.compiler.ast.Reference) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference) ModuleReference(org.eclipse.jdt.internal.compiler.ast.ModuleReference) QualifiedSuperReference(org.eclipse.jdt.internal.compiler.ast.QualifiedSuperReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) ArrayReference(org.eclipse.jdt.internal.compiler.ast.ArrayReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) ImportReference(org.eclipse.jdt.internal.compiler.ast.ImportReference) LocalVariableBinding(org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding) VariableBinding(org.eclipse.jdt.internal.compiler.lookup.VariableBinding) TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding)

Example 4 with Reference

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

the class ProblemReporter method nullityMismatchingTypeAnnotation.

public void nullityMismatchingTypeAnnotation(Expression expression, TypeBinding providedType, TypeBinding requiredType, NullAnnotationMatching status) {
    // $IDENTITY-COMPARISON$
    if (providedType == requiredType)
        return;
    // try to improve nonnull vs. null:
    if (providedType.id == TypeIds.T_null || status.nullStatus == FlowInfo.NULL) {
        nullityMismatchIsNull(expression, requiredType);
        return;
    }
    if ((requiredType.tagBits & TagBits.AnnotationNonNull) != 0) {
        // try to improve nonnull vs. nullable:
        if (status.isPotentiallyNullMismatch() && (providedType.tagBits & TagBits.AnnotationNullable) == 0) {
            if (this.options.pessimisticNullAnalysisForFreeTypeVariablesEnabled && providedType.isTypeVariable() && !providedType.hasNullTypeAnnotations()) {
                nullityMismatchIsFreeTypeVariable(providedType, expression.sourceStart, expression.sourceEnd);
                return;
            }
            nullityMismatchPotentiallyNull(expression, requiredType, this.options.nonNullAnnotationName);
            return;
        }
        VariableBinding var = expression.localVariableBinding();
        if (var == null && expression instanceof Reference) {
            var = ((Reference) expression).lastFieldBinding();
        }
        if (var != null && var.type.isFreeTypeVariable()) {
            nullityMismatchVariableIsFreeTypeVariable(var, expression);
            return;
        }
    }
    String[] arguments;
    String[] shortArguments;
    int problemId = 0;
    String superHint = null;
    String superHintShort = null;
    if (status.superTypeHint != null && requiredType.isParameterizedType()) {
        problemId = (status.isAnnotatedToUnannotated() ? IProblem.AnnotatedTypeArgumentToUnannotatedSuperHint : (status.isUnchecked() ? IProblem.NullityUncheckedTypeAnnotationDetailSuperHint : IProblem.NullityMismatchingTypeAnnotationSuperHint));
        superHint = status.superTypeHintName(this.options, false);
        superHintShort = status.superTypeHintName(this.options, true);
    } else {
        problemId = (status.isAnnotatedToUnannotated() ? IProblem.AnnotatedTypeArgumentToUnannotated : (status.isUnchecked() ? IProblem.NullityUncheckedTypeAnnotationDetail : (requiredType.isTypeVariable() && !requiredType.hasNullTypeAnnotations()) ? IProblem.NullityMismatchAgainstFreeTypeVariable : IProblem.NullityMismatchingTypeAnnotation));
        if (problemId == IProblem.NullityMismatchAgainstFreeTypeVariable) {
            // don't show bounds here
            arguments = new String[] { null, null, new String(requiredType.sourceName()) };
            shortArguments = new String[] { null, null, new String(requiredType.sourceName()) };
        } else {
            arguments = new String[2];
            shortArguments = new String[2];
        }
    }
    String requiredName;
    String requiredNameShort;
    if (problemId == IProblem.NullityMismatchAgainstFreeTypeVariable) {
        // don't show bounds here
        requiredName = new String(requiredType.sourceName());
        // don't show bounds here
        requiredNameShort = new String(requiredType.sourceName());
    } else {
        requiredName = new String(requiredType.nullAnnotatedReadableName(this.options, false));
        requiredNameShort = new String(requiredType.nullAnnotatedReadableName(this.options, true));
    }
    String providedName = String.valueOf(providedType.nullAnnotatedReadableName(this.options, false));
    String providedNameShort = String.valueOf(providedType.nullAnnotatedReadableName(this.options, true));
    // assemble arguments:
    if (superHint != null) {
        arguments = new String[] { requiredName, providedName, superHint };
        shortArguments = new String[] { requiredNameShort, providedNameShort, superHintShort };
    } else {
        arguments = new String[] { requiredName, providedName };
        shortArguments = new String[] { requiredNameShort, providedNameShort };
    }
    this.handle(problemId, arguments, shortArguments, expression.sourceStart, expression.sourceEnd);
}
Also used : NameReference(org.eclipse.jdt.internal.compiler.ast.NameReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) Reference(org.eclipse.jdt.internal.compiler.ast.Reference) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference) ModuleReference(org.eclipse.jdt.internal.compiler.ast.ModuleReference) QualifiedSuperReference(org.eclipse.jdt.internal.compiler.ast.QualifiedSuperReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) ArrayReference(org.eclipse.jdt.internal.compiler.ast.ArrayReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) ImportReference(org.eclipse.jdt.internal.compiler.ast.ImportReference) LocalVariableBinding(org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding) VariableBinding(org.eclipse.jdt.internal.compiler.lookup.VariableBinding) TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding)

Aggregations

Reference (org.eclipse.jdt.internal.compiler.ast.Reference)4 LocalVariableBinding (org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding)4 ArrayQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference)3 ArrayReference (org.eclipse.jdt.internal.compiler.ast.ArrayReference)3 ArrayTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference)3 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)3 ImportReference (org.eclipse.jdt.internal.compiler.ast.ImportReference)3 ModuleReference (org.eclipse.jdt.internal.compiler.ast.ModuleReference)3 NameReference (org.eclipse.jdt.internal.compiler.ast.NameReference)3 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)3 ParameterizedSingleTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference)3 QualifiedNameReference (org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)3 QualifiedSuperReference (org.eclipse.jdt.internal.compiler.ast.QualifiedSuperReference)3 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)3 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)3 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)3 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)3 TypeVariableBinding (org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding)3 VariableBinding (org.eclipse.jdt.internal.compiler.lookup.VariableBinding)3 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)2