Search in sources :

Example 1 with ArrayReference

use of org.eclipse.jdt.internal.compiler.ast.ArrayReference 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 2 with ArrayReference

use of org.eclipse.jdt.internal.compiler.ast.ArrayReference 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)

Aggregations

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