use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class PublicApiCheckerTest method getPublicApiVisitor.
private SubscriptionVisitor getPublicApiVisitor() {
return new SubscriptionVisitor() {
private final Deque<ClassTree> classTrees = Lists.newLinkedList();
private final Deque<MethodTree> methodTrees = Lists.newLinkedList();
@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.values());
}
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) tree;
String name = variableTree.simpleName().name();
Tree parent = classTrees.peek();
if (!methodTrees.isEmpty()) {
parent = methodTrees.peek();
}
assertThat(PublicApiChecker.isPublicApi(parent, tree)).as(name).isEqualTo(name.endsWith("Public"));
} else if (tree.is(PublicApiChecker.methodKinds())) {
MethodTree methodTree = (MethodTree) tree;
methodTrees.push(methodTree);
String name = methodTree.simpleName().name();
// getters and setters are included in the public API
assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).as(name).isEqualTo(name.endsWith("Public") || name.contains("GetSet"));
} else if (tree.is(PublicApiChecker.classKinds())) {
IdentifierTree className = ((ClassTree) tree).simpleName();
if (className == null) {
assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).isFalse();
} else {
assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).as(className.name()).isEqualTo(className != null && className.name().endsWith("Public"));
}
classTrees.push((ClassTree) tree);
} else {
assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).isFalse();
}
}
@Override
public void leaveNode(Tree tree) {
if (tree.is(PublicApiChecker.classKinds())) {
classTrees.pop();
} else if (tree.is(PublicApiChecker.methodKinds())) {
methodTrees.pop();
}
}
};
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class AccessorsUtils method referencePrivateProperty.
private static boolean referencePrivateProperty(ReturnStatementTree returnStatementTree, ClassTree classTree) {
ExpressionTree expression = returnStatementTree.expression();
String variableName = "";
if (expression == null) {
return false;
} else if (expression.is(Tree.Kind.IDENTIFIER)) {
variableName = ((IdentifierTree) expression).name();
}
return !StringUtils.isEmpty(variableName) && referencePrivateProperty(variableName, classTree);
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class SonarSymbolTableVisitor method createSymbol.
private void createSymbol(IdentifierTree declaration, List<IdentifierTree> usages) {
SyntaxToken syntaxToken = declaration.identifierToken();
NewSymbol newSymbol = newSymbolTable.newSymbol(syntaxToken.line(), syntaxToken.column(), syntaxToken.line(), syntaxToken.text().length() + syntaxToken.column());
for (IdentifierTree usage : usages) {
syntaxToken = usage.identifierToken();
newSymbol.newReference(syntaxToken.line(), syntaxToken.column(), syntaxToken.line(), syntaxToken.text().length() + syntaxToken.column());
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class SonarSymbolTableVisitor method visitMethod.
@Override
public void visitMethod(MethodTree tree) {
List<IdentifierTree> usages = tree.symbol().usages();
createSymbol(tree.simpleName(), usages);
for (TypeParameterTree typeParameterTree : tree.typeParameters()) {
createSymbol(typeParameterTree.identifier(), typeParameterTree);
}
super.visitMethod(tree);
}
use of org.sonar.plugins.java.api.tree.IdentifierTree 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());
}
}
Aggregations