Search in sources :

Example 6 with VariableTreeImpl

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

the class StringToPrimitiveConversionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        if (tree.is(Tree.Kind.VARIABLE)) {
            VariableTreeImpl variableTree = (VariableTreeImpl) tree;
            Type variableType = variableTree.type().symbolType();
            PrimitiveCheck primitiveCheck = getPrimitiveCheck(variableType);
            ExpressionTree initializer = variableTree.initializer();
            if (primitiveCheck != null && initializer != null) {
                primitiveCheck.checkInstantiation(initializer);
            }
        } else {
            MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
            for (PrimitiveCheck primitiveCheck : primitiveChecks) {
                primitiveCheck.checkMethodInvocation(methodInvocationTree);
            }
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl)

Example 7 with VariableTreeImpl

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

the class TypeAndReferenceSolver method visitEnumConstant.

@Override
public void visitEnumConstant(EnumConstantTree tree) {
    scan(tree.modifiers());
    NewClassTree newClassTree = tree.initializer();
    scan(newClassTree.enclosingExpression());
    // register identifier type
    registerType(newClassTree.identifier(), ((VariableTreeImpl) tree).getSymbol().getType());
    scan(newClassTree.typeArguments());
    scan(newClassTree.arguments());
    ClassTree classBody = newClassTree.classBody();
    if (classBody != null) {
        scan(classBody);
        ((ClassJavaType) classBody.symbol().type()).supertype = getType(newClassTree.identifier());
    }
    resolveConstructorSymbol(tree.simpleName(), newClassTree.identifier().symbolType(), semanticModel.getEnv(tree), getParameterTypes(newClassTree.arguments()));
}
Also used : NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl)

Example 8 with VariableTreeImpl

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

the class TypeAndReferenceSolverTest method annotation_on_method_parameter.

@Test
public void annotation_on_method_parameter() {
    CompilationUnitTree compilationUnit = treeOf("@interface MyAnnotation { } class Class { void method(@MyAnnotation int a) { } }");
    ClassTreeImpl annotation = (ClassTreeImpl) compilationUnit.types().get(0);
    ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(1);
    MethodTreeImpl method = (MethodTreeImpl) clazz.members().get(0);
    VariableTreeImpl parameter = (VariableTreeImpl) method.parameters().get(0);
    List<AnnotationInstance> annotations = parameter.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) MethodTreeImpl(org.sonar.java.model.declaration.MethodTreeImpl) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) AnnotationInstance(org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance) Test(org.junit.Test)

Example 9 with VariableTreeImpl

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

the class TreeFactory method newVariableDeclarators.

public VariableDeclaratorListTreeImpl newVariableDeclarators(VariableTreeImpl variable, Optional<List<Tuple<InternalSyntaxToken, VariableTreeImpl>>> rests) {
    ImmutableList.Builder<VariableTreeImpl> variables = ImmutableList.builder();
    variables.add(variable);
    if (rests.isPresent()) {
        VariableTreeImpl previousVariable = variable;
        for (Tuple<InternalSyntaxToken, VariableTreeImpl> rest : rests.get()) {
            VariableTreeImpl newVariable = rest.second();
            InternalSyntaxToken separator = rest.first();
            variables.add(newVariable);
            // store the separator
            previousVariable.setEndToken(separator);
            previousVariable = newVariable;
        }
    }
    return new VariableDeclaratorListTreeImpl(variables.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken)

Example 10 with VariableTreeImpl

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

the class TreeFactory method newInferedParameters.

public LambdaParameterListTreeImpl newInferedParameters(InternalSyntaxToken openParenToken, Optional<Tuple<VariableTreeImpl, Optional<List<Tuple<InternalSyntaxToken, VariableTreeImpl>>>>> identifiersOpt, InternalSyntaxToken closeParenToken) {
    ImmutableList.Builder<VariableTreeImpl> params = ImmutableList.builder();
    if (identifiersOpt.isPresent()) {
        Tuple<VariableTreeImpl, Optional<List<Tuple<InternalSyntaxToken, VariableTreeImpl>>>> identifiers = identifiersOpt.get();
        VariableTreeImpl variable = identifiers.first();
        params.add(variable);
        VariableTreeImpl previousVariable = variable;
        if (identifiers.second().isPresent()) {
            for (Tuple<InternalSyntaxToken, VariableTreeImpl> identifier : identifiers.second().get()) {
                variable = identifier.second();
                params.add(variable);
                InternalSyntaxToken comma = identifier.first();
                previousVariable.setEndToken(comma);
                previousVariable = variable;
            }
        }
    }
    return new LambdaParameterListTreeImpl(openParenToken, params.build(), closeParenToken);
}
Also used : Optional(com.sonar.sslr.api.typed.Optional) ImmutableList(com.google.common.collect.ImmutableList) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken)

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