Community
Participate
Working Groups
The following code: String input = "class T { void m() { for (i=0, j=0; i<10; i++, j++) ; }}"; org.eclipse.jdt.core.dom.ASTParser p = org.eclipse.jdt.core.dom.ASTParser.newParser(org.eclipse.jdt.core.dom.AST.JLS3); p.setSource(input.toCharArray()); org.eclipse.jdt.core.dom.ASTNode a = p.createAST(null); System.out.println("input = " + input); System.out.println("output = " + a.toString()); produces input = class T { void m() { for (i=0, j=0; i<10; i++, j++) ; }} output = class T { void m(){ for (i=0j=0; i < 10; i++j++) ; } } Note the lack of commas (and a bit of space) in the rendering of the for statement. The problem is in org.eclipse.jdt.core.dom.NaiveASTFlattener: public boolean visit(ForStatement node) { printIndent(); this.buffer.append("for (");//$NON-NLS-1$ for (Iterator it = node.initializers().iterator(); it.hasNext (); ) { Expression e = (Expression) it.next(); e.accept(this); } this.buffer.append("; ");//$NON-NLS-1$ if (node.getExpression() != null) { node.getExpression().accept(this); } this.buffer.append("; ");//$NON-NLS-1$ for (Iterator it = node.updaters().iterator(); it.hasNext(); ) { Expression e = (Expression) it.next(); e.accept(this); } this.buffer.append(") ");//$NON-NLS-1$ node.getBody().accept(this); return false; } The loops for initializers and updaters need correcting, something like public boolean visit(ForStatement node) { printIndent(); this.buffer.append("for (");//$NON-NLS-1$ for (Iterator it = node.initializers().iterator(); it.hasNext (); ) { Expression e = (Expression) it.next(); e.accept(this); if (it.hasNext()) buffer.append(", "); } this.buffer.append("; ");//$NON-NLS-1$ if (node.getExpression() != null) { node.getExpression().accept(this); } this.buffer.append("; ");//$NON-NLS-1$ for (Iterator it = node.updaters().iterator(); it.hasNext(); ) { Expression e = (Expression) it.next(); e.accept(this); if (it.hasNext()) buffer.append(", "); } this.buffer.append(") ");//$NON-NLS-1$ node.getBody().accept(this); return false; }
Fixed and released in HEAD. Regression test added in org.eclipse.jdt.core.tests.dom.ASTConverterTestAST3_2.test0614. Note that this is for debug purpose only. You should not use the result of toString() on a ASTNode to serialize your DOM compilation unit. To do that, you should use the rewrite infrastructure.
Verified with I20050808-2000 that the rendering contains the semi-colons. It still contains extra spaces, but as Olivier said the toString() is for debugging purpose only.