Search in sources :

Example 11 with MethodTree

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

the class ExpressionUtilsTest method test_simple_assignments.

@Test
public void test_simple_assignments() throws Exception {
    File file = new File("src/test/java/org/sonar/java/model/ExpressionUtilsTest.java");
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
    MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(1);
    List<AssignmentExpressionTree> assignments = findAssignmentExpressionTrees(methodTree);
    assertThat(assignments).hasSize(4);
    assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(0))).isTrue();
    assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(1))).isTrue();
    assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(2))).isFalse();
    assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(3))).isFalse();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) File(java.io.File) Test(org.junit.Test)

Example 12 with MethodTree

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

the class UtilityClassWithPublicConstructorCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    if (!hasSemantic() || !isUtilityClass(classTree) || isPrivateInnerClass(classTree)) {
        return;
    }
    boolean hasImplicitPublicConstructor = true;
    for (MethodTree explicitConstructor : getExplicitConstructors(classTree)) {
        hasImplicitPublicConstructor = false;
        if (isPublicConstructor(explicitConstructor)) {
            reportIssue(explicitConstructor.simpleName(), "Hide this public constructor.");
        }
    }
    if (hasImplicitPublicConstructor) {
        reportIssue(classTree.simpleName(), "Add a private constructor to hide the implicit public one.");
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 13 with MethodTree

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

the class RequestMappingMethodPublicCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    Symbol.MethodSymbol methodSymbol = methodTree.symbol();
    if (isClassController(methodSymbol) && isRequestMappingAnnotated(methodSymbol) && !methodSymbol.isPublic()) {
        reportIssue(methodTree.simpleName(), "Make this method \"public\".");
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 14 with MethodTree

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

the class TwoLocksWaitCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    if (tree.is(METHOD, CONSTRUCTOR)) {
        MethodTree methodTree = (MethodTree) tree;
        int initialCounter = findModifier(methodTree.modifiers(), SYNCHRONIZED).map(m -> 1).orElse(0);
        synchronizedStack.push(new Counter(initialCounter));
        findWaitInvocation(methodTree);
    }
}
Also used : SYNCHRONIZED(org.sonar.plugins.java.api.tree.Modifier.SYNCHRONIZED) BaseTreeVisitor(org.sonar.plugins.java.api.tree.BaseTreeVisitor) Tree(org.sonar.plugins.java.api.tree.Tree) METHOD(org.sonar.plugins.java.api.tree.Tree.Kind.METHOD) SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree) Deque(java.util.Deque) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) List(java.util.List) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) MethodMatcher(org.sonar.java.matcher.MethodMatcher) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Optional(java.util.Optional) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) CONSTRUCTOR(org.sonar.plugins.java.api.tree.Tree.Kind.CONSTRUCTOR) Rule(org.sonar.check.Rule) LinkedList(java.util.LinkedList) ModifiersUtils.findModifier(org.sonar.java.model.ModifiersUtils.findModifier) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 15 with MethodTree

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

the class WriteObjectTheOnlySynchronizedMethodCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    Symbol.TypeSymbol enclosingClass = methodTree.symbol().enclosingClass();
    String className = enclosingClass.type().fullyQualifiedName();
    MethodMatcher writeObjectMatcher = SerializableContract.writeObjectMatcher(className);
    if (writeObjectMatcher.matches(methodTree) && hasModifier(methodTree.modifiers(), SYNCHRONIZED)) {
        SynchronizationVisitor visitor = new SynchronizationVisitor(methodTree);
        enclosingClass.declaration().accept(visitor);
        if (!visitor.moreThanSingleLock) {
            reportIssue(ModifiersUtils.getModifier(methodTree.modifiers(), SYNCHRONIZED), "Remove this \"synchronized\" keyword.");
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodMatcher(org.sonar.java.matcher.MethodMatcher)

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