Search in sources :

Example 16 with MethodTree

use of org.sonar.plugins.java.api.tree.MethodTree 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 17 with MethodTree

use of org.sonar.plugins.java.api.tree.MethodTree 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 18 with MethodTree

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

the class TransactionalMethodVisibilityCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree method = (MethodTree) tree;
    boolean isPublic = method.modifiers().contains(Modifier.PUBLIC);
    if (hasSemantic()) {
        isPublic = method.symbol().isPublic();
    }
    if (!isPublic && hasTransactionalAnnotation(method)) {
        reportIssue(method.simpleName(), "Make this method \"public\" or remove the \"@Transactional\" annotation");
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 19 with MethodTree

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

the class SerializableContract method hasSpecialHandlingSerializationMethods.

public static boolean hasSpecialHandlingSerializationMethods(ClassTree classTree) {
    boolean hasWriteObject = false;
    boolean hasReadObject = false;
    String classFullyQualifiedName = classTree.symbol().type().fullyQualifiedName();
    for (Tree member : classTree.members()) {
        MethodMatcher writeObjectMatcher = writeObjectMatcher(classFullyQualifiedName);
        MethodMatcher readObjectMatcher = readObjectMatcher(classFullyQualifiedName);
        if (member.is(Tree.Kind.METHOD)) {
            MethodTree methodTree = (MethodTree) member;
            if (ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.PRIVATE)) {
                hasWriteObject |= writeObjectMatcher.matches(methodTree) && methodThrows(methodTree, "java.io.IOException");
                hasReadObject |= readObjectMatcher.matches(methodTree) && methodThrows(methodTree, "java.io.IOException", "java.lang.ClassNotFoundException");
            }
        }
    }
    return hasReadObject && hasWriteObject;
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodMatcher(org.sonar.java.matcher.MethodMatcher)

Example 20 with MethodTree

use of org.sonar.plugins.java.api.tree.MethodTree 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)

Aggregations

MethodTree (org.sonar.plugins.java.api.tree.MethodTree)143 Test (org.junit.Test)59 Tree (org.sonar.plugins.java.api.tree.Tree)43 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)39 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)34 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)30 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)27 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)23 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)20 Type (org.sonar.plugins.java.api.semantic.Type)19 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)19 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)18 List (java.util.List)16 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)16 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)15 ArrayList (java.util.ArrayList)14 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)14 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)12