Search in sources :

Example 1 with VariableTreeImpl

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

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

the class SecondPass method complete.

private void complete(JavaSymbol.MethodJavaSymbol symbol) {
    MethodTree methodTree = symbol.declaration;
    Resolve.Env env = semanticModel.getEnv(symbol);
    completeTypeParameters(methodTree.typeParameters(), env);
    ImmutableList.Builder<JavaType> thrownTypes = ImmutableList.builder();
    for (TypeTree throwClause : methodTree.throwsClauses()) {
        JavaType thrownType = resolveType(env, throwClause);
        if (thrownType != null) {
            thrownTypes.add(thrownType);
        }
    }
    JavaType returnType = null;
    List<JavaType> argTypes = Lists.newArrayList();
    // no return type for constructor
    if (!CONSTRUCTOR_NAME.equals(symbol.name)) {
        returnType = resolveType(env, methodTree.returnType());
        if (returnType != null) {
            symbol.returnType = returnType.symbol;
        }
    } else if (!symbol.enclosingClass().isStatic()) {
        JavaSymbol owner = symbol.enclosingClass().owner();
        if (!owner.isPackageSymbol()) {
            // JLS8 - 8.8.1 & 8.8.9 : constructors of inner class have an implicit first arg of its directly enclosing class type
            argTypes.add(owner.enclosingClass().type);
        }
    }
    List<VariableTree> parametersTree = methodTree.parameters();
    List<JavaSymbol> scopeSymbols = symbol.parameters.scopeSymbols();
    for (int i = 0; i < parametersTree.size(); i += 1) {
        VariableTree variableTree = parametersTree.get(i);
        JavaSymbol param = scopeSymbols.get(i);
        if (variableTree.simpleName().name().equals(param.getName())) {
            param.complete();
            argTypes.add(param.getType());
        }
        if (((VariableTreeImpl) variableTree).isVararg()) {
            symbol.flags |= Flags.VARARGS;
        }
    }
    MethodJavaType methodType = new MethodJavaType(argTypes, returnType, thrownTypes.build(), (JavaSymbol.TypeJavaSymbol) symbol.owner);
    symbol.setMethodType(methodType);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ImmutableList(com.google.common.collect.ImmutableList) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) TypeTree(org.sonar.plugins.java.api.tree.TypeTree)

Example 3 with VariableTreeImpl

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

the class TreeFactory method newVariableDeclaratorId.

public VariableTreeImpl newVariableDeclaratorId(InternalSyntaxToken identifierToken, Optional<List<Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>>>> dims) {
    IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
    ArrayTypeTreeImpl nestedDimensions = newArrayTypeTreeWithAnnotations(dims);
    return new VariableTreeImpl(identifier, nestedDimensions);
}
Also used : IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ArrayTypeTreeImpl(org.sonar.java.model.JavaTree.ArrayTypeTreeImpl) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl)

Example 4 with VariableTreeImpl

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

the class TreeFactory method completeVariableDeclarator.

public VariableTreeImpl completeVariableDeclarator(InternalSyntaxToken identifierToken, Optional<List<Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>>>> dimensions, Optional<VariableTreeImpl> partial) {
    IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
    ArrayTypeTreeImpl nestedDimensions = newArrayTypeTreeWithAnnotations(dimensions);
    if (partial.isPresent()) {
        return partial.get().completeIdentifierAndDims(identifier, nestedDimensions);
    } else {
        return new VariableTreeImpl(identifier, nestedDimensions);
    }
}
Also used : IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ArrayTypeTreeImpl(org.sonar.java.model.JavaTree.ArrayTypeTreeImpl) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl)

Example 5 with VariableTreeImpl

use of org.sonar.java.model.declaration.VariableTreeImpl 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)

Aggregations

VariableTreeImpl (org.sonar.java.model.declaration.VariableTreeImpl)11 ImmutableList (com.google.common.collect.ImmutableList)4 ClassTreeImpl (org.sonar.java.model.declaration.ClassTreeImpl)3 Test (org.junit.Test)2 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)2 ArrayTypeTreeImpl (org.sonar.java.model.JavaTree.ArrayTypeTreeImpl)2 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)2 AnnotationInstance (org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance)2 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)2 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)2 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 Optional (com.sonar.sslr.api.typed.Optional)1 JavaTree (org.sonar.java.model.JavaTree)1 MethodTreeImpl (org.sonar.java.model.declaration.MethodTreeImpl)1 ModifiersTreeImpl (org.sonar.java.model.declaration.ModifiersTreeImpl)1 NewClassTreeImpl (org.sonar.java.model.expression.NewClassTreeImpl)1 Type (org.sonar.plugins.java.api.semantic.Type)1 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)1 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)1