use of org.sonar.plugins.java.api.tree.TypeParameterTree 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.TypeParameterTree in project sonar-java by SonarSource.
the class FirstPass method visitMethod.
@Override
public void visitMethod(MethodTree tree) {
String name = tree.returnType() == null ? CONSTRUCTOR_NAME : tree.simpleName().name();
JavaSymbol.MethodJavaSymbol symbol = new JavaSymbol.MethodJavaSymbol(computeFlags(tree.modifiers(), tree), name, env.scope.owner);
symbol.declaration = tree;
if (Flags.isFlagged(env.scope.owner.flags, Flags.ENUM) && tree.returnType() == null) {
// enum constructors are private.
symbol.flags |= Flags.PRIVATE;
}
enterSymbol(tree, symbol);
symbol.parameters = new Scope(symbol);
symbol.completer = completer;
uncompleted.add(symbol);
((MethodTreeImpl) tree).setSymbol(symbol);
createNewEnvironment(tree.typeParameters());
for (TypeParameterTree typeParameterTree : tree.typeParameters()) {
JavaSymbol.TypeVariableJavaSymbol typeVariableSymbol = new JavaSymbol.TypeVariableJavaSymbol(typeParameterTree.identifier().name(), symbol);
symbol.addTypeParameter((TypeVariableJavaType) typeVariableSymbol.type);
enterSymbol(typeParameterTree, typeVariableSymbol);
}
// Save current environment to be able to complete method later
semanticModel.saveEnv(symbol, env);
symbol.typeParameters = env.scope;
// Create new environment - this is required, because new scope is created
Resolve.Env methodEnv = env.dup();
methodEnv.scope = symbol.parameters;
methodEnv.outer = env;
env = methodEnv;
scan(tree.modifiers());
// skip type parameters.
scan(tree.returnType());
scan(tree.parameters());
scan(tree.throwsClauses());
scan(tree.defaultValue());
symbol.defaultValue = getDefaultValueFromTree(tree.defaultValue());
scan(tree.block());
restoreEnvironment(tree);
restoreEnvironment(tree);
}
use of org.sonar.plugins.java.api.tree.TypeParameterTree 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);
}
use of org.sonar.plugins.java.api.tree.TypeParameterTree in project sonar-java by SonarSource.
the class SonarSymbolTableVisitor method visitMethod.
@Override
public void visitMethod(MethodTree tree) {
List<IdentifierTree> usages = tree.symbol().usages();
createSymbol(tree.simpleName(), usages);
for (TypeParameterTree typeParameterTree : tree.typeParameters()) {
createSymbol(typeParameterTree.identifier(), typeParameterTree);
}
super.visitMethod(tree);
}
use of org.sonar.plugins.java.api.tree.TypeParameterTree in project sonar-java by SonarSource.
the class SecondPass method completeTypeParameters.
private void completeTypeParameters(TypeParameters typeParameters, Resolve.Env env) {
for (TypeParameterTree typeParameterTree : typeParameters) {
List<JavaType> bounds = Lists.newArrayList();
if (typeParameterTree.bounds().isEmpty()) {
bounds.add(symbols.objectType);
} else {
for (Tree boundTree : typeParameterTree.bounds()) {
bounds.add(resolveType(env, boundTree));
}
}
((TypeVariableJavaType) semanticModel.getSymbol(typeParameterTree).type()).bounds = bounds;
}
}
Aggregations