Search in sources :

Example 46 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.

the class ClassComplexityCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    int size = context.getComplexityNodes(tree).size();
    if (size > max) {
        ClassTree classTree = (ClassTree) tree;
        Tree report = classTree.simpleName() == null ? classTree.openBraceToken() : classTree.simpleName();
        reportIssue(report, "The Cyclomatic Complexity of this class is " + size + " which is greater than " + max + " authorized.", ImmutableList.<JavaFileScannerContext.Location>of(), size - max);
    }
}
Also used : JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree)

Example 47 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.

the class SubClassStaticReferenceCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    Type classType = classTree.symbol().type();
    List<Tree> members = classTree.members();
    // JLS 12.4. Initialization of Classes and Interfaces:
    // Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables)
    // declared in the class.
    checkStaticVariables(members, classType);
    checkStaticInitializers(members, classType);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 48 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.

the class StaticFieldInitializationCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    switch(tree.kind()) {
        case CLASS:
            classWithSynchronizedMethod.push(hasSynchronizedMethod((ClassTree) tree));
            break;
        case STATIC_INITIALIZER:
            withinStaticInitializer.push(true);
            break;
        case METHOD:
            methodUsesLocks.push(false);
            break;
        case METHOD_INVOCATION:
            if (locks.anyMatch((MethodInvocationTree) tree) && methodUsesLocks.size() != 1) {
                methodUsesLocks.pop();
                methodUsesLocks.push(true);
            }
            break;
        case ASSIGNMENT:
            AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
            if (hasSemantic() && aet.variable().is(Tree.Kind.IDENTIFIER) && !isInSyncBlock() && !isInStaticInitializer() && !isUsingLock() && isInClassWithSynchronizedMethod()) {
                IdentifierTree variable = (IdentifierTree) aet.variable();
                if (isStaticNotVolatileObject(variable)) {
                    reportIssue(variable, "Synchronize this lazy initialization of '" + variable.name() + "'");
                }
            }
            break;
        default:
    }
    super.visitNode(tree);
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 49 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.

the class RightCurlyBraceStartLineCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.BLOCK, Tree.Kind.STATIC_INITIALIZER, Tree.Kind.INITIALIZER)) {
        BlockTree blockTree = (BlockTree) tree;
        checkBlockBody(blockTree.openBraceToken(), blockTree.closeBraceToken(), blockTree.body());
    } else {
        ClassTree classTree = (ClassTree) tree;
        checkBlockBody(classTree.openBraceToken(), classTree.closeBraceToken(), classTree.members());
    }
}
Also used : ClassTree(org.sonar.plugins.java.api.tree.ClassTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 50 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.

the class UndocumentedApiCheck method isPublicApi.

private boolean isPublicApi(Tree tree) {
    Tree currentParent = currentParents.peek();
    if (tree.is(CLASS_KINDS)) {
        classTrees.push((ClassTree) tree);
        currentParents.push(tree);
    } else if (tree.is(METHOD_KINDS)) {
        currentParents.push(tree);
    }
    return PublicApiChecker.isPublicApi(currentParent, tree);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

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