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