Search in sources :

Example 26 with MethodInvocationTree

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

the class CollectionMethodsWithLinearComplexityCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodInvocationTree mit = (MethodInvocationTree) tree;
    matcherActualTypeMap.forEach((methodMatcher, actualTypes) -> {
        if (methodMatcher.matches(mit) && invocationInMethod(mit)) {
            Symbol target = invocationTarget(mit);
            if (target != null && isField(target) && matchesActualType(target, actualTypes)) {
                IdentifierTree methodName = ExpressionUtils.methodName(mit);
                reportIssue(methodName, "This call to \"" + methodName.name() + "()\" may be a performance hot spot if the collection is large.");
            }
        }
    });
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 27 with MethodInvocationTree

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

the class CaseInsensitiveComparisonCheck method isToUpperCaseOrToLowerCase.

private static boolean isToUpperCaseOrToLowerCase(ExpressionTree expression) {
    if (expression.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocation = (MethodInvocationTree) expression;
        if (methodInvocation.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
            MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) methodInvocation.methodSelect();
            String name = memberSelect.identifier().name();
            return "toUpperCase".equals(name) || "toLowerCase".equals(name);
        }
    }
    return false;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree)

Example 28 with MethodInvocationTree

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

the class FlowComputationTest method test_getArgumentIdentifier.

@Test
public void test_getArgumentIdentifier() throws Exception {
    MethodInvocationTree mit = (MethodInvocationTree) Result.createForJavaFile("src/test/files/se/FlowComputationGetArgumentIdentifier").referenceTree(6, 5).parent();
    assertThatThrownBy(() -> FlowComputation.getArgumentIdentifier(mit, -1)).isInstanceOf(IllegalArgumentException.class).hasMessage("index must be within arguments range.");
    assertThat(FlowComputation.getArgumentIdentifier(mit, 0).name()).isEqualTo("localVariable");
    assertThat(FlowComputation.getArgumentIdentifier(mit, 1).name()).isEqualTo("field");
    assertThat(FlowComputation.getArgumentIdentifier(mit, 2)).isNull();
    assertThatThrownBy(() -> FlowComputation.getArgumentIdentifier(mit, 4)).isInstanceOf(IllegalArgumentException.class).hasMessage("index must be within arguments range.");
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Test(org.junit.Test)

Example 29 with MethodInvocationTree

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

the class StringToPrimitiveConversionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        if (tree.is(Tree.Kind.VARIABLE)) {
            VariableTreeImpl variableTree = (VariableTreeImpl) tree;
            Type variableType = variableTree.type().symbolType();
            PrimitiveCheck primitiveCheck = getPrimitiveCheck(variableType);
            ExpressionTree initializer = variableTree.initializer();
            if (primitiveCheck != null && initializer != null) {
                primitiveCheck.checkInstantiation(initializer);
            }
        } else {
            MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
            for (PrimitiveCheck primitiveCheck : primitiveChecks) {
                primitiveCheck.checkMethodInvocation(methodInvocationTree);
            }
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl)

Example 30 with MethodInvocationTree

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

the class StaticFieldInitializationCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    switch(tree.kind()) {
        case CLASS:
            classWithSynchronizedMethod.push(hasSynchronizedMethod((ClassTree) tree));
            break;
        case STATIC_INITIALIZER:
            withinStaticInitializer.push(true);
            break;
        case METHOD:
            methodUsesLocks.push(false);
            break;
        case METHOD_INVOCATION:
            if (locks.anyMatch((MethodInvocationTree) tree) && methodUsesLocks.size() != 1) {
                methodUsesLocks.pop();
                methodUsesLocks.push(true);
            }
            break;
        case ASSIGNMENT:
            AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
            if (hasSemantic() && aet.variable().is(Tree.Kind.IDENTIFIER) && !isInSyncBlock() && !isInStaticInitializer() && !isUsingLock() && isInClassWithSynchronizedMethod()) {
                IdentifierTree variable = (IdentifierTree) aet.variable();
                if (isStaticNotVolatileObject(variable)) {
                    reportIssue(variable, "Synchronize this lazy initialization of '" + variable.name() + "'");
                }
            }
            break;
        default:
    }
    super.visitNode(tree);
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Aggregations

MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)87 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)44 Test (org.junit.Test)30 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)30 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 Symbol (org.sonar.plugins.java.api.semantic.Symbol)26 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 Tree (org.sonar.plugins.java.api.tree.Tree)21 Type (org.sonar.plugins.java.api.semantic.Type)14 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)14 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)13 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)10 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)10 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)9 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)8 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)7