Search in sources :

Example 36 with IdentifierTree

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);
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 37 with IdentifierTree

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);
}
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 38 with IdentifierTree

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);
}
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 39 with IdentifierTree

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);
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 40 with IdentifierTree

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);
}
Also used : TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) TypeParameterListTreeImpl(org.sonar.java.ast.parser.TypeParameterListTreeImpl) Test(org.junit.Test)

Aggregations

IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)142 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)52 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)50 Symbol (org.sonar.plugins.java.api.semantic.Symbol)32 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)29 Tree (org.sonar.plugins.java.api.tree.Tree)27 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)20 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)15 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)10 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)9 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)8 ArrayList (java.util.ArrayList)7 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7