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 335173 | Differences between
and this patch

Collapse All | Expand All

(-)core extension/org/eclipse/jdt/internal/corext/fix/ExpressionsFix.java (-158 / +200 lines)
Lines 86-254 Link Here
86
		}
86
		}
87
87
88
		public boolean visit(ParenthesizedExpression node) {
88
		public boolean visit(ParenthesizedExpression node) {
89
			if (canRemoveParenthesis(node)) {
89
			if (MissingNecessaryParenthesisChecker.canRemoveParenthesis(node)) {
90
				fNodes.add(node);
90
				fNodes.add(node);
91
			}
91
			}
92
92
93
			return true;
93
			return true;
94
		}
94
		}
95
96
		/*
97
		 * Can the parenthesis around node be removed?
98
		 */
99
		private boolean canRemoveParenthesis(ParenthesizedExpression node) {
100
			ASTNode parent= node.getParent();
101
			if (!(parent instanceof Expression))
102
				return true;
103
104
			Expression parentExpression= (Expression) parent;
105
			if (parentExpression instanceof ParenthesizedExpression)
106
				return true;
107
108
			Expression expression= getExpression(node);
109
110
			int expressionPrecedence= OperatorPrecedence.getExpressionPrecedence(expression);
111
			int parentPrecedence= OperatorPrecedence.getExpressionPrecedence(parentExpression);
112
113
			if (expressionPrecedence > parentPrecedence)
114
				//(opEx) opParent and opEx binds more -> can safely remove
115
				return true;
116
117
			if (expressionPrecedence < parentPrecedence)
118
				//(opEx) opParent and opEx binds less -> do not remove
119
				return false;
120
121
			//(opEx) opParent binds equal
122
123
			if (parentExpression instanceof InfixExpression) {
124
				InfixExpression parentInfix= (InfixExpression) parentExpression;
125
				if (parentInfix.getLeftOperand() == node) {
126
					//we have (expr op expr) op expr
127
					//infix expressions are evaluated from left to right -> can safely remove
128
					return true;
129
				} else if (isAssociative(parentInfix)) {
130
					//we have parent op (expr op expr) and op is associative
131
					//left op (right) == (right) op left == right op left
132
					if (expression instanceof InfixExpression) {
133
						InfixExpression infixExpression= (InfixExpression) expression;
134
						Operator operator= infixExpression.getOperator();
135
						if (parentInfix.getOperator() != InfixExpression.Operator.TIMES)
136
							return true;
137
138
						if (operator == InfixExpression.Operator.TIMES)
139
							// x * (y * z) == x * y * z
140
							return true;
141
142
						if (operator == InfixExpression.Operator.REMAINDER)
143
							// x * (y % z) != x * y % z
144
							return false;
145
146
						//x * (y / z) == z * y / z  iff no rounding
147
						ITypeBinding binding= infixExpression.resolveTypeBinding();
148
						if (binding == null)
149
							return false;
150
151
						if (!binding.isPrimitive())
152
							return false;
153
154
						String name= binding.getName();
155
						if (isIntegerNumber(name))
156
							//rounding involved
157
							return false;
158
159
						return true;
160
					}
161
					return true;
162
				} else {
163
					return false;
164
				}
165
			} else if (parentExpression instanceof ConditionalExpression) {
166
				ConditionalExpression conditionalExpression= (ConditionalExpression) parentExpression;
167
				if (conditionalExpression.getElseExpression() != node)
168
					return false;
169
			}
170
171
			return true;
172
		}
173
174
		private boolean isIntegerNumber(String name) {
175
			return "int".equals(name) || "long".equals(name) || "byte".equals(name) || "char".equals(name) || "short".equals(name); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
176
		}
177
178
		/*
179
		 * Get the expression wrapped by the parentheses
180
		 * i.e. ((((expression)))) -> expression
181
		 */
