Search in sources :

Example 76 with VariableTree

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

the class IncorrectOrderOfMembersCheck method visitClass.

@Override
public void visitClass(ClassTree tree) {
    int prev = 0;
    for (int i = 0; i < tree.members().size(); i++) {
        final Tree member = tree.members().get(i);
        final int priority;
        IdentifierTree identifier;
        if (member.is(Tree.Kind.VARIABLE)) {
            priority = 0;
            identifier = ((VariableTree) member).simpleName();
        } else if (member.is(Tree.Kind.CONSTRUCTOR)) {
            priority = 1;
            identifier = ((MethodTree) member).simpleName();
        } else if (member.is(Tree.Kind.METHOD)) {
            priority = 2;
            identifier = ((MethodTree) member).simpleName();
        } else {
            continue;
        }
        if (priority < prev) {
            context.reportIssue(this, identifier, "Move this " + NAMES[priority] + " to comply with Java Code Conventions.");
        } else {
            prev = priority;
        }
    }
    super.visitClass(tree);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) 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) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 77 with VariableTree

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

the class ForLoopCounterChangedCheck method visitForStatement.

@Override
public void visitForStatement(ForStatementTree tree) {
    Set<String> pendingLoopCounters = Sets.newHashSet();
    for (StatementTree statementTree : tree.initializer()) {
        if (statementTree.is(Tree.Kind.VARIABLE)) {
            pendingLoopCounters.add(((VariableTree) statementTree).simpleName().name());
        }
    }
    scan(tree.initializer());
    scan(tree.condition());
    scan(tree.update());
    loopCounters.addAll(pendingLoopCounters);
    scan(tree.statement());
    loopCounters.removeAll(pendingLoopCounters);
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 78 with VariableTree

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

the class FieldModifierCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    classTree.members().stream().filter(FieldModifierCheck::isConsentWithCheck).forEach(member -> {
        IdentifierTree simpleName = ((VariableTree) member).simpleName();
        reportIssue(simpleName, "Explicitly declare the visibility for \"" + simpleName.name() + "\".");
    });
}
Also used : ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 79 with VariableTree

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

the class EqualsParametersMarkedNonNullCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    List<VariableTree> params = methodTree.parameters();
    if ("equals".equals(methodTree.symbol().name()) && params.size() == 1) {
        VariableTree variable = params.get(0);
        if (variable.type().symbolType().is("java.lang.Object") && NullableAnnotationUtils.isAnnotatedNonNull(variable.symbol())) {
            reportIssue(variable, "\"equals\" method parameters should not be marked \"@Nonnull\".");
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 80 with VariableTree

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

the class UnusedTestRuleCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    for (Tree member : classTree.members()) {
        if (member.is(Tree.Kind.VARIABLE)) {
            VariableTree variableTree = (VariableTree) member;
            Symbol symbol = variableTree.symbol();
            if (isTestNameOrTemporaryFolderRule(symbol) && symbol.usages().isEmpty()) {
                reportIssue(variableTree.simpleName(), "Remove this unused \"" + symbol.type() + "\".");
            }
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree)

Aggregations

VariableTree (org.sonar.plugins.java.api.tree.VariableTree)86 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)32 Tree (org.sonar.plugins.java.api.tree.Tree)32 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)27 Test (org.junit.Test)25 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)22 Symbol (org.sonar.plugins.java.api.semantic.Symbol)18 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)18 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)17 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)16 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 List (java.util.List)12 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)12 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)12 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)11 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)10 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9