Search in sources :

Example 81 with MethodTree

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

the class RedundantThrowsDeclarationCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree methodTree = (MethodTree) tree;
    ListTree<TypeTree> thrownList = methodTree.throwsClauses();
    Set<Type> thrownExceptions = thrownExceptionsFromBody(methodTree);
    boolean hasTryWithResourceInBody = hasTryWithResourceInBody(methodTree);
    Set<String> reported = new HashSet<>();
    for (TypeTree typeTree : thrownList) {
        Type exceptionType = typeTree.symbolType();
        if (hasTryWithResourceInBody && (exceptionType.is("java.io.IOException") || exceptionType.is("java.lang.Exception"))) {
            // method 'close()' from 'java.io.Closeable' interface throws 'java.io.IOException"
            continue;
        }
        String fullyQualifiedName = exceptionType.fullyQualifiedName();
        if (!reported.contains(fullyQualifiedName)) {
            String superTypeName = isSubclassOfAny(exceptionType, thrownList);
            if (superTypeName != null) {
                reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s' which is a subclass of '%s'.", fullyQualifiedName, superTypeName));
            } else if (exceptionType.isSubtypeOf("java.lang.RuntimeException")) {
                reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s' which is a runtime exception.", fullyQualifiedName));
            } else if (declaredMoreThanOnce(fullyQualifiedName, thrownList)) {
                reportIssue(typeTree, String.format("Remove the redundant '%s' thrown exception declaration(s).", fullyQualifiedName));
            } else if (canNotBeThrown(methodTree, exceptionType, thrownExceptions)) {
                reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s', as it cannot be thrown from %s's body.", fullyQualifiedName, methodTreeType(methodTree)));
            }
            reported.add(fullyQualifiedName);
        }
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) JavaType(org.sonar.java.resolve.JavaType) Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) HashSet(java.util.HashSet)

Example 82 with MethodTree

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

the class RedundantModifierCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    for (Tree member : classTree.members()) {
        if (member.is(Tree.Kind.METHOD)) {
            MethodTree methodTree = (MethodTree) member;
            ModifiersTree modifiers = methodTree.modifiers();
            if (isInterfaceOrAnnotation(tree)) {
                checkRedundantModifier(modifiers, Modifier.ABSTRACT);
                checkRedundantModifier(modifiers, Modifier.PUBLIC);
            } else if (ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.FINAL)) {
                checkRedundantModifier(modifiers, Modifier.FINAL);
            }
        } else if (member.is(Tree.Kind.VARIABLE) && isInterfaceOrAnnotation(tree)) {
            VariableTree variableTree = (VariableTree) member;
            ModifiersTree modifiers = variableTree.modifiers();
            checkRedundantModifier(modifiers, Modifier.PUBLIC);
            checkRedundantModifier(modifiers, Modifier.STATIC);
            checkRedundantModifier(modifiers, Modifier.FINAL);
        } else if (member.is(Kind.CONSTRUCTOR) && tree.is(Kind.ENUM)) {
            checkRedundantModifier(((MethodTree) member).modifiers(), Modifier.PRIVATE);
        }
    }
}
Also used : ModifiersTree(org.sonar.plugins.java.api.tree.ModifiersTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ModifiersTree(org.sonar.plugins.java.api.tree.ModifiersTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 83 with MethodTree

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

the class ThreadLocalWithInitialCheck method onConstructorFound.

@Override
protected void onConstructorFound(NewClassTree newClassTree) {
    ClassTree classTree = newClassTree.classBody();
    if (classTree == null) {
        return;
    }
    List<Tree> members = classTree.members();
    if (members.size() != 1) {
        return;
    }
    members.stream().filter(tree -> tree.is(Tree.Kind.METHOD)).map(t -> ((MethodTree) t)).filter(t -> "initialValue".equals(t.simpleName().name())).filter(t -> t.parameters().isEmpty()).findFirst().ifPresent(t -> reportIssue(newClassTree.identifier(), "Replace this anonymous class with a call to \"ThreadLocal.withInitial\"." + context.getJavaVersion().java8CompatibilityMessage()));
}
Also used : JavaVersionAwareVisitor(org.sonar.java.JavaVersionAwareVisitor) JavaVersion(org.sonar.plugins.java.api.JavaVersion) List(java.util.List) AbstractMethodDetection(org.sonar.java.checks.methods.AbstractMethodDetection) MethodMatcher(org.sonar.java.matcher.MethodMatcher) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Rule(org.sonar.check.Rule) Tree(org.sonar.plugins.java.api.tree.Tree) Collections(java.util.Collections) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 84 with MethodTree

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

the class ThreadRunCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
    Tree parent = mit.parent();
    while (parent != null && !parent.is(Tree.Kind.METHOD)) {
        parent = parent.parent();
    }
    if (parent != null && THREAD_RUN_METHOD_MATCHER.matches((MethodTree) parent)) {
        return;
    }
    reportIssue(ExpressionUtils.methodName(mit), "Call the method Thread.start() to execute the content of the run() method in a dedicated thread.");
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 85 with MethodTree

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

the class SyncGetterAndSetterCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    checkMethodTree(methodTree, GETTER, SETTER);
    checkMethodTree(methodTree, GETTER_BOOLEAN, SETTER);
    checkMethodTree(methodTree, SETTER, GETTER);
    checkMethodTree(methodTree, SETTER, GETTER_BOOLEAN);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

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