182
		private Expression getExpression(ParenthesizedExpression node) {
183
			Expression expression= node.getExpression();
184
			while (expression instanceof ParenthesizedExpression) {
185
				expression= ((ParenthesizedExpression) expression).getExpression();
186
			}
187
			return expression;
188
		}
189
190
		/**
191
		 * Is the given expression associative?
192
		 * <p>
193
		 * This is true if and only if:<br>
194
		 * <code>left operator (right) == (right) operator left == right operator left</code>
195
		 * </p>
196
		 *
197
		 * @param expression the expression to inspect
198
		 * @return true if expression is associative
199
		 */
200
		public static boolean isAssociative(InfixExpression expression) {
201
			Operator operator= expression.getOperator();
202
			if (operator == InfixExpression.Operator.PLUS) {
203
				return isAllOperandsHaveSameType(expression);
204
			}
205
206
			if (operator == Operator.LESS || operator == Operator.LESS_EQUALS || operator == Operator.GREATER || operator == Operator.GREATER_EQUALS) {
207
				return isAllOperandsHaveSameType(expression);
208
			}
209
210
			if (operator == InfixExpression.Operator.CONDITIONAL_AND)
211
				return true;
212
213
			if (operator == InfixExpression.Operator.CONDITIONAL_OR)
214
				return true;
215
216
			if (operator == InfixExpression.Operator.AND)
217
				return true;
218
219
			if (operator == InfixExpression.Operator.OR)
220
				return true;
221
222
			if (operator == InfixExpression.Operator.XOR)
223
				return true;
224
225
			if (operator == InfixExpression.Operator.TIMES)
226
				return true;
227
228
			return false;
229
		}
230
231
		/*
232
		 * Do all operands in expression have same type
233
		 */
234
		private static boolean isAllOperandsHaveSameType(InfixExpression expression) {
235
			ITypeBinding binding= expression.getLeftOperand().resolveTypeBinding();
236
			if (binding == null)
237
				return false;
238
239
			ITypeBinding current= expression.getRightOperand().resolveTypeBinding();
240
			if (binding != current)
241
				return false;
242
243
			for (Iterator iterator= expression.extendedOperands().iterator(); iterator.hasNext();) {
244
				Expression operand= (Expression) iterator.next();
245
				current= operand.resolveTypeBinding();
246
				if (binding != current)
247
					return false;
248
			}
249
250
			return true;
251
		}
252
	}
95
	}
253
96
254
	private static class AddParenthesisOperation extends CompilationUnitRewriteOperation {
97
	private static class AddParenthesisOperation extends CompilationUnitRewriteOperation {
Lines 386-389 Link Here
386
		super(name, compilationUnit, fixRewriteOperations);
229
		super(name, compilationUnit, fixRewriteOperations);
387
	}
230
	}
388
231
232
	/**
233
	 * Is the given expression associative?
234
	 * <p>
235
	 * This is true if and only if:<br>
236
	 * <code>left operator (right) == (right) operator left == right operator left</code>
237
	 * </p>
238
	 *
239
	 * @param expression the expression to inspect
240
	 * @return true if expression is associative
241
	 */
242
	public static boolean isAssociative(InfixExpression expression) {
243
		Operator operator= expression.getOperator();
244
		if (operator == InfixExpression.Operator.PLUS) {
245
			return isAllOperandsHaveSameType(expression);
246
		}
247
	
248
		//TODO: This if statement does not look right to me, remove it
249
		/*	if (operator == Operator.LESS || operator == Operator.LESS_EQUALS || operator == Operator.GREATER || operator == Operator.GREATER_EQUALS) {
250
				return isAllOperandsHaveSameType(expression);
251
			}*/
252
	
253
		if (operator == InfixExpression.Operator.CONDITIONAL_AND)
254
			return true;
255
	
256
		if (operator == InfixExpression.Operator.CONDITIONAL_OR)
257
			return true;
258
	
259
		if (operator == InfixExpression.Operator.AND)
260
			return true;
261
	
262
		if (operator == InfixExpression.Operator.OR)
263
			return true;
264
	
265
		if (operator == InfixExpression.Operator.XOR)
266
			return true;
267
	
268
		if (operator == InfixExpression.Operator.TIMES)
269
			return true;
270
	
271
		return false;
272
	}
