View | Details | Raw Unified | Return to bug 136580
Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingStatementsTest.java (+74 lines)
Lines 1195-1200 Link Here
1195
1195
1196
	}		
1196
	}		
1197
	
1197
	
1198
	public void testForStatement2() throws Exception {
1199
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
1200
		StringBuffer buf= new StringBuffer();
1201
		buf.append("package test1;\n");
1202
		buf.append("public class E {\n");
1203
		buf.append("    public void foo() {\n");
1204
		buf.append("        for (;;) {\n");
1205
		buf.append("        }\n");	
1206
		buf.append("    }\n");
1207
		buf.append("}\n");	
1208
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
1209
		
1210
		CompilationUnit astRoot= createAST(cu);
1211
		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
1212
		AST ast= astRoot.getAST();
1213
1214
		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
1215
		MethodDeclaration methodDecl= findMethodDeclaration(type, "foo");
1216
		Block block= methodDecl.getBody();
1217
		assertTrue("Parse errors", (block.getFlags() & ASTNode.MALFORMED) == 0);
1218
		
1219
		List statements= block.statements();
1220
		assertTrue("Number of statements not 1", statements.size() == 1);
1221
1222
		{ // replace for statement
1223
			ForStatement forStatement= (ForStatement) statements.get(0);
1224
			
1225
			ForStatement newForStatement= ast.newForStatement();
1226
			List initializers= newForStatement.initializers();
1227
			
1228
			Assignment init1= ast.newAssignment();
1229
			init1.setLeftHandSide(ast.newSimpleName("x"));
1230
			init1.setRightHandSide(ast.newNumberLiteral("1"));
1231
			initializers.add(init1);
1232
			
1233
			Assignment init2= ast.newAssignment();
1234
			init2.setLeftHandSide(ast.newSimpleName("y"));
1235
			init2.setRightHandSide(ast.newNumberLiteral("10"));
1236
			initializers.add(init2);
1237
			
1238
			InfixExpression expression= ast.newInfixExpression();
1239
			expression.setOperator(InfixExpression.Operator.LESS);
1240
			expression.setRightOperand(ast.newSimpleName("y"));
1241
			expression.setLeftOperand(ast.newSimpleName("x"));
1242
			newForStatement.setExpression(expression);
1243
			
1244
			List updaters= newForStatement.updaters();
1245
			PrefixExpression upd1= ast.newPrefixExpression();
1246
			upd1.setOperator(PrefixExpression.Operator.INCREMENT);
1247
			upd1.setOperand(ast.newSimpleName("x"));
1248
			updaters.add(upd1);
1249
			
1250
			PrefixExpression upd2= ast.newPrefixExpression();
1251
			upd2.setOperator(PrefixExpression.Operator.DECREMENT);
1252
			upd2.setOperand(ast.newSimpleName("y"));
1253
			updaters.add(upd2);
1254
			
1255
			newForStatement.setBody(ast.newBlock());
1256
			rewrite.replace(forStatement, newForStatement, null);
1257
		}
1258
		
1259
		String preview= evaluateRewrite(cu, rewrite);
1260
		
1261
		buf= new StringBuffer();
1262
		buf.append("package test1;\n");
1263
		buf.append("public class E {\n");
1264
		buf.append("    public void foo() {\n");
1265
		buf.append("        for (x = 1, y = 10; x < y; ++x, --y) {\n");
1266
		buf.append("        }\n");	
1267
		buf.append("    }\n");
1268
		buf.append("}\n");	
1269
		assertEqualString(preview, buf.toString());
1270
	}
1271
1198
	
1272
	
1199
	public void testIfStatement() throws Exception {
1273
	public void testIfStatement() throws Exception {
1200
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
1274
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
(-)dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java (-2 / +2 lines)
Lines 455-468 Link Here
455
	 */
455
	 */
456
	public boolean visit(ForStatement node) {
456
	public boolean visit(ForStatement node) {
457
		this.result.append("for ("); //$NON-NLS-1$
457
		this.result.append("for ("); //$NON-NLS-1$
458
		visitList(node, ForStatement.INITIALIZERS_PROPERTY, null);
458
		visitList(node, ForStatement.INITIALIZERS_PROPERTY, String.valueOf(','));
459
		this.result.append(';');
459
		this.result.append(';');
460
		ASTNode expression= getChildNode(node, ForStatement.EXPRESSION_PROPERTY);
460
		ASTNode expression= getChildNode(node, ForStatement.EXPRESSION_PROPERTY);
461
		if (expression != null) {
461
		if (expression != null) {
462
			expression.accept(this);
462
			expression.accept(this);
463
		}
463
		}
464
		this.result.append(';');
464
		this.result.append(';');
465
		visitList(node, ForStatement.UPDATERS_PROPERTY, null);
465
		visitList(node, ForStatement.UPDATERS_PROPERTY, String.valueOf(','));
466
		this.result.append(')');
466
		this.result.append(')');
467
		getChildNode(node, ForStatement.BODY_PROPERTY).accept(this);
467
		getChildNode(node, ForStatement.BODY_PROPERTY).accept(this);
468
		return false;
468
		return false;

Return to bug 136580