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