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();
}
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();
}
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);
}
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);
}
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();
}
Aggregations