Java Code Examples for com.sun.source.tree.NewArrayTree#getInitializers()

The following examples show how to use com.sun.source.tree.NewArrayTree#getInitializers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
static int rowLength(List<? extends ExpressionTree> row) {
    int size = 0;
    for (ExpressionTree tree : row) {
        if (tree.getKind() != NEW_ARRAY) {
            size++;
            continue;
        }
        NewArrayTree array = (NewArrayTree) tree;
        if (array.getInitializers() == null) {
            size++;
            continue;
        }
        size += rowLength(array.getInitializers());
    }
    return size;
}
 
Example 2
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static List<AnnotationTree> findArrayValue(AnnotationTree at, String name) {
    ExpressionTree fixesArray = findValue(at, name);
    List<AnnotationTree> fixes = new LinkedList<AnnotationTree>();

    if (fixesArray != null && fixesArray.getKind() == Kind.NEW_ARRAY) {
        NewArrayTree trees = (NewArrayTree) fixesArray;

        for (ExpressionTree fix : trees.getInitializers()) {
            if (fix.getKind() == Kind.ANNOTATION) {
                fixes.add((AnnotationTree) fix);
            }
        }
    }

    if (fixesArray != null && fixesArray.getKind() == Kind.ANNOTATION) {
        fixes.add((AnnotationTree) fixesArray);
    }
    
    return fixes;
}
 
Example 3
Source File: ConstantNameHint.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static boolean checkZeroSizeArray(CompilationInfo info, TreePath val) {
    if (val.getLeaf().getKind() != Tree.Kind.VARIABLE) {
        return false;
    }
    VariableTree vt = (VariableTree)val.getLeaf();
    ExpressionTree xpr = vt.getInitializer();
    if (xpr == null) {
        return false;
    }
    if (xpr.getKind() == Tree.Kind.NEW_ARRAY) {
        NewArrayTree nat = (NewArrayTree)xpr;
        List<? extends ExpressionTree> dims = nat.getDimensions();
        if (dims != null && !dims.isEmpty()) {
            Object size = ArithmeticUtilities.compute(info, 
                    new TreePath(
                        new TreePath(val, xpr),
                        dims.get(dims.size() -1)), 
                    true);
            return ArithmeticUtilities.isRealValue(size) && Integer.valueOf(0).equals(size);
        } else {
            return nat.getInitializers() != null && nat.getInitializers().isEmpty();
        }
    }
    return false;
}
 
Example 4
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
static int rowLength(List<? extends ExpressionTree> row) {
    int size = 0;
    for (ExpressionTree tree : row) {
        if (tree.getKind() != NEW_ARRAY) {
            size++;
            continue;
        }
        NewArrayTree array = (NewArrayTree) tree;
        if (array.getInitializers() == null) {
            size++;
            continue;
        }
        size += rowLength(array.getInitializers());
    }
    return size;
}
 
Example 5
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
static int rowLength(List<? extends ExpressionTree> row) {
  int size = 0;
  for (ExpressionTree tree : row) {
    if (tree.getKind() != NEW_ARRAY) {
      size++;
      continue;
    }
    NewArrayTree array = (NewArrayTree) tree;
    if (array.getInitializers() == null) {
      size++;
      continue;
    }
    size += rowLength(array.getInitializers());
  }
  return size;
}
 
Example 6
Source File: TreeConverter.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private TreeNode convertNewArray(NewArrayTree node, TreePath parent) {
  TreePath path = getTreePath(parent, node);
  ArrayCreation newNode = new ArrayCreation();
  List<Expression> dimensions = new ArrayList<>();
  for (ExpressionTree dimension : node.getDimensions()) {
    dimensions.add((Expression) convert(dimension, path));
  }
  javax.lang.model.type.ArrayType type = (javax.lang.model.type.ArrayType) getTypeMirror(path);
  if (node.getInitializers() != null) {
    ArrayInitializer initializers = new ArrayInitializer(type);
    for (ExpressionTree initializer : node.getInitializers()) {
      initializers.addExpression((Expression) convert(initializer, path));
    }
    newNode.setInitializer(initializers);
  }
  return newNode
      .setType((ArrayType) new ArrayType(type).setPosition(getPosition(node)))
      .setDimensions(dimensions);
}
 
