private void checkVariable(CheckerContext context, MethodTree tree, final Symbol symbol) { String nonNullAnnotation = nonNullAnnotation(symbol); if (nonNullAnnotation == null || symbol.isStatic()) { return; } if (isUndefinedOrNull(context, symbol)) { context.reportIssue(tree.simpleName(), this, MessageFormat.format("\"{0}\" is marked \"{1}\" but is not initialized in this constructor.", symbol.name(), nonNullAnnotation)); } }
private void checkNullArgument(Tree syntaxTree, JavaSymbol.MethodJavaSymbol symbol, JavaSymbol argumentSymbol, SymbolicValue argumentValue, int index) { ObjectConstraint constraint = programState.getConstraint(argumentValue, ObjectConstraint.class); if (constraint != null && constraint.isNull()) { String nonNullAnnotation = nonNullAnnotation(argumentSymbol); if (nonNullAnnotation != null) { String message = "Parameter {0} to this {1} is marked \"{2}\" but null could be passed."; reportIssue(syntaxTree, message, index + 1, (symbol.isConstructor() ? "constructor" : "call"), nonNullAnnotation); } } } }
private void checkVariable(CheckerContext context, MethodTree tree, final Symbol symbol) { String nonNullAnnotation = nonNullAnnotation(symbol); if (nonNullAnnotation == null || symbol.isStatic()) { return; } if (isUndefinedOrNull(context, symbol)) { context.reportIssue(tree.simpleName(), this, MessageFormat.format("\"{0}\" is marked \"{1}\" but is not initialized in this constructor.", symbol.name(), nonNullAnnotation)); } }
private void checkNullArgument(Tree syntaxTree, JavaSymbol.MethodJavaSymbol symbol, JavaSymbol argumentSymbol, SymbolicValue argumentValue, int index) { ObjectConstraint constraint = programState.getConstraint(argumentValue, ObjectConstraint.class); if (constraint != null && constraint.isNull()) { String nonNullAnnotation = nonNullAnnotation(argumentSymbol); if (nonNullAnnotation != null) { String message = "Parameter {0} to this {1} is marked \"{2}\" but null could be passed."; reportIssue(syntaxTree, message, index + 1, (symbol.isConstructor() ? "constructor" : "call"), nonNullAnnotation); } } } }
@Override public void visitReturnStatement(ReturnStatementTree tree) { Tree parent = tree.parent(); while (!parent.is(Tree.Kind.METHOD)) { parent = parent.parent(); if (parent == null) { // This occurs when the return statement is within a constructor return; } } String nonNullAnnotation = nonNullAnnotation(((MethodTree) parent).symbol()); if (nonNullAnnotation == null) { return; } if (isLocalExpression(tree.expression())) { checkReturnedValue(tree, nonNullAnnotation); } }
@Override public void visitAssignmentExpression(AssignmentExpressionTree tree) { if (ExpressionUtils.isSimpleAssignment(tree)) { IdentifierTree variable = ExpressionUtils.extractIdentifier(tree); Symbol symbol = variable.symbol(); String nonNullAnnotation = nonNullAnnotation(symbol); if (nonNullAnnotation == null) { return; } SymbolicValue assignedValue = programState.peekValue(); ObjectConstraint constraint = programState.getConstraint(assignedValue, ObjectConstraint.class); if (constraint != null && constraint.isNull()) { reportIssue(tree, "\"{0}\" is marked \"{1}\" but is set to null.", symbol.name(), nonNullAnnotation); } } }
@Override public void visitReturnStatement(ReturnStatementTree tree) { Tree parent = tree.parent(); while (!parent.is(Tree.Kind.METHOD)) { parent = parent.parent(); if (parent == null) { // This occurs when the return statement is within a constructor return; } } String nonNullAnnotation = nonNullAnnotation(((MethodTree) parent).symbol()); if (nonNullAnnotation == null) { return; } if (isLocalExpression(tree.expression())) { checkReturnedValue(tree, nonNullAnnotation); } }
@Override public void visitAssignmentExpression(AssignmentExpressionTree tree) { if (ExpressionUtils.isSimpleAssignment(tree)) { IdentifierTree variable = ExpressionUtils.extractIdentifier(tree); Symbol symbol = variable.symbol(); String nonNullAnnotation = nonNullAnnotation(symbol); if (nonNullAnnotation == null) { return; } SymbolicValue assignedValue = programState.peekValue(); ObjectConstraint constraint = programState.getConstraint(assignedValue, ObjectConstraint.class); if (constraint != null && constraint.isNull()) { reportIssue(tree, "\"{0}\" is marked \"{1}\" but is set to null.", symbol.name(), nonNullAnnotation); } } }