Search in sources :

Example 21 with IdentifierTree

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

the class UnusedLocalVariableCheck method checkVariableUsages.

private void checkVariableUsages() {
    for (VariableTree variableTree : variables) {
        Symbol symbol = variableTree.symbol();
        if (symbol.usages().size() == assignments.get(symbol).size()) {
            IdentifierTree simpleName = variableTree.simpleName();
            reportIssue(simpleName, "Remove this unused \"" + simpleName + "\" local variable.");
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 22 with IdentifierTree

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

the class UnusedMethodParameterCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    if (methodTree.block() == null || isExcluded(methodTree)) {
        return;
    }
    Set<String> documentedParameters = documentedParameters(methodTree);
    boolean overridableMethod = overridableMethod(methodTree.symbol());
    List<IdentifierTree> unused = Lists.newArrayList();
    for (VariableTree var : methodTree.parameters()) {
        Symbol symbol = var.symbol();
        if (symbol.usages().isEmpty() && !symbol.metadata().isAnnotatedWith(AUTHORIZED_ANNOTATION) && !isStrutsActionParameter(var) && (!overridableMethod || !documentedParameters.contains(symbol.name()))) {
            unused.add(var.simpleName());
        }
    }
    Set<String> unresolvedIdentifierNames = unresolvedIdentifierNames(methodTree.block());
    // kill the noise regarding unresolved identifiers, and remove the one with matching names from the list of unused
    unused = unused.stream().filter(id -> !unresolvedIdentifierNames.contains(id.name())).collect(Collectors.toList());
    if (!unused.isEmpty()) {
        reportUnusedParameters(unused);
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodSymbol(org.sonar.plugins.java.api.semantic.Symbol.MethodSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 23 with IdentifierTree

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

the class UnusedPrivateMethodCheck method reportUnusedPrivateMethods.

private void reportUnusedPrivateMethods() {
    unusedPrivateMethods.stream().filter(methodTree -> !unresolvedMethodNames.contains(methodTree.simpleName().name())).forEach(methodTree -> {
        IdentifierTree simpleName = methodTree.simpleName();
        reportIssue(simpleName, String.format("Remove this unused private \"%s\" %s.", simpleName.name(), methodTree.is(Tree.Kind.CONSTRUCTOR) ? "constructor" : "method"));
    });
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) SerializableContract(org.sonar.java.checks.serialization.SerializableContract) RspecKey(org.sonar.java.RspecKey) Set(java.util.Set) ExpressionUtils(org.sonar.java.model.ExpressionUtils) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) Tree(org.sonar.plugins.java.api.tree.Tree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ArrayList(java.util.ArrayList) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) HashSet(java.util.HashSet) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 24 with IdentifierTree

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

the class BadAbstractClassNameCheck method visitClass.

@Override
public void visitClass(ClassTree tree) {
    IdentifierTree simpleName = tree.simpleName();
    if (tree.is(Tree.Kind.CLASS) && simpleName != null) {
        if (pattern.matcher(simpleName.name()).matches()) {
            if (!isAbstract(tree)) {
                context.reportIssue(this, simpleName, "Make this class abstract or rename it, since it matches the regular expression '" + format + "'.");
            }
        } else {
            if (isAbstract(tree)) {
                context.reportIssue(this, simpleName, "Rename this abstract class name to match the regular expression '" + format + "'.");
            }
        }
    }
    super.visitClass(tree);
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 25 with IdentifierTree

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

the class BadTestClassNameCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    IdentifierTree simpleName = classTree.simpleName();
    if (hasInvalidName(simpleName) && hasTestMethod(classTree.members())) {
        reportIssue(simpleName, "Rename class \"" + simpleName + "\" to match the regular expression: '" + format + "'");
    }
}
Also used : ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

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