Example 7
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Override
    public Void visitNewArray(NewArrayTree node, Void unused) {
        if (node.getType() != null) {
            builder.open(plusFour);
            token("new");
            builder.space();

            TypeWithDims extractedDims = DimensionHelpers.extractDims(node.getType(), SortedDims.YES);
            Tree base = extractedDims.node;

            Deque<ExpressionTree> dimExpressions = new ArrayDeque<>(node.getDimensions());

            Deque<List<AnnotationTree>> annotations = new ArrayDeque<>();
            // TODO: 22-Jul-17 missing method java8
//            annotations.add(ImmutableList.copyOf(node.getAnnotations()));
//            annotations.addAll((List<List<AnnotationTree>>) node.getDimAnnotations());
            annotations.addAll(extractedDims.dims);

            scan(base, null);
            builder.open(ZERO);
            maybeAddDims(dimExpressions, annotations);
            builder.close();
            builder.close();
        }
        if (node.getInitializers() != null) {
            if (node.getType() != null) {
                builder.space();
            }
            visitArrayInitializer(node.getInitializers());
        }
        return null;
    }
 
Example 8
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static List<? extends TypeMirror> computeNewArray(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    NewArrayTree nat = (NewArrayTree) parent.getLeaf();
    
    if (nat.getType() == error) {
        types.add(ElementKind.CLASS);
        types.add(ElementKind.ENUM);
        types.add(ElementKind.INTERFACE);
        
        return null;
    }
    
    for (Tree dimension : nat.getDimensions()) {
        if (dimension == error) {
            types.add(ElementKind.PARAMETER);
            types.add(ElementKind.LOCAL_VARIABLE);
            types.add(ElementKind.FIELD);
            
            return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.INT));
        }
    }
    
    for (Tree init : nat.getInitializers()) {
        if (init == error) {
            TypeMirror whole = info.getTrees().getTypeMirror(parent);
            
            if (whole == null || whole.getKind() != TypeKind.ARRAY)
                return null;
            
            types.add(ElementKind.PARAMETER);
            types.add(ElementKind.LOCAL_VARIABLE);
            types.add(ElementKind.FIELD);
            
            return Collections.singletonList(((ArrayType) whole).getComponentType());
        }
    }
    
    return null;
}
 
Example 9
Source File: Unbalanced.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@TriggerPattern(value="$mods$ $type[] $name = $init$;")
public static ErrorDescription after(HintContext ctx) {
    VariableElement var = testElement(ctx);

    if (var == null) return null;

    Tree parent = ctx.getPath().getParentPath().getLeaf();

    if (parent.getKind() == Kind.ENHANCED_FOR_LOOP
        && ((EnhancedForLoopTree) parent).getVariable() == ctx.getPath().getLeaf()) {
        return null;
    }
    
    TreePath init = ctx.getVariables().get("$init$");

    if (init != null) {
        boolean asWrite = true;
        
        if (init.getLeaf().getKind() == Kind.NEW_ARRAY) {
            NewArrayTree nat = (NewArrayTree) init.getLeaf();

            if (nat.getInitializers() == null || nat.getInitializers().isEmpty()) {
                asWrite = false;
            }
        }
        
        if (asWrite) {
            record(ctx.getInfo(), var, State.WRITE);
        }
    }

    return produceWarning(ctx, "ERR_UnbalancedArray");
}
 
