Search in sources :

Example 1 with TypeParameterTree

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);
            }
        }
    }
}
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 TypeParameterTree

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);
}
Also used : TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) MethodTreeImpl(org.sonar.java.model.declaration.MethodTreeImpl)

Example 3 with TypeParameterTree

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

Example 4 with TypeParameterTree

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

Example 5 with TypeParameterTree

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;
    }
}
Also used : TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Aggregations

TypeParameterTree (org.sonar.plugins.java.api.tree.TypeParameterTree)7 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)3 Test (org.junit.Test)1 TypeParameterListTreeImpl (org.sonar.java.ast.parser.TypeParameterListTreeImpl)1 AbstractTypedTree (org.sonar.java.model.AbstractTypedTree)1 ClassTreeImpl (org.sonar.java.model.declaration.ClassTreeImpl)1 MethodTreeImpl (org.sonar.java.model.declaration.MethodTreeImpl)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 TypeParameters (org.sonar.plugins.java.api.tree.TypeParameters)1 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1