use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class StringLiteralDuplicatedCheck method scanFile.
@Override
public void scanFile(JavaFileScannerContext context) {
occurrences.clear();
constants.clear();
scan(context.getTree());
for (String entry : occurrences.keySet()) {
Collection<LiteralTree> literalTrees = occurrences.get(entry);
int literalOccurrence = literalTrees.size();
if (constants.containsKey(entry)) {
VariableTree constant = constants.get(entry);
List<LiteralTree> duplications = literalTrees.stream().filter(literal -> literal.parent() != constant).collect(Collectors.toList());
context.reportIssue(this, duplications.iterator().next(), "Use already-defined constant '" + constant.simpleName() + "' instead of duplicating its value here.", secondaryLocations(duplications.subList(1, duplications.size())), literalOccurrence);
} else if (literalOccurrence >= threshold) {
context.reportIssue(this, literalTrees.iterator().next(), "Define a constant instead of duplicating this literal " + entry + " " + literalOccurrence + " times.", secondaryLocations(literalTrees), literalOccurrence);
}
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method type_arguments.
/**
* 4.5.1. Type Arguments and Wildcards
*/
@Test
public void type_arguments() {
VariableTree variableTree = (VariableTree) firstMethodFirstStatement("public class T { void m() { ClassType<? extends A, ? super B, ?, C> var; } }");
assertThatChildrenIteratorHasSize(variableTree, 4);
ParameterizedTypeTree parameterizedTypeTree = (ParameterizedTypeTree) variableTree.type();
assertThatChildrenIteratorHasSize(parameterizedTypeTree, 2);
TypeArguments typeArguments = parameterizedTypeTree.typeArguments();
assertThat(typeArguments).hasSize(4);
assertThat(typeArguments.separators()).hasSize(3);
assertThatChildrenIteratorHasSize(typeArguments, 9);
WildcardTree wildcard = (WildcardTree) typeArguments.get(0);
assertThat(wildcard.is(Tree.Kind.EXTENDS_WILDCARD)).isTrue();
assertThat(wildcard.bound()).isInstanceOf(IdentifierTree.class);
assertThat(wildcard.queryToken()).isNotNull();
assertThat(wildcard.queryToken().text()).isEqualTo("?");
assertThat(wildcard.extendsOrSuperToken()).isNotNull();
assertThat(wildcard.extendsOrSuperToken().text()).isEqualTo("extends");
assertThatChildrenIteratorHasSize(wildcard, 3);
wildcard = (WildcardTree) typeArguments.get(1);
assertThat(wildcard.is(Tree.Kind.SUPER_WILDCARD)).isTrue();
assertThat(wildcard.bound()).isInstanceOf(IdentifierTree.class);
assertThat(wildcard.queryToken()).isNotNull();
assertThat(wildcard.queryToken().text()).isEqualTo("?");
assertThat(wildcard.extendsOrSuperToken()).isNotNull();
assertThat(wildcard.extendsOrSuperToken().text()).isEqualTo("super");
assertThatChildrenIteratorHasSize(wildcard, 3);
wildcard = (WildcardTree) typeArguments.get(2);
assertThat(wildcard.is(Tree.Kind.UNBOUNDED_WILDCARD)).isTrue();
assertThat(wildcard.bound()).isNull();
assertThat(wildcard.queryToken().text()).isEqualTo("?");
assertThat(wildcard.queryToken()).isNotNull();
assertThat(wildcard.extendsOrSuperToken()).isNull();
assertThatChildrenIteratorHasSize(wildcard, 1);
assertThat(typeArguments.get(3)).isInstanceOf(IdentifierTree.class);
variableTree = (VariableTree) firstMethodFirstStatement("public class T { void m() { ClassType<@Foo ? extends A> var; } }");
parameterizedTypeTree = (ParameterizedTypeTree) variableTree.type();
assertThatChildrenIteratorHasSize(parameterizedTypeTree, 2);
typeArguments = parameterizedTypeTree.typeArguments();
assertThatChildrenIteratorHasSize(typeArguments, 3);
wildcard = (WildcardTree) typeArguments.get(0);
assertThat(wildcard.is(Tree.Kind.EXTENDS_WILDCARD)).isTrue();
assertThat(wildcard.bound()).isInstanceOf(IdentifierTree.class);
assertThat(wildcard.queryToken().text()).isEqualTo("?");
assertThat(wildcard.annotations()).hasSize(1);
assertThat(wildcard.extendsOrSuperToken().text()).isEqualTo("extends");
assertThatChildrenIteratorHasSize(wildcard, 4);
variableTree = (VariableTree) firstMethodFirstStatement("public class T { void m() { ClassType<? extends @Foo @Bar A> var; } }");
parameterizedTypeTree = (ParameterizedTypeTree) variableTree.type();
assertThatChildrenIteratorHasSize(parameterizedTypeTree, 2);
typeArguments = parameterizedTypeTree.typeArguments();
assertThatChildrenIteratorHasSize(typeArguments, 3);
wildcard = (WildcardTree) typeArguments.get(0);
assertThat(wildcard.is(Tree.Kind.EXTENDS_WILDCARD)).isTrue();
assertThat(wildcard.bound()).isInstanceOf(IdentifierTree.class);
assertThat(wildcard.annotations()).isEmpty();
assertThat(wildcard.queryToken().text()).isEqualTo("?");
assertThat(wildcard.extendsOrSuperToken().text()).isEqualTo("extends");
assertThatChildrenIteratorHasSize(wildcard, 3);
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method array_creation_expression.
/**
* 15.10. Array Creation Expressions
*/
@Test
public void array_creation_expression() {
NewArrayTree tree = (NewArrayTree) expressionOfReturnStatement("class T { int[][] m() { return new int[][]{{1}, {2, 3}}; } }");
assertThat(tree.is(Tree.Kind.NEW_ARRAY)).isTrue();
assertThat(tree.type()).isNotNull();
assertThat(tree.dimensions()).hasSize(2);
assertThat(tree.initializers()).hasSize(2);
assertThat(tree.initializers().separators()).hasSize(1);
assertThat(tree.newKeyword()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 7);
ArrayDimensionTree dimension = tree.dimensions().get(0);
assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
assertThat(dimension.annotations()).isEmpty();
assertThat(dimension.openBracketToken().text()).isEqualTo("[");
assertThat(dimension.expression()).isNull();
assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
assertThatChildrenIteratorHasSize(dimension, 2);
dimension = tree.dimensions().get(1);
assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
assertThat(dimension.annotations()).isEmpty();
assertThat(dimension.openBracketToken().text()).isEqualTo("[");
assertThat(dimension.expression()).isNull();
assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
assertThatChildrenIteratorHasSize(dimension, 2);
NewArrayTree firstDim = (NewArrayTree) tree.initializers().get(0);
assertThat(firstDim.initializers()).hasSize(1);
assertThat(firstDim.initializers().separators()).isEmpty();
assertThatChildrenIteratorHasSize(firstDim, 3);
NewArrayTree secondDim = (NewArrayTree) tree.initializers().get(1);
assertThat(secondDim.initializers()).hasSize(2);
assertThat(secondDim.initializers().separators()).hasSize(1);
assertThatChildrenIteratorHasSize(secondDim, 3);
tree = (NewArrayTree) expressionOfReturnStatement("class T { int[][] m() { return new int[2][2]; } }");
assertThat(tree.is(Tree.Kind.NEW_ARRAY)).isTrue();
assertThat(tree.type()).isNotNull();
assertThat(tree.dimensions()).hasSize(2);
assertThat(tree.initializers()).isEmpty();
assertThat(tree.newKeyword()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 5);
dimension = tree.dimensions().get(0);
assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
assertThat(dimension.annotations()).isEmpty();
assertThat(dimension.openBracketToken().text()).isEqualTo("[");
assertThat(dimension.expression().is(Tree.Kind.INT_LITERAL)).isTrue();
assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
assertThatChildrenIteratorHasSize(dimension, 3);
tree = (NewArrayTree) expressionOfReturnStatement("class T { int[][] m() { return new int[] @Bar [] {{}, {}}; } }");
assertThat(tree.is(Tree.Kind.NEW_ARRAY)).isTrue();
assertThat(tree.type()).isNotNull();
assertThat(tree.dimensions()).hasSize(2);
assertThat(tree.initializers()).hasSize(2);
assertThat(tree.newKeyword()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 7);
dimension = tree.dimensions().get(0);
assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
assertThat(dimension.annotations()).isEmpty();
assertThat(dimension.openBracketToken().text()).isEqualTo("[");
assertThat(dimension.expression()).isNull();
assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
assertThatChildrenIteratorHasSize(dimension, 2);
dimension = tree.dimensions().get(1);
assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
assertThat(dimension.annotations()).hasSize(1);
assertThat(dimension.openBracketToken().text()).isEqualTo("[");
assertThat(dimension.expression()).isNull();
assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
assertThatChildrenIteratorHasSize(dimension, 3);
tree = (NewArrayTree) expressionOfReturnStatement("class T { int[][] m() { return new int[2] @Bar []; } }");
assertThat(tree.is(Tree.Kind.NEW_ARRAY)).isTrue();
assertThat(tree.type()).isNotNull();
assertThat(tree.dimensions()).hasSize(2);
assertThat(tree.initializers()).isEmpty();
assertThat(tree.newKeyword()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 5);
dimension = tree.dimensions().get(0);
assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
assertThat(dimension.annotations()).isEmpty();
assertThat(dimension.openBracketToken().text()).isEqualTo("[");
assertThat(dimension.expression().is(Tree.Kind.INT_LITERAL)).isTrue();
assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
assertThatChildrenIteratorHasSize(dimension, 3);
dimension = tree.dimensions().get(1);
assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
assertThat(dimension.annotations()).hasSize(1);
assertThat(dimension.openBracketToken().text()).isEqualTo("[");
assertThat(dimension.expression()).isNull();
assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
assertThatChildrenIteratorHasSize(dimension, 3);
tree = (NewArrayTree) expressionOfReturnStatement("class T { int[] m() { return new int @Foo [2]; } }");
assertThat(tree.is(Tree.Kind.NEW_ARRAY)).isTrue();
assertThat(tree.type()).isNotNull();
assertThat(tree.dimensions()).hasSize(1);
assertThat(tree.initializers()).isEmpty();
assertThat(tree.newKeyword()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 4);
dimension = tree.dimensions().get(0);
assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
assertThat(dimension.annotations()).hasSize(1);
assertThat(dimension.openBracketToken().text()).isEqualTo("[");
assertThat(dimension.expression().is(Tree.Kind.INT_LITERAL)).isTrue();
assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
assertThatChildrenIteratorHasSize(dimension, 4);
tree = (NewArrayTree) ((VariableTree) firstMethodFirstStatement("class T { void m() { int[] a = {,}; } }")).initializer();
assertThat(tree.is(Tree.Kind.NEW_ARRAY)).isTrue();
assertThat(tree.type()).isNull();
assertThat(tree.dimensions()).isEmpty();
assertThat(tree.initializers()).isEmpty();
assertThat(tree.initializers().separators()).hasSize(1);
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method enum_field.
@Test
public void enum_field() {
List<Tree> declarations = firstType("enum T { ; public int f1 = 42, f2[]; }").members();
assertThat(declarations).hasSize(3);
assertThat(declarations.get(0).is(Tree.Kind.EMPTY_STATEMENT)).isTrue();
VariableTree tree = (VariableTree) declarations.get(1);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.simpleName().name()).isEqualTo("f1");
assertThat(tree.initializer()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 6);
tree = (VariableTree) declarations.get(2);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThatArrayTypeHasBrackets((ArrayTypeTree) tree.type());
assertThat(tree.simpleName().name()).isEqualTo("f2");
assertThat(tree.initializer()).isNull();
assertThatChildrenIteratorHasSize(tree, 4);
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method class_field.
@Test
public void class_field() {
List<Tree> declarations = firstType("class T { public int f1 = 42, f2[]; }").members();
assertThat(declarations).hasSize(2);
VariableTree tree = (VariableTree) declarations.get(0);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.simpleName().name()).isEqualTo("f1");
assertThat(tree.initializer()).isNotNull();
assertThat(tree.endToken()).isNotNull();
assertThat(tree.endToken().text()).isEqualTo(",");
assertThatChildrenIteratorHasSize(tree, 6);
tree = (VariableTree) declarations.get(1);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.type()).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets((ArrayTypeTree) tree.type());
assertThat(tree.simpleName().name()).isEqualTo("f2");
assertThat(tree.initializer()).isNull();
assertThat(tree.endToken()).isNotNull();
assertThat(tree.endToken().text()).isEqualTo(";");
assertThatChildrenIteratorHasSize(tree, 4);
}
Aggregations