Search in sources :

Example 1 with TypeParameters

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

the class UnusedTypeParameterCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        TypeParameters typeParameters;
        String messageEnd;
        if (tree.is(Tree.Kind.METHOD)) {
            typeParameters = ((MethodTree) tree).typeParameters();
            messageEnd = "method.";
        } else {
            typeParameters = ((ClassTree) tree).typeParameters();
            messageEnd = "class.";
            if (tree.is(Tree.Kind.INTERFACE)) {
                messageEnd = "interface.";
            }
        }
        for (TypeParameterTree typeParameter : typeParameters) {
            Symbol symbol = semanticModel.getSymbol(typeParameter);
            if (symbol.usages().isEmpty()) {
                String message = new StringBuilder(typeParameter.identifier().name()).append(" is not used in the ").append(messageEnd).toString();
                reportIssue(typeParameter.identifier(), message);
            }
        }
    }
}
Also used : TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) TypeParameters(org.sonar.plugins.java.api.tree.TypeParameters) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 2 with TypeParameters

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

the class LeftCurlyBraceBaseTreeVisitor method getLastTokenFromSignature.

@CheckForNull
private static SyntaxToken getLastTokenFromSignature(ClassTree classTree) {
    List<TypeTree> superInterfaces = classTree.superInterfaces();
    if (!superInterfaces.isEmpty()) {
        return getIdentifierToken(Iterables.getLast(superInterfaces));
    }
    TypeTree superClass = classTree.superClass();
    if (superClass != null) {
        return getIdentifierToken(superClass);
    }
    TypeParameters typeParameters = classTree.typeParameters();
    if (!typeParameters.isEmpty()) {
        return typeParameters.closeBracketToken();
    }
    IdentifierTree simpleName = classTree.simpleName();
    if (simpleName != null) {
        return simpleName.identifierToken();
    }
    // enum constants and new class trees are handled separately
    return null;
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeParameters(org.sonar.plugins.java.api.tree.TypeParameters) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) CheckForNull(javax.annotation.CheckForNull)

Example 3 with TypeParameters

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

the class JavaTreeModelTest method interface_declaration.

/*
   * 9. Interfaces
   */
@Test
public void interface_declaration() {
    ClassTree tree = firstType("public interface T<U> extends I1, I2 { }");
    assertThat(tree.is(Tree.Kind.INTERFACE)).isTrue();
    assertThat(tree.modifiers().modifiers()).hasSize(1);
    assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
    assertThat(tree.simpleName().name()).isEqualTo("T");
    TypeParameters typeParameters = tree.typeParameters();
    assertThatChildrenIteratorHasSize(typeParameters, 3);
    assertThat(typeParameters).isNotEmpty();
    assertThat(tree.superClass()).isNull();
    assertThat(tree.superInterfaces()).hasSize(2);
    assertThat(tree.superInterfaces().separators()).hasSize(1);
    assertThat(tree.declarationKeyword().text()).isEqualTo("interface");
    assertThatChildrenIteratorHasSize(tree, 8);
    tree = firstType("public interface T { }");
    assertThat(tree.is(Tree.Kind.INTERFACE)).isTrue();
    assertThat(tree.modifiers().modifiers()).hasSize(1);
    assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
    assertThat(tree.simpleName().name()).isEqualTo("T");
    assertThat(tree.typeParameters()).isEmpty();
    assertThat(tree.superClass()).isNull();
    assertThat(tree.openBraceToken().text()).isEqualTo("{");
    assertThat(tree.closeBraceToken().text()).isEqualTo("}");
    assertThat(tree.superInterfaces()).isEmpty();
    assertThat(tree.declarationKeyword().text()).isEqualTo("interface");
    assertThatChildrenIteratorHasSize(tree, 7);
}
Also used : TypeParameters(org.sonar.plugins.java.api.tree.TypeParameters) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 4 with TypeParameters

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

the class JavaTreeModelTest method class_declaration.

/*
   * 8. Classes
   */
@Test
public void class_declaration() {
    ClassTree tree = firstType("public class T<U> extends C implements I1, I2 { }");
    assertThat(tree.is(Tree.Kind.CLASS)).isTrue();
    List<ModifierKeywordTree> modifiers = tree.modifiers().modifiers();
    assertThat(modifiers).hasSize(1);
    assertThat(modifiers.get(0).modifier()).isEqualTo(Modifier.PUBLIC);
    assertThat(modifiers.get(0).keyword().text()).isEqualTo("public");
    assertThat(tree.simpleName().name()).isEqualTo("T");
    TypeParameters typeParameters = tree.typeParameters();
    assertThat(typeParameters).isNotEmpty();
    assertThat(typeParameters.separators()).isEmpty();
    assertThatChildrenIteratorHasSize(typeParameters, 3);
    assertThat(tree.openBraceToken().text()).isEqualTo("{");
    assertThat(tree.superClass()).isNotNull();
    assertThat(tree.superInterfaces()).hasSize(2);
    assertThat(tree.superInterfaces().separators()).hasSize(1);
    assertThat(tree.superInterfaces().separators().get(0).text()).isEqualTo(",");
    assertThat(tree.closeBraceToken().text()).isEqualTo("}");
    assertThat(tree.declarationKeyword().text()).isEqualTo("class");
    tree = firstType("public class T { }");
    modifiers = tree.modifiers().modifiers();
    assertThat(modifiers).hasSize(1);
    assertThat(modifiers.get(0).modifier()).isEqualTo(Modifier.PUBLIC);
    assertThat(modifiers.get(0).keyword().text()).isEqualTo("public");
    assertThat(tree.simpleName().name()).isEqualTo("T");
    assertThat(tree.typeParameters()).isEmpty();
    assertThat(tree.superClass()).isNull();
    assertThat(tree.superInterfaces()).isEmpty();
    assertThat(tree.declarationKeyword().text()).isEqualTo("class");
    tree = firstType("class T<U,V> { }");
    assertThat(tree.modifiers()).isEmpty();
    assertThat(tree.simpleName().name()).isEqualTo("T");
    typeParameters = tree.typeParameters();
    assertThat(typeParameters).hasSize(2);
    assertThat(typeParameters.separators()).hasSize(1);
    assertThatChildrenIteratorHasSize(typeParameters, 5);
    assertThat(tree.superClass()).isNull();
    assertThat(tree.superInterfaces()).isEmpty();
    assertThat(tree.declarationKeyword().text()).isEqualTo("class");
    tree = firstType("@Deprecated class T { }");
    assertThat(tree.is(Tree.Kind.CLASS)).isTrue();
    assertThat(tree.modifiers().annotations()).hasSize(1);
    assertThat(tree.declarationKeyword().text()).isEqualTo("class");
}
Also used : ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) TypeParameters(org.sonar.plugins.java.api.tree.TypeParameters) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Aggregations

TypeParameters (org.sonar.plugins.java.api.tree.TypeParameters)4 Test (org.junit.Test)2 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)2 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)2 CheckForNull (javax.annotation.CheckForNull)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 ModifierKeywordTree (org.sonar.plugins.java.api.tree.ModifierKeywordTree)1 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)1 TypeParameterTree (org.sonar.plugins.java.api.tree.TypeParameterTree)1 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)1