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;
}
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);
}
Aggregations