Search in sources :

Example 6 with TypeParameterTree

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

the class FirstPass method visitClass.

@Override
public void visitClass(ClassTree tree) {
    int flag = 0;
    boolean anonymousClass = tree.simpleName() == null;
    String name = "";
    if (!anonymousClass) {
        name = tree.simpleName().name();
        flag = computeClassFlags(tree);
    }
    JavaSymbol.TypeJavaSymbol symbol = new JavaSymbol.TypeJavaSymbol(flag, name, env.scope.owner);
    symbol.declaration = tree;
    ((ClassTreeImpl) tree).setSymbol(symbol);
    // TODO : register also based on flags ?
    if (!anonymousClass) {
        if (env.scope.owner.kind == JavaSymbol.TYP || env.scope.owner.kind == JavaSymbol.PCK) {
            resolve.registerClass(symbol);
        }
        enterSymbol(tree, symbol);
    }
    symbol.members = new Scope(symbol);
    symbol.completer = completer;
    uncompleted.add(symbol);
    // Define type parameters:
    createNewEnvironment(tree.typeParameters());
    // Save current environment to be able to complete class later
    semanticModel.saveEnv(symbol, env);
    for (TypeParameterTree typeParameterTree : tree.typeParameters()) {
        JavaSymbol.TypeVariableJavaSymbol typeVariableSymbol = new JavaSymbol.TypeVariableJavaSymbol(typeParameterTree.identifier().name(), symbol);
        symbol.addTypeParameter((TypeVariableJavaType) typeVariableSymbol.type);
        enterSymbol(typeParameterTree, typeVariableSymbol);
    }
    symbol.typeParameters = env.scope;
    Resolve.Env classEnv = env.dup();
    classEnv.outer = env;
    classEnv.enclosingClass = symbol;
    classEnv.scope = symbol.members;
    env = classEnv;
    semanticModel.associateEnv(tree, env);
    scan(tree.modifiers());
    // skip type parameters
    scan(tree.superClass());
    scan(tree.superInterfaces());
    scan(tree.members());
    if (tree.is(Tree.Kind.ENUM)) {
        // implicit methods from enum: JLS8 : 8.9.2
        // add 'public static E[] values()'
        JavaSymbol.MethodJavaSymbol valuesMethod = new JavaSymbol.MethodJavaSymbol((symbol.flags & Flags.ACCESS_FLAGS) | Flags.STATIC, "values", symbol);
        ArrayJavaType enumArrayType = new ArrayJavaType(symbol.type, symbols.arrayClass);
        MethodJavaType valuesMethodType = new MethodJavaType(ImmutableList.<JavaType>of(), enumArrayType, ImmutableList.<JavaType>of(), symbol);
        valuesMethod.setMethodType(valuesMethodType);
        valuesMethod.parameters = new Scope(valuesMethod);
        classEnv.scope.enter(valuesMethod);
        // add 'public static E valueOf(String name)'
        JavaSymbol.MethodJavaSymbol valueOfMethod = new JavaSymbol.MethodJavaSymbol((symbol.flags & Flags.ACCESS_FLAGS) | Flags.STATIC, "valueOf", symbol);
        MethodJavaType valueOfMethodType = new MethodJavaType(ImmutableList.<JavaType>of(symbols.stringType), symbol.type, ImmutableList.<JavaType>of(), symbol);
        valueOfMethod.setMethodType(valueOfMethodType);
        valueOfMethod.parameters = new Scope(valueOfMethod);
        valueOfMethod.parameters.enter(new JavaSymbol.VariableJavaSymbol(0, "name", symbols.stringType, valueOfMethod));
        classEnv.scope.enter(valueOfMethod);
    }
    restoreEnvironment(tree);
    restoreEnvironment(tree);
}
Also used : ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree)

Example 7 with TypeParameterTree

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

the class SonarSymbolTableVisitor method visitClass.

@Override
public void visitClass(ClassTree tree) {
    IdentifierTree simpleName = tree.simpleName();
    if (simpleName != null) {
        createSymbol(simpleName, tree.symbol().usages());
    }
    for (TypeParameterTree typeParameterTree : tree.typeParameters()) {
        createSymbol(typeParameterTree.identifier(), typeParameterTree);
    }
    super.visitClass(tree);
}
Also used : TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

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