Search in sources :

Example 76 with MethodTree

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

the class SpringConstructorInjectionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    if (isClassTreeAnnotatedWith(classTree, "org.springframework.stereotype.Controller", "org.springframework.stereotype.Repository", "org.springframework.stereotype.Service")) {
        List<Tree> toReport = classTree.members().stream().filter(SpringConstructorInjectionCheck::isMemberAutowired).map(SpringConstructorInjectionCheck::toReportTree).collect(Collectors.toList());
        if (!toReport.isEmpty()) {
            int cost = toReport.size();
            // find constructor
            classTree.members().stream().filter(m -> m.is(Kind.CONSTRUCTOR)).map(m -> ((MethodTree) m).simpleName()).findFirst().ifPresent(toReport::add);
            reportIssue(toReport.get(0), "Remove this annotation and use constructor injection instead.", toReport.stream().skip(1).map(i -> new JavaFileScannerContext.Location("", i)).collect(Collectors.toList()), cost);
        }
    }
}
Also used : Arrays(java.util.Arrays) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) Stream(java.util.stream.Stream) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 77 with MethodTree

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

the class JavaParserTest method receiver_type_should_be_parsed.

@Test
public void receiver_type_should_be_parsed() throws Exception {
    try {
        String code = "class Main { class Inner { Inner(Main Main.this) {}};}";
        CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse(code);
        Tree constructor = ((ClassTree) ((ClassTree) cut.types().get(0)).members().get(0)).members().get(0);
        assertThat(constructor).isInstanceOf(MethodTree.class);
        assertThat(((MethodTree) constructor).parameters().get(0).simpleName().name()).isEqualTo("this");
    } catch (Exception ex) {
        fail("Receiver type of inner classes should be parsed correctly", ex);
    }
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 78 with MethodTree

use of org.sonar.plugins.java.api.tree.MethodTree in project JavaForFun by gumartinm.

the class ParameterCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree method = (MethodTree) tree;
    if (method.parameters().size() == 1) {
        MethodSymbol symbol = method.symbol();
        Type firstParameterType = symbol.parameterTypes().get(0);
        Type returnType = symbol.returnType().type();
        if (returnType.is(firstParameterType.fullyQualifiedName())) {
            reportIssue(method.simpleName(), "Remove this method");
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodSymbol(org.sonar.plugins.java.api.semantic.Symbol.MethodSymbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 79 with MethodTree

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

the class SelectorMethodArgumentCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    if (Boolean.TRUE.equals(methodTree.isOverriding())) {
        return;
    }
    List<Symbol> booleanParameterSymbols = getBooleanParametersAsSymbol(methodTree.parameters());
    BlockTree blockTree = methodTree.block();
    if (isPublic(methodTree) && blockTree != null && !booleanParameterSymbols.isEmpty()) {
        for (Symbol variable : booleanParameterSymbols) {
            Collection<IdentifierTree> usages = variable.usages();
            if (usages.size() == 1) {
                blockTree.accept(new ConditionalStatementVisitor(variable.name(), Iterables.get(usages, 0), methodTree));
            }
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 80 with MethodTree

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

the class StandardFunctionalInterfaceCheck method lookupMatchingStandardInterface.

private static Optional<String> lookupMatchingStandardInterface(MethodSymbol functionalMethod) {
    MethodTree declaration = functionalMethod.declaration();
    if (!functionalMethod.thrownTypes().isEmpty() || (declaration != null && !declaration.typeParameters().isEmpty())) {
        return Optional.empty();
    }
    Type returnType = declaration != null ? declaration.returnType().symbolType() : functionalMethod.returnType().type();
    return STD_INTERFACE_BY_PARAMETER_COUNT.getOrDefault(functionalMethod.parameterTypes().size(), Collections.emptyList()).stream().map(standardInterface -> standardInterface.matchingSpecialization(functionalMethod, returnType)).filter(Objects::nonNull).findFirst();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) 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