273
274
	/*
275
	 * Do all operands in expression have same type
276
	 */
277
	private static boolean isAllOperandsHaveSameType(InfixExpression expression) {
278
		ITypeBinding binding= expression.getLeftOperand().resolveTypeBinding();
279
		if (binding == null)
280
			return false;
281
	
282
		ITypeBinding current= expression.getRightOperand().resolveTypeBinding();
283
		if (binding != current)
284
			return false;
285
	
286
		for (Iterator iterator= expression.extendedOperands().iterator(); iterator.hasNext();) {
287
			Expression operand= (Expression) iterator.next();
288
			current= operand.resolveTypeBinding();
289
			if (binding != current)
290
				return false;
291
		}
292
	
293
		return true;
294
	}
295
296
297
	public static class MissingNecessaryParenthesisChecker {
298
299
		/*
300
		 * Get the expression wrapped by the parentheses
301
		 * i.e. ((((expression)))) -> expression
302
		 */
303
		private static Expression getExpression(ParenthesizedExpression node) {
304
			Expression expression= node.getExpression();
305
			while (expression instanceof ParenthesizedExpression) {
306
				expression= ((ParenthesizedExpression)expression).getExpression();
307
			}
308
			return expression;
309
		}
310
311
		private static boolean isLeftOperand(Expression node, ASTNode parent) {
312
			if (parent instanceof InfixExpression) {
313
				return ((InfixExpression)parent).getLeftOperand() == node;
314
			}
315
			return false;
316
		}
317
318
		private static boolean isElseExpression(Expression node, ASTNode parent) {
319
			if (parent instanceof ConditionalExpression) {
320
				return ((ConditionalExpression)parent).getElseExpression() == node;
321
			}
322
			return false;
323
		}
324
325
		public static boolean canRemoveParenthesis(Expression node) {
326
			return canRemoveParenthesis(node, node.getParent());
327
		}
328
329
		public static boolean canRemoveParenthesis(Expression node, ASTNode parent) {
330
			return canRemoveParenthesis(node, node.getParent(), isLeftOperand(node, parent), isElseExpression(node, parent));
331
		}
332
333
334
		public static boolean canRemoveParenthesis(Expression node, ASTNode parent, boolean isLeftOperand, boolean isElseExpression) {
335
			if (!(node instanceof ParenthesizedExpression)) {
336
				return false;
337
			}
338
			return !isParenthesisNeeded(getExpression((ParenthesizedExpression)node), parent, isLeftOperand, isElseExpression);
339
		}
340
341
342
		public static boolean isParenthesisNeeded(Expression node) {
343
			return isParenthesisNeeded(node, node.getParent());
344
		}
345
346
		public static boolean isParenthesisNeeded(Expression node, ASTNode parent) {
347
			return isParenthesisNeeded(node, node.getParent(), isLeftOperand(node, parent), isElseExpression(node, parent));
348
		}
349
350
		/*
351
		 * does the node need parenthesis?
352
		 */
353
		public static boolean isParenthesisNeeded(Expression node, ASTNode parent, boolean isLeftOperand, boolean isElseExpression) {
354
			if (!(parent instanceof Expression))
355
				return false;
356
357
			Expression parentExpression= (Expression)parent;
358
			if (parentExpression instanceof ParenthesizedExpression)
359
				return false;
360
361
			Expression expression= node;
362
363
			int expressionPrecedence= OperatorPrecedence.getExpressionPrecedence(expression);
364
			int parentPrecedence= OperatorPrecedence.getExpressionPrecedence(parentExpression);
365
366
			if (expressionPrecedence > parentPrecedence)
367
				//(opEx) opParent and opEx binds more -> can safely remove
368
				return false;
369
370
			if (expressionPrecedence < parentPrecedence)
371
				//(opEx) opParent and opEx binds less -> do not remove
372
				return true;
373
374
			//(opEx) opParent binds equal
375
376
			if (parentExpression instanceof InfixExpression) {
377
				InfixExpression parentInfix= (InfixExpression)parentExpression;
378
				if (isLeftOperand) {
379
					//we have (expr op expr) op expr
380
					//infix expressions are evaluated from left to right -> can safely remove
381
					return false;
382
				} else if (isAssociative(parentInfix)) {
383
					//we have parent op (expr op expr) and op is associative
384
					//left op (right) == (right) op left == right op left
385
					if (expression instanceof InfixExpression) {
386
						InfixExpression infixExpression= (InfixExpression)expression;
387
						Operator operator= infixExpression.getOperator();
388
						if (parentInfix.getOperator() != InfixExpression.Operator.TIMES)
389
							return false;
390
391
						if (operator == InfixExpression.Operator.TIMES)
392
							// x * (y * z) == x * y * z
393
							return false;
394
395
						if (operator == InfixExpression.Operator.REMAINDER)
396
							// x * (y % z) != x * y % z
397
							return true;
398
399
						//x * (y / z) == z * y / z  iff no rounding
400
						ITypeBinding binding= infixExpression.resolveTypeBinding();
401
						if (binding == null)
402
							return true;
403
404
						if (!binding.isPrimitive())
405
							return true;
406
407
						String name= binding.getName();
408
						if (isIntegerNumber(name))
409
							//rounding involved
410
							return true;
411
412
						return false;
413
					}
414
					return false;
415
				} else {
416
					return true;
417
				}
418
			} else if (parentExpression instanceof ConditionalExpression) {
419
				if (!isElseExpression) {
420
					return true;
421
				}
422
			}
423
424
			return false;
425
		}
426
427
		private static boolean isIntegerNumber(String name) {
428
			return "int".equals(name) || "long".equals(name) || "byte".equals(name) || "char".equals(name) || "short".equals(name); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
429
		}
430
	}
