Search in sources :

Example 6 with NewArrayTree

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;
}
Also used : MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Aggregations

NewArrayTree (org.sonar.plugins.java.api.tree.NewArrayTree)6 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)4 Test (org.junit.Test)2 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 MethodJavaType (org.sonar.java.resolve.MethodJavaType)1 Type (org.sonar.plugins.java.api.semantic.Type)1 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)1 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)1 ArrayTypeTree (org.sonar.plugins.java.api.tree.ArrayTypeTree)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1 MethodReferenceTree (org.sonar.plugins.java.api.tree.MethodReferenceTree)1 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)1 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)1 PrimitiveTypeTree (org.sonar.plugins.java.api.tree.PrimitiveTypeTree)1 TypeCastTree (org.sonar.plugins.java.api.tree.TypeCastTree)1 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)1 UnionTypeTree (org.sonar.plugins.java.api.tree.UnionTypeTree)1