Search in sources :

Example 6 with MethodMatcher

use of org.sonar.java.matcher.MethodMatcher in project sonar-java by SonarSource.

the class UnusedReturnedDataCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        CHECKED_METHODS.stream().map(matcher -> isTreeMethodInvocation(((ExpressionStatementTree) tree).expression(), matcher)).filter(Objects::nonNull).forEach(mit -> raiseIssue(ExpressionUtils.methodName(mit)));
    } else {
        BinaryExpressionTree expressionTree = (BinaryExpressionTree) tree;
        ExpressionTree leftOperand = expressionTree.leftOperand();
        ExpressionTree rightOperand = expressionTree.rightOperand();
        for (MethodMatcher matcher : CHECKED_METHODS) {
            MethodInvocationTree leftMit = isTreeMethodInvocation(leftOperand, matcher);
            if (leftMit != null && isTreeLiteralNull(rightOperand)) {
                raiseIssue(ExpressionUtils.methodName(leftMit));
            }
            MethodInvocationTree rightMit = isTreeMethodInvocation(rightOperand, matcher);
            if (rightMit != null && isTreeLiteralNull(leftOperand)) {
                raiseIssue(ExpressionUtils.methodName(rightMit));
            }
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) MethodMatcher(org.sonar.java.matcher.MethodMatcher)

Aggregations

MethodMatcher (org.sonar.java.matcher.MethodMatcher)6 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 CheckForNull (javax.annotation.CheckForNull)1 JavaType (org.sonar.java.resolve.JavaType)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 Type (org.sonar.plugins.java.api.semantic.Type)1 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1