use of org.sonar.plugins.java.api.tree.ClassTree 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.ClassTree in project sonar-java by SonarSource.
the class InnerStaticClassesCheck method visitClass.
@Override
public void visitClass(ClassTree tree) {
Symbol.TypeSymbol symbol = tree.symbol();
if (!tree.is(Tree.Kind.CLASS)) {
return;
}
outerClasses.push(symbol);
atLeastOneReference.push(Boolean.FALSE);
scan(tree.members());
Boolean oneReference = atLeastOneReference.pop();
outerClasses.pop();
if (!symbol.isStatic() && !oneReference && couldBeDeclaredStatic(symbol)) {
Tree reportTree = tree.simpleName();
if (reportTree == null) {
// Ignore issues on anonymous classes
return;
}
String message = "Make this a \"static\" inner class.";
if (symbol.owner().isMethodSymbol()) {
message = "Make this local class a \"static\" inner class.";
}
context.reportIssue(this, reportTree, message);
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class NestedEnumStaticCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree classTree = (ClassTree) tree;
ModifierKeywordTree staticKeyword = ModifiersUtils.getModifier(classTree.modifiers(), Modifier.STATIC);
if (staticKeyword != null) {
List<JavaFileScannerContext.Location> secondary = Collections.singletonList(new JavaFileScannerContext.Location("", classTree.declarationKeyword()));
reportIssue(staticKeyword, "Remove this redundant \"static\" qualifier.", secondary, null);
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class PrivateFieldUsedLocallyCheck method visitNode.
@Override
public void visitNode(Tree tree) {
TypeSymbol classSymbol = ((ClassTree) tree).symbol();
Set<Symbol> fieldsReadOnAnotherInstance = FieldsReadOnAnotherInstanceVisitor.getFrom(tree);
classSymbol.memberSymbols().stream().filter(PrivateFieldUsedLocallyCheck::isPrivateField).filter(s -> !(s.isFinal() && s.isStatic())).filter(s -> !hasAnnotation(s)).filter(s -> !s.usages().isEmpty()).filter(s -> !fieldsReadOnAnotherInstance.contains(s)).forEach(s -> checkPrivateField(s, classSymbol));
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class CFGTest method build_partial_cfg.
private void build_partial_cfg(String breakOrContinue) {
String methodCode = "void meth(){ try {fun(); } catch ( Exception e) {e.printStackTrace(); " + breakOrContinue + "; } }";
CompilationUnitTree cut = (CompilationUnitTree) parser.parse("class A {" + methodCode + "}");
SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
MethodTree methodTree = (MethodTree) ((ClassTree) cut.types().get(0)).members().get(0);
List<StatementTree> body = methodTree.block().body();
CFG cfg = CFG.buildCFG(body, true);
cfg.setMethodSymbol(methodTree.symbol());
assertThat(cfg.blocks()).hasSize(5);
assertThat(cfg.methodSymbol()).isSameAs(methodTree.symbol());
try {
CFG.buildCFG(body, false);
fail("IllegalStateException should have been thrown");
} catch (IllegalStateException iae) {
assertThat(iae).hasMessage("'" + breakOrContinue + "' statement not in loop or switch statement");
}
}
Aggregations