389
}
431
}
(-)core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/SourceProvider.java (-5 / +20 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 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
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 65-70 Link Here
65
import org.eclipse.jdt.core.dom.ITypeBinding;
65
import org.eclipse.jdt.core.dom.ITypeBinding;
66
import org.eclipse.jdt.core.dom.IVariableBinding;
66
import org.eclipse.jdt.core.dom.IVariableBinding;
67
import org.eclipse.jdt.core.dom.IfStatement;
67
import org.eclipse.jdt.core.dom.IfStatement;
68
import org.eclipse.jdt.core.dom.InfixExpression;
68
import org.eclipse.jdt.core.dom.LabeledStatement;
69
import org.eclipse.jdt.core.dom.LabeledStatement;
69
import org.eclipse.jdt.core.dom.MethodDeclaration;
70
import org.eclipse.jdt.core.dom.MethodDeclaration;
70
import org.eclipse.jdt.core.dom.MethodInvocation;
71
import org.eclipse.jdt.core.dom.MethodInvocation;
Lines 85-90 Link Here
85
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
86
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
86
import org.eclipse.jdt.internal.corext.dom.Bindings;
87
import org.eclipse.jdt.internal.corext.dom.Bindings;
87
import org.eclipse.jdt.internal.corext.dom.CodeScopeBuilder;
88
import org.eclipse.jdt.internal.corext.dom.CodeScopeBuilder;
89
import org.eclipse.jdt.internal.corext.fix.ExpressionsFix.MissingNecessaryParenthesisChecker;
88
import org.eclipse.jdt.internal.corext.refactoring.code.SourceAnalyzer.NameData;
90
import org.eclipse.jdt.internal.corext.refactoring.code.SourceAnalyzer.NameData;
89
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringFileBuffers;
91
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringFileBuffers;
90
import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
92
import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
Lines 413-426 Link Here
413
		return new String[] {};
415
		return new String[] {};
414
	}
