List of usage examples for org.eclipse.jdt.core.dom ArrayCreation getInitializer
public ArrayInitializer getInitializer()
null if there is none. From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java
License:Open Source License
@Override public boolean visit(ArrayCreation node) { this.fBuffer.append("new ");//$NON-NLS-1$ ArrayType at = node.getType(); int dims = at.getDimensions(); Type elementType = at.getElementType(); elementType.accept(this); for (Iterator<Expression> it = node.dimensions().iterator(); it.hasNext();) { this.fBuffer.append("[");//$NON-NLS-1$ Expression e = it.next(); e.accept(this); this.fBuffer.append("]");//$NON-NLS-1$ dims--;/* ww w . j ava 2 s. c o m*/ } // add empty "[]" for each extra array dimension for (int i = 0; i < dims; i++) { this.fBuffer.append("[]");//$NON-NLS-1$ } if (node.getInitializer() != null) { node.getInitializer().accept(this); } return false; }
From source file:boa.datagen.util.Java7Visitor.java
License:Apache License
@Override public boolean visit(ArrayCreation node) { boa.types.Ast.Expression.Builder b = boa.types.Ast.Expression.newBuilder(); // b.setPosition(pos.build()); b.setKind(boa.types.Ast.Expression.ExpressionKind.NEWARRAY); boa.types.Ast.Type.Builder tb = boa.types.Ast.Type.newBuilder(); tb.setName(getIndex(typeName(node.getType()))); tb.setKind(boa.types.Ast.TypeKind.OTHER); b.setNewType(tb.build());//from w w w .j a va 2s . co m for (Object e : node.dimensions()) { ((org.eclipse.jdt.core.dom.Expression) e).accept(this); b.addExpressions(expressions.pop()); } if (node.getInitializer() != null) { node.getInitializer().accept(this); // FIXME b.addExpressions(expressions.pop()); } expressions.push(b.build()); return false; }
From source file:coloredide.utils.CopiedNaiveASTFlattener.java
License:Open Source License
public boolean visit(ArrayCreation node) { this.buffer.append("new ");//$NON-NLS-1$ ArrayType at = node.getType(); int dims = at.getDimensions(); Type elementType = at.getElementType(); elementType.accept(this); for (Iterator it = node.dimensions().iterator(); it.hasNext();) { this.buffer.append("[");//$NON-NLS-1$ Expression e = (Expression) it.next(); e.accept(this); this.buffer.append("]");//$NON-NLS-1$ dims--;// w w w .j av a2 s . c o m } // add empty "[]" for each extra array dimension for (int i = 0; i < dims; i++) { this.buffer.append("[]");//$NON-NLS-1$ } if (node.getInitializer() != null) { node.getInitializer().accept(this); } return false; }
From source file:com.google.dart.java2dart.SyntaxTranslator.java
License:Open Source License
@Override public boolean visit(org.eclipse.jdt.core.dom.ArrayCreation node) { TypeName listType = translate(node.getType()); TypeArgumentList typeArgs = listType.getTypeArguments(); if (node.getInitializer() != null) { List<Expression> elements = translateExpressionList(node.getInitializer().expressions()); return done(listLiteral(null, typeArgs, elements)); } else {//ww w .ja v a2s .co m List<Expression> arguments = translateArguments(null, node.dimensions()); // may be primitive array element { String arrayElementTypeName = typeArgs.getArguments().get(0).getName().getName(); Expression initializer = getPrimitiveTypeDefaultValue(arrayElementTypeName); if (initializer != null) { arguments.add(initializer); return done(instanceCreationExpression(Keyword.NEW, listType, "filled", arguments)); } } // non-primitive array element return done(instanceCreationExpression(Keyword.NEW, listType, arguments)); } }
From source file:com.google.devtools.j2cpp.gen.CppStatementGenerator.java
License:Open Source License
@Override public boolean visit(ArrayCreation node) { @SuppressWarnings("unchecked") List<Expression> dimensions = node.dimensions(); // safe by definition ArrayInitializer init = node.getInitializer(); if (init != null) { // Create an expression like [IOSArrayInt arrayWithInts:(int[]){ 1, 2, 3 }]. ArrayType at = node.getType(); ITypeBinding componentType = Types.getTypeBinding(node).getComponentType(); // New array needs to be retained if it's a new assignment, since the // arrayWith* methods return an autoreleased object. boolean shouldRetain = useReferenceCounting && isNewAssignment(node); if (shouldRetain) { buffer.append("[["); } else {//from w w w .j a va 2s . c om buffer.append('['); } String elementType = at.getElementType().toString(); buffer.append(elementType); buffer.append(' '); IOSArrayTypeBinding iosArrayBinding = Types.resolveArrayType(componentType); buffer.append(iosArrayBinding.getInitMethod()); buffer.append(':'); printArrayLiteral(init); buffer.append(" count:"); buffer.append(init.expressions().size()); if (elementType.equals("IOSObjectArray")) { buffer.append(" type:"); printObjectArrayType(componentType); } buffer.append(']'); if (shouldRetain) { buffer.append(" retain]"); } } else if (node.dimensions().size() > 1) { printMultiDimArray(Types.getTypeBinding(node).getElementType(), dimensions); } else { assert dimensions.size() == 1; printSingleDimArray(Types.getTypeBinding(node).getElementType(), dimensions.get(0), useReferenceCounting && !isNewAssignment(node)); } return false; }
From source file:com.google.devtools.j2cpp.translate.Rewriter.java
License:Open Source License
@Override public boolean visit(Block node) { // split array declarations so that initializers are in separate statements. @SuppressWarnings("unchecked") List<Statement> stmts = node.statements(); // safe by definition int n = stmts.size(); for (int i = 0; i < n; i++) { Statement s = stmts.get(i); if (s instanceof VariableDeclarationStatement) { VariableDeclarationStatement var = (VariableDeclarationStatement) s; Map<VariableDeclarationFragment, Expression> initializers = Maps.newLinkedHashMap(); @SuppressWarnings("unchecked") List<VariableDeclarationFragment> fragments = var.fragments(); for (VariableDeclarationFragment fragment : fragments) { ITypeBinding varType = Types.getTypeBinding(fragment); if (varType.isArray()) { fragment.setExtraDimensions(0); Expression initializer = fragment.getInitializer(); if (initializer != null) { initializers.put(fragment, initializer); if (initializer instanceof ArrayCreation) { ArrayCreation creator = (ArrayCreation) initializer; if (creator.getInitializer() != null) { // replace this redundant array creation node with its // rewritten initializer initializer = creator.getInitializer(); } else { continue; }/*from www .ja v a 2 s. c om*/ } if (initializer instanceof ArrayInitializer) { fragment.setInitializer(createIOSArrayInitializer(Types.getTypeBinding(fragment), (ArrayInitializer) initializer)); } } } } } else if (s instanceof ExpressionStatement && ((ExpressionStatement) s).getExpression() instanceof Assignment) { Assignment assign = (Assignment) ((ExpressionStatement) s).getExpression(); ITypeBinding assignType = Types.getTypeBinding(assign); if (assign.getRightHandSide() instanceof ArrayInitializer) { ArrayInitializer arrayInit = (ArrayInitializer) assign.getRightHandSide(); assert assignType.isArray() : "array initializer assigned to non-array"; assign.setRightHandSide(createIOSArrayInitializer(assignType, arrayInit)); } else if (assign.getRightHandSide() instanceof ArrayCreation) { ArrayCreation arrayCreate = (ArrayCreation) assign.getRightHandSide(); ArrayInitializer arrayInit = arrayCreate.getInitializer(); if (arrayInit != null) { // Replace ArrayCreation node with its initializer. AST ast = node.getAST(); Assignment newAssign = ast.newAssignment(); Types.addBinding(newAssign, assignType); newAssign.setLeftHandSide(NodeCopier.copySubtree(ast, assign.getLeftHandSide())); newAssign.setRightHandSide(createIOSArrayInitializer(assignType, arrayInit)); ((ExpressionStatement) s).setExpression(newAssign); } } } } return true; }
From source file:com.google.devtools.j2objc.ast.DebugASTPrinter.java
License:Apache License
@Override public boolean visit(ArrayCreation node) { sb.print("new "); node.getType().accept(this); for (Expression dim : node.getDimensions()) { sb.print('['); dim.accept(this); sb.print(']'); }/*from ww w . j av a 2 s . c om*/ int emptyDims = node.getTypeBinding().getDimensions() - node.getDimensions().size(); for (int i = 0; i < emptyDims; i++) { sb.print("[]"); } if (node.getInitializer() != null) { node.getInitializer().accept(this); } return false; }
From source file:com.google.gdt.eclipse.designer.gwtext.model.widgets.GridPanelInfo.java
License:Open Source License
private void bindColumns(List<JavaInfo> javaInfoList) throws Exception { ArrayCreation columnsArray = getColumnsArray(false); if (columnsArray == null) { return;//from w ww. j av a 2 s . c om } @SuppressWarnings("unchecked") List<Expression> arrayExpressions = columnsArray.getInitializer().expressions(); // check ColumnConfig-s for (JavaInfo javaInfo : javaInfoList) { if (javaInfo instanceof ColumnConfigInfo && javaInfo.getParent() == null) { ColumnConfigInfo column = (ColumnConfigInfo) javaInfo; for (Expression arrayExpression : arrayExpressions) { if (column.isRepresentedBy(arrayExpression)) { addChild(column); column.setAssociation(new ArrayAssociation(columnsArray)); } } } } }
From source file:com.google.gdt.eclipse.designer.gwtext.model.widgets.GridPanelInfo.java
License:Open Source License
/** * Creates new {@link ColumnConfigInfo}. *//*from w ww.j av a 2s . c om*/ public void command_CREATE(ColumnConfigInfo column, ColumnConfigInfo nextColumn) throws Exception { ArrayCreation columnsArray = getColumnsArray(true); Assert.isNotNull(columnsArray); ArrayInitializer arrayInitializer = columnsArray.getInitializer(); // fire before event getBroadcast(ObjectInfoChildAddBefore.class).invoke(this, column, new ObjectInfo[] { nextColumn }); getBroadcastJava().addBefore(this, column); // setup hierarchy int index = nextColumn == null ? arrayInitializer.expressions().size() : getColumns().indexOf(nextColumn); addChild(column, nextColumn); // add source StatementTarget statementTarget = new StatementTarget(AstNodeUtils.getEnclosingStatement(columnsArray), true); String source = column.getCreationSupport().add_getSource(new NodeTarget(statementTarget)); getCreateItemExpression(column, arrayInitializer, index, source); // set association column.setAssociation(new ArrayAssociation(columnsArray)); // fire after event getBroadcastJava().addAfter(this, column); getBroadcast(ObjectInfoChildAddAfter.class).invoke(this, column); }
From source file:com.google.gdt.eclipse.designer.gwtext.model.widgets.GridPanelInfo.java
License:Open Source License
/** * Moves existing {@link ColumnConfigInfo}. */// w ww. jav a2s.c o m public void command_MOVE(ColumnConfigInfo column, ColumnConfigInfo nextColumn) throws Exception { ArrayCreation columnsArray = getColumnsArray(true); Assert.isNotNull(columnsArray); ArrayInitializer arrayInitializer = columnsArray.getInitializer(); JavaInfo oldParent = column.getParent() instanceof JavaInfo ? column.getParentJava() : null; int oldIndex = column.getParent().getChildren(ColumnConfigInfo.class).indexOf(column); int newIndex = getChildren(ColumnConfigInfo.class).indexOf(nextColumn); newIndex = newIndex == -1 ? arrayInitializer.expressions().size() : newIndex; // fire before event getBroadcastObject().childMoveBefore(getParent(), column, nextColumn); getBroadcastJava().moveBefore(column, oldParent, this); // move hierarchy if (column.getParent() == this && column.getAssociation() instanceof ArrayAssociation) { // move inside this grid moveChild(column, nextColumn); // exchange elements getEditor().moveArrayElement(arrayInitializer, arrayInitializer, oldIndex, newIndex); } else { // try optimize source if (column.getVariableSupport() instanceof LocalUniqueVariableSupport) { LocalUniqueVariableSupport localVariableSupport = (LocalUniqueVariableSupport) column .getVariableSupport(); if (localVariableSupport.canInline()) { localVariableSupport.inline(); } } // source String source = null; if (column.getVariableSupport() instanceof EmptyVariableSupport) { source = getEditor() .getSource(((EmptyVariableSupport) column.getVariableSupport()).getInitializer()); } // remove from old place Association association = column.getAssociation(); if (association != null) { if (association.remove()) { column.setAssociation(null); } } column.getParent().removeChild(column); // add to array addChild(column, nextColumn); if (!(column.getVariableSupport() instanceof EmptyVariableSupport)) { StatementTarget statementTarget = new StatementTarget( AstNodeUtils.getEnclosingStatement(columnsArray), true); column.getVariableSupport().ensureInstanceReadyAt(statementTarget); source = column.getVariableSupport().getReferenceExpression(new NodeTarget(statementTarget)); } Assert.isNotNull(source, "No source found for."); getCreateItemExpression(column, arrayInitializer, newIndex, source); } // set association column.setAssociation(new ArrayAssociation(columnsArray)); // fire after event getBroadcastJava().moveAfter(column, oldParent, this); getBroadcastObject().childMoveAfter(getParent(), column, nextColumn, oldIndex, newIndex); }