Example 10
Source File: SuspiciousToArray.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
    WorkingCopy wc = ctx.getWorkingCopy();
    TreePath path = ctx.getPath();
    TypeMirror compType = ctype.resolve(wc);
    if (compType == null) {
        return;
    }
    TreeMaker mk = wc.getTreeMaker();
    Tree l = path.getLeaf();
    if (l.getKind() == Tree.Kind.NEW_ARRAY) {
        NewArrayTree nat = (NewArrayTree)l;
        // if there are some initializers, we should probably rewrite the whole expression.
        if (nat.getInitializers() == null) {
            rewriteNewArrayTree(wc, mk, path, compType);
            return;
        }
    }
    // replace the entire tree
    TreePath colRef = null;
    if (colReference != null) {
        colRef = colReference.resolve(wc);
        if (colRef == null) {
            return;
        }
    }
    GeneratorUtilities gu = GeneratorUtilities.get(wc);
    Tree lc = gu.importComments(l, wc.getCompilationUnit());
    Tree newArrayTree = mk.NewArray(mk.Type(compType), Collections.<ExpressionTree>singletonList(
            mk.MethodInvocation(Collections.<ExpressionTree>emptyList(), 
                colRef == null ? mk.Identifier("size") :
                mk.MemberSelect((ExpressionTree)colRef.getLeaf(), "size"), // NOI18N
                Collections.<ExpressionTree>emptyList())),
            null);
    gu.copyComments(lc, newArrayTree, true);
    gu.copyComments(lc, newArrayTree, false);
    wc.rewrite(lc, newArrayTree);
}
 
Example 11
Source File: ReplaceBufferByString.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean visitNewArray(NewArrayTree node, Void p) {
    if (node.getInitializers() != null) {
        for (ExpressionTree et : node.getInitializers()) {
            if (scan(et, p) == Boolean.TRUE) {
                assignedToArray = true;
                break;
            }
        }
    }
    return false;
}
 
Example 12
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static List<? extends TypeMirror> computeNewArray(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    NewArrayTree nat = (NewArrayTree) parent.getLeaf();
    
    if (nat.getType() == error) {
        types.add(ElementKind.CLASS);
        types.add(ElementKind.ENUM);
        types.add(ElementKind.INTERFACE);
        
        return null;
    }
    
    for (Tree dimension : nat.getDimensions()) {
        if (dimension == error) {
            types.add(ElementKind.PARAMETER);
            types.add(ElementKind.LOCAL_VARIABLE);
            types.add(ElementKind.FIELD);
            
            return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.INT));
        }
    }
    
    for (Tree init : nat.getInitializers()) {
        if (init == error) {
            TypeMirror whole = info.getTrees().getTypeMirror(parent);
            
            if (whole == null || whole.getKind() != TypeKind.ARRAY)
                return null;
            
            types.add(ElementKind.PARAMETER);
            types.add(ElementKind.LOCAL_VARIABLE);
            types.add(ElementKind.FIELD);
            
            return Collections.singletonList(((ArrayType) whole).getComponentType());
        }
    }
    
    return null;
}
 
Example 13
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
    public Void visitNewArray(NewArrayTree node, Void unused) {
        if (node.getType() != null) {
            builder.open(plusFour);
            token("new");
            builder.space();

            TypeWithDims extractedDims = DimensionHelpers.extractDims(node.getType(), SortedDims.YES);
            Tree base = extractedDims.node;

            Deque<ExpressionTree> dimExpressions = new ArrayDeque<>(node.getDimensions());

            Deque<List<AnnotationTree>> annotations = new ArrayDeque<>();
            // TODO: 22-Jul-17 missing method java8
//            annotations.add(ImmutableList.copyOf(node.getAnnotations()));
//            annotations.addAll((List<List<AnnotationTree>>) node.getDimAnnotations());
            annotations.addAll(extractedDims.dims);

            scan(base, null);
            builder.open(ZERO);
            maybeAddDims(dimExpressions, annotations);
            builder.close();
            builder.close();
        }
        if (node.getInitializers() != null) {
            if (node.getType() != null) {
                builder.space();
            }
            visitArrayInitializer(node.getInitializers());
        }
        return null;
    }
 
