Search in sources :

Example 56 with IdentifierTree

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();
            }
        }
    };
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Deque(java.util.Deque)

Example 57 with IdentifierTree

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);
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 58 with IdentifierTree

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());
    }
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) NewSymbol(org.sonar.api.batch.sensor.symbol.NewSymbol)

Example 59 with IdentifierTree

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);
}
Also used : TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 60 with IdentifierTree

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());
    }
}
Also used : ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Aggregations

IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)142 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)52 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)50 Symbol (org.sonar.plugins.java.api.semantic.Symbol)32 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)29 Tree (org.sonar.plugins.java.api.tree.Tree)27 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)20 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)15 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)10 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)9 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)8 ArrayList (java.util.ArrayList)7 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7