use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method array_field.
@Test
public void array_field() {
VariableTree field;
ArrayTypeTree arrayTypeTree, childArrayTypeTree;
field = (VariableTree) firstTypeMember("class T { int[] a; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
field = (VariableTree) firstTypeMember("class T { int[][] a; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
assertThat(arrayTypeTree.type()).isInstanceOf(ArrayTypeTree.class);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
field = (VariableTree) firstTypeMember("class T { int @Foo [] a; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
field = (VariableTree) firstTypeMember("class T { int @Foo @bar [] a; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 2);
assertThatChildrenIteratorHasSize(arrayTypeTree, 5);
field = (VariableTree) firstTypeMember("class T { int[] @Foo [] a; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
field = (VariableTree) firstTypeMember("class T { int[] a[]; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
field = (VariableTree) firstTypeMember("class T { int[] a @Foo []; }");
assertThatChildrenIteratorHasSize(field, 4);
assertThat(field.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) field.type();
assertThat(arrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method annotation_constant.
@Test
public void annotation_constant() {
List<Tree> members = firstType("@interface T { int c1 = 1, c2[] = { 2 }; }").members();
assertThat(members).hasSize(2);
VariableTree tree = (VariableTree) members.get(0);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.simpleName().name()).isEqualTo("c1");
assertThat(tree.initializer()).isNotNull();
// 1+5, as empty modifiers are always included
assertThatChildrenIteratorHasSize(tree, 6);
tree = (VariableTree) members.get(1);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.type()).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets((ArrayTypeTree) tree.type());
assertThat(tree.simpleName().name()).isEqualTo("c2");
assertThat(tree.initializer()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 6);
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class AbstractInjectionChecker method isIdentifierDynamicString.
protected boolean isIdentifierDynamicString(Tree methodTree, IdentifierTree arg, @Nullable Symbol currentlyChecking, boolean firstLevel) {
Symbol symbol = arg.symbol();
if (isExcluded(currentlyChecking, symbol)) {
return false;
}
boolean isLocalVar = symbol.owner().isMethodSymbol() && !((JavaSymbol.MethodJavaSymbol) symbol.owner()).getParameters().scopeSymbols().contains(symbol);
if (isLocalVar) {
// symbol is a local variable, check it is not a dynamic string.
// Check declaration
VariableTree declaration = ((Symbol.VariableSymbol) symbol).declaration();
ExpressionTree initializer = declaration.initializer();
if (initializer != null && isDynamicString(methodTree, initializer, currentlyChecking)) {
return true;
}
// check usages by revisiting the enclosing tree.
Collection<IdentifierTree> usages = symbol.usages();
LocalVariableDynamicStringVisitor visitor = new LocalVariableDynamicStringVisitor(symbol, usages, methodTree);
Tree argEnclosingDeclarationTree = semanticModel.getTree(semanticModel.getEnv(symbol));
argEnclosingDeclarationTree.accept(visitor);
return visitor.dynamicString;
}
// arg is not a local variable nor a constant, so it is a parameter or a field.
parameterName = "\"" + arg.name() + "\"";
return symbol.owner().isMethodSymbol() && !firstLevel;
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ChildClassShadowFieldCheck method visitNode.
@Override
public void visitNode(Tree tree) {
TypeTree superClass = ((ClassTree) tree).superClass();
if (superClass != null) {
Symbol.TypeSymbol superclassSymbol = superClass.symbolType().symbol();
((ClassTree) tree).members().stream().filter(m -> m.is(Kind.VARIABLE)).map(VariableTree.class::cast).map(VariableTree::simpleName).forEach(fieldSimpleName -> {
if (!IGNORED_FIELDS.contains(fieldSimpleName.name())) {
checkForIssue(superclassSymbol, fieldSimpleName);
}
});
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ClassFieldCountCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
long fieldCount = classTree.members().stream().filter(member -> member.is(Tree.Kind.VARIABLE) && shouldBeCounted((VariableTree) member)).count();
if (fieldCount > threshold) {
String message = String.format("Refactor this class so it has no more than %d %sfields, rather than the %d it currently has.", threshold, countNonPublicFields ? "" : "public ", fieldCount);
reportIssue(reportOnClassTree(classTree), message);
}
}
Aggregations