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

The following examples show how to use com.sun.source.tree.NewArrayTree#getType() . 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 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 2
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 3
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 4
Source File: Move.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private ExpressionTree transformInitializer(ExpressionTree initializer, Tree type, TreeMaker make) {
    if(initializer.getKind() == Tree.Kind.NEW_ARRAY) {
        NewArrayTree nat = (NewArrayTree) initializer;
        if(nat.getType() == null) {
            if(type.getKind() == Tree.Kind.ARRAY_TYPE) {
                ArrayTypeTree arrayTypeTree = (ArrayTypeTree) type;
                type = arrayTypeTree.getType();
            }
            return make.NewArray(type, nat.getDimensions(), nat.getInitializers());
        }
    }
    return initializer;
}
 
Example 5
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 6
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 7
Source File: ConvertToVarHint.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isValidVarType(HintContext ctx) {
    TreePath treePath = ctx.getPath();
    TreePath initTreePath = ctx.getVariables().get("$init");  //NOI18N
    TreePath expressionTreePath = ctx.getVariables().get("$expression"); //NOI18N
    TreePath typeTreePath = ctx.getVariables().get("$type"); //NOI18N
    if (initTreePath != null) {
        Tree.Kind kind = initTreePath.getLeaf().getKind();
        switch (kind) {
            case NEW_CLASS:
                NewClassTree nct = (NewClassTree) (initTreePath.getLeaf());
                //anonymous class type
                if (nct.getClassBody() != null) {
                    return false;
                }
                break;
            case NEW_ARRAY:
                NewArrayTree nat = (NewArrayTree) ((VariableTree) treePath.getLeaf()).getInitializer();
                //array initializer expr type
                if (nat.getType() == null) {
                    return false;
                }
                break;
            case LAMBDA_EXPRESSION:
                return false;
            default:
                break;
        }
        // variable initializer type should be same as variable type.
        TypeMirror initTypeMirror = ctx.getInfo().getTrees().getTypeMirror(initTreePath);
        TypeMirror variableTypeMirror = ctx.getInfo().getTrees().getElement(treePath).asType();
        if ((!Utilities.isValidType(initTypeMirror)) || (!ctx.getInfo().getTypes().isSameType(variableTypeMirror, Utilities.resolveCapturedType(ctx.getInfo(), initTypeMirror)))) {
            return false;
        }
        return true;
    } else if (expressionTreePath != null) {
        ExecutableElement iterator = ExpandEnhancedForLoop.findIterable(ctx.getInfo());
        TypeMirror expTypeMirror = ctx.getInfo().getTrees().getTypeMirror(expressionTreePath);
        TypeMirror typeTypeMirror = ctx.getInfo().getTrees().getTypeMirror(typeTreePath);
        if (expTypeMirror.getKind() == TypeKind.DECLARED) {
            DeclaredType dt = (DeclaredType) expTypeMirror;
            if (dt.getTypeArguments().size() > 0) {
                TypeMirror paramType = dt.getTypeArguments().get(0);
                if ((!Utilities.isValidType(typeTypeMirror)) || (!ctx.getInfo().getTypes().isSameType(typeTypeMirror, paramType))) {
                    return false;
                }
            }
        } else {
            ArrayType arrayTypeExp = (ArrayType) Utilities.resolveCapturedType(ctx.getInfo(), expTypeMirror);
            Type arrayTypeExpType = arrayTypeExp.getComponentType();
            if ((!Utilities.isValidType(typeTypeMirror)) || (!ctx.getInfo().getTypes().isSameType(typeTypeMirror, arrayTypeExpType))) {
                return false;
            }
        }
        return (iterator != null);
    } else {
        return false;
    }
}