Search in sources :

Example 36 with MethodInvocationTree

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

the class RedundantStreamCollectCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree collectMIT) {
    ExpressionTree argument = collectMIT.arguments().get(0);
    if (argument.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocation = (MethodInvocationTree) argument;
        REPLACEMENTS.entrySet().stream().filter(e -> e.getKey().matches(methodInvocation)).findFirst().ifPresent(e -> context.reportIssue(this, ExpressionUtils.methodName(methodInvocation), "Use \"" + e.getValue() + "\" instead."));
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Example 37 with MethodInvocationTree

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

the class RedundantTypeCastCheck method requiredForMemberAccess.

private static boolean requiredForMemberAccess(TypeCastTree typeCastTree) {
    ExpressionTree expression = typeCastTree.expression();
    if (!expression.is(Tree.Kind.METHOD_INVOCATION)) {
        Tree parent = typeCastTree.parent();
        return expression.is(Tree.Kind.METHOD_REFERENCE) && parent != null && skipParentheses(parent).is(Tree.Kind.MEMBER_SELECT);
    }
    Symbol symbol = ((MethodInvocationTree) expression).symbol();
    if (!symbol.isMethodSymbol()) {
        return false;
    }
    Type returnType = ((Symbol.MethodSymbol) symbol).returnType().type();
    if (!(returnType instanceof TypeVariableJavaType) || ((TypeVariableJavaType) returnType).bounds().get(0).is("java.lang.Object")) {
        return false;
    }
    // as the member accessed could have also been part of initial type
    return skipParentheses(typeCastTree.parent()).is(Tree.Kind.MEMBER_SELECT);
}
Also used : JavaType(org.sonar.java.resolve.JavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) JavaSymbol(org.sonar.java.resolve.JavaSymbol) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Tree(org.sonar.plugins.java.api.tree.Tree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 38 with MethodInvocationTree

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

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

the class StringMethodsWithLocaleCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
    Tree report = mit.methodSelect();
    if (STRING_FORMAT.matches(mit) && (isLocaleVariant(mit) || !usesLocaleDependentFormatteer(mit.arguments().get(0)))) {
        return;
    }
    if (report.is(Tree.Kind.MEMBER_SELECT)) {
        report = ((MemberSelectExpressionTree) report).identifier();
    }
    reportIssue(report, "Define the locale to be used in this String operation.");
}
Also used : LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)

Example 40 with MethodInvocationTree

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

the class MethodMatcherTest method does_not_match_without_callSite_enclosingClass.

@Test
public void does_not_match_without_callSite_enclosingClass() throws Exception {
    MethodMatcher matcher = MethodMatcher.create().name(NameCriteria.any()).withAnyParameters().callSite(TypeCriteria.anyType());
    Symbol symbol = mock(Symbol.class);
    when(symbol.enclosingClass()).thenReturn(null);
    IdentifierTree id = mock(IdentifierTree.class);
    when(id.is(Tree.Kind.IDENTIFIER)).thenReturn(true);
    when(id.symbol()).thenReturn(symbol);
    MethodInvocationTree tree = mock(MethodInvocationTree.class);
    when(tree.methodSelect()).thenReturn(id);
    assertThat(matcher.matches(tree)).isFalse();
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

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