Search in sources :

Example 1 with ClassTreeImpl

use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.

the class TypeAndReferenceSolverTest method annotation_on_variable.

@Test
public void annotation_on_variable() {
    CompilationUnitTree compilationUnit = treeOf("@interface MyAnnotation { } class Class { @MyAnnotation Object field; }");
    ClassTreeImpl annotation = (ClassTreeImpl) compilationUnit.types().get(0);
    ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(1);
    VariableTreeImpl variable = (VariableTreeImpl) clazz.members().get(0);
    List<AnnotationInstance> annotations = variable.getSymbol().metadata().annotations();
    assertThat(annotations.size()).isEqualTo(1);
    assertThat(annotations.get(0).symbol().type().is(annotation.symbol().name())).isTrue();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) AnnotationInstance(org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance) Test(org.junit.Test)

Example 2 with ClassTreeImpl

use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.

the class TypeAndReferenceSolverTest method identifier_of_variable_symbol.

@Test
public void identifier_of_variable_symbol() {
    CompilationUnitTree compilationUnit = treeOf("class A { Object field; }");
    ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(0);
    VariableTree variable = (VariableTree) clazz.members().get(0);
    assertThat(variable.symbol().isUnknown()).isFalse();
    assertThat(variable.symbol().usages()).isEmpty();
    assertThat(variable.simpleName().symbol().isUnknown()).isFalse();
    assertThat(variable.simpleName().symbol()).isEqualTo(variable.symbol());
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Test(org.junit.Test)

Example 3 with ClassTreeImpl

use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.

the class TreeFactory method newAnnotationType.

public ClassTreeImpl newAnnotationType(InternalSyntaxToken openBraceToken, Optional<List<JavaTree>> annotationTypeElementDeclarations, InternalSyntaxToken closeBraceToken) {
    // TODO
    ModifiersTreeImpl emptyModifiers = ModifiersTreeImpl.emptyModifiers();
    ImmutableList.Builder<Tree> members = ImmutableList.builder();
    if (annotationTypeElementDeclarations.isPresent()) {
        for (JavaTree annotationTypeElementDeclaration : annotationTypeElementDeclarations.get()) {
            if (annotationTypeElementDeclaration.getGrammarRuleKey().equals(JavaLexer.VARIABLE_DECLARATORS)) {
                for (VariableTreeImpl variable : (VariableDeclaratorListTreeImpl) annotationTypeElementDeclaration) {
                    members.add(variable);
                }
            } else if (!annotationTypeElementDeclaration.is(Kind.TOKEN)) {
                members.add(annotationTypeElementDeclaration);
            }
        }
    }
    return new ClassTreeImpl(emptyModifiers, openBraceToken, members.build(), closeBraceToken);
}
Also used : ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) NewClassTreeImpl(org.sonar.java.model.expression.NewClassTreeImpl) ImmutableList(com.google.common.collect.ImmutableList) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) ModuleNameTree(org.sonar.plugins.java.api.tree.ModuleNameTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ListTree(org.sonar.plugins.java.api.tree.ListTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) JavaTree(org.sonar.java.model.JavaTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ModifierTree(org.sonar.plugins.java.api.tree.ModifierTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) PackageDeclarationTree(org.sonar.plugins.java.api.tree.PackageDeclarationTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) ModuleDirectiveTree(org.sonar.plugins.java.api.tree.ModuleDirectiveTree) ModuleDeclarationTree(org.sonar.plugins.java.api.tree.ModuleDeclarationTree) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) ModifiersTreeImpl(org.sonar.java.model.declaration.ModifiersTreeImpl) JavaTree(org.sonar.java.model.JavaTree)

Example 4 with ClassTreeImpl

use of org.sonar.java.model.declaration.ClassTreeImpl 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 5 with ClassTreeImpl

use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.

the class TypeAndReferenceSolverTest method assertUnary.

private void assertUnary(String input, Type.Primitives expectedPrimitive) {
    CompilationUnitTree compilationUnit = treeOf(input);
    ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(0);
    Type type = ((ReturnStatementTree) ((MethodTree) clazz.members().get(1)).block().body().get(0)).expression().symbolType();
    assertThat(type.isPrimitive(expectedPrimitive)).isTrue();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Aggregations

ClassTreeImpl (org.sonar.java.model.declaration.ClassTreeImpl)12 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)8 Test (org.junit.Test)6 AnnotationInstance (org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance)4 VariableTreeImpl (org.sonar.java.model.declaration.VariableTreeImpl)3 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)3 ImmutableList (com.google.common.collect.ImmutableList)2 JavaTree (org.sonar.java.model.JavaTree)2 MethodTreeImpl (org.sonar.java.model.declaration.MethodTreeImpl)2 NewClassTreeImpl (org.sonar.java.model.expression.NewClassTreeImpl)2 Type (org.sonar.plugins.java.api.semantic.Type)2 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)2 TypeParameterTree (org.sonar.plugins.java.api.tree.TypeParameterTree)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)1 EnumConstantTreeImpl (org.sonar.java.model.declaration.EnumConstantTreeImpl)1 ModifiersTreeImpl (org.sonar.java.model.declaration.ModifiersTreeImpl)1 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)1