use of org.sonar.plugins.java.api.tree.NewArrayTree in project sonar-java by SonarSource.
the class ConstantsShouldBeStaticFinalCheck method hasConstantInitializer.
private static boolean hasConstantInitializer(VariableTree variableTree) {
ExpressionTree init = variableTree.initializer();
if (init != null) {
if (ExpressionUtils.skipParentheses(init).is(Tree.Kind.METHOD_REFERENCE)) {
MethodReferenceTree methodRef = (MethodReferenceTree) ExpressionUtils.skipParentheses(init);
if (isInstanceIdentifier(methodRef.expression())) {
return false;
}
}
boolean arrayWithInitializer = true;
if (init.is(Tree.Kind.NEW_ARRAY)) {
// exclude allocations : new int[6] but allow initialization new int[]{1,2};
NewArrayTree newArrayTree = (NewArrayTree) init;
arrayWithInitializer = newArrayTree.dimensions().isEmpty() || newArrayTree.openBraceToken() != null;
}
return arrayWithInitializer && !containsChildrenOfKind((JavaTree) init, Tree.Kind.METHOD_INVOCATION, Tree.Kind.NEW_CLASS);
}
return false;
}
Aggregations