Search in sources :

Example 1 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol 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 Symbol

use of org.sonar.plugins.java.api.semantic.Symbol 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 Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class AnnotationArgumentOrderCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    AnnotationTree annotationTree = (AnnotationTree) tree;
    TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
    if (annotationSymbol.isUnknown()) {
        return;
    }
    List<String> declarationNames = new ArrayList<>();
    for (Symbol symbol : annotationSymbol.memberSymbols()) {
        declarationNames.add(symbol.name());
    }
    List<String> annotationArguments = new ArrayList<>();
    for (ExpressionTree argument : annotationTree.arguments()) {
        if (argument.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
            IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
            annotationArguments.add(nameTree.name());
        }
    }
    declarationNames.retainAll(annotationArguments);
    if (!declarationNames.equals(annotationArguments)) {
        reportIssue(annotationTree.annotationType(), "Reorder annotation arguments to match the order of declaration.");
    }
}
Also used : TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ArrayList(java.util.ArrayList) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)

Example 4 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class KeySetInsteadOfEntrySetCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        ForEachStatement forEachTree = (ForEachStatement) tree;
        ExpressionTree expressionTree = forEachTree.expression();
        if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
            MethodInvocationTree methodTree = (MethodInvocationTree) expressionTree;
            Symbol ownerSymbol = getOwnerSymbol(methodTree);
            if (ownerSymbol != null && MAP_KEYSET_METHOD.matches(methodTree)) {
                new GetUsageVisitor().isCallingGetWithSymbol(forEachTree, forEachTree.variable().symbol(), ownerSymbol);
            }
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ForEachStatement(org.sonar.plugins.java.api.tree.ForEachStatement)

Example 5 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class InnerStaticClassesCheck method visitVariable.

@Override
public void visitVariable(VariableTree tree) {
    Symbol symbol = tree.symbol();
    if (symbol != null && !symbol.isStatic()) {
        scan(tree.modifiers());
        scan(tree.type());
        // skip the simple name
        scan(tree.initializer());
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Aggregations

Symbol (org.sonar.plugins.java.api.semantic.Symbol)140 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)47 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)41 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)33 Tree (org.sonar.plugins.java.api.tree.Tree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)28 JavaSymbol (org.sonar.java.resolve.JavaSymbol)27 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)26 Type (org.sonar.plugins.java.api.semantic.Type)24 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)24 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)23 List (java.util.List)19 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)17 Collectors (java.util.stream.Collectors)14 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 Set (java.util.Set)12 ImmutableList (com.google.common.collect.ImmutableList)11 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)11