416
	}
415
417
416
	private boolean argumentNeedsParenthesis(Expression expression, ParameterData param) {
418
	private boolean argumentNeedsParenthesis(Expression expression, ParameterData param, ASTNode reference) {
417
		if (expression instanceof CastExpression || expression instanceof ArrayCreation)
419
		if (expression instanceof CastExpression || expression instanceof ArrayCreation)
418
			return true;
420
			return true;
419
		int argPrecedence= OperatorPrecedence.getExpressionPrecedence(expression);
421
422
		ASTNode parent= reference.getParent();
423
		boolean isLeftOperand= false;
424
		if (parent instanceof InfixExpression) {
425
			isLeftOperand= ((InfixExpression)parent).getLeftOperand() == reference;
426
		}
427
		boolean isElseExpression= false;
428
		if (parent instanceof ConditionalExpression) {
429
			isElseExpression= ((ConditionalExpression)parent).getElseExpression() == reference;
430
		}
431
432
		return MissingNecessaryParenthesisChecker.isParenthesisNeeded(expression, parent, isLeftOperand, isElseExpression);
433
434
		/*int argPrecedence= OperatorPrecedence.getExpressionPrecedence(expression);
420
		int paramPrecedence= param.getOperatorPrecedence();
435
		int paramPrecedence= param.getOperatorPrecedence();
421
		if (argPrecedence != Integer.MAX_VALUE && paramPrecedence != Integer.MAX_VALUE)
436
		if (argPrecedence != Integer.MAX_VALUE && paramPrecedence != Integer.MAX_VALUE)
422
			return argPrecedence <= paramPrecedence;
437
			return argPrecedence <= paramPrecedence;
423
		return false;
438
		return false;*/
424
	}
439
	}
425
440
426
	private Expression createParenthesizedExpression(Expression newExpression, AST ast) {
441
	private Expression createParenthesizedExpression(Expression newExpression, AST ast) {
Lines 462-468 Link Here
462
						ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(expression, importRewrite);
477
						ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(expression, importRewrite);
463
						cast.setType(importRewrite.addImport(explicitCast, ast, importRewriteContext));
478
						cast.setType(importRewrite.addImport(explicitCast, ast, importRewriteContext));
464
						newExpression= createParenthesizedExpression(cast, ast);
479
						newExpression= createParenthesizedExpression(cast, ast);
465
					} else if (argumentNeedsParenthesis(expression, parameter)) {
480
					} else if (argumentNeedsParenthesis(expression, parameter, element)) {
466
						newExpression= createParenthesizedExpression(newExpression, ast);
481
						newExpression= createParenthesizedExpression(newExpression, ast);
467
					}
482
					}
468
					rewriter.replace(element, newExpression, null);
483
					rewriter.replace(element, newExpression, null);
