Search in sources :

Example 71 with MethodInvocationTree

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

the class ExpressionUtilsTest method method_name.

@Test
public void method_name() throws Exception {
    File file = new File("src/test/files/model/ExpressionUtilsMethodNameTest.java");
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
    MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(0);
    MethodInvocationTree firstMIT = (MethodInvocationTree) ((ExpressionStatementTree) methodTree.block().body().get(0)).expression();
    MethodInvocationTree secondMIT = (MethodInvocationTree) ((ExpressionStatementTree) methodTree.block().body().get(1)).expression();
    assertThat(ExpressionUtils.methodName(firstMIT).name()).isEqualTo("foo");
    assertThat(ExpressionUtils.methodName(secondMIT).name()).isEqualTo("foo");
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) File(java.io.File) Test(org.junit.Test)

Example 72 with MethodInvocationTree

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

the class SyntaxTreeDebug method memberSelectString.

private static String memberSelectString(MemberSelectExpressionTree expression) {
    StringBuilder buffer = new StringBuilder();
    ExpressionTree target = expression.expression();
    switch(target.kind()) {
        case IDENTIFIER:
            buffer.append(identifierString((IdentifierTree) target));
            break;
        case METHOD_INVOCATION:
            buffer.append(methodInvocationString((MethodInvocationTree) target));
            break;
        case VARIABLE:
            buffer.append(variableString((VariableTree) target));
            break;
        case INT_LITERAL:
            buffer.append(literalString((LiteralTree) target));
            break;
        default:
            break;
    }
    buffer.append('.');
    buffer.append(identifierString(expression.identifier()));
    return buffer.toString();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 73 with MethodInvocationTree

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

the class TypeSubstitutionSolverTest method type_hierarchy_visit_should_be_limited.

@Test
public void type_hierarchy_visit_should_be_limited() {
    ParametrizedTypeCache parametrizedTypeCache = new ParametrizedTypeCache();
    BytecodeCompleter bytecodeCompleter = new BytecodeCompleter(new SquidClassLoader(new ArrayList<>()), parametrizedTypeCache);
    Symbols symbols = new Symbols(bytecodeCompleter);
    ActionParser<Tree> parser = JavaParser.createParser();
    SemanticModel semanticModel = new SemanticModel(bytecodeCompleter);
    Resolve resolve = new Resolve(symbols, bytecodeCompleter, parametrizedTypeCache);
    TypeAndReferenceSolver typeAndReferenceSolver = new TypeAndReferenceSolver(semanticModel, symbols, resolve, parametrizedTypeCache);
    CompilationUnitTree tree = (CompilationUnitTree) parser.parse(new File("src/test/files/sym/ComplexHierarchy.java"));
    new FirstPass(semanticModel, symbols, resolve, parametrizedTypeCache, typeAndReferenceSolver).visitCompilationUnit(tree);
    typeAndReferenceSolver.visitCompilationUnit(tree);
    ClassTree classTree = (ClassTree) tree.types().get(tree.types().size() - 1);
    JavaType site = (JavaType) classTree.symbol().type();
    MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) ((MethodTree) classTree.members().get(0)).block().body().get(0)).expression();
    TypeSubstitutionSolver typeSubstitutionSolver = Mockito.spy(new TypeSubstitutionSolver(parametrizedTypeCache, symbols));
    // call with empty formals should return.
    typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(new ArrayList<>(), site);
    verify(typeSubstitutionSolver, times(0)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
    JavaSymbol.MethodJavaSymbol methodJavaSymbol = (JavaSymbol.MethodJavaSymbol) mit.symbol();
    typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(((MethodJavaType) methodJavaSymbol.type).argTypes, site);
    verify(typeSubstitutionSolver, times(11)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayList(java.util.ArrayList) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) File(java.io.File) Test(org.junit.Test)

Example 74 with MethodInvocationTree

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

the class ImmediateReverseBoxingCheck method checkForBoxing.

private void checkForBoxing(ExpressionTree expression) {
    if (expression.is(Tree.Kind.NEW_CLASS)) {
        NewClassTree newClassTree = (NewClassTree) expression;
        Symbol.TypeSymbol classSymbol = wrapperClassSymbol(newClassTree);
        if (classSymbol != null) {
            ExpressionTree boxingArg = newClassTree.arguments().get(0);
            if (boxingArg.symbolType().isPrimitive()) {
                addBoxingIssue(newClassTree, classSymbol, boxingArg);
            }
        }
    } else if (expression.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expression;
        if (isValueOfInvocation(methodInvocationTree)) {
            ExpressionTree boxingArg = methodInvocationTree.arguments().get(0);
            addBoxingIssue(expression, methodInvocationTree.symbol().owner(), boxingArg);
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 75 with MethodInvocationTree

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

the class IdenticalOperandOnBinaryExpressionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) tree;
        checkEqualsMethods(mit);
        return;
    }
    BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
    ExpressionTree rightOperand = binaryExpressionTree.rightOperand();
    ExpressionTree equivalentOperand = equivalentOperand(binaryExpressionTree, rightOperand);
    if (equivalentOperand != null) {
        reportIssue(rightOperand, "Correct one of the identical sub-expressions on both sides of operator \"" + binaryExpressionTree.operatorToken().text() + "\"", ImmutableList.of(new JavaFileScannerContext.Location("", equivalentOperand)), null);
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

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