Search in sources :

Example 1 with ClassTree

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

the class AbstractClassWithoutAbstractMethodCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    Symbol.TypeSymbol typeSymbol = classTree.symbol();
    if (typeSymbol.isAbstract()) {
        Collection<Symbol> members = typeSymbol.memberSymbols();
        int nbAbstractMethod = countAbstractMethods(members);
        // don't count this and super as members
        int nbOfMembers = members.size() - 2;
        if (hasDefaultConstructor(members)) {
            // remove default constructor from members
            nbOfMembers -= 1;
        }
        if (isExtendingObject(classTree) && nbAbstractMethod == nbOfMembers) {
            // emtpy abstract class or only abstract method
            context.reportIssue(this, classTree.simpleName(), "Convert this \"" + typeSymbol + "\" class to an interface");
        }
        if (nbOfMembers > 0 && nbAbstractMethod == 0 && !isPartialImplementation(classTree)) {
            // Not empty abstract class with no abstract method
            context.reportIssue(this, classTree.simpleName(), "Convert this \"" + typeSymbol + "\" class to a concrete class with a private constructor");
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 2 with ClassTree

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

the class AbstractSerializableInnerClassRule method visitClassTree.

private void visitClassTree(ClassTree classTree) {
    Symbol.TypeSymbol symbol = classTree.symbol();
    if (isInnerClass(symbol) && directlyImplementsSerializable(symbol)) {
        Tree reportTree = ExpressionsHelper.reportOnClassTree(classTree);
        Symbol owner = symbol.owner();
        if (owner.isTypeSymbol()) {
            Symbol.TypeSymbol ownerType = (Symbol.TypeSymbol) owner;
            if (isMatchingOuterClass(ownerType.type()) && !symbol.isStatic()) {
                reportIssue(reportTree, "Make this inner class static");
            }
        } else if (owner.isMethodSymbol()) {
            Symbol.TypeSymbol methodOwner = (Symbol.TypeSymbol) owner.owner();
            if (isMatchingOuterClass(methodOwner.type()) && !owner.isStatic()) {
                String methodName = owner.name();
                reportIssue(reportTree, "Make \"" + methodName + "\" static");
            }
        }
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree)

Example 3 with ClassTree

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

the class InterfaceOrSuperclassShadowingCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    if (hasSemantic()) {
        Symbol.TypeSymbol classSymbol = classTree.symbol();
        checkSuperType(classTree, classSymbol.superClass());
        for (Type interfaceType : classSymbol.interfaces()) {
            checkSuperType(classTree, interfaceType);
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 4 with ClassTree

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

the class HiddenFieldCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (isClassTree(tree)) {
        ClassTree classTree = (ClassTree) tree;
        ImmutableMap.Builder<String, VariableTree> builder = ImmutableMap.builder();
        for (Tree member : classTree.members()) {
            if (member.is(Tree.Kind.VARIABLE)) {
                VariableTree variableTree = (VariableTree) member;
                builder.put(variableTree.simpleName().name(), variableTree);
            }
        }
        fields.push(builder.build());
        excludedVariables.push(Lists.<VariableTree>newArrayList());
    } else if (tree.is(Tree.Kind.VARIABLE)) {
        VariableTree variableTree = (VariableTree) tree;
        isVariableHidingField(variableTree);
    } else if (tree.is(Tree.Kind.STATIC_INITIALIZER)) {
        excludeVariablesFromBlock((BlockTree) tree);
    } else {
        MethodTree methodTree = (MethodTree) tree;
        excludedVariables.peek().addAll(methodTree.parameters());
        flattenExcludedVariables.addAll(methodTree.parameters());
        if (ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.STATIC)) {
            excludeVariablesFromBlock(methodTree.block());
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) JavaTree(org.sonar.java.model.JavaTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 5 with ClassTree

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

the class EmptyMethodsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    if (!ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.ABSTRACT)) {
        List<Tree> members = classTree.members();
        checkMethods(members);
        checkSingleNoArgPublicConstructor(members);
    }
}
Also used : ClassTree(org.sonar.plugins.java.api.tree.ClassTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) 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