Search in sources :

Example 1 with ParameterizedTypeTree

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

the class NewClassTreeImpl method getConstructorIdentifier.

private static IdentifierTree getConstructorIdentifier(Tree constructorSelect) {
    IdentifierTree constructorIdentifier;
    if (constructorSelect.is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree mset = (MemberSelectExpressionTree) constructorSelect;
        constructorIdentifier = mset.identifier();
    } else if (constructorSelect.is(Tree.Kind.IDENTIFIER)) {
        constructorIdentifier = (IdentifierTree) constructorSelect;
    } else if (constructorSelect.is(Tree.Kind.PARAMETERIZED_TYPE)) {
        constructorIdentifier = getConstructorIdentifier(((ParameterizedTypeTree) constructorSelect).type());
    } else {
        throw new IllegalStateException("Constructor select is not of the expected type " + constructorSelect);
    }
    return constructorIdentifier;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 2 with ParameterizedTypeTree

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

the class JavaTreeModelTest method annotations.

@Test
public void annotations() {
    ClassTree tree = firstType("@SuppressWarnings(\"unchecked\") class T { }");
    List<AnnotationTree> annotations = tree.modifiers().annotations();
    assertThat(annotations).hasSize(1);
    AnnotationTree annotation = annotations.get(0);
    assertThat(annotation.annotationType().is(Tree.Kind.IDENTIFIER)).isTrue();
    assertThat(annotation.arguments().openParenToken()).isNotNull();
    assertThat(annotation.arguments().separators()).isEmpty();
    assertThat(annotation.arguments()).hasSize(1);
    assertThat(annotation.arguments().get(0).is(Tree.Kind.STRING_LITERAL)).isTrue();
    assertThat(annotation.arguments().closeParenToken()).isNotNull();
    assertThat(annotation.atToken()).isNotNull();
    assertThatChildrenIteratorHasSize(annotation, 3);
    tree = firstType("@Target( ) class U {}");
    annotations = tree.modifiers().annotations();
    assertThat(annotations).hasSize(1);
    annotation = annotations.get(0);
    assertThat(annotation.arguments().openParenToken()).isNotNull();
    assertThat(annotation.arguments()).isEmpty();
    assertThat(annotation.arguments().separators()).isEmpty();
    assertThat(annotation.arguments().closeParenToken()).isNotNull();
    assertThat(annotation.atToken()).isNotNull();
    assertThatChildrenIteratorHasSize(annotation, 3);
    tree = firstType("@Target({ElementType.METHOD}) class U {}");
    annotations = tree.modifiers().annotations();
    assertThat(annotations).hasSize(1);
    annotation = annotations.get(0);
    assertThat(annotation.arguments().openParenToken()).isNotNull();
    assertThat(annotation.arguments()).hasSize(1);
    assertThat(annotation.arguments().get(0).is(Tree.Kind.NEW_ARRAY)).isTrue();
    assertThat(annotation.arguments().separators()).isEmpty();
    assertThat(annotation.arguments().closeParenToken()).isNotNull();
    assertThat(annotation.atToken()).isNotNull();
    assertThatChildrenIteratorHasSize(annotation, 3);
    tree = firstType("@SuppressWarnings({\"hello\",}) class U {}");
    annotations = tree.modifiers().annotations();
    assertThat(annotations).hasSize(1);
    annotation = annotations.get(0);
    assertThat(annotation.arguments().openParenToken()).isNotNull();
    assertThat(annotation.arguments()).hasSize(1);
    assertThat(annotation.arguments().get(0).is(Tree.Kind.NEW_ARRAY)).isTrue();
    NewArrayTree arg = (NewArrayTree) annotation.arguments().get(0);
    assertThat(arg.initializers()).hasSize(1);
    assertThat(arg.initializers().get(0).is(Tree.Kind.STRING_LITERAL)).isTrue();
    assertThat(arg.initializers().separators()).hasSize(1);
    assertThat(annotation.arguments().closeParenToken()).isNotNull();
    assertThat(annotation.atToken()).isNotNull();
    assertThatChildrenIteratorHasSize(annotation, 3);
    tree = firstType("@Target(value={ElementType.METHOD}, value2=\"toto\") class T { }");
    annotations = tree.modifiers().annotations();
    assertThat(annotations).hasSize(1);
    annotation = annotations.get(0);
    assertThat(annotation.annotationType().is(Tree.Kind.IDENTIFIER)).isTrue();
    assertThat(annotation.arguments().openParenToken()).isNotNull();
    assertThat(annotation.arguments()).hasSize(2);
    assertThat(annotation.arguments().separators()).hasSize(1);
    assertThat(annotation.arguments().get(0).is(Tree.Kind.ASSIGNMENT)).isTrue();
    assertThat(annotation.arguments().closeParenToken()).isNotNull();
    assertThat(annotation.atToken()).isNotNull();
    assertThatChildrenIteratorHasSize(annotation, 3);
    VariableTree variable = (VariableTree) firstMethodFirstStatement("class T { private void meth() { @NonNullable String str;}}");
    assertThatChildrenIteratorHasSize(variable, 4);
    annotations = variable.modifiers().annotations();
    assertThat(annotations).hasSize(1);
    annotation = annotations.get(0);
    assertThat(annotation.annotationType().is(Tree.Kind.IDENTIFIER)).isTrue();
    assertThat(annotation.atToken()).isNotNull();
    assertThat(annotation.arguments()).isEmpty();
    assertThatChildrenIteratorHasSize(annotation, 3);
    annotations = compilationUnit("@PackageLevelAnnotation package blammy;").packageDeclaration().annotations();
    assertThat(annotations).hasSize(1);
    assertThat(annotations.get(0).atToken()).isNotNull();
    assertThat(annotation.arguments()).isEmpty();
    assertThatChildrenIteratorHasSize(annotation, 3);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { @Foo Integer foo; } }");
    assertThat(variable.modifiers().annotations()).hasSize(1);
    assertThat(variable.type().is(Tree.Kind.IDENTIFIER)).isTrue();
    assertThat(variable.type().annotations()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 4);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { @Foo java.lang.Integer foo; } }");
    assertThat(variable.modifiers().annotations()).hasSize(1);
    assertThat(variable.type().is(Tree.Kind.MEMBER_SELECT)).isTrue();
    assertThat(variable.type().annotations()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 4);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { java.lang.@Foo Integer foo; } }");
    assertThat(variable.modifiers()).isEmpty();
    assertThat(variable.type().is(Tree.Kind.MEMBER_SELECT)).isTrue();
    assertThat(variable.type().annotations()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 4);
    assertThat(((MemberSelectExpressionTree) variable.type()).identifier().annotations()).hasSize(1);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { a.B.C foo = a.B.new @Foo C(); } }");
    assertThat(variable.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 6);
    TypeTree type = ((NewClassTree) variable.initializer()).identifier();
    assertThat(type.is(Tree.Kind.IDENTIFIER)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 2);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { a.b.C<Integer> foo = a.B.new @Foo C<Integer>(); } }");
    assertThat(variable.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 6);
    type = ((NewClassTree) variable.initializer()).identifier();
    assertThat(type.is(Tree.Kind.PARAMETERIZED_TYPE)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 3);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { a.B.C foo = new @Foo a.B.C(); } }");
    assertThat(variable.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 6);
    type = ((NewClassTree) variable.initializer()).identifier();
    assertThat(type.is(Tree.Kind.MEMBER_SELECT)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 4);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { a.b.C<Integer> foo = new @Foo a.b.C<Integer>(); } }");
    assertThat(variable.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 6);
    type = ((NewClassTree) variable.initializer()).identifier();
    assertThat(type.is(Tree.Kind.PARAMETERIZED_TYPE)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 3);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { int[] foo = new @Foo int[42]; } }");
    assertThat(variable.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 6);
    type = ((NewArrayTree) variable.initializer()).type();
    assertThat(type.is(Tree.Kind.PRIMITIVE_TYPE)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 2);
    variable = ((TryStatementTree) firstMethodFirstStatement("class T { private void m() { try{ } catch (@Foo E1 | E2 e) {}; } }")).catches().get(0).parameter();
    assertThat(variable.modifiers()).hasSize(1);
    assertThatChildrenIteratorHasSize(variable, 3);
    type = variable.type();
    assertThat(type.is(Tree.Kind.UNION_TYPE)).isTrue();
    assertThat(type.annotations()).isEmpty();
    assertThat(((UnionTypeTree) type).typeAlternatives().separators()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 1);
    ClassTree classTree = firstType("class T extends @Foo a.b.C {}");
    assertThat(classTree.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(classTree, 9);
    type = classTree.superClass();
    assertThat(type.is(Tree.Kind.MEMBER_SELECT)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 4);
    classTree = firstType("class T extends @Foo a.b.C<Integer> {}");
    assertThat(classTree.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(classTree, 9);
    type = classTree.superClass();
    assertThat(type.is(Tree.Kind.PARAMETERIZED_TYPE)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 3);
    classTree = (ClassTree) firstMethodFirstStatement("class MyClass<A, B, C> { void foo() { class MyOtherClass extends @Foo MyClass<A, B, C>.MyInnerClass {} } public class MyInnerClass {}}");
    assertThat(classTree.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(classTree, 9);
    type = classTree.superClass();
    assertThat(type.is(Tree.Kind.MEMBER_SELECT)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    type = (ParameterizedTypeTree) ((MemberSelectExpressionTree) type).expression();
    assertThat(type.is(Tree.Kind.PARAMETERIZED_TYPE)).isTrue();
    assertThat(type.annotations()).hasSize(0);
    assertThatChildrenIteratorHasSize(type, 2);
    TypeCastTree typeCast = (TypeCastTree) ((ReturnStatementTree) firstMethodFirstStatement("class T { private long m(int a) { return (@Foo long) a; } }")).expression();
    assertThat(typeCast.type()).isNotNull();
    assertThatChildrenIteratorHasSize(typeCast, 5);
    type = typeCast.type();
    assertThat(type.is(Tree.Kind.PRIMITIVE_TYPE)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 2);
}
Also used : TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) Test(org.junit.Test)

Example 3 with ParameterizedTypeTree

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

the class DiamondOperatorCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    NewClassTree newClassTree = (NewClassTree) tree;
    TypeTree newTypeTree = newClassTree.identifier();
    if (newClassTree.classBody() == null && isParameterizedType(newTypeTree)) {
        TypeTree type = getTypeFromExpression(tree.parent(), expressionKindsToCheck);
        if (type != null && isParameterizedType(type)) {
            reportIssue(((ParameterizedTypeTree) newTypeTree).typeArguments(), "Replace the type specification in this constructor call with the diamond operator (\"<>\")." + context.getJavaVersion().java7CompatibilityMessage());
        }
    }
}
Also used : ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 4 with ParameterizedTypeTree

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

the class JavaTreeModelTest method type_arguments.

/**
 * 4.5.1. Type Arguments and Wildcards
 */
@Test
public void type_arguments() {
    VariableTree variableTree = (VariableTree) firstMethodFirstStatement("public class T { void m() { ClassType<? extends A, ? super B, ?, C> var; } }");
    assertThatChildrenIteratorHasSize(variableTree, 4);
    ParameterizedTypeTree parameterizedTypeTree = (ParameterizedTypeTree) variableTree.type();
    assertThatChildrenIteratorHasSize(parameterizedTypeTree, 2);
    TypeArguments typeArguments = parameterizedTypeTree.typeArguments();
    assertThat(typeArguments).hasSize(4);
    assertThat(typeArguments.separators()).hasSize(3);
    assertThatChildrenIteratorHasSize(typeArguments, 9);
    WildcardTree wildcard = (WildcardTree) typeArguments.get(0);
    assertThat(wildcard.is(Tree.Kind.EXTENDS_WILDCARD)).isTrue();
    assertThat(wildcard.bound()).isInstanceOf(IdentifierTree.class);
    assertThat(wildcard.queryToken()).isNotNull();
    assertThat(wildcard.queryToken().text()).isEqualTo("?");
    assertThat(wildcard.extendsOrSuperToken()).isNotNull();
    assertThat(wildcard.extendsOrSuperToken().text()).isEqualTo("extends");
    assertThatChildrenIteratorHasSize(wildcard, 3);
    wildcard = (WildcardTree) typeArguments.get(1);
    assertThat(wildcard.is(Tree.Kind.SUPER_WILDCARD)).isTrue();
    assertThat(wildcard.bound()).isInstanceOf(IdentifierTree.class);
    assertThat(wildcard.queryToken()).isNotNull();
    assertThat(wildcard.queryToken().text()).isEqualTo("?");
    assertThat(wildcard.extendsOrSuperToken()).isNotNull();
    assertThat(wildcard.extendsOrSuperToken().text()).isEqualTo("super");
    assertThatChildrenIteratorHasSize(wildcard, 3);
    wildcard = (WildcardTree) typeArguments.get(2);
    assertThat(wildcard.is(Tree.Kind.UNBOUNDED_WILDCARD)).isTrue();
    assertThat(wildcard.bound()).isNull();
    assertThat(wildcard.queryToken().text()).isEqualTo("?");
    assertThat(wildcard.queryToken()).isNotNull();
    assertThat(wildcard.extendsOrSuperToken()).isNull();
    assertThatChildrenIteratorHasSize(wildcard, 1);
    assertThat(typeArguments.get(3)).isInstanceOf(IdentifierTree.class);
    variableTree = (VariableTree) firstMethodFirstStatement("public class T { void m() { ClassType<@Foo ? extends A> var; } }");
    parameterizedTypeTree = (ParameterizedTypeTree) variableTree.type();
    assertThatChildrenIteratorHasSize(parameterizedTypeTree, 2);
    typeArguments = parameterizedTypeTree.typeArguments();
    assertThatChildrenIteratorHasSize(typeArguments, 3);
    wildcard = (WildcardTree) typeArguments.get(0);
    assertThat(wildcard.is(Tree.Kind.EXTENDS_WILDCARD)).isTrue();
    assertThat(wildcard.bound()).isInstanceOf(IdentifierTree.class);
    assertThat(wildcard.queryToken().text()).isEqualTo("?");
    assertThat(wildcard.annotations()).hasSize(1);
    assertThat(wildcard.extendsOrSuperToken().text()).isEqualTo("extends");
    assertThatChildrenIteratorHasSize(wildcard, 4);
    variableTree = (VariableTree) firstMethodFirstStatement("public class T { void m() { ClassType<? extends @Foo @Bar A> var; } }");
    parameterizedTypeTree = (ParameterizedTypeTree) variableTree.type();
    assertThatChildrenIteratorHasSize(parameterizedTypeTree, 2);
    typeArguments = parameterizedTypeTree.typeArguments();
    assertThatChildrenIteratorHasSize(typeArguments, 3);
    wildcard = (WildcardTree) typeArguments.get(0);
    assertThat(wildcard.is(Tree.Kind.EXTENDS_WILDCARD)).isTrue();
    assertThat(wildcard.bound()).isInstanceOf(IdentifierTree.class);
    assertThat(wildcard.annotations()).isEmpty();
    assertThat(wildcard.queryToken().text()).isEqualTo("?");
    assertThat(wildcard.extendsOrSuperToken().text()).isEqualTo("extends");
    assertThatChildrenIteratorHasSize(wildcard, 3);
}
Also used : ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) WildcardTree(org.sonar.plugins.java.api.tree.WildcardTree) Test(org.junit.Test)