Example 14
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitNewArray(NewArrayTree node, Void unused) {
  if (node.getType() != null) {
    builder.open(plusFour);
    token("new");
    builder.space();

    TypeWithDims extractedDims = DimensionHelpers.extractDims(node.getType(), SortedDims.YES);
    Tree base = extractedDims.node;

    Deque<ExpressionTree> dimExpressions = new ArrayDeque<>(node.getDimensions());

    Deque<List<? extends AnnotationTree>> annotations = new ArrayDeque<>();
    annotations.add(ImmutableList.copyOf(node.getAnnotations()));
    annotations.addAll(node.getDimAnnotations());
    annotations.addAll(extractedDims.dims);

    scan(base, null);
    builder.open(ZERO);
    maybeAddDims(dimExpressions, annotations);
    builder.close();
    builder.close();
  }
  if (node.getInitializers() != null) {
    if (node.getType() != null) {
      builder.space();
    }
    visitArrayInitializer(node.getInitializers());
  }
  return null;
}
 
Example 15
Source File: ASTPathCriterion.java    From annotation-tools with MIT License 4 votes vote down vote up
private boolean checkNull(List<Tree> path, int ix) {
    Tree node = path.get(path.size()-1);
    int last = astPath.size() - 1;
    ASTPath.ASTEntry entry = astPath.get(ix);
    Tree.Kind kind = entry.getTreeKind();

    switch (kind) {
    // case ANNOTATION:
    // case INTERFACE:
    case CLASS:  // "extends" clause?
        return ASTPath.isClassEquiv(kind)
                && ix == last && entry.getArgument() == -1
                && entry.childSelectorIs(ASTPath.BOUND);
    case TYPE_PARAMETER:
        return node.getKind() == Tree.Kind.TYPE_PARAMETER
                && ix == last && entry.getArgument() == 0
                && entry.childSelectorIs(ASTPath.BOUND);
    case METHOD:  // nullary constructor? receiver?
        if (node.getKind() != Tree.Kind.METHOD) { return false; }
        MethodTree method = (MethodTree) node;
        List<? extends VariableTree> params = method.getParameters();
        if ("<init>".equals(method.getName().toString())) {
            if (ix == last) { return true; }
            ASTPath.ASTEntry next = astPath.get(++ix);
            String selector = next.getChildSelector();
            Tree typeTree =
                ASTPath.TYPE_PARAMETER.equals(selector)
                    ? method.getTypeParameters().get(next.getArgument())
              : ASTPath.PARAMETER.equals(selector)
                    ? params.get(next.getArgument()).getType()
              : null;
            return typeTree != null && checkTypePath(ix, typeTree);
        } else if (entry.childSelectorIs(ASTPath.PARAMETER)
                && entry.getArgument() == -1) {
            if (ix == last) { return true; }
            VariableTree rcvrParam = method.getReceiverParameter();
            if (rcvrParam == null) {  // TODO
              // ClassTree clazz = methodReceiverType(path);
              // return checkReceiverType(ix,
              //    ((JCTree.JCClassDecl) clazz).type);
            } else {
              return checkTypePath(ix+1, rcvrParam.getType());
            }
        }
        return false;
    case NEW_ARRAY:
        if (node.getKind() != Tree.Kind.NEW_ARRAY) { return false; }
        NewArrayTree newArray = (NewArrayTree) node;
        int arg = entry.getArgument();
        if (entry.childSelectorIs(ASTPath.TYPE)) {
            if (ix == last) { return true; }
            // Tree t = newArray.getType();
            // int depth = 1;
            // while (t.getKind() == Tree.Kind.ARRAY_TYPE) {
            //    t = ((ArrayTypeTree) t).getType();
            //    ++depth;
            // }
            return arg == arrayDepth(newArray);
        } else {
            List<? extends ExpressionTree> typeTrees =
                    entry.childSelectorIs(ASTPath.DIMENSION)
                            ? newArray.getDimensions()
                  : entry.childSelectorIs(ASTPath.INITIALIZER)
                            ? newArray.getInitializers()
                  : null;
            return typeTrees != null && arg < typeTrees.size()
                  && checkTypePath(ix+1, typeTrees.get(arg));
        }
    case UNBOUNDED_WILDCARD:
        return isBoundableWildcard(path, path.size()-1);
    default:  // TODO: casts?
        return false;
    }
}