(-)ui/org/eclipse/jdt/internal/ui/text/correction/AdvancedQuickAssistProcessor.java (-15 / +36 lines)
Lines 85-90 Link Here
85
import org.eclipse.jdt.internal.corext.dom.LinkedNodeFinder;
85
import org.eclipse.jdt.internal.corext.dom.LinkedNodeFinder;
86
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
86
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
87
import org.eclipse.jdt.internal.corext.fix.ExpressionsFix;
87
import org.eclipse.jdt.internal.corext.fix.ExpressionsFix;
88
import org.eclipse.jdt.internal.corext.fix.ExpressionsFix.MissingNecessaryParenthesisChecker;
88
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
89
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
89
import org.eclipse.jdt.internal.corext.refactoring.code.OperatorPrecedence;
90
import org.eclipse.jdt.internal.corext.refactoring.code.OperatorPrecedence;
90
import org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer;
91
import org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer;
Lines 522-528 Link Here
522
		if (expression instanceof PrefixExpression) {
523
		if (expression instanceof PrefixExpression) {
523
			PrefixExpression prefixExpression= (PrefixExpression) expression;
524
			PrefixExpression prefixExpression= (PrefixExpression) expression;
524
			if (prefixExpression.getOperator() == PrefixExpression.Operator.NOT) {
525
			if (prefixExpression.getOperator() == PrefixExpression.Operator.NOT) {
525
				return getRenamedNameCopy(provider, rewrite, prefixExpression.getOperand());
526
				ASTNode parent= expression.getParent();
527
				boolean isLeftOperand= false;
528
				if (parent instanceof InfixExpression) {
529
					isLeftOperand= ((InfixExpression)parent).getLeftOperand() == expression;
530
				}
531
				boolean isElseExpression= false;
532
				if (parent instanceof ConditionalExpression) {
533
					isElseExpression= ((ConditionalExpression)parent).getElseExpression() == expression;
534
				}
535
536
				Expression operand= prefixExpression.getOperand();
537
				if ((operand instanceof ParenthesizedExpression) && MissingNecessaryParenthesisChecker.canRemoveParenthesis(operand, parent, isLeftOperand, isElseExpression)) {
538
					operand= ((ParenthesizedExpression)operand).getExpression();
539
				}
540
				return getRenamedNameCopy(provider, rewrite, operand);
526
			}
541
			}
527
		}
542
		}
528
		if (expression instanceof InstanceofExpression) {
543
		if (expression instanceof InstanceofExpression) {
Lines 1253-1266 Link Here
1253
			}
1268
			}
1254
		}
1269
		}
1255
1270
1256
		// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=332019
1271
		if (MissingNecessaryParenthesisChecker.isParenthesisNeeded(leftExpression, infixExpression, false, false)) {
1257
		if (operator == InfixExpression.Operator.EQUALS || operator == InfixExpression.Operator.NOT_EQUALS) {
1272
			leftExpression= getParenthesizedExpression(ast, leftExpression);
1258
			if (leftExpression instanceof InfixExpression && !(leftExpression instanceof ParenthesizedExpression)) {
1273
		}
1259
				leftExpression= getParenthesizedExpression(ast, leftExpression);
1274
		if (MissingNecessaryParenthesisChecker.isParenthesisNeeded(rightExpression, infixExpression, true, false)) {
1260
			}
1275
			rightExpression= getParenthesizedExpression(ast, rightExpression);
1261
			if (rightExpression instanceof InfixExpression && !(rightExpression instanceof ParenthesizedExpression)) {
1262
				rightExpression= getParenthesizedExpression(ast, rightExpression);
1263
			}
1264
		}
1276
		}
1265
1277
1266
		if (operator == InfixExpression.Operator.LESS) {
1278
		if (operator == InfixExpression.Operator.LESS) {
Lines 1318-1338 Link Here
1318
		}
1330
		}
1319
	}
1331
	}
1320
1332
1321
	private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression nodeToAdd, boolean removeParentheses, Operator operator) {
1333
	private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression originalNode, boolean removeParentheses, Operator operator) {
1322
		if (existing == null && removeParentheses) {
1334
		if (existing == null && removeParentheses) {
1323
			while (nodeToAdd instanceof ParenthesizedExpression) {
1335
			while (originalNode instanceof ParenthesizedExpression) {
1324
				nodeToAdd= ((ParenthesizedExpression) nodeToAdd).getExpression();
1336
				originalNode= ((ParenthesizedExpression)originalNode).getExpression();
1325
			}
1337
			}
1326
		}
1338
		}
1327
		Expression newRight= (Expression) rewrite.createMoveTarget(nodeToAdd);
1339
		Expression placeholder= (Expression)rewrite.createMoveTarget(originalNode);
1340
		if (originalNode instanceof InfixExpression) {
1341
			((InfixExpression)placeholder).setOperator(((InfixExpression)originalNode).getOperator());
1342
		}
1343
1344
		/*Expression newRight= (Expression) rewrite.createMoveTarget(nodeToAdd);
1345
		if (nodeToAdd instanceof InfixExpression) {
1346
			((InfixExpression)newRight).setOperator(((InfixExpression)nodeToAdd).getOperator());
1347
		}
1348
		*/
1328
		if (existing == null) {
1349
		if (existing == null) {
1329
			return newRight;
1350
			return placeholder;
1330
		}
1351
		}
1331
		AST ast= rewrite.getAST();
1352
		AST ast= rewrite.getAST();
1332
		InfixExpression infix= ast.newInfixExpression();
1353
		InfixExpression infix= ast.newInfixExpression();
1333
		infix.setOperator(operator);
1354
		infix.setOperator(operator);
1334
		infix.setLeftOperand(existing);
1355
		infix.setLeftOperand(existing);
1335
		infix.setRightOperand(newRight);
1356
		infix.setRightOperand(placeholder);
1336
		return infix;
1357
		return infix;
1337
	}
