Search in sources :

Example 41 with ClassTree

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);
}
Also used : TextSpan(org.sonar.java.AnalyzerMessage.TextSpan) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 42 with ClassTree

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++;
        }
    }
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ModifiersTree(org.sonar.plugins.java.api.tree.ModifiersTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 43 with ClassTree

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

Example 44 with ClassTree

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;
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 45 with ClassTree

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

Aggregations

ClassTree (org.sonar.plugins.java.api.tree.ClassTree)116 Tree (org.sonar.plugins.java.api.tree.Tree)53 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)47 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)45 Test (org.junit.Test)41 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)37 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)32 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)23 List (java.util.List)19 Type (org.sonar.plugins.java.api.semantic.Type)18 File (java.io.File)14 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)13 Rule (org.sonar.check.Rule)12 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)12 Collectors (java.util.stream.Collectors)10 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)10 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)10 ImmutableList (com.google.common.collect.ImmutableList)9 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)9