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