Search in sources :

Example 16 with MethodInvocationTree

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

the class JavaPropertiesHelperTest method null_if_variable_not_initialized.

@Test
public void null_if_variable_not_initialized() throws Exception {
    ExpressionTree tree = firstExpression("void foo(String prop){ foo(myValue);} " + "String myValue;");
    ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(((MethodInvocationTree) tree).arguments().get(0));
    assertThat(defaultValue).isNull();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) Test(org.junit.Test)

Example 17 with MethodInvocationTree

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

the class JavaPropertiesHelperTest method null_if_variable_not_initilialized_method_invocation.

@Test
public void null_if_variable_not_initilialized_method_invocation() throws Exception {
    ExpressionTree tree = firstExpression("void foo(String prop){ foo(myValue);} " + "java.util.Properties props = new java.util.Properties();" + "String myValue = \"hello\";");
    ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(((MethodInvocationTree) tree).arguments().get(0));
    assertThat(defaultValue).isNull();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) Test(org.junit.Test)

Example 18 with MethodInvocationTree

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

the class JavaTreeModelTest method method_invocation_expression.

/**
 * 15.12. Method Invocation Expressions
 */
@Test
public void method_invocation_expression() {
    // TODO test NonWildTypeArguments
    MethodInvocationTree tree = (MethodInvocationTree) expressionOfFirstStatement("class T { void m() { identifier(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("identifier");
    assertThat(tree.arguments().openParenToken()).isNotNull();
    assertThat(tree.arguments()).hasSize(2);
    assertThat(tree.arguments().separators()).hasSize(1);
    assertThat(tree.arguments().closeParenToken()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 2);
    tree = (MethodInvocationTree) expressionOfFirstStatement("class T { void m() { <T>identifier(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("identifier");
    assertThat(tree.arguments()).hasSize(2);
    assertThatChildrenIteratorHasSize(tree, 3);
    tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { super.identifier(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    MemberSelectExpressionTree memberSelectExpression = (MemberSelectExpressionTree) tree.methodSelect();
    assertThatChildrenIteratorHasSize(memberSelectExpression, 3);
    assertThat(memberSelectExpression.identifier().name()).isEqualTo("identifier");
    assertThat(memberSelectExpression.operatorToken()).isNotNull();
    assertThat(((IdentifierTree) memberSelectExpression.expression()).name()).isEqualTo("super");
    assertThat(tree.arguments()).hasSize(2);
    assertThatChildrenIteratorHasSize(tree, 2);
    tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { TypeName.super.identifier(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    memberSelectExpression = (MemberSelectExpressionTree) tree.methodSelect();
    assertThat(memberSelectExpression.identifier().name()).isEqualTo("identifier");
    assertThat(memberSelectExpression.operatorToken()).isNotNull();
    memberSelectExpression = (MemberSelectExpressionTree) memberSelectExpression.expression();
    assertThatChildrenIteratorHasSize(memberSelectExpression, 3);
    assertThat(memberSelectExpression.identifier().name()).isEqualTo("super");
    assertThat(memberSelectExpression.operatorToken()).isNotNull();
    assertThat(((IdentifierTree) memberSelectExpression.expression()).name()).isEqualTo("TypeName");
    assertThat(tree.arguments()).hasSize(2);
    assertThatChildrenIteratorHasSize(tree, 2);
    tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { TypeName.identifier(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    memberSelectExpression = (MemberSelectExpressionTree) tree.methodSelect();
    assertThatChildrenIteratorHasSize(memberSelectExpression, 3);
    assertThat(memberSelectExpression.identifier().name()).isEqualTo("identifier");
    assertThat(memberSelectExpression.operatorToken()).isNotNull();
    assertThat(((IdentifierTree) memberSelectExpression.expression()).name()).isEqualTo("TypeName");
    assertThat(tree.arguments()).hasSize(2);
    assertThatChildrenIteratorHasSize(tree, 2);
    tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { TypeName.<T>identifier(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    memberSelectExpression = (MemberSelectExpressionTree) tree.methodSelect();
    assertThatChildrenIteratorHasSize(memberSelectExpression, 3);
    assertThat(memberSelectExpression.identifier().name()).isEqualTo("identifier");
    assertThat(memberSelectExpression.operatorToken()).isNotNull();
    assertThat(((IdentifierTree) memberSelectExpression.expression()).name()).isEqualTo("TypeName");
    assertThat(tree.arguments()).hasSize(2);
    assertThatChildrenIteratorHasSize(tree, 3);
    tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { primary().<T>identifier(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    memberSelectExpression = (MemberSelectExpressionTree) tree.methodSelect();
    assertThatChildrenIteratorHasSize(memberSelectExpression, 3);
    assertThat(memberSelectExpression.identifier().name()).isEqualTo("identifier");
    assertThat(memberSelectExpression.expression()).isInstanceOf(MethodInvocationTree.class);
    assertThat(memberSelectExpression.operatorToken()).isNotNull();
    assertThat(tree.arguments()).hasSize(2);
    assertThatChildrenIteratorHasSize(tree, 3);
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 19 with MethodInvocationTree

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

the class JavaTreeModelTest method method_reference_expression_should_not_break_AST.

@Test
public void method_reference_expression_should_not_break_AST() throws Exception {
    String code = "class T { public void meth(){IntStream.range(1,12).map(new MethodReferences()::<String>square).map(super::myMethod).map(int[]::new).forEach(System.out::println);}}";
    MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) firstMethodFirstStatement(code)).expression();
    MethodReferenceTree mrt = (MethodReferenceTree) mit.arguments().get(0);
    assertThat(mrt.expression().is(Kind.MEMBER_SELECT)).isTrue();
    assertThat(mrt.doubleColon()).isNotNull();
    assertThatChildrenIteratorHasSize(mrt, 3);
    mit = (MethodInvocationTree) ((MemberSelectExpressionTree) mit.methodSelect()).expression();
    mrt = (MethodReferenceTree) mit.arguments().get(0);
    assertThat(mrt.expression().is(Kind.ARRAY_TYPE)).isTrue();
    assertThat(mrt.doubleColon()).isNotNull();
    assertThat(mrt.method().name()).isEqualTo("new");
    assertThatChildrenIteratorHasSize(mrt, 3);
    mit = (MethodInvocationTree) ((MemberSelectExpressionTree) mit.methodSelect()).expression();
    mrt = (MethodReferenceTree) mit.arguments().get(0);
    assertThat(mrt.expression().is(Kind.IDENTIFIER)).isTrue();
    assertThat(((IdentifierTree) mrt.expression()).name()).isEqualTo("super");
    assertThat(mrt.doubleColon()).isNotNull();
    assertThatChildrenIteratorHasSize(mrt, 3);
    mit = (MethodInvocationTree) ((MemberSelectExpressionTree) mit.methodSelect()).expression();
    mrt = (MethodReferenceTree) mit.arguments().get(0);
    assertThat(mrt.expression().is(Kind.NEW_CLASS)).isTrue();
    assertThat(mrt.doubleColon()).isNotNull();
    assertThat(mrt.typeArguments()).isNotNull();
    assertThat(mrt.method().name()).isEqualTo("square");
    assertThatChildrenIteratorHasSize(mrt, 4);
}
Also used : MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 20 with MethodInvocationTree

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

the class MethodReferenceResolutionTest method MethodReferenceUsingThis.

@Test
public void MethodReferenceUsingThis() throws Exception {
    Result result = Result.createFor("MethodReferencesThis");
    JavaSymbol bar1 = result.symbol("bar1");
    assertThat(bar1.usages()).hasSize(2);
    JavaSymbol bar = result.symbol("bar");
    assertThat(bar.usages()).hasSize(1);
    MethodReferenceTree methodRef = (MethodReferenceTree) bar.usages().get(0).parent();
    assertThat(methodRef.symbolType().is("java.util.function.Consumer")).isTrue();
    MethodInvocationTree foreach = (MethodInvocationTree) methodRef.parent().parent();
    assertThat(foreach.symbol().owner().type().is("java.lang.Iterable")).isTrue();
}
Also used : MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) 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