1358
	}
1338
1359
(-)ui/org/eclipse/jdt/ui/tests/quickfix/AdvancedQuickAssistTest.java (-1 / +65 lines)
Lines 1639-1645 Link Here
1639
		buf.append("package test1;\n");
1639
		buf.append("package test1;\n");
1640
		buf.append("public class E {\n");
1640
		buf.append("public class E {\n");
1641
		buf.append("    public boolean foo(int a, int b) {\n");
1641
		buf.append("    public boolean foo(int a, int b) {\n");
1642
		buf.append("        return (a == b) != (b > 0);\n");
1642
		buf.append("        return (a == b) != b > 0;\n");
1643
		buf.append("    }\n");
1643
		buf.append("    }\n");
1644
		buf.append("}\n");
1644
		buf.append("}\n");
1645
		String expected1= buf.toString();
1645
		String expected1= buf.toString();
Lines 1679-1684 Link Here
1679
1679
1680
	}
1680
	}
1681
1681
1682
	public void testExchangeOperandsBug332019_4() throws Exception {
1683
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1684
		StringBuffer buf= new StringBuffer();
1685
		buf.append("package test1;\n");
1686
		buf.append("public class E {\n");
1687
		buf.append("    public boolean foo(int a, int b) {\n");
1688
		buf.append("        return b + 1 != a - 1;\n");
1689
		buf.append("    }\n");
1690
		buf.append("}\n");
1691
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
1692
1693
		int offset= buf.toString().indexOf("!=");
1694
		AssistContext context= getCorrectionContext(cu, offset, 0);
1695
		assertNoErrors(context);
1696
		List proposals= collectAssists(context, false);
1697
1698
		assertCorrectLabels(proposals);
1699
1700
		buf= new StringBuffer();
1701
		buf.append("package test1;\n");
1702
		buf.append("public class E {\n");
1703
		buf.append("    public boolean foo(int a, int b) {\n");
1704
		buf.append("        return a - 1 != b + 1;\n");
1705
		buf.append("    }\n");
1706
		buf.append("}\n");
1707
		String expected1= buf.toString();
1708
1709
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1710
1711
	}
1712
1682
	public void testAssignAndCast1() throws Exception {
1713
	public void testAssignAndCast1() throws Exception {
1683
		//https://bugs.eclipse.org/bugs/show_bug.cgi?id=75066
1714
		//https://bugs.eclipse.org/bugs/show_bug.cgi?id=75066
1684
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1715
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
Lines 2895-2900 Link Here
2895
2926
2896
	}
2927
	}
