use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class SymbolTableTest method parameterized_method_type.
@Test
public void parameterized_method_type() throws Exception {
Result result = Result.createFor("Generics");
MethodTree method3 = (MethodTree) result.symbol("method3").declaration();
VariableTree variable = (VariableTree) method3.block().body().get(0);
assertThat(variable.initializer().symbolType().symbol().name()).isEqualTo("String");
MethodTree method4 = (MethodTree) result.symbol("method4").declaration();
variable = (VariableTree) method4.block().body().get(0);
Type symbolType = variable.initializer().symbolType();
assertThat(symbolType).isInstanceOf(ParametrizedTypeJavaType.class);
ParametrizedTypeJavaType ptt = (ParametrizedTypeJavaType) symbolType;
assertThat(ptt.typeSubstitution.substitutedTypes().iterator().next().getSymbol().getName()).isEqualTo("String");
assertThat(result.reference(58, 25)).isSameAs(result.symbol("method_of_e"));
}
use of org.sonar.plugins.java.api.tree.VariableTree 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());
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class UnusedLocalVariableCheck method checkVariableUsages.
private void checkVariableUsages() {
for (VariableTree variableTree : variables) {
Symbol symbol = variableTree.symbol();
if (symbol.usages().size() == assignments.get(symbol).size()) {
IdentifierTree simpleName = variableTree.simpleName();
reportIssue(simpleName, "Remove this unused \"" + simpleName + "\" local variable.");
}
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class UnusedMethodParameterCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
if (methodTree.block() == null || isExcluded(methodTree)) {
return;
}
Set<String> documentedParameters = documentedParameters(methodTree);
boolean overridableMethod = overridableMethod(methodTree.symbol());
List<IdentifierTree> unused = Lists.newArrayList();
for (VariableTree var : methodTree.parameters()) {
Symbol symbol = var.symbol();
if (symbol.usages().isEmpty() && !symbol.metadata().isAnnotatedWith(AUTHORIZED_ANNOTATION) && !isStrutsActionParameter(var) && (!overridableMethod || !documentedParameters.contains(symbol.name()))) {
unused.add(var.simpleName());
}
}
Set<String> unresolvedIdentifierNames = unresolvedIdentifierNames(methodTree.block());
// kill the noise regarding unresolved identifiers, and remove the one with matching names from the list of unused
unused = unused.stream().filter(id -> !unresolvedIdentifierNames.contains(id.name())).collect(Collectors.toList());
if (!unused.isEmpty()) {
reportUnusedParameters(unused);
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class BadConstantNameCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree classTree = (ClassTree) tree;
for (Tree member : classTree.members()) {
if (member.is(Tree.Kind.VARIABLE) && hasSemantic()) {
VariableTree variableTree = (VariableTree) member;
Type symbolType = variableTree.type().symbolType();
if (isConstantType(symbolType) && (classTree.is(Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE) || isStaticFinal(variableTree))) {
checkName(variableTree);
}
} else if (member.is(Tree.Kind.ENUM_CONSTANT)) {
checkName((VariableTree) member);
}
}
}
Aggregations