Search in sources :

Example 26 with IdentifierTree

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

the class SerializableFieldInSerializableClassCheck method checkCollectionAssignments.

private void checkCollectionAssignments(List<IdentifierTree> usages) {
    for (IdentifierTree usage : usages) {
        Tree parentTree = usage.parent();
        if (parentTree.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignment = (AssignmentExpressionTree) parentTree;
            ExpressionTree expression = assignment.expression();
            if (usage.equals(assignment.variable()) && !expression.is(Tree.Kind.NULL_LITERAL) && isUnserializableCollection(expression.symbolType())) {
                reportIssue(usage);
            }
        }
    }
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) WildcardTree(org.sonar.plugins.java.api.tree.WildcardTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 27 with IdentifierTree

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

the class KeywordAsIdentifierCheck method visitVariable.

@Override
public void visitVariable(VariableTree tree) {
    IdentifierTree simpleName = tree.simpleName();
    if (FORBIDDEN_IDENTIFIERS.contains(simpleName.name())) {
        context.reportIssue(this, simpleName, "Use a different name than \"" + simpleName.name() + "\".");
    }
    super.visitVariable(tree);
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 28 with IdentifierTree

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

the class BooleanMethodNameCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    IdentifierTree simpleName = methodTree.simpleName();
    if (returnsBoolean(methodTree) && isBooleanGetter(simpleName) && isNotOverriding(methodTree)) {
        reportIssue(simpleName, "Rename this method to start with \"is\" or \"has\".");
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 29 with IdentifierTree

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

the class FieldNameMatchingTypeNameCheck method visitClass.

@Override
public void visitClass(ClassTree tree) {
    IdentifierTree simpleName = tree.simpleName();
    if (simpleName != null) {
        Symbol.TypeSymbol classSymbol = tree.symbol();
        Collection<Symbol> members = classSymbol.memberSymbols();
        for (Symbol sym : members) {
            if (sym.isVariableSymbol() && !staticFieldSameType(classSymbol, sym)) {
                // Exclude static fields of the same type.
                fields.add(((Symbol.VariableSymbol) sym).declaration());
            }
        }
        currentClassName = simpleName.name();
    }
    super.visitClass(tree);
    currentClassName = "";
    fields.clear();
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 30 with IdentifierTree

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

the class MethodNameSameAsClassCheck method visitClass.

@Override
public void visitClass(ClassTree tree) {
    super.visitClass(tree);
    IdentifierTree classSimpleName = tree.simpleName();
    if (classSimpleName == null) {
        return;
    }
    String className = classSimpleName.name();
    for (Tree member : tree.members()) {
        if (member.is(Tree.Kind.METHOD)) {
            IdentifierTree simpleName = ((MethodTree) member).simpleName();
            if (className.equals(simpleName.name())) {
                context.reportIssue(this, simpleName, "Rename this method to prevent any misunderstanding or make it a constructor.");
            }
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

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