Search in sources :

Example 31 with SyntaxToken

use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.

the class MethodIdenticalImplementationsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    List<MethodWithUsedVariables> methods = classTree.members().stream().filter(member -> member.is(Tree.Kind.METHOD)).map(MethodTree.class::cast).filter(methodTree -> isDuplicateCandidate(methodTree, classTree)).map(MethodWithUsedVariables::new).collect(Collectors.toList());
    if (methods.size() <= 1) {
        return;
    }
    Set<MethodTree> reported = new HashSet<>();
    for (int i = 0; i < methods.size(); i++) {
        MethodWithUsedVariables methodWithVariables = methods.get(i);
        MethodTree method = methodWithVariables.method;
        SyntaxToken methodIdentifier = method.simpleName().identifierToken();
        List<StatementTree> methodBody = method.block().body();
        methods.stream().skip(i + 1L).filter(otherMethodWithVariables -> !reported.contains(otherMethodWithVariables.method)).filter(otherMethodWithVariables -> !methodIdentifier.text().equals(otherMethodWithVariables.method.simpleName().name())).filter(otherMethodWithVariables -> SyntacticEquivalence.areEquivalent(methodBody, otherMethodWithVariables.method.block().body())).filter(methodWithVariables::isUsingSameVariablesWithSameTypes).forEach(otherMethodWithVariables -> {
            MethodTree otherMethod = otherMethodWithVariables.method;
            reportIssue(otherMethod.simpleName(), String.format(ISSUE_MSG, methodIdentifier.text(), methodIdentifier.line()), Collections.singletonList(new JavaFileScannerContext.Location("original implementation", methodIdentifier)), null);
            reported.add(otherMethod);
        });
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) AccessorsUtils(org.sonar.java.ast.visitors.AccessorsUtils) BaseTreeVisitor(org.sonar.plugins.java.api.tree.BaseTreeVisitor) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Set(java.util.Set) HashMap(java.util.HashMap) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) List(java.util.List) SyntacticEquivalence(org.sonar.java.model.SyntacticEquivalence) Map(java.util.Map) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) HashSet(java.util.HashSet)

Example 32 with SyntaxToken

use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.

the class MultilineBlocksCurlyBracesCheck method check.

private void check(StatementTree current, StatementTree previous) {
    StatementTree block = null;
    boolean condition = false;
    if (previous.is(Tree.Kind.FOR_EACH_STATEMENT)) {
        block = ((ForEachStatement) previous).statement();
    } else if (previous.is(Tree.Kind.FOR_STATEMENT)) {
        block = ((ForStatementTree) previous).statement();
    } else if (previous.is(Tree.Kind.WHILE_STATEMENT)) {
        block = ((WhileStatementTree) previous).statement();
    } else if (previous.is(Tree.Kind.IF_STATEMENT)) {
        block = getIfStatementLastBlock(previous);
        condition = true;
    }
    if (block != null && !block.is(Tree.Kind.BLOCK)) {
        SyntaxToken previousToken = block.firstToken();
        int previousColumn = previousToken.column();
        int previousLine = previousToken.line();
        SyntaxToken currentToken = current.firstToken();
        int currentColumn = currentToken.column();
        int currentLine = currentToken.line();
        if ((previousColumn == currentColumn && previousLine + 1 == currentLine) || (previousLine == previous.firstToken().line() && previous.firstToken().column() < currentColumn)) {
            int lines = 1 + currentLine - previousLine;
            context.reportIssue(this, current, getMessage(condition, lines));
        }
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Example 33 with SyntaxToken

use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.

the class NestedIfStatementsCheck method visitWhileStatement.

@Override
public void visitWhileStatement(WhileStatementTree tree) {
    SyntaxToken whileKeyword = tree.whileKeyword();
    checkNesting(whileKeyword);
    nestingLevel.push(whileKeyword);
    super.visitWhileStatement(tree);
    nestingLevel.pop();
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Example 34 with SyntaxToken

use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.

the class NestedIfStatementsCheck method visitDoWhileStatement.

@Override
public void visitDoWhileStatement(DoWhileStatementTree tree) {
    SyntaxToken doKeyword = tree.doKeyword();
    checkNesting(doKeyword);
    nestingLevel.push(doKeyword);
    super.visitDoWhileStatement(tree);
    nestingLevel.pop();
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Example 35 with SyntaxToken

use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.

the class NestedIfStatementsCheck method visitSwitchStatement.

@Override
public void visitSwitchStatement(SwitchStatementTree tree) {
    SyntaxToken switchKeyword = tree.switchKeyword();
    checkNesting(switchKeyword);
    nestingLevel.push(switchKeyword);
    super.visitSwitchStatement(tree);
    nestingLevel.pop();
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Aggregations

SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)47 Tree (org.sonar.plugins.java.api.tree.Tree)9 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)7 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)7 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)7 Test (org.junit.Test)6 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)6 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)5 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)5 ImmutableList (com.google.common.collect.ImmutableList)4 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)4 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)4 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)3 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)3 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 CheckForNull (javax.annotation.CheckForNull)2