2897
2928
2929
	public void testInverseCondition2() throws Exception {
2930
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
2931
		StringBuffer buf= new StringBuffer();
2932
		buf.append("package test1;\n");
2933
		buf.append("public class E {\n");
2934
		buf.append("    public void foo(Object a) {\n");
2935
		buf.append("        if (!(a instanceof String)) {\n");
2936
		buf.append("        }\n");
2937
		buf.append("    }\n");
2938
		buf.append("}\n");
2939
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
2940
2941
		int offset= buf.toString().indexOf("!");
2942
		int length= "!(a instanceof String)".length();
2943
		AssistContext context= getCorrectionContext(cu, offset, length);
2944
		List proposals= collectAssists(context, false);
2945
2946
		assertCorrectLabels(proposals);
2947
2948
		buf= new StringBuffer();
2949
		buf.append("package test1;\n");
2950
		buf.append("public class E {\n");
2951
		buf.append("    public void foo(Object a) {\n");
2952
		buf.append("        if (a instanceof String) {\n");
2953
		buf.append("        }\n");
2954
		buf.append("    }\n");
2955
		buf.append("}\n");
2956
		String expected1= buf.toString();
2957
2958
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
2959
2960
	}
2961
2898
	public void testPushNegationDown1() throws Exception {
2962
	public void testPushNegationDown1() throws Exception {
2899
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
2963
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
2900
		StringBuffer buf= new StringBuffer();
2964
		StringBuffer buf= new StringBuffer();
(-)resources/InlineMethodWorkspace/TestCases/operator_in/TestDiffPlus.java (+13 lines)
Added Link Here
1
package operator_in;
2
3
public class TestPlusPlus {
4
	int result;
5
	
6
	public void foo() {
7
		result= /*]*/inline(20 - 10)/*[*/;
8
	}
9
	
10
	public int inline(int x) {
11
		return 1 + x;
12
	}
13
}
(-)resources/InlineMethodWorkspace/TestCases/operator_out/TestDiffPlus.java (+13 lines)
Added Link Here
1
package operator_out;
2
3
public class TestPlusPlus {
4
	int result;
5
	
6
	public void foo() {
7
		result= /*]*/1 + 20 - 10/*[*/;
8
	}
9
	
10
	public int inline(int x) {
11
		return 1 + x;
12
	}
13
}
(-)resources/InlineMethodWorkspace/TestCases/operator_out/TestPlusPlus.java (-1 / +1 lines)
Lines 4-10 Link Here
4
	int result;
4
	int result;
5
	
5
	
6
	public void foo() {
6
	public void foo() {
7
		result= /*]*/1 + (10 + 10)/*[*/;
7
		result= /*]*/1 + 10 + 10/*[*/;
8
	}
8
	}
9
	
9
	
10
	public int inline(int x) {
10
	public int inline(int x) {
(-)resources/InlineMethodWorkspace/TestCases/operator_out/TestTimesTimes.java (-1 / +1 lines)
Lines 4-10 Link Here
4
	int result;
4
	int result;
5
	
5
	
6
	public void foo() {
6
	public void foo() {
7
		result= /*]*/1 * (10 * 10)/*[*/;
7
		result= /*]*/1 * 10 * 10/*[*/;
8
	}
8
	}
9
	
9
	
10
	public int inline(int x) {
10
	public int inline(int x) {
(-)resources/InlineMethodWorkspace/TestCases/simple_out/TestComment2.java (-1 / +1 lines)
Lines 2-7 Link Here
2
2
3
public class TestComment2 {
3
public class TestComment2 {
4
	public void ref() {
4
	public void ref() {
5
		int toInline = 42 * (5 /*op1*/ * /*op2*/ 2);
5
		int toInline = 42 * 5 /*op1*/ * /*op2*/ 2;
6
	}
6
	}
7
}
7
}
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java (-1 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 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
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 956-961 Link Here
956
		performOperatorTest();
956
		performOperatorTest();
957
	}
957
	}
958
958
959
	public void testDiffPlus() throws Exception {
960
		performOperatorTest();
961
	}
962
959
	public void testTimesPlus() throws Exception {
963
	public void testTimesPlus() throws Exception {
960
		performOperatorTest();
964
		performOperatorTest();
961
	}
965
	}

Return to bug 335173