use of org.sonar.plugins.java.api.tree.NewArrayTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method annotations.
@Test
public void annotations() {
ClassTree tree = firstType("@SuppressWarnings(\"unchecked\") class T { }");
List<AnnotationTree> annotations = tree.modifiers().annotations();
assertThat(annotations).hasSize(1);
AnnotationTree annotation = annotations.get(0);
assertThat(annotation.annotationType().is(Tree.Kind.IDENTIFIER)).isTrue();
assertThat(annotation.arguments().openParenToken()).isNotNull();
assertThat(annotation.arguments().separators()).isEmpty();
assertThat(annotation.arguments()).hasSize(1);
assertThat(annotation.arguments().get(0).is(Tree.Kind.STRING_LITERAL)).isTrue();
assertThat(annotation.arguments().closeParenToken()).isNotNull();
assertThat(annotation.atToken()).isNotNull();
assertThatChildrenIteratorHasSize(annotation, 3);
tree = firstType("@Target( ) class U {}");
annotations = tree.modifiers().annotations();
assertThat(annotations).hasSize(1);
annotation = annotations.get(0);
assertThat(annotation.arguments().openParenToken()).isNotNull();
assertThat(annotation.arguments()).isEmpty();
assertThat(annotation.arguments().separators()).isEmpty();
assertThat(annotation.arguments().closeParenToken()).isNotNull();
assertThat(annotation.atToken()).isNotNull();
assertThatChildrenIteratorHasSize(annotation, 3);
tree = firstType("@Target({ElementType.METHOD}) class U {}");
annotations = tree.modifiers().annotations();
assertThat(annotations).hasSize(1);
annotation = annotations.get(0);
assertThat(annotation.arguments().openParenToken()).isNotNull();
assertThat(annotation.arguments()).hasSize(1);
assertThat(annotation.arguments().get(0).is(Tree.Kind.NEW_ARRAY)).isTrue();
assertThat(annotation.arguments().separators()).isEmpty();
assertThat(annotation.arguments().closeParenToken()).isNotNull();
assertThat(annotation.atToken()).isNotNull();
assertThatChildrenIteratorHasSize(annotation, 3);
tree = firstType("@SuppressWarnings({\"hello\",}) class U {}");
annotations = tree.modifiers().annotations();
assertThat(annotations).hasSize(1);
annotation = annotations.get(0);
assertThat(annotation.arguments().openParenToken()).isNotNull();
assertThat(annotation.arguments()).hasSize(1);
assertThat(annotation.arguments().get(0).is(Tree.Kind.NEW_ARRAY)).isTrue();
NewArrayTree arg = (NewArrayTree) annotation.arguments().get(0);
assertThat(arg.initializers()).hasSize(1);
assertThat(arg.initializers().get(0).is(Tree.Kind.STRING_LITERAL)).isTrue();
assertThat(arg.initializers().separators()).hasSize(1);
assertThat(annotation.arguments().closeParenToken()).isNotNull();
assertThat(annotation.atToken()).isNotNull();
assertThatChildrenIteratorHasSize(annotation, 3);
tree = firstType("@Target(value={ElementType.METHOD}, value2=\"toto\") class T { }");
annotations = tree.modifiers().annotations();
assertThat(annotations).hasSize(1);
annotation = annotations.get(0);
assertThat(annotation.annotationType().is(Tree.Kind.IDENTIFIER)).isTrue();
assertThat(annotation.arguments().openParenToken()).isNotNull();
assertThat(annotation.arguments()).hasSize(2);
assertThat(annotation.arguments().separators()).hasSize(1);
assertThat(annotation.arguments().get(0).is(Tree.Kind.ASSIGNMENT)).isTrue();
assertThat(annotation.arguments().closeParenToken()).isNotNull();
assertThat(annotation.atToken()).isNotNull();
assertThatChildrenIteratorHasSize(annotation, 3);
VariableTree variable = (VariableTree) firstMethodFirstStatement("class T { private void meth() { @NonNullable String str;}}");
assertThatChildrenIteratorHasSize(variable, 4);
annotations = variable.modifiers().annotations();
assertThat(annotations).hasSize(1);
annotation = annotations.get(0);
assertThat(annotation.annotationType().is(Tree.Kind.IDENTIFIER)).isTrue();
assertThat(annotation.atToken()).isNotNull();
assertThat(annotation.arguments()).isEmpty();
assertThatChildrenIteratorHasSize(annotation, 3);
annotations = compilationUnit("@PackageLevelAnnotation package blammy;").packageDeclaration().annotations();
assertThat(annotations).hasSize(1);
assertThat(annotations.get(0).atToken()).isNotNull();
assertThat(annotation.arguments()).isEmpty();
assertThatChildrenIteratorHasSize(annotation, 3);
variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { @Foo Integer foo; } }");
assertThat(variable.modifiers().annotations()).hasSize(1);
assertThat(variable.type().is(Tree.Kind.IDENTIFIER)).isTrue();
assertThat(variable.type().annotations()).isEmpty();
assertThatChildrenIteratorHasSize(variable, 4);
variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { @Foo java.lang.Integer foo; } }");
assertThat(variable.modifiers().annotations()).hasSize(1);
assertThat(variable.type().is(Tree.Kind.MEMBER_SELECT)).isTrue();
assertThat(variable.type().annotations()).isEmpty();
assertThatChildrenIteratorHasSize(variable, 4);
variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { java.lang.@Foo Integer foo; } }");
assertThat(variable.modifiers()).isEmpty();
assertThat(variable.type().is(Tree.Kind.MEMBER_SELECT)).isTrue();
assertThat(variable.type().annotations()).isEmpty();
assertThatChildrenIteratorHasSize(variable, 4);
assertThat(((MemberSelectExpressionTree) variable.type()).identifier().annotations()).hasSize(1);
variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { a.B.C foo = a.B.new @Foo C(); } }");
assertThat(variable.modifiers()).isEmpty();
assertThatChildrenIteratorHasSize(variable, 6);
TypeTree type = ((NewClassTree) variable.initializer()).identifier();
assertThat(type.is(Tree.Kind.IDENTIFIER)).isTrue();
assertThat(type.annotations()).hasSize(1);
assertThatChildrenIteratorHasSize(type, 2);
variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { a.b.C<Integer> foo = a.B.new @Foo C<Integer>(); } }");
assertThat(variable.modifiers()).isEmpty();
assertThatChildrenIteratorHasSize(variable, 6);
type = ((NewClassTree) variable.initializer()).identifier();
assertThat(type.is(Tree.Kind.PARAMETERIZED_TYPE)).isTrue();
assertThat(type.annotations()).hasSize(1);
assertThatChildrenIteratorHasSize(type, 3);
variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { a.B.C foo = new @Foo a.B.C(); } }");
assertThat(variable.modifiers()).isEmpty();
assertThatChildrenIteratorHasSize(variable, 6);
type = ((NewClassTree) variable.initializer()).identifier();
assertThat(type.is(Tree.Kind.MEMBER_SELECT)).isTrue();
assertThat(type.annotations()).hasSize(1);
assertThatChildrenIteratorHasSize(type, 4);
variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { a.b.C<Integer> foo = new @Foo a.b.C<Integer>(); } }");
assertThat(variable.modifiers()).isEmpty();
assertThatChildrenIteratorHasSize(variable, 6);
type = ((NewClassTree) variable.initializer()).identifier();
assertThat(type.is(Tree.Kind.PARAMETERIZED_TYPE)).isTrue();
assertThat(type.annotations()).hasSize(1);
assertThatChildrenIteratorHasSize(type, 3);
variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { int[] foo = new @Foo int[42]; } }");
assertThat(variable.modifiers()).isEmpty();
assertThatChildrenIteratorHasSize(variable, 6);
type = ((NewArrayTree) variable.initializer()).type();
assertThat(type.is(Tree.Kind.PRIMITIVE_TYPE)).isTrue();
assertThat(type.annotations()).hasSize(1);
assertThatChildrenIteratorHasSize(type, 2);
variable = ((TryStatementTree) firstMethodFirstStatement("class T { private void m() { try{ } catch (@Foo E1 | E2 e) {}; } }")).catches().get(0).parameter();
assertThat(variable.modifiers()).hasSize(1);
assertThatChildrenIteratorHasSize(variable, 3);
type = variable.type();
assertThat(type.is(Tree.Kind.UNION_TYPE)).isTrue();
assertThat(type.annotations()).isEmpty();
assertThat(((UnionTypeTree) type).typeAlternatives().separators()).hasSize(1);
assertThatChildrenIteratorHasSize(type, 1);
ClassTree classTree = firstType("class T extends @Foo a.b.C {}");
assertThat(classTree.modifiers()).isEmpty();
assertThatChildrenIteratorHasSize(classTree, 9);
type = classTree.superClass();
assertThat(type.is(Tree.Kind.MEMBER_SELECT)).isTrue();
assertThat(type.annotations()).hasSize(1);
assertThatChildrenIteratorHasSize(type, 4);
classTree = firstType("class T extends @Foo a.b.C<Integer> {}");
assertThat(classTree.modifiers()).isEmpty();
assertThatChildrenIteratorHasSize(classTree, 9);
type = classTree.superClass();
assertThat(type.is(Tree.Kind.PARAMETERIZED_TYPE)).isTrue();
assertThat(type.annotations()).hasSize(1);
assertThatChildrenIteratorHasSize(type, 3);
classTree = (ClassTree) firstMethodFirstStatement("class MyClass<A, B, C> { void foo() { class MyOtherClass extends @Foo MyClass<A, B, C>.MyInnerClass {} } public class MyInnerClass {}}");
assertThat(classTree.modifiers()).isEmpty();
assertThatChildrenIteratorHasSize(classTree, 9);
type = classTree.superClass();
assertThat(type.is(Tree.Kind.MEMBER_SELECT)).isTrue();
assertThat(type.annotations()).hasSize(1);
type = (ParameterizedTypeTree) ((MemberSelectExpressionTree) type).expression();
assertThat(type.is(Tree.Kind.PARAMETERIZED_TYPE)).isTrue();
assertThat(type.annotations()).hasSize(0);
assertThatChildrenIteratorHasSize(type, 2);
TypeCastTree typeCast = (TypeCastTree) ((ReturnStatementTree) firstMethodFirstStatement("class T { private long m(int a) { return (@Foo long) a; } }")).expression();
assertThat(typeCast.type()).isNotNull();
assertThatChildrenIteratorHasSize(typeCast, 5);
type = typeCast.type();
assertThat(type.is(Tree.Kind.PRIMITIVE_TYPE)).isTrue();
assertThat(type.annotations()).hasSize(1);
assertThatChildrenIteratorHasSize(type, 2);
}
use of org.sonar.plugins.java.api.tree.NewArrayTree in project sonar-java by SonarSource.
the class ArrayForVarArgCheck method checkInvokedMethod.
private void checkInvokedMethod(JavaSymbol.MethodJavaSymbol methodSymbol, @Nullable MethodJavaType methodType, ExpressionTree lastArg) {
if (methodSymbol.isVarArgs() && lastArg.is(Tree.Kind.NEW_ARRAY)) {
if (lastParamHasSameType(methodSymbol, methodType, lastArg.symbolType())) {
String message = "Remove this array creation";
NewArrayTree newArrayTree = (NewArrayTree) lastArg;
if (newArrayTree.openBraceToken() == null) {
ExpressionTree expression = newArrayTree.dimensions().get(0).expression();
Integer literalValue = LiteralUtils.intLiteralValue(expression);
if (literalValue == null || literalValue != 0 || isCallingOverload(methodSymbol, lastArg)) {
return;
}
} else if (!newArrayTree.initializers().isEmpty()) {
message += " and simply pass the elements";
}
reportIssue(lastArg, message + ".");
} else {
String type = ((Type.ArrayType) getLastParameterType(methodSymbol.parameterTypes())).elementType().name();
reportIssue(lastArg, "Disambiguate this call by either casting as \"" + type + "\" or \"" + type + "[]\".");
}
}
}
use of org.sonar.plugins.java.api.tree.NewArrayTree in project sonar-java by SonarSource.
the class MagicNumberCheck method visitVariable.
@Override
public void visitVariable(VariableTree tree) {
ExpressionTree initializer = tree.initializer();
boolean arrayNotInitialized = initializer != null && initializer.is(Kind.NEW_ARRAY) && ((NewArrayTree) initializer).initializers().isEmpty();
boolean isFinalOrNoSemantic = context.getSemanticModel() == null || tree.symbol().isFinal();
if (arrayNotInitialized || !isFinalOrNoSemantic) {
super.visitVariable(tree);
}
}
use of org.sonar.plugins.java.api.tree.NewArrayTree in project sonar-java by SonarSource.
the class OSCommandInjectionCheck method isDynamicArray.
private boolean isDynamicArray(ExpressionTree argument, Tree mit) {
ExpressionTree arg = getDeclaration(argument);
if (arg.is(Tree.Kind.NEW_ARRAY)) {
NewArrayTree nat = (NewArrayTree) arg;
for (ExpressionTree expressionTree : nat.initializers()) {
if (isDynamicString(mit, expressionTree, null)) {
return true;
}
}
return false;
}
setParameterNameFromArgument(arg);
boolean argIsString = arg.symbolType().is("java.lang.String");
return !argIsString || isDynamicString(mit, arg, null);
}
use of org.sonar.plugins.java.api.tree.NewArrayTree 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);
}
Aggregations