Example 5 with ParameterizedTypeTree

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

the class JavaTreeModelTest method type_parameters_tokens.

@Test
public void type_parameters_tokens() {
    ParameterizedTypeTree tree = (ParameterizedTypeTree) firstType("class Foo<E> extends List<E> {}").superClass();
    assertThat(tree).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 2);
    TypeArguments typeArguments = tree.typeArguments();
    assertThat(typeArguments).isNotNull();
    assertThat(typeArguments).hasSize(1);
    assertThat(typeArguments.separators()).isEmpty();
    assertThat(typeArguments.openBracketToken()).isNotNull();
    assertThat(typeArguments.closeBracketToken()).isNotNull();
    assertThatChildrenIteratorHasSize(typeArguments, 3);
    tree = (ParameterizedTypeTree) firstType("class Mop<K,V> implements Map<K,V> {}").superInterfaces().get(0);
    assertThat(tree).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 2);
    typeArguments = tree.typeArguments();
    assertThat(typeArguments).isNotNull();
    assertThat(typeArguments).hasSize(2);
    assertThat(typeArguments.separators()).hasSize(1);
    assertThat(typeArguments.openBracketToken()).isNotNull();
    assertThat(typeArguments.closeBracketToken()).isNotNull();
    assertThatChildrenIteratorHasSize(typeArguments, 5);
}
Also used : ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) Test(org.junit.Test)

Aggregations

ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)6 Test (org.junit.Test)3 TypeArguments (org.sonar.plugins.java.api.tree.TypeArguments)3 ArrayTypeTree (org.sonar.plugins.java.api.tree.ArrayTypeTree)2 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)2 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)2 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 Resolution (org.sonar.java.resolve.Resolve.Resolution)1 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 NewArrayTree (org.sonar.plugins.java.api.tree.NewArrayTree)1 PrimitiveTypeTree (org.sonar.plugins.java.api.tree.PrimitiveTypeTree)1 TypeCastTree (org.sonar.plugins.java.api.tree.TypeCastTree)1 UnionTypeTree (org.sonar.plugins.java.api.tree.UnionTypeTree)1 WildcardTree (org.sonar.plugins.java.api.tree.WildcardTree)1