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