Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 404343
Collapse All | Expand All

(-)a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingExpressionsTest.java (+59 lines)
Lines 2190-2193 Link Here
2190
		assertEqualString(preview, buf.toString());
2190
		assertEqualString(preview, buf.toString());
2191
	}
2191
	}
2192
	public void testBug404343() throws Exception {
2193
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
2194
		StringBuffer buf= new StringBuffer();
2195
		buf.append("package test1;\n");
2196
		buf.append("public class E {\n");
2197
		buf.append("    public void foo() {\n");
2198
		buf.append("       int a = 0;\n");
2199
		buf.append("       String x = \"\";\n");
2200
		buf.append("       x = a + \"Processing \"+\" of \" + \"...\";\n");
2201
		buf.append("       x = a + \"Processing \";\n");
2202
		buf.append("    }\n");
2203
		buf.append("}\n");
2204
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
2192
2205
2206
		CompilationUnit astRoot= createAST(cu);
2207
		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
2208
2209
		AST ast= astRoot.getAST();
2210
2211
		assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
2212
		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
2213
		MethodDeclaration methodDecl= findMethodDeclaration(type, "foo");
2214
		Block block= methodDecl.getBody();
2215
		List statements= block.statements();
2216
		assertTrue("Number of statements not 4", statements.size() == 4);
2217
		{ // modify operand and operation
2218
			ExpressionStatement stmt= (ExpressionStatement) statements.get(2);
2219
			Assignment assignment= (Assignment) stmt.getExpression();
2220
			InfixExpression infix = (InfixExpression) assignment.getRightHandSide();
2221
2222
			ListRewrite listRewrite = rewrite.getListRewrite(infix, InfixExpression.EXTENDED_OPERANDS_PROPERTY);
2223
			PrefixExpression prefix = ast.newPrefixExpression();
2224
			prefix.setOperand(ast.newSimpleName("c"));
2225
			prefix.setOperator(PrefixExpression.Operator.INCREMENT);
2226
			listRewrite.insertFirst(prefix, null);
2227
2228
			stmt= (ExpressionStatement) statements.get(3);
2229
			assignment= (Assignment) stmt.getExpression();
2230
			infix = (InfixExpression) assignment.getRightHandSide();
2231
			listRewrite = rewrite.getListRewrite(infix, InfixExpression.EXTENDED_OPERANDS_PROPERTY);
2232
			prefix = ast.newPrefixExpression();
2233
			prefix.setOperand(ast.newSimpleName("c"));
2234
			prefix.setOperator(PrefixExpression.Operator.INCREMENT);
2235
			listRewrite.insertFirst(prefix, null);
2236
		}
2237
2238
		String preview= evaluateRewrite(cu, rewrite);
2239
2240
		buf= new StringBuffer();
2241
		buf.append("package test1;\n");
2242
		buf.append("public class E {\n");
2243
		buf.append("    public void foo() {\n");
2244
		buf.append("       int a = 0;\n");
2245
		buf.append("       String x = \"\";\n");
2246
		buf.append("       x = a + \"Processing \"+ ++c + \" of \" + \"...\";\n");
2247
		buf.append("       x = a + \"Processing \" + ++c;\n");
2248
		buf.append("    }\n");
2249
		buf.append("}\n");
2250
		assertEqualString(preview, buf.toString());
2251
	}
2193
}
2252
}
(-)a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java (-2 / +29 lines)
Lines 1-4 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2012 IBM Corporation and others.
2
 * Copyright (c) 2000, 2013 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
Lines 2505-2509 Link Here
2505
			}
2505
			}
2506
		}
2506
		}
2507
		rewriteNodeList(node, InfixExpression.EXTENDED_OPERANDS_PROPERTY, pos, prefixString, prefixString);
2507
2508
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=404251
2509
		// When the first is an insert with more nodes following (including replaced), a space must be inserted
2510
		boolean firstInsert = false;
2511
		boolean moreNodes = false;
2512
		if (event != null && event.getChangeKind() != RewriteEvent.UNCHANGED) {
2513
			RewriteEvent[] extendedOperands= event.getChildren();
2514
			for (int i = 0; i < extendedOperands.length; i++) {
2515
				if (extendedOperands[i].getChangeKind() != RewriteEvent.INSERTED) {
2516
					if (i == 0) break;
2517
					ASTNode elem = (ASTNode) extendedOperands[i].getOriginalValue();
2518
					pos = getExtendedOffset(elem);
2519
					moreNodes = true;
2520
					break;
2521
				} else {
2522
					if (i == 0) firstInsert = true;
2523
				}
2524
			}
2525
		}
2526
		String keyword = Util.EMPTY_STRING;
2527
		if (firstInsert) {
2528
			if (moreNodes) {
2529
				doTextInsert(pos, " ", getEditGroup(node, InfixExpression.OPERATOR_PROPERTY)); //$NON-NLS-1$
2530
			} else {
2531
				keyword = prefixString;
2532
			}
2533
		}
2534
		rewriteNodeList(node, InfixExpression.EXTENDED_OPERANDS_PROPERTY, pos, keyword, prefixString);
2508
		return false;
2535
		return false;
2509
	}
2536
	}

Return to bug 404343