use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method this_expression.
/**
* 15.8.3. this
*/
@Test
public void this_expression() {
IdentifierTree tree = (IdentifierTree) expressionOfReturnStatement("class T { Object m() { return this; } }");
assertThat(tree.is(Tree.Kind.IDENTIFIER)).isTrue();
assertThat(tree).isNotNull();
assertThat(tree.identifierToken().text()).isEqualTo("this");
assertThat(tree.name()).isEqualTo("this");
assertThatChildrenIteratorHasSize(tree, 1);
}
use of org.sonar.plugins.java.api.tree.IdentifierTree 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.IdentifierTree 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.IdentifierTree in project sonar-java by SonarSource.
the class GenericsTest method methodHasUsagesWithSameTypeAs.
private static void methodHasUsagesWithSameTypeAs(JavaType type, String methodName, int methodIndex, String... variables) {
JavaSymbol.MethodJavaSymbol method = getMethodSymbol(type, methodName, methodIndex);
List<IdentifierTree> usages = method.usages();
assertThat(usages).overridingErrorMessage("Method '" + methodName + "' should have " + variables.length + " reference(s) but has " + usages.size() + ".").hasSize(variables.length);
for (int i = 0; i < variables.length; i++) {
String variableName = variables[i];
if (variableName != null) {
Type methodInvocationType = getMethodInvocationType(method, i);
Type variableType = getVariableType(type, variableName);
assertThat(methodInvocationType).overridingErrorMessage("Type of expression should be the same as type of variable '" + variableName + "'.").isSameAs(variableType);
}
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method type_parameters_and_bounds.
@Test
public void type_parameters_and_bounds() {
TypeParameterListTreeImpl tree = (TypeParameterListTreeImpl) firstType("class Foo<T, U extends Object & Number> {}").typeParameters();
assertThat(tree.openBracketToken().text()).isEqualTo("<");
assertThat(tree.closeBracketToken().text()).isEqualTo(">");
assertThat(tree).hasSize(2);
assertThat(tree.separators()).hasSize(1);
assertThatChildrenIteratorHasSize(tree, 5);
TypeParameterTree param = tree.get(0);
assertThat(param.identifier().name()).isEqualTo("T");
assertThat(param.bounds()).isEmpty();
assertThat(param.bounds().separators()).isEmpty();
assertThatChildrenIteratorHasSize(param, 1);
param = tree.get(1);
assertThat(param.identifier().name()).isEqualTo("U");
assertThat(param.bounds()).hasSize(2);
assertThat(param.bounds().separators()).hasSize(1);
assertThat(((IdentifierTree) param.bounds().get(0)).name()).isEqualTo("Object");
assertThat(((IdentifierTree) param.bounds().get(1)).name()).isEqualTo("Number");
assertThatChildrenIteratorHasSize(param, 3);
}
Aggregations