use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class AnalyzerMessageTest method textSpanForTrees.
@Test
public void textSpanForTrees() {
CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse("class A {\n}\n");
ClassTree classTree = (ClassTree) cut.types().get(0);
TextSpan textSpan;
textSpan = AnalyzerMessage.textSpanFor(classTree);
assertThat(textSpan.startLine).isEqualTo(1);
assertThat(textSpan.startCharacter).isEqualTo(0);
assertThat(textSpan.endLine).isEqualTo(2);
assertThat(textSpan.endCharacter).isEqualTo(1);
textSpan = AnalyzerMessage.textSpanBetween(classTree.declarationKeyword(), classTree.openBraceToken());
assertThat(textSpan.startLine).isEqualTo(1);
assertThat(textSpan.startCharacter).isEqualTo(0);
assertThat(textSpan.endLine).isEqualTo(1);
assertThat(textSpan.endCharacter).isEqualTo(9);
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class PublicApiChecker method visitNode.
private void visitNode(Tree tree) {
Tree currentParent = currentParents.peek();
if (tree.is(PublicApiChecker.CLASS_KINDS)) {
classTrees.push((ClassTree) tree);
currentParents.push(tree);
} else if (tree.is(PublicApiChecker.METHOD_KINDS)) {
currentParents.push(tree);
}
if (isPublicApi(currentParent, tree)) {
publicApi++;
if (getApiJavadoc(tree) != null) {
documentedPublicApi++;
}
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class AbstractClassNoFieldShouldBeInterfaceCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree classTree = (ClassTree) tree;
if (classTree.superClass() == null && classIsAbstract(classTree) && classHasNoFieldAndProtectedMethod(classTree) && supportPrivateMethod(classTree)) {
IdentifierTree simpleName = classTree.simpleName();
reportIssue(simpleName, "Convert the abstract class \"" + simpleName.name() + "\" into an interface." + context.getJavaVersion().java8CompatibilityMessage());
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class DeadStoreCheck method checkElement.
private Set<Symbol> checkElement(Symbol.MethodSymbol methodSymbol, Set<Symbol> outVar, Set<Tree> assignmentLHS, Tree element) {
Set<Symbol> out = outVar;
switch(element.kind()) {
case PLUS_ASSIGNMENT:
case DIVIDE_ASSIGNMENT:
case MINUS_ASSIGNMENT:
case MULTIPLY_ASSIGNMENT:
case OR_ASSIGNMENT:
case XOR_ASSIGNMENT:
case AND_ASSIGNMENT:
case LEFT_SHIFT_ASSIGNMENT:
case RIGHT_SHIFT_ASSIGNMENT:
case UNSIGNED_RIGHT_SHIFT_ASSIGNMENT:
case REMAINDER_ASSIGNMENT:
case ASSIGNMENT:
handleAssignment(out, assignmentLHS, (AssignmentExpressionTree) element);
break;
case IDENTIFIER:
handleIdentifier(out, assignmentLHS, (IdentifierTree) element);
break;
case VARIABLE:
handleVariable(out, (VariableTree) element);
break;
case NEW_CLASS:
handleNewClass(out, methodSymbol, (NewClassTree) element);
break;
case LAMBDA_EXPRESSION:
LambdaExpressionTree lambda = (LambdaExpressionTree) element;
out.addAll(getUsedLocalVarInSubTree(lambda.body(), methodSymbol));
break;
case METHOD_REFERENCE:
MethodReferenceTree methodRef = (MethodReferenceTree) element;
out.addAll(getUsedLocalVarInSubTree(methodRef.expression(), methodSymbol));
break;
case TRY_STATEMENT:
handleTryStatement(out, methodSymbol, (TryStatementTree) element);
break;
case PREFIX_DECREMENT:
case PREFIX_INCREMENT:
handlePrefixExpression(out, (UnaryExpressionTree) element);
break;
case POSTFIX_INCREMENT:
case POSTFIX_DECREMENT:
handlePostfixExpression(out, (UnaryExpressionTree) element);
break;
case CLASS:
case ENUM:
case ANNOTATION_TYPE:
case INTERFACE:
ClassTree classTree = (ClassTree) element;
out.addAll(getUsedLocalVarInSubTree(classTree, methodSymbol));
break;
default:
}
return out;
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class CloneableImplementingCloneCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree classTree = (ClassTree) tree;
Symbol.TypeSymbol classSymbol = classTree.symbol();
if (isCloneable(classTree) && !classSymbol.isAbstract() && !declaresCloneMethod(classSymbol)) {
reportIssue(classTree.simpleName(), "Add a \"clone()\" method to this class.");
}
}
Aggregations