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/codemanipulation/GetterSetterUtil.java (-4 / +11 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 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 23-28 Link Here
23
import org.eclipse.jdt.core.dom.AST;
23
import org.eclipse.jdt.core.dom.AST;
24
import org.eclipse.jdt.core.dom.ASTNode;
24
import org.eclipse.jdt.core.dom.ASTNode;
25
import org.eclipse.jdt.core.dom.Assignment;
25
import org.eclipse.jdt.core.dom.Assignment;
26
import org.eclipse.jdt.core.dom.Assignment.Operator;
26
import org.eclipse.jdt.core.dom.CastExpression;
27
import org.eclipse.jdt.core.dom.CastExpression;
27
import org.eclipse.jdt.core.dom.Expression;
28
import org.eclipse.jdt.core.dom.Expression;
28
import org.eclipse.jdt.core.dom.ITypeBinding;
29
import org.eclipse.jdt.core.dom.ITypeBinding;
Lines 33-42 Link Here
33
import org.eclipse.jdt.core.dom.PostfixExpression;
34
import org.eclipse.jdt.core.dom.PostfixExpression;
34
import org.eclipse.jdt.core.dom.PrefixExpression;
35
import org.eclipse.jdt.core.dom.PrefixExpression;
35
import org.eclipse.jdt.core.dom.PrimitiveType;
36
import org.eclipse.jdt.core.dom.PrimitiveType;
36
import org.eclipse.jdt.core.dom.Assignment.Operator;
37
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
37
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
38
38
39
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
39
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
40
import org.eclipse.jdt.internal.corext.fix.NecessaryParenthesesChecker;
40
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
41
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
41
import org.eclipse.jdt.internal.corext.util.JdtFlags;
42
import org.eclipse.jdt.internal.corext.util.JdtFlags;
42
43
Lines 260-267 Link Here
260
				InfixExpression infix= ast.newInfixExpression();
261
				InfixExpression infix= ast.newInfixExpression();
261
				infix.setLeftOperand(getterExpression);
262
				infix.setLeftOperand(getterExpression);
262
				infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator()));
263
				infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator()));
263
				infix.setRightOperand(copiedRightOp);
264
				ITypeBinding infixType= infix.resolveTypeBinding();
264
				ITypeBinding infixType= infix.resolveTypeBinding();
265
				if (NecessaryParenthesesChecker.needsParentheses(copiedRightOp, infix, InfixExpression.RIGHT_OPERAND_PROPERTY)) {
266
					//TODO: this introduces extra parentheses as bindings cannot be resolved
267
					ParenthesizedExpression p= ast.newParenthesizedExpression();
268
					p.setExpression(copiedRightOp);
269
					copiedRightOp= p;
270
				}
271
				infix.setRightOperand(copiedRightOp);
265
				return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
272
				return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
266
			}
273
			}
267
		} else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
274
		} else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
Lines 336-342 Link Here
336
			castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
343
			castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
337
		if (castTo != null) {
344
		if (castTo != null) {
338
			CastExpression cast= ast.newCastExpression();
345
			CastExpression cast= ast.newCastExpression();
339
			if (ASTNodes.needsParentheses(expression)) {
346
			if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
340
				ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
347
				ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
341
				parenthesized.setExpression(expression);
348
				parenthesized.setExpression(expression);
342
				cast.setExpression(parenthesized);
349
				cast.setExpression(parenthesized);
(-)core extension/org/eclipse/jdt/internal/corext/dom/ASTNodes.java (-58 / +1 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 42-48 Link Here
42
import org.eclipse.jdt.core.dom.ASTVisitor;
42
import org.eclipse.jdt.core.dom.ASTVisitor;
43
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
43
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
44
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
44
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
45
import org.eclipse.jdt.core.dom.ArrayAccess;
46
import org.eclipse.jdt.core.dom.ArrayType;
45
import org.eclipse.jdt.core.dom.ArrayType;
47
import org.eclipse.jdt.core.dom.Assignment;
46
import org.eclipse.jdt.core.dom.Assignment;
48
import org.eclipse.jdt.core.dom.BodyDeclaration;
47
import org.eclipse.jdt.core.dom.BodyDeclaration;
Lines 73-85 Link Here
73
import org.eclipse.jdt.core.dom.PrimitiveType;
72
import org.eclipse.jdt.core.dom.PrimitiveType;
74
import org.eclipse.jdt.core.dom.QualifiedName;
73
import org.eclipse.jdt.core.dom.QualifiedName;
75
import org.eclipse.jdt.core.dom.QualifiedType;
74
import org.eclipse.jdt.core.dom.QualifiedType;
76
import org.eclipse.jdt.core.dom.ReturnStatement;
77
import org.eclipse.jdt.core.dom.SimpleName;
75
import org.eclipse.jdt.core.dom.SimpleName;
78
import org.eclipse.jdt.core.dom.SimpleType;
76
import org.eclipse.jdt.core.dom.SimpleType;
79
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
77
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
80
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
78
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
81
import org.eclipse.jdt.core.dom.SwitchCase;
82
import org.eclipse.jdt.core.dom.SwitchStatement;
83
import org.eclipse.jdt.core.dom.Type;
79
import org.eclipse.jdt.core.dom.Type;
84
import org.eclipse.jdt.core.dom.VariableDeclaration;
80
import org.eclipse.jdt.core.dom.VariableDeclaration;
85
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
81
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
Lines 88-94 Link Here
88
import org.eclipse.jdt.core.dom.WhileStatement;
84
import org.eclipse.jdt.core.dom.WhileStatement;
89
85
90
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
86
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
91
import org.eclipse.jdt.internal.corext.refactoring.code.OperatorPrecedence;
92
import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
87
import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
93
import org.eclipse.jdt.internal.corext.util.Strings;
88
import org.eclipse.jdt.internal.corext.util.Strings;
94
89
Lines 429-486 Link Here
429
			|| locationInParent == DoStatement.BODY_PROPERTY;
424
			|| locationInParent == DoStatement.BODY_PROPERTY;
430
	}
425
	}
431
426
432
	public static boolean needsParentheses(Expression expression) {
433
		int type= expression.getNodeType();
434
		return type == ASTNode.INFIX_EXPRESSION || type == ASTNode.CONDITIONAL_EXPRESSION ||
435
			type == ASTNode.PREFIX_EXPRESSION || type == ASTNode.POSTFIX_EXPRESSION ||
436
			type == ASTNode.CAST_EXPRESSION || type == ASTNode.INSTANCEOF_EXPRESSION;
437
	}
438
439
	/**
440
	 * Checks whether <code>substitute</code> must be parenthesized when used to replace
441
	 * <code>location</code>.
442
	 * 
443
	 * @param substitute substitute expression
444
	 * @param location expression to be replaced
445
	 * @return <code>true</code> iff <code>substitute</code> must be parenthesized when used to
446
	 *         replace <code>location</code>
447
	 */
448
	public static boolean substituteMustBeParenthesized(Expression substitute, Expression location) {
449
		if (substitute instanceof Assignment) //for esthetic reasons
450
			return true;
451
452
    	if (!needsParentheses(substitute))
453
    		return false;
454
455
    	ASTNode parent= location.getParent();
456
    	StructuralPropertyDescriptor locationInParent= location.getLocationInParent();
457
		if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) {
458
			// e.g. argument lists of MethodInvocation, ClassInstanceCreation, ...
459
   			return false;
460
    	} else if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
461
   			return false;
462
    	} else if (locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY) {
463
    		return false;
464
    	} else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
465
    		return false;
466
    	} else if (locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY) {
467
    		return false;
468
    	} else if (locationInParent == SwitchStatement.EXPRESSION_PROPERTY) {
469
    		return false;
470
    	} else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY) {
471
    		return false;
472
    	} else if (locationInParent == ArrayAccess.INDEX_PROPERTY) {
473
    		return false;
474
    	} else if (parent instanceof Expression) {
475
			int substitutePrecedence= OperatorPrecedence.getExpressionPrecedence(substitute);
476
			int locationPrecedence= OperatorPrecedence.getExpressionPrecedence((Expression)parent);
477
			if (substitutePrecedence > locationPrecedence)
478
				return false;
479
		}
480
481
        return true;
482
	}
483
484
	/**
427
	/**
485
	 * Returns the type to which an inlined variable initializer should be cast, or
428
	 * Returns the type to which an inlined variable initializer should be cast, or
486
	 * <code>null</code> if no cast is necessary.
429
	 * <code>null</code> if no cast is necessary.
(-)core extension/org/eclipse/jdt/internal/corext/fix/ExpressionsFix.java (-164 / +1 lines)
Lines 12-18 Link Here
12
12
13
import java.util.ArrayList;
13
import java.util.ArrayList;
14
import java.util.HashSet;
14
import java.util.HashSet;
15
import java.util.Iterator;
16
15
17
import org.eclipse.core.runtime.CoreException;
16
import org.eclipse.core.runtime.CoreException;
18
17
Lines 22-37 Link Here
22
import org.eclipse.jdt.core.dom.ASTNode;
21
import org.eclipse.jdt.core.dom.ASTNode;
23
import org.eclipse.jdt.core.dom.ASTVisitor;
22
import org.eclipse.jdt.core.dom.ASTVisitor;
24
import org.eclipse.jdt.core.dom.CompilationUnit;
23
import org.eclipse.jdt.core.dom.CompilationUnit;
25
import org.eclipse.jdt.core.dom.ConditionalExpression;
26
import org.eclipse.jdt.core.dom.Expression;
24
import org.eclipse.jdt.core.dom.Expression;
27
import org.eclipse.jdt.core.dom.ITypeBinding;
28
import org.eclipse.jdt.core.dom.InfixExpression;
25
import org.eclipse.jdt.core.dom.InfixExpression;
29
import org.eclipse.jdt.core.dom.InfixExpression.Operator;
30
import org.eclipse.jdt.core.dom.InstanceofExpression;
26
import org.eclipse.jdt.core.dom.InstanceofExpression;
31
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
27
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
32
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
28
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
33
29
34
import org.eclipse.jdt.internal.corext.refactoring.code.OperatorPrecedence;
35
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
30
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
36
import org.eclipse.jdt.internal.corext.refactoring.util.NoCommentSourceRangeComputer;
31
import org.eclipse.jdt.internal.corext.refactoring.util.NoCommentSourceRangeComputer;
37
32
Lines 86-254 Link Here
86
		}
81
		}
87
82
88
		public boolean visit(ParenthesizedExpression node) {
83
		public boolean visit(ParenthesizedExpression node) {
89
			if (canRemoveParenthesis(node)) {
84
			if (NecessaryParenthesesChecker.canRemoveParentheses(node)) {
90
				fNodes.add(node);
85
				fNodes.add(node);
91
			}
86
			}
92
87
93
			return true;
88
			return true;
94
		}
89
		}
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
	}
90
	}
253
91
254
	private static class AddParenthesisOperation extends CompilationUnitRewriteOperation {
92
	private static class AddParenthesisOperation extends CompilationUnitRewriteOperation {
Lines 385-389 Link Here
385
	protected ExpressionsFix(String name, CompilationUnit compilationUnit, CompilationUnitRewriteOperation[] fixRewriteOperations) {
223
	protected ExpressionsFix(String name, CompilationUnit compilationUnit, CompilationUnitRewriteOperation[] fixRewriteOperations) {
386
		super(name, compilationUnit, fixRewriteOperations);
224
		super(name, compilationUnit, fixRewriteOperations);
387
	}
225
	}
388
389
}
226
}
(-)core (+303 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.jdt.internal.corext.fix;
13
14
import java.util.Iterator;
15
16
import org.eclipse.jdt.core.dom.ASTNode;
17
import org.eclipse.jdt.core.dom.ArrayAccess;
18
import org.eclipse.jdt.core.dom.AssertStatement;
19
import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
20
import org.eclipse.jdt.core.dom.ConditionalExpression;
21
import org.eclipse.jdt.core.dom.DoStatement;
22
import org.eclipse.jdt.core.dom.EnhancedForStatement;
23
import org.eclipse.jdt.core.dom.Expression;
24
import org.eclipse.jdt.core.dom.ForStatement;
25
import org.eclipse.jdt.core.dom.ITypeBinding;
26
import org.eclipse.jdt.core.dom.IfStatement;
27
import org.eclipse.jdt.core.dom.InfixExpression;
28
import org.eclipse.jdt.core.dom.InfixExpression.Operator;
29
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
30
import org.eclipse.jdt.core.dom.ReturnStatement;
31
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
32
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
33
import org.eclipse.jdt.core.dom.SwitchCase;
34
import org.eclipse.jdt.core.dom.SwitchStatement;
35
import org.eclipse.jdt.core.dom.SynchronizedStatement;
36
import org.eclipse.jdt.core.dom.ThrowStatement;
37
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
38
import org.eclipse.jdt.core.dom.WhileStatement;
39
40
import org.eclipse.jdt.internal.corext.refactoring.code.OperatorPrecedence;
41
42
/**
43
 * Helper class to check if an expression requires parentheses.
44
 * 
45
 * @since 3.7
46
 */
47
public class NecessaryParenthesesChecker {
48
49
	/*
50
	 * Get the expression wrapped by the parentheses
51
	 * i.e. ((((expression)))) -> expression
52
	 */
53
	private static Expression getExpression(ParenthesizedExpression node) {
54
		Expression expression= node.getExpression();
55
		while (expression instanceof ParenthesizedExpression) {
56
			expression= ((ParenthesizedExpression)expression).getExpression();
57
		}
58
		return expression;
59
	}
60
61
	private static boolean expressionTypeNeedsParentheses(Expression expression) {
62
		int type= expression.getNodeType();
63
		return type == ASTNode.INFIX_EXPRESSION
64
				|| type == ASTNode.CONDITIONAL_EXPRESSION
65
				|| type == ASTNode.PREFIX_EXPRESSION
66
				|| type == ASTNode.POSTFIX_EXPRESSION
67
				|| type == ASTNode.CAST_EXPRESSION
68
				|| type == ASTNode.INSTANCEOF_EXPRESSION
69
				|| type == ASTNode.ARRAY_CREATION
70
				|| type == ASTNode.ASSIGNMENT;
71
	}
72
73
	private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) {
74
		if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) {
75
			// e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation ...
76
			return false;
77
		}
78
		if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
79
				|| locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
80
				|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY
81
				|| locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY
82
				|| locationInParent == ForStatement.EXPRESSION_PROPERTY
83
				|| locationInParent == WhileStatement.EXPRESSION_PROPERTY
84
				|| locationInParent == DoStatement.EXPRESSION_PROPERTY
85
				|| locationInParent == AssertStatement.EXPRESSION_PROPERTY
86
				|| locationInParent == AssertStatement.MESSAGE_PROPERTY
87
				|| locationInParent == IfStatement.EXPRESSION_PROPERTY
88
				|| locationInParent == SwitchStatement.EXPRESSION_PROPERTY
89
				|| locationInParent == SwitchCase.EXPRESSION_PROPERTY
90
				|| locationInParent == ArrayAccess.INDEX_PROPERTY
91
				|| locationInParent == ThrowStatement.EXPRESSION_PROPERTY
92
				|| locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY) {
93
			return false;
94
		}
95
		return true;
96
	}
97
98
	/*
99
	 * Do all operands in expression have same type
100
	 */
101
	private static boolean isAllOperandsHaveSameType(InfixExpression expression) {
102
		ITypeBinding binding= expression.getLeftOperand().resolveTypeBinding();
103
		if (binding == null)
104
			return false;
105
	
106
		ITypeBinding current= expression.getRightOperand().resolveTypeBinding();
107
		if (binding != current)
108
			return false;
109
	
110
		for (Iterator iterator= expression.extendedOperands().iterator(); iterator.hasNext();) {
111
			Expression operand= (Expression) iterator.next();
112
			current= operand.resolveTypeBinding();
113
			if (binding != current)
114
				return false;
115
		}
116
	
117
		return true;
118
	}
119
120
	/*
121
	 * Is the expression of integer type
122
	 */
123
	private static boolean isExpressionIntegerType(Expression expression) {
124
		ITypeBinding binding= expression.resolveTypeBinding();
125
		if (binding == null)
126
			return false;
127
128
		if (!binding.isPrimitive())
129
			return false;
130
131
		String name= binding.getName();
132
		if (isIntegerNumber(name))
133
			return true;
134
135
		return false;
136
	}
137
138
	private static boolean isExpressionStringType(Expression expression) {
139
		ITypeBinding binding= expression.resolveTypeBinding();
140
		if (binding == null)
141
			return false;
142
143
		return "java.lang.String".equals(binding.getQualifiedName()); //$NON-NLS-1$
144
	}
145
146
	/*
147
	 * Is the given expression associative?
148
	 * 
149
	 * This is true if and only if:<br>
150
	 * <code>left operator (right) == (right) operator left == right operator left</code>
151
	 */
152
	private static boolean isAssociative(InfixExpression expression) {
153
		Operator operator= expression.getOperator();
154
155
		if (operator == InfixExpression.Operator.PLUS)
156
			return isExpressionStringType(expression) || isExpressionIntegerType(expression) && isAllOperandsHaveSameType(expression);
157
158
		if (operator == InfixExpression.Operator.TIMES)
159
			return isExpressionIntegerType(expression) && isAllOperandsHaveSameType(expression);
160
161
		if (operator == InfixExpression.Operator.CONDITIONAL_AND
162
				|| operator == InfixExpression.Operator.CONDITIONAL_OR
163
				|| operator == InfixExpression.Operator.AND
164
				|| operator == InfixExpression.Operator.OR
165
				|| operator == InfixExpression.Operator.XOR)
166
			return true;
167
	
168
		return false;
169
	}
170
171
	private static boolean isIntegerNumber(String name) {
172
		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$
173
	}
174
175
	private static boolean needsParenthesesInInfixExpression(Expression expression, InfixExpression parentInfix, StructuralPropertyDescriptor locationInParent) {
176
		if (locationInParent == InfixExpression.LEFT_OPERAND_PROPERTY) {
177
			//we have (expr op expr) op expr
178
			//infix expressions are evaluated from left to right -> parentheses not needed
179
			return false;
180
		} else if (isAssociative(parentInfix)) {
181
			//we have parent op (expr op expr) and op is associative
182
			//left op (right) == (right) op left == right op left
183
			if (expression instanceof InfixExpression) {
184
				InfixExpression infixExpression= (InfixExpression)expression;
185
				Operator operator= infixExpression.getOperator();
186
187
				if (isExpressionStringType(parentInfix)) {
188
					if (parentInfix.getOperator() == InfixExpression.Operator.PLUS && operator == InfixExpression.Operator.PLUS && isExpressionStringType(infixExpression)) {
189
						// 1 + ("" + 2) == 1 + "" + 2
190
						// 1 + (2 + "") != 1 + 2 + ""
191
						// "" + (2 + "") == "" + 2 + ""
192
						return !isExpressionStringType(infixExpression.getLeftOperand()) && !isExpressionStringType(parentInfix.getLeftOperand());
193
					}
194
					//"" + (1 + 2), "" + (1 - 2) etc
195
					return true;
196
				}
197
198
				if (parentInfix.getOperator() != InfixExpression.Operator.TIMES)
199
					return false;
200
	
201
				if (operator == InfixExpression.Operator.TIMES)
202
					// x * (y * z) == x * y * z
203
					return false;
204
	
205
				if (operator == InfixExpression.Operator.REMAINDER || operator == InfixExpression.Operator.DIVIDE)
206
					// x * (y % z) != x * y % z , x * (y / z) == x * y / z rounding involved
207
					return true;
208
	
209
				return false;
210
			}
211
			return false;
212
		} else {
213
			return true;
214
		}
215
	}
216
217
	/**
218
	 * Can the parentheses be removed from the parenthesized expression ?
219
	 * 
220
	 * <p>
221
	 * <b>Note:</b> The parenthesized expression must not be an unparented node.
222
	 * </p>
223
	 * 
224
	 * @param expression the parenthesized expression
225
	 * @return <code>true</code> if parentheses can be removed, <code>false</code> otherwise.
226
	 */
227
	public static boolean canRemoveParentheses(Expression expression) {
228
		return canRemoveParentheses(expression, expression.getParent(), expression.getLocationInParent());
229
	}
230
231
	/**
232
	 * Can the parentheses be removed from the parenthesized expression when inserted into
233
	 * <code>parent</code> at <code>locationInParent</code> ?
234
	 * 
235
	 * <p>
236
	 * <b>Note:</b> The parenthesized expression can be an unparented node.
237
	 * </p>
238
	 * 
239
	 * @param expression the parenthesized expression
240
	 * @param parent the parent node
241
	 * @param locationInParent location of expression in the parent
242
	 * @return <code>true</code> if parentheses can be removed, <code>false</code> otherwise.
243
	 */
244
	public static boolean canRemoveParentheses(Expression expression, ASTNode parent, StructuralPropertyDescriptor locationInParent) {
245
		if (!(expression instanceof ParenthesizedExpression)) {
246
			return false;
247
		}
248
		return !needsParentheses(getExpression((ParenthesizedExpression)expression), parent, locationInParent);
249
	}
250
251
	/**
252
	 * Does the <code>expression</code> need parentheses when inserted into <code>parent</code> at
253
	 * <code>locationInParent</code> ?
254
	 * 
255
	 * <p>
256
	 * <b>Note:</b> The expression can be an unparented node.
257
	 * </p>
258
	 * 
259
	 * @param expression the expression
260
	 * @param parent the parent node
261
	 * @param locationInParent location of expression in the parent
262
	 * @return <code>true</code> if the expression needs parentheses, <code>false</code> otherwise.
263
	 */
264
	public static boolean needsParentheses(Expression expression, ASTNode parent, StructuralPropertyDescriptor locationInParent) {
265
		if (!expressionTypeNeedsParentheses(expression))
266
			return false;
267
268
		if (!locationNeedsParentheses(locationInParent)) {
269
			return false;
270
		}
271
272
		if (parent instanceof Expression) {
273
			Expression parentExpression= (Expression)parent;
274
			if (parentExpression instanceof ParenthesizedExpression)
275
				return false;
276
277
			int expressionPrecedence= OperatorPrecedence.getExpressionPrecedence(expression);
278
			int parentPrecedence= OperatorPrecedence.getExpressionPrecedence(parentExpression);
279
280
			if (expressionPrecedence > parentPrecedence)
281
				//(opEx) opParent and opEx binds more -> parentheses not needed
282
				return false;
283
284
			if (expressionPrecedence < parentPrecedence)
285
				//(opEx) opParent and opEx binds less -> parentheses needed
286
				return true;
287
288
			//(opEx) opParent binds equal
289
290
			if (parentExpression instanceof InfixExpression) {
291
				return needsParenthesesInInfixExpression(expression, (InfixExpression)parentExpression, locationInParent);
292
			}
293
294
			if (parentExpression instanceof ConditionalExpression && locationInParent == ConditionalExpression.EXPRESSION_PROPERTY) {
295
				return true;
296
			}
297
298
			return false;
299
		}
300
301
		return true;
302
	}
303
}
(-)core extension/org/eclipse/jdt/internal/corext/fix/UnusedCodeFix.java (-5 / +10 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 435-444 Link Here
435
			Expression expression= cast.getExpression();
435
			Expression expression= cast.getExpression();
436
			ASTNode placeholder= rewrite.createCopyTarget(expression);
436
			ASTNode placeholder= rewrite.createCopyTarget(expression);
437
437
438
			if (ASTNodes.needsParentheses(expression)) {
438
			if (NecessaryParenthesesChecker.needsParentheses(expression, cast.getParent(), cast.getLocationInParent())) {
439
				rewrite.replace(fCast, placeholder, group);
439
				rewrite.replace(fCast, placeholder, group);
440
			} else {
440
			} else {
441
				rewrite.replace(fSelectedNode, placeholder, group);
441
				rewrite.replace(fCast.getParent() instanceof ParenthesizedExpression ? fCast.getParent() : fSelectedNode, placeholder, group);
442
			}
442
			}
443
		}
443
		}
444
	}
444
	}
Lines 468-474 Link Here
468
					fUnnecessaryCasts.remove(down);
468
					fUnnecessaryCasts.remove(down);
469
				}
469
				}
470
470
471
				ASTNode move= rewrite.createMoveTarget(down.getExpression());
471
				Expression expression= down.getExpression();
472
				ASTNode move= rewrite.createMoveTarget(expression);
472
473
473
				CastExpression top= castExpression;
474
				CastExpression top= castExpression;
474
				while (fUnnecessaryCasts.contains(top.getParent())) {
475
				while (fUnnecessaryCasts.contains(top.getParent())) {
Lines 476-482 Link Here
476
					fUnnecessaryCasts.remove(top);
477
					fUnnecessaryCasts.remove(top);
477
				}
478
				}
478
479
479
				rewrite.replace(top, move, group);
480
				ASTNode toReplace= top;
481
				if (top.getParent() instanceof ParenthesizedExpression && !NecessaryParenthesesChecker.needsParentheses(expression, top.getParent(), top.getLocationInParent())) {
482
					toReplace= top.getParent();
483
				}
484
				rewrite.replace(toReplace, move, group);
480
			}
485
			}
481
		}
486
		}
482
	}
487
	}
(-)core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/CallInliner.java (-15 / +2 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 646-652 Link Here
646
					node= castExpression;
646
					node= castExpression;
647
				}
647
				}
648
648
649
				if (needsParenthesis()) {
649
				if (fSourceProvider.needsReturnedExpressionParenthesis(fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
650
					ParenthesizedExpression pExp= fTargetNode.getAST().newParenthesizedExpression();
650
					ParenthesizedExpression pExp= fTargetNode.getAST().newParenthesizedExpression();
651
					pExp.setExpression((Expression)node);
651
					pExp.setExpression((Expression)node);
652
					node= pExp;
652
					node= pExp;
Lines 721-739 Link Here
721
		return false;
721
		return false;
722
	}
722
	}
723
723
724
	private boolean needsParenthesis() {
725
		if (!fSourceProvider.needsReturnedExpressionParenthesis())
726
			return false;
727
		ASTNode parent= fTargetNode.getParent();
728
		int type= parent.getNodeType();
729
		return
730
			type == ASTNode.METHOD_INVOCATION ||
731
			(parent instanceof Expression && type != ASTNode.ASSIGNMENT) ||
732
			(fSourceProvider.returnsConditionalExpression() &&
733
				type == ASTNode.VARIABLE_DECLARATION_FRAGMENT &&
734
				((VariableDeclarationFragment)parent).getInitializer() == fTargetNode);
735
	}
736
737
	private VariableDeclarationStatement createLocalDeclaration(ITypeBinding type, String name, Expression initializer) {
724
	private VariableDeclarationStatement createLocalDeclaration(ITypeBinding type, String name, Expression initializer) {
738
		ImportRewriteContext context= new ContextSensitiveImportRewriteContext(fTargetNode, fImportRewrite);
725
		ImportRewriteContext context= new ContextSensitiveImportRewriteContext(fTargetNode, fImportRewrite);
739
		String typeName= fImportRewrite.addImport(type, context);
726
		String typeName= fImportRewrite.addImport(type, context);
(-)core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractConstantRefactoring.java (-3 / +3 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 492-503 Link Here
492
		Type type= getConstantType();
492
		Type type= getConstantType();
493
493
494
		IExpressionFragment fragment= getSelectedExpression();
494
		IExpressionFragment fragment= getSelectedExpression();
495
		String initializerSource= fCu.getBuffer().getText(fragment.getStartPosition(), fragment.getLength());
495
		Expression initializer= getSelectedExpression().createCopyTarget(fCuRewrite.getASTRewrite(), true);
496
496
497
		AST ast= fCuRewrite.getAST();
497
		AST ast= fCuRewrite.getAST();
498
		VariableDeclarationFragment variableDeclarationFragment= ast.newVariableDeclarationFragment();
498
		VariableDeclarationFragment variableDeclarationFragment= ast.newVariableDeclarationFragment();
499
		variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
499
		variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
500
		variableDeclarationFragment.setInitializer((Expression) fCuRewrite.getASTRewrite().createStringPlaceholder(initializerSource, ASTNode.SIMPLE_NAME));
500
		variableDeclarationFragment.setInitializer(initializer);
501
501
502
		FieldDeclaration fieldDeclaration= ast.newFieldDeclaration(variableDeclarationFragment);
502
		FieldDeclaration fieldDeclaration= ast.newFieldDeclaration(variableDeclarationFragment);
503
		fieldDeclaration.setType(type);
503
		fieldDeclaration.setType(type);
(-)core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractMethodRefactoring.java (-4 / +14 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 75-80 Link Here
75
import org.eclipse.jdt.core.dom.MethodDeclaration;
75
import org.eclipse.jdt.core.dom.MethodDeclaration;
76
import org.eclipse.jdt.core.dom.MethodInvocation;
76
import org.eclipse.jdt.core.dom.MethodInvocation;
77
import org.eclipse.jdt.core.dom.Modifier;
77
import org.eclipse.jdt.core.dom.Modifier;
78
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
78
import org.eclipse.jdt.core.dom.QualifiedName;
79
import org.eclipse.jdt.core.dom.QualifiedName;
79
import org.eclipse.jdt.core.dom.ReturnStatement;
80
import org.eclipse.jdt.core.dom.ReturnStatement;
80
import org.eclipse.jdt.core.dom.SimpleName;
81
import org.eclipse.jdt.core.dom.SimpleName;
Lines 909-915 Link Here
909
			if (!duplicate.isMethodBody()) {
910
			if (!duplicate.isMethodBody()) {
910
				if (isDestinationReachable(duplicate.getEnclosingMethod())) {
911
				if (isDestinationReachable(duplicate.getEnclosingMethod())) {
911
					ASTNode[] callNodes= createCallNodes(duplicate, modifiers);
912
					ASTNode[] callNodes= createCallNodes(duplicate, modifiers);
912
					new StatementRewrite(fRewriter, duplicate.getNodes()).replace(callNodes, description);
913
					ASTNode[] duplicateNodes= duplicate.getNodes();
914
					for (int i= 0; i < duplicateNodes.length; i++) {
915
						ASTNode parent= duplicateNodes[i].getParent();
916
						if (parent instanceof ParenthesizedExpression) {
917
							duplicateNodes[i]= parent;
918
						}
919
					}
920
					new StatementRewrite(fRewriter, duplicateNodes).replace(callNodes, description);
913
				}
921
				}
914
			}
922
			}
915
		}
923
		}
Lines 1059-1071 Link Here
1059
			ITypeBinding binding= fAnalyzer.getExpressionBinding();
1067
			ITypeBinding binding= fAnalyzer.getExpressionBinding();
1060
			if (binding != null && (!binding.isPrimitive() || !"void".equals(binding.getName()))) { //$NON-NLS-1$
1068
			if (binding != null && (!binding.isPrimitive() || !"void".equals(binding.getName()))) { //$NON-NLS-1$
1061
				ReturnStatement rs= fAST.newReturnStatement();
1069
				ReturnStatement rs= fAST.newReturnStatement();
1062
				rs.setExpression((Expression)fRewriter.createMoveTarget(selectedNodes[0]));
1070
				rs.setExpression((Expression)fRewriter.createMoveTarget(selectedNodes[0] instanceof ParenthesizedExpression
1071
						? ((ParenthesizedExpression)selectedNodes[0]).getExpression()
1072
						: selectedNodes[0]));
1063
				statements.insertLast(rs, null);
1073
				statements.insertLast(rs, null);
1064
			} else {
1074
			} else {
1065
				ExpressionStatement st= fAST.newExpressionStatement((Expression)fRewriter.createMoveTarget(selectedNodes[0]));
1075
				ExpressionStatement st= fAST.newExpressionStatement((Expression)fRewriter.createMoveTarget(selectedNodes[0]));
1066
				statements.insertLast(st, null);
1076
				statements.insertLast(st, null);
1067
			}
1077
			}
1068
			fRewriter.replace(selectedNodes[0], replacementNode, substitute);
1078
			fRewriter.replace(selectedNodes[0].getParent() instanceof ParenthesizedExpression ? selectedNodes[0].getParent() : selectedNodes[0], replacementNode, substitute);
1069
		} else {
1079
		} else {
1070
			if (selectedNodes.length == 1) {
1080
			if (selectedNodes.length == 1) {
1071
				statements.insertLast(fRewriter.createMoveTarget(selectedNodes[0]), substitute);
1081
				statements.insertLast(fRewriter.createMoveTarget(selectedNodes[0]), substitute);
(-)core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/InlineConstantRefactoring.java (-4 / +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 83-90 Link Here
83
import org.eclipse.jdt.core.dom.VariableDeclaration;
83
import org.eclipse.jdt.core.dom.VariableDeclaration;
84
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
84
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
85
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
85
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
86
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
87
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
86
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
87
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
88
import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
88
import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
89
import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
89
import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
90
import org.eclipse.jdt.core.refactoring.descriptors.InlineConstantDescriptor;
90
import org.eclipse.jdt.core.refactoring.descriptors.InlineConstantDescriptor;
Lines 102-107 Link Here
102
import org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor;
102
import org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor;
103
import org.eclipse.jdt.internal.corext.dom.fragments.ASTFragmentFactory;
103
import org.eclipse.jdt.internal.corext.dom.fragments.ASTFragmentFactory;
104
import org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment;
104
import org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment;
105
import org.eclipse.jdt.internal.corext.fix.NecessaryParenthesesChecker;
105
import org.eclipse.jdt.internal.corext.refactoring.Checks;
106
import org.eclipse.jdt.internal.corext.refactoring.Checks;
106
import org.eclipse.jdt.internal.corext.refactoring.IRefactoringSearchRequestor;
107
import org.eclipse.jdt.internal.corext.refactoring.IRefactoringSearchRequestor;
107
import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment;
108
import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment;
Lines 501-507 Link Here
501
			if (explicitCast != null) {
502
			if (explicitCast != null) {
502
				CastExpression cast= ast.newCastExpression();
503
				CastExpression cast= ast.newCastExpression();
503
				Expression modifiedInitializerExpr= (Expression) fCuRewrite.getASTRewrite().createStringPlaceholder(modifiedInitializer, reference.getNodeType());
504
				Expression modifiedInitializerExpr= (Expression) fCuRewrite.getASTRewrite().createStringPlaceholder(modifiedInitializer, reference.getNodeType());
504
				if (ASTNodes.substituteMustBeParenthesized(fInitializer, cast)) {
505
				if (NecessaryParenthesesChecker.needsParentheses(fInitializer, cast, CastExpression.EXPRESSION_PROPERTY)) {
505
					ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
506
					ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
506
					parenthesized.setExpression(modifiedInitializerExpr);
507
					parenthesized.setExpression(modifiedInitializerExpr);
507
					modifiedInitializerExpr= parenthesized;
508
					modifiedInitializerExpr= parenthesized;
Lines 531-537 Link Here
531
				isStringPlaceholder= true;
532
				isStringPlaceholder= true;
532
			}
533
			}
533
534
534
			if (ASTNodes.substituteMustBeParenthesized((isStringPlaceholder ? fInitializer : newReference), reference)) {
535
			if (NecessaryParenthesesChecker.needsParentheses((isStringPlaceholder ? fInitializer : newReference), reference.getParent(), reference.getLocationInParent())) {
535
				ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
536
				ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
536
				parenthesized.setExpression(newReference);
537
				parenthesized.setExpression(newReference);
537
				newReference= parenthesized;
538
				newReference= parenthesized;
(-)core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/InlineTempRefactoring.java (-4 / +4 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 79-84 Link Here
79
import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory;
79
import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory;
80
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
80
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
81
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
81
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
82
import org.eclipse.jdt.internal.corext.fix.NecessaryParenthesesChecker;
82
import org.eclipse.jdt.internal.corext.refactoring.Checks;
83
import org.eclipse.jdt.internal.corext.refactoring.Checks;
83
import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment;
84
import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment;
84
import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
85
import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
Lines 333-340 Link Here
333
334
334
	private Expression getInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException {
335
	private Expression getInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException {
335
		Expression copy= getModifiedInitializerSource(rewrite, reference);
336
		Expression copy= getModifiedInitializerSource(rewrite, reference);
336
		boolean brackets= ASTNodes.substituteMustBeParenthesized(copy, reference);
337
		if (NecessaryParenthesesChecker.needsParentheses(copy, reference.getParent(), reference.getLocationInParent())) {
337
		if (brackets) {
338
			ParenthesizedExpression parentExpr= rewrite.getAST().newParenthesizedExpression();
338
			ParenthesizedExpression parentExpr= rewrite.getAST().newParenthesizedExpression();
339
			parentExpr.setExpression(copy);
339
			parentExpr.setExpression(copy);
340
			return parentExpr;
340
			return parentExpr;
Lines 367-373 Link Here
367
		ITypeBinding explicitCast= ASTNodes.getExplicitCast(initializer, reference);
367
		ITypeBinding explicitCast= ASTNodes.getExplicitCast(initializer, reference);
368
		if (explicitCast != null) {
368
		if (explicitCast != null) {
369
			CastExpression cast= ast.newCastExpression();
369
			CastExpression cast= ast.newCastExpression();
370
			if (ASTNodes.substituteMustBeParenthesized(copy, cast)) {
370
			if (NecessaryParenthesesChecker.needsParentheses(initializer, cast, CastExpression.EXPRESSION_PROPERTY)) {
371
				ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
371
				ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
372
				parenthesized.setExpression(copy);
372
				parenthesized.setExpression(copy);
373
				copy= parenthesized;
373
				copy= parenthesized;
(-)core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/IntroduceParameterRefactoring.java (-3 / +6 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 55-60 Link Here
55
import org.eclipse.jdt.core.dom.Name;
55
import org.eclipse.jdt.core.dom.Name;
56
import org.eclipse.jdt.core.dom.NodeFinder;
56
import org.eclipse.jdt.core.dom.NodeFinder;
57
import org.eclipse.jdt.core.dom.NullLiteral;
57
import org.eclipse.jdt.core.dom.NullLiteral;
58
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
58
import org.eclipse.jdt.core.dom.QualifiedName;
59
import org.eclipse.jdt.core.dom.QualifiedName;
59
import org.eclipse.jdt.core.dom.SimpleName;
60
import org.eclipse.jdt.core.dom.SimpleName;
60
import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
61
import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
Lines 254-260 Link Here
254
		ITypeBinding typeBinding= Bindings.normalizeForDeclarationUse(fSelectedExpression.resolveTypeBinding(), fSelectedExpression.getAST());
255
		ITypeBinding typeBinding= Bindings.normalizeForDeclarationUse(fSelectedExpression.resolveTypeBinding(), fSelectedExpression.getAST());
255
		String typeName= cuRewrite.getImportRewrite().addImport(typeBinding);
256
		String typeName= cuRewrite.getImportRewrite().addImport(typeBinding);
256
		String name= fParameterName != null ? fParameterName : guessedParameterName();
257
		String name= fParameterName != null ? fParameterName : guessedParameterName();
257
		String defaultValue= fSourceCU.getBuffer().getText(fSelectedExpression.getStartPosition(), fSelectedExpression.getLength());
258
		Expression expression= fSelectedExpression instanceof ParenthesizedExpression ? ((ParenthesizedExpression)fSelectedExpression).getExpression() : fSelectedExpression;
259
		String defaultValue= fSourceCU.getBuffer().getText(expression.getStartPosition(), expression.getLength());
258
		fParameter= ParameterInfo.createInfoForAddedParameter(typeBinding, typeName, name, defaultValue);
260
		fParameter= ParameterInfo.createInfoForAddedParameter(typeBinding, typeName, name, defaultValue);
259
		if (fArguments == null) {
261
		if (fArguments == null) {
260
			List parameterInfos= fChangeSignatureProcessor.getParameterInfos();
262
			List parameterInfos= fChangeSignatureProcessor.getParameterInfos();
Lines 276-282 Link Here
276
278
277
		ASTNode newExpression= cuRewrite.getRoot().getAST().newSimpleName(fParameter.getNewName());
279
		ASTNode newExpression= cuRewrite.getRoot().getAST().newSimpleName(fParameter.getNewName());
278
		String description= RefactoringCoreMessages.IntroduceParameterRefactoring_replace;
280
		String description= RefactoringCoreMessages.IntroduceParameterRefactoring_replace;
279
		cuRewrite.getASTRewrite().replace(expression, newExpression, cuRewrite.createGroupDescription(description));
281
		cuRewrite.getASTRewrite().replace(expression.getParent() instanceof ParenthesizedExpression
282
				? expression.getParent() : expression, newExpression, cuRewrite.createGroupDescription(description));
280
	}
283
	}
281
284
282
	private void initializeSelectedExpression(CompilationUnitRewrite cuRewrite) throws JavaModelException {
285
	private void initializeSelectedExpression(CompilationUnitRewrite cuRewrite) throws JavaModelException {
(-)core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/SourceProvider.java (-17 / +9 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 48-54 Link Here
48
import org.eclipse.jdt.core.dom.AST;
48
import org.eclipse.jdt.core.dom.AST;
49
import org.eclipse.jdt.core.dom.ASTNode;
49
import org.eclipse.jdt.core.dom.ASTNode;
50
import org.eclipse.jdt.core.dom.ASTVisitor;
50
import org.eclipse.jdt.core.dom.ASTVisitor;
51
import org.eclipse.jdt.core.dom.ArrayCreation;
52
import org.eclipse.jdt.core.dom.Block;
51
import org.eclipse.jdt.core.dom.Block;
53
import org.eclipse.jdt.core.dom.CastExpression;
52
import org.eclipse.jdt.core.dom.CastExpression;
54
import org.eclipse.jdt.core.dom.ChildPropertyDescriptor;
53
import org.eclipse.jdt.core.dom.ChildPropertyDescriptor;
Lines 75-80 Link Here
75
import org.eclipse.jdt.core.dom.SimpleName;
74
import org.eclipse.jdt.core.dom.SimpleName;
76
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
75
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
77
import org.eclipse.jdt.core.dom.Statement;
76
import org.eclipse.jdt.core.dom.Statement;
77
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
78
import org.eclipse.jdt.core.dom.ThisExpression;
78
import org.eclipse.jdt.core.dom.ThisExpression;
79
import org.eclipse.jdt.core.dom.WhileStatement;
79
import org.eclipse.jdt.core.dom.WhileStatement;
80
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
80
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
Lines 85-90 Link Here
85
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
85
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
86
import org.eclipse.jdt.internal.corext.dom.Bindings;
86
import org.eclipse.jdt.internal.corext.dom.Bindings;
87
import org.eclipse.jdt.internal.corext.dom.CodeScopeBuilder;
87
import org.eclipse.jdt.internal.corext.dom.CodeScopeBuilder;
88
import org.eclipse.jdt.internal.corext.fix.NecessaryParenthesesChecker;
88
import org.eclipse.jdt.internal.corext.refactoring.code.SourceAnalyzer.NameData;
89
import org.eclipse.jdt.internal.corext.refactoring.code.SourceAnalyzer.NameData;
89
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringFileBuffers;
90
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringFileBuffers;
90
import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
91
import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
Lines 314-323 Link Here
314
		return fTypeRoot;
315
		return fTypeRoot;
315
	}
316
	}
316
317
317
	public boolean needsReturnedExpressionParenthesis() {
318
	public boolean needsReturnedExpressionParenthesis(ASTNode parent, StructuralPropertyDescriptor locationInParent) {
318
		ASTNode last= getLastStatement();
319
		ASTNode last= getLastStatement();
319
		if (last instanceof ReturnStatement) {
320
		if (last instanceof ReturnStatement) {
320
			return ASTNodes.needsParentheses(((ReturnStatement)last).getExpression());
321
			return NecessaryParenthesesChecker.needsParentheses(((ReturnStatement)last).getExpression(), parent, locationInParent);
321
		}
322
		}
322
		return false;
323
		return false;
323
	}
324
	}
Lines 413-428 Link Here
413
		return new String[] {};
414
		return new String[] {};
414
	}
415
	}
415
416
416
	private boolean argumentNeedsParenthesis(Expression expression, ParameterData param) {
417
		if (expression instanceof CastExpression || expression instanceof ArrayCreation)
418
			return true;
419
		int argPrecedence= OperatorPrecedence.getExpressionPrecedence(expression);
420
		int paramPrecedence= param.getOperatorPrecedence();
421
		if (argPrecedence != Integer.MAX_VALUE && paramPrecedence != Integer.MAX_VALUE)
422
			return argPrecedence <= paramPrecedence;
423
		return false;
424
	}
425
426
	private Expression createParenthesizedExpression(Expression newExpression, AST ast) {
417
	private Expression createParenthesizedExpression(Expression newExpression, AST ast) {
427
		ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
418
		ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
428
		parenthesized.setExpression(newExpression);
419
		parenthesized.setExpression(newExpression);
Lines 455-468 Link Here
455
					ITypeBinding explicitCast= ASTNodes.getExplicitCast(expression, (Expression)element);
446
					ITypeBinding explicitCast= ASTNodes.getExplicitCast(expression, (Expression)element);
456
					if (explicitCast != null) {
447
					if (explicitCast != null) {
457
						CastExpression cast= ast.newCastExpression();
448
						CastExpression cast= ast.newCastExpression();
458
						if (ASTNodes.substituteMustBeParenthesized(newExpression, cast)) {
449
						if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
459
							newExpression= createParenthesizedExpression(newExpression, ast);
450
							newExpression= createParenthesizedExpression(newExpression, ast);
460
						}
451
						}
461
						cast.setExpression(newExpression);
452
						cast.setExpression(newExpression);
462
						ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(expression, importRewrite);
453
						ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(expression, importRewrite);
463
						cast.setType(importRewrite.addImport(explicitCast, ast, importRewriteContext));
454
						cast.setType(importRewrite.addImport(explicitCast, ast, importRewriteContext));
464
						newExpression= createParenthesizedExpression(cast, ast);
455
						expression= newExpression= cast;
465
					} else if (argumentNeedsParenthesis(expression, parameter)) {
456
					}
457
					if (NecessaryParenthesesChecker.needsParentheses(expression, element.getParent(), element.getLocationInParent())) {
466
						newExpression= createParenthesizedExpression(newExpression, ast);
458
						newExpression= createParenthesizedExpression(newExpression, ast);
467
					}
459
					}
468
					rewriter.replace(element, newExpression, null);
460
					rewriter.replace(element, newExpression, null);
(-)core refactoring/org/eclipse/jdt/internal/corext/refactoring/sef/AccessAnalyzer.java (-3 / +4 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 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 51-56 Link Here
51
import org.eclipse.jdt.internal.corext.SourceRangeFactory;
51
import org.eclipse.jdt.internal.corext.SourceRangeFactory;
52
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
52
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
53
import org.eclipse.jdt.internal.corext.dom.Bindings;
53
import org.eclipse.jdt.internal.corext.dom.Bindings;
54
import org.eclipse.jdt.internal.corext.fix.NecessaryParenthesesChecker;
54
import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
55
import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
55
import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext;
56
import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext;
56
57
Lines 138-144 Link Here
138
				arguments.add(fRewriter.createCopyTarget(node.getRightHandSide()));
139
				arguments.add(fRewriter.createCopyTarget(node.getRightHandSide()));
139
			} else {
140
			} else {
140
				// This is the compound assignment case: field+= 10;
141
				// This is the compound assignment case: field+= 10;
141
				boolean needsParentheses= ASTNodes.needsParentheses(node.getRightHandSide());
142
				InfixExpression exp= ast.newInfixExpression();
142
				InfixExpression exp= ast.newInfixExpression();
143
				exp.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
143
				exp.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
144
				MethodInvocation getter= ast.newMethodInvocation();
144
				MethodInvocation getter= ast.newMethodInvocation();
Lines 148-154 Link Here
148
					getter.setExpression((Expression)fRewriter.createCopyTarget(receiver));
148
					getter.setExpression((Expression)fRewriter.createCopyTarget(receiver));
149
				exp.setLeftOperand(getter);
149
				exp.setLeftOperand(getter);
150
				Expression rhs= (Expression)fRewriter.createCopyTarget(node.getRightHandSide());
150
				Expression rhs= (Expression)fRewriter.createCopyTarget(node.getRightHandSide());
151
				if (needsParentheses) {
151
				if (NecessaryParenthesesChecker.needsParentheses(node.getRightHandSide(), exp, InfixExpression.RIGHT_OPERAND_PROPERTY)) {
152
					//TODO: this introduces extra parentheses as bindings cannot be resolved
152
					ParenthesizedExpression p= ast.newParenthesizedExpression();
153
					ParenthesizedExpression p= ast.newParenthesizedExpression();
153
					p.setExpression(rhs);
154
					p.setExpression(rhs);
154
					rhs= p;
155
					rhs= p;
(-)ui/org/eclipse/jdt/internal/ui/text/correction/AdvancedQuickAssistProcessor.java (-44 / +32 lines)
Lines 87-92 Link Here
87
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
87
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
88
import org.eclipse.jdt.internal.corext.fix.ExpressionsFix;
88
import org.eclipse.jdt.internal.corext.fix.ExpressionsFix;
89
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
89
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
90
import org.eclipse.jdt.internal.corext.fix.NecessaryParenthesesChecker;
90
import org.eclipse.jdt.internal.corext.refactoring.code.OperatorPrecedence;
91
import org.eclipse.jdt.internal.corext.refactoring.code.OperatorPrecedence;
91
import org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer;
92
import org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer;
92
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
93
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
Lines 523-529 Link Here
523
		if (expression instanceof PrefixExpression) {
524
		if (expression instanceof PrefixExpression) {
524
			PrefixExpression prefixExpression= (PrefixExpression) expression;
525
			PrefixExpression prefixExpression= (PrefixExpression) expression;
525
			if (prefixExpression.getOperator() == PrefixExpression.Operator.NOT) {
526
			if (prefixExpression.getOperator() == PrefixExpression.Operator.NOT) {
526
				return getRenamedNameCopy(provider, rewrite, prefixExpression.getOperand());
527
				Expression operand= prefixExpression.getOperand();
528
				if ((operand instanceof ParenthesizedExpression)
529
						&& NecessaryParenthesesChecker.canRemoveParentheses(operand, expression.getParent(), expression.getLocationInParent())) {
530
					operand= ((ParenthesizedExpression)operand).getExpression();
531
				}
532
				return getRenamedNameCopy(provider, rewrite, operand);
527
			}
533
			}
528
		}
534
		}
529
		if (expression instanceof InstanceofExpression) {
535
		if (expression instanceof InstanceofExpression) {
Lines 745-756 Link Here
745
				//
751
				//
746
				AST ast= node.getAST();
752
				AST ast= node.getAST();
747
				ASTRewrite rewrite= ASTRewrite.create(ast);
753
				ASTRewrite rewrite= ASTRewrite.create(ast);
748
				// prepare condition parts, add parentheses if needed
749
				Expression outerCondition= getParenthesizedForAndIfNeeded(ast, rewrite, outerIf.getExpression());
750
				Expression innerCondition= getParenthesizedForAndIfNeeded(ast, rewrite, ifStatement.getExpression());
751
				// create compound condition
754
				// create compound condition
752
				InfixExpression condition= ast.newInfixExpression();
755
				InfixExpression condition= ast.newInfixExpression();
753
				condition.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
756
				condition.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
757
				// prepare condition parts, add parentheses if needed
758
				Expression outerCondition= getParenthesizedExpressionIfNeeded(ast, rewrite, outerIf.getExpression(), condition, InfixExpression.LEFT_OPERAND_PROPERTY);
759
				Expression innerCondition= getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.RIGHT_OPERAND_PROPERTY);
754
				condition.setLeftOperand(outerCondition);
760
				condition.setLeftOperand(outerCondition);
755
				condition.setRightOperand(innerCondition);
761
				condition.setRightOperand(innerCondition);
756
				// create new IfStatement
762
				// create new IfStatement
Lines 784-795 Link Here
784
				//
790
				//
785
				AST ast= node.getAST();
791
				AST ast= node.getAST();
786
				ASTRewrite rewrite= ASTRewrite.create(ast);
792
				ASTRewrite rewrite= ASTRewrite.create(ast);
787
				// prepare condition parts, add parentheses if needed
788
				Expression outerCondition= getParenthesizedForAndIfNeeded(ast, rewrite, ifStatement.getExpression());
789
				Expression innerCondition= getParenthesizedForAndIfNeeded(ast, rewrite, innerIf.getExpression());
790
				// create compound condition
793
				// create compound condition
791
				InfixExpression condition= ast.newInfixExpression();
794
				InfixExpression condition= ast.newInfixExpression();
792
				condition.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
795
				condition.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
796
				// prepare condition parts, add parentheses if needed
797
				Expression outerCondition= getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.LEFT_OPERAND_PROPERTY);
798
				Expression innerCondition= getParenthesizedExpressionIfNeeded(ast, rewrite, innerIf.getExpression(), condition, InfixExpression.RIGHT_OPERAND_PROPERTY);
793
				condition.setLeftOperand(outerCondition);
799
				condition.setLeftOperand(outerCondition);
794
				condition.setRightOperand(innerCondition);
800
				condition.setRightOperand(innerCondition);
795
				// create new IfStatement
801
				// create new IfStatement
Lines 808-823 Link Here
808
		return true;
814
		return true;
809
	}
815
	}
810
816
811
	private static Expression getParenthesizedForAndIfNeeded(AST ast, ASTRewrite rewrite, Expression expression) {
817
	private static Expression getParenthesizedExpressionIfNeeded(AST ast, ASTRewrite rewrite, Expression expression, ASTNode parent, StructuralPropertyDescriptor locationInParent) {
812
		boolean addParentheses= false;
818
		boolean addParentheses= NecessaryParenthesesChecker.needsParentheses(expression, parent, locationInParent);
813
		int nodeType= expression.getNodeType();
819
		expression= (Expression)rewrite.createCopyTarget(expression);
814
		if (nodeType == ASTNode.INFIX_EXPRESSION) {
815
			InfixExpression infixExpression= (InfixExpression) expression;
816
			addParentheses= infixExpression.getOperator() == InfixExpression.Operator.CONDITIONAL_OR;
817
		} else {
818
			addParentheses= nodeType == ASTNode.CONDITIONAL_EXPRESSION || nodeType == ASTNode.ASSIGNMENT || nodeType == ASTNode.INSTANCEOF_EXPRESSION;
819
		}
820
		expression= (Expression) rewrite.createCopyTarget(expression);
821
		if (addParentheses) {
820
		if (addParentheses) {
822
			return getParenthesizedExpression(ast, expression);
821
			return getParenthesizedExpression(ast, expression);
823
		}
822
		}
Lines 976-994 Link Here
976
			IfStatement ifStatement= (IfStatement) iter.next();
975
			IfStatement ifStatement= (IfStatement) iter.next();
977
			if (thenStatement == null)
976
			if (thenStatement == null)
978
				thenStatement= (Statement) rewrite.createCopyTarget(ifStatement.getThenStatement());
977
				thenStatement= (Statement) rewrite.createCopyTarget(ifStatement.getThenStatement());
979
			Expression ifCondition= getParenthesizedForOrIfNeeded(ast, rewrite, ifStatement.getExpression());
980
			if (condition == null) {
978
			if (condition == null) {
981
				condition= ast.newInfixExpression();
979
				condition= ast.newInfixExpression();
982
				condition.setOperator(orOperator);
980
				condition.setOperator(orOperator);
983
				condition.setLeftOperand(ifCondition);
981
				condition.setLeftOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.LEFT_OPERAND_PROPERTY));
984
			} else if (!hasRightOperand) {
982
			} else if (!hasRightOperand) {
985
				condition.setRightOperand(ifCondition);
983
				condition.setRightOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.RIGHT_OPERAND_PROPERTY));
986
				hasRightOperand= true;
984
				hasRightOperand= true;
987
			} else {
985
			} else {
988
				InfixExpression newCondition= ast.newInfixExpression();
986
				InfixExpression newCondition= ast.newInfixExpression();
989
				newCondition.setOperator(orOperator);
987
				newCondition.setOperator(orOperator);
990
				newCondition.setLeftOperand(condition);
988
				newCondition.setLeftOperand(condition);
991
				newCondition.setRightOperand(ifCondition);
989
				newCondition.setRightOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.RIGHT_OPERAND_PROPERTY));
992
				condition= newCondition;
990
				condition= newCondition;
993
			}
991
			}
994
		}
992
		}
Lines 1020-1036 Link Here
1020
		return true;
1018
		return true;
1021
	}
1019
	}
1022
1020
1023
	private static Expression getParenthesizedForOrIfNeeded(AST ast, ASTRewrite rewrite, Expression expression) {
1024
		boolean addParentheses= false;
1025
		int nodeType= expression.getNodeType();
1026
		addParentheses= nodeType == ASTNode.CONDITIONAL_EXPRESSION || nodeType == ASTNode.ASSIGNMENT || nodeType == ASTNode.INSTANCEOF_EXPRESSION;
1027
		expression= (Expression) rewrite.createCopyTarget(expression);
1028
		if (addParentheses) {
1029
			return getParenthesizedExpression(ast, expression);
1030
		}
1031
		return expression;
1032
	}
1033
1034
	public static boolean getSplitOrConditionProposals(IInvocationContext context, ASTNode node, Collection resultingCollections) {
1021
	public static boolean getSplitOrConditionProposals(IInvocationContext context, ASTNode node, Collection resultingCollections) {
1035
		Operator orOperator= InfixExpression.Operator.CONDITIONAL_OR;
1022
		Operator orOperator= InfixExpression.Operator.CONDITIONAL_OR;
1036
		// check that user invokes quick assist on infix expression
1023
		// check that user invokes quick assist on infix expression
Lines 1254-1267 Link Here
1254
			}
1241
			}
1255
		}
1242
		}
1256
1243
1257
		// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=332019
1244
		if (NecessaryParenthesesChecker.needsParentheses(leftExpression, infixExpression, InfixExpression.RIGHT_OPERAND_PROPERTY)) {
1258
		if (operator == InfixExpression.Operator.EQUALS || operator == InfixExpression.Operator.NOT_EQUALS) {
1245
			leftExpression= getParenthesizedExpression(ast, leftExpression);
1259
			if (leftExpression instanceof InfixExpression && !(leftExpression instanceof ParenthesizedExpression)) {
1246
		}
1260
				leftExpression= getParenthesizedExpression(ast, leftExpression);
1247
		if (NecessaryParenthesesChecker.needsParentheses(rightExpression, infixExpression, InfixExpression.LEFT_OPERAND_PROPERTY)) {
1261
			}
1248
			rightExpression= getParenthesizedExpression(ast, rightExpression);
1262
			if (rightExpression instanceof InfixExpression && !(rightExpression instanceof ParenthesizedExpression)) {
1263
				rightExpression= getParenthesizedExpression(ast, rightExpression);
1264
			}
1265
		}
1249
		}
1266
1250
1267
		if (operator == InfixExpression.Operator.LESS) {
1251
		if (operator == InfixExpression.Operator.LESS) {
Lines 1319-1331 Link Here
1319
		}
1303
		}
1320
	}
1304
	}
1321
1305
1322
	private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression nodeToAdd, boolean removeParentheses, Operator operator) {
1306
	private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression originalNode, boolean removeParentheses, Operator operator) {
1323
		if (existing == null && removeParentheses) {
1307
		if (existing == null && removeParentheses) {
1324
			while (nodeToAdd instanceof ParenthesizedExpression) {
1308
			while (originalNode instanceof ParenthesizedExpression) {
1325
				nodeToAdd= ((ParenthesizedExpression) nodeToAdd).getExpression();
1309
				originalNode= ((ParenthesizedExpression)originalNode).getExpression();
1326
			}
1310
			}
1327
		}
1311
		}
1328
		Expression newRight= (Expression) rewrite.createMoveTarget(nodeToAdd);
1312
		Expression newRight= (Expression)rewrite.createMoveTarget(originalNode);
1313
		if (originalNode instanceof InfixExpression) {
1314
			((InfixExpression)newRight).setOperator(((InfixExpression)originalNode).getOperator());
1315
		}
1316
1329
		if (existing == null) {
1317
		if (existing == null) {
1330
			return newRight;
1318
			return newRight;
1331
		}
1319
		}
(-)ui/org/eclipse/jdt/ui/tests/quickfix/AdvancedQuickAssistTest.java (-1 / +272 lines)
Lines 382-387 Link Here
382
382
383
	}
383
	}
384
384
385
	public void testJoinAndIfStatementsBug335173() throws Exception {
386
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
387
		StringBuffer buf= new StringBuffer();
388
		buf.append("package test1;\n");
389
		buf.append("public class E {\n");
390
		buf.append("    public void foo(Object a, int x) {\n");
391
		buf.append("        if (a instanceof String) {\n");
392
		buf.append("            if (x > 2) {\n");
393
		buf.append("            }\n");
394
		buf.append("        }\n");
395
		buf.append("    }\n");
396
		buf.append("}\n");
397
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
398
399
		int offset= buf.toString().indexOf("if (a");
400
		AssistContext context= getCorrectionContext(cu, offset, 0);
401
		List proposals= collectAssists(context, false);
402
403
		assertCorrectLabels(proposals);
404
405
		buf= new StringBuffer();
406
		buf.append("package test1;\n");
407
		buf.append("public class E {\n");
408
		buf.append("    public void foo(Object a, int x) {\n");
409
		buf.append("        if (a instanceof String && x > 2) {\n");
410
		buf.append("        }\n");
411
		buf.append("    }\n");
412
		buf.append("}\n");
413
		String expected1= buf.toString();
414
415
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
416
417
	}
418
385
	public void testJoinOrIfStatements1() throws Exception {
419
	public void testJoinOrIfStatements1() throws Exception {
386
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
420
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
387
		StringBuffer buf= new StringBuffer();
421
		StringBuffer buf= new StringBuffer();
Lines 425-430 Link Here
425
459
426
	}
460
	}
427
461
462
	public void testJoinOrIfStatementsBug335173() throws Exception {
463
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
464
		StringBuffer buf= new StringBuffer();
465
		buf.append("package test1;\n");
466
		buf.append("public class E {\n");
467
		buf.append("    public void foo(Object a, int x) {\n");
468
		buf.append("        if (a instanceof String)\n");
469
		buf.append("            return;\n");
470
		buf.append("        if (x > 2)\n");
471
		buf.append("            return;\n");
472
		buf.append("        x= 9;\n");
473
		buf.append("    }\n");
474
		buf.append("}\n");
475
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
476
477
		int offset1= buf.toString().indexOf("if (a");
478
		int offset2= buf.toString().lastIndexOf("x= 9;");
479
		AssistContext context= getCorrectionContext(cu, offset1, offset2 - offset1);
480
		List proposals= collectAssists(context, false);
481
482
		for (Iterator I= proposals.iterator(); I.hasNext();) {
483
			Object o= I.next();
484
			if (!(o instanceof CUCorrectionProposal))
485
				I.remove();
486
		}
487
488
		assertCorrectLabels(proposals);
489
490
		buf= new StringBuffer();
491
		buf.append("package test1;\n");
492
		buf.append("public class E {\n");
493
		buf.append("    public void foo(Object a, int x) {\n");
494
		buf.append("        if (a instanceof String || x > 2)\n");
495
		buf.append("            return;\n");
496
		buf.append("        x= 9;\n");
497
		buf.append("    }\n");
498
		buf.append("}\n");
499
		String expected1= buf.toString();
500
501
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
502
503
	}
504
428
	public void testSplitOrCondition1() throws Exception {
505
	public void testSplitOrCondition1() throws Exception {
429
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
506
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
430
		StringBuffer buf= new StringBuffer();
507
		StringBuffer buf= new StringBuffer();
Lines 1639-1645 Link Here
1639
		buf.append("package test1;\n");
1716
		buf.append("package test1;\n");
1640
		buf.append("public class E {\n");
1717
		buf.append("public class E {\n");
1641
		buf.append("    public boolean foo(int a, int b) {\n");
1718
		buf.append("    public boolean foo(int a, int b) {\n");
1642
		buf.append("        return (a == b) != (b > 0);\n");
1719
		buf.append("        return (a == b) != b > 0;\n");
1643
		buf.append("    }\n");
1720
		buf.append("    }\n");
1644
		buf.append("}\n");
1721
		buf.append("}\n");
1645
		String expected1= buf.toString();
1722
		String expected1= buf.toString();
Lines 1679-1684 Link Here
1679
1756
1680
	}
1757
	}
1681
1758
1759
	public void testExchangeOperandsBug332019_4() throws Exception {
1760
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1761
		StringBuffer buf= new StringBuffer();
1762
		buf.append("package test1;\n");
1763
		buf.append("public class E {\n");
1764
		buf.append("    public boolean foo(int a, int b) {\n");
1765
		buf.append("        return b + 1 != a - 1;\n");
1766
		buf.append("    }\n");
1767
		buf.append("}\n");
1768
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
1769
1770
		int offset= buf.toString().indexOf("!=");
1771
		AssistContext context= getCorrectionContext(cu, offset, 0);
1772
		assertNoErrors(context);
1773
		List proposals= collectAssists(context, false);
1774
1775
		assertCorrectLabels(proposals);
1776
1777
		buf= new StringBuffer();
1778
		buf.append("package test1;\n");
1779
		buf.append("public class E {\n");
1780
		buf.append("    public boolean foo(int a, int b) {\n");
1781
		buf.append("        return a - 1 != b + 1;\n");
1782
		buf.append("    }\n");
1783
		buf.append("}\n");
1784
		String expected1= buf.toString();
1785
1786
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
1787
1788
	}
1789
1682
	public void testAssignAndCast1() throws Exception {
1790
	public void testAssignAndCast1() throws Exception {
1683
		//https://bugs.eclipse.org/bugs/show_bug.cgi?id=75066
1791
		//https://bugs.eclipse.org/bugs/show_bug.cgi?id=75066
1684
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1792
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
Lines 2895-2900 Link Here
2895
3003
2896
	}
3004
	}
2897
3005
3006
	public void testInverseCondition2() throws Exception {
3007
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
3008
		StringBuffer buf= new StringBuffer();
3009
		buf.append("package test1;\n");
3010
		buf.append("public class E {\n");
3011
		buf.append("    public void foo(Object a) {\n");
3012
		buf.append("        if (!(a instanceof String)) {\n");
3013
		buf.append("        }\n");
3014
		buf.append("    }\n");
3015
		buf.append("}\n");
3016
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
3017
3018
		int offset= buf.toString().indexOf("!");
3019
		int length= "!(a instanceof String)".length();
3020
		AssistContext context= getCorrectionContext(cu, offset, length);
3021
		List proposals= collectAssists(context, false);
3022
3023
		assertCorrectLabels(proposals);
3024
3025
		buf= new StringBuffer();
3026
		buf.append("package test1;\n");
3027
		buf.append("public class E {\n");
3028
		buf.append("    public void foo(Object a) {\n");
3029
		buf.append("        if (a instanceof String) {\n");
3030
		buf.append("        }\n");
3031
		buf.append("    }\n");
3032
		buf.append("}\n");
3033
		String expected1= buf.toString();
3034
3035
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
3036
3037
	}
3038
3039
	public void testInverseCondition3() throws Exception {
3040
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
3041
		StringBuffer buf= new StringBuffer();
3042
		buf.append("package test1;\n");
3043
		buf.append("public class E {\n");
3044
		buf.append("    public void foo(Object a) {\n");
3045
		buf.append("        while (!(a instanceof String)) {\n");
3046
		buf.append("        }\n");
3047
		buf.append("    }\n");
3048
		buf.append("}\n");
3049
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
3050
3051
		int offset= buf.toString().indexOf("!");
3052
		int length= "!(a instanceof String)".length();
3053
		AssistContext context= getCorrectionContext(cu, offset, length);
3054
		List proposals= collectAssists(context, false);
3055
3056
		assertCorrectLabels(proposals);
3057
3058
		buf= new StringBuffer();
3059
		buf.append("package test1;\n");
3060
		buf.append("public class E {\n");
3061
		buf.append("    public void foo(Object a) {\n");
3062
		buf.append("        while (a instanceof String) {\n");
3063
		buf.append("        }\n");
3064
		buf.append("    }\n");
3065
		buf.append("}\n");
3066
		String expected1= buf.toString();
3067
3068
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
3069
3070
	}
3071
3072
	public void testInverseCondition4() throws Exception {
3073
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
3074
		StringBuffer buf= new StringBuffer();
3075
		buf.append("package test1;\n");
3076
		buf.append("public class E {\n");
3077
		buf.append("    public void foo(Object a) {\n");
3078
		buf.append("        for (int i = 0; !(a instanceof String); i++) {\n");
3079
		buf.append("        }\n");
3080
		buf.append("    }\n");
3081
		buf.append("}\n");
3082
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
3083
3084
		int offset= buf.toString().indexOf("!");
3085
		int length= "!(a instanceof String)".length();
3086
		AssistContext context= getCorrectionContext(cu, offset, length);
3087
		List proposals= collectAssists(context, false);
3088
3089
		assertCorrectLabels(proposals);
3090
3091
		buf= new StringBuffer();
3092
		buf.append("package test1;\n");
3093
		buf.append("public class E {\n");
3094
		buf.append("    public void foo(Object a) {\n");
3095
		buf.append("        for (int i = 0; a instanceof String; i++) {\n");
3096
		buf.append("        }\n");
3097
		buf.append("    }\n");
3098
		buf.append("}\n");
3099
		String expected1= buf.toString();
3100
3101
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
3102
3103
	}
3104
3105
	public void testInverseCondition5() throws Exception {
3106
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
3107
		StringBuffer buf= new StringBuffer();
3108
		buf.append("package test1;\n");
3109
		buf.append("public class E {\n");
3110
		buf.append("    public void foo(Object a) {\n");
3111
		buf.append("        do {\n");
3112
		buf.append("        } while (!(a instanceof String));\n");
3113
		buf.append("    }\n");
3114
		buf.append("}\n");
3115
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
3116
3117
		int offset= buf.toString().indexOf("!");
3118
		int length= "!(a instanceof String)".length();
3119
		AssistContext context= getCorrectionContext(cu, offset, length);
3120
		List proposals= collectAssists(context, false);
3121
3122
		assertCorrectLabels(proposals);
3123
3124
		buf= new StringBuffer();
3125
		buf.append("package test1;\n");
3126
		buf.append("public class E {\n");
3127
		buf.append("    public void foo(Object a) {\n");
3128
		buf.append("        do {\n");
3129
		buf.append("        } while (a instanceof String);\n");
3130
		buf.append("    }\n");
3131
		buf.append("}\n");
3132
		String expected1= buf.toString();
3133
3134
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
3135
3136
	}
3137
3138
	public void testInverseCondition6() throws Exception {
3139
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
3140
		StringBuffer buf= new StringBuffer();
3141
		buf.append("package test1;\n");
3142
		buf.append("public class E {\n");
3143
		buf.append("    public void foo(Object a) {\n");
3144
		buf.append("        assert !(a instanceof String);\n");
3145
		buf.append("    }\n");
3146
		buf.append("}\n");
3147
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
3148
3149
		int offset= buf.toString().indexOf("!");
3150
		int length= "!(a instanceof String)".length();
3151
		AssistContext context= getCorrectionContext(cu, offset, length);
3152
		List proposals= collectAssists(context, false);
3153
3154
		assertCorrectLabels(proposals);
3155
3156
		buf= new StringBuffer();
3157
		buf.append("package test1;\n");
3158
		buf.append("public class E {\n");
3159
		buf.append("    public void foo(Object a) {\n");
3160
		buf.append("        assert a instanceof String;\n");
3161
		buf.append("    }\n");
3162
		buf.append("}\n");
3163
		String expected1= buf.toString();
3164
3165
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
3166
3167
	}
3168
2898
	public void testPushNegationDown1() throws Exception {
3169
	public void testPushNegationDown1() throws Exception {
2899
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
3170
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
2900
		StringBuffer buf= new StringBuffer();
3171
		StringBuffer buf= new StringBuffer();
(-)ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java (-1 / +704 lines)
Lines 877-882 Link Here
877
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
877
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
878
	}
878
	}
879
879
880
	public void testUnusedCodeBug335173_1() throws Exception {
881
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
882
		StringBuffer buf= new StringBuffer();
883
884
		buf.append("package test1;\n");
885
		buf.append("import java.util.Comparator;\n");
886
		buf.append("\n");
887
		buf.append("class IntComp implements Comparator<Integer> {\n");
888
		buf.append("    public int compare(Integer o1, Integer o2) {\n");
889
		buf.append("        return ((Integer) o1).intValue() - ((Integer) o2).intValue();\n");
890
		buf.append("    }\n");
891
		buf.append("}\n");
892
893
		ICompilationUnit cu1= pack1.createCompilationUnit("IntComp.java", buf.toString(), false, null);
894
895
		enable(CleanUpConstants.REMOVE_UNNECESSARY_CASTS);
896
897
		buf= new StringBuffer();
898
		buf.append("package test1;\n");
899
		buf.append("import java.util.Comparator;\n");
900
		buf.append("\n");
901
		buf.append("class IntComp implements Comparator<Integer> {\n");
902
		buf.append("    public int compare(Integer o1, Integer o2) {\n");
903
		buf.append("        return o1.intValue() - o2.intValue();\n");
904
		buf.append("    }\n");
905
		buf.append("}\n");
906
		String expected1= buf.toString();
907
908
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
909
	}
910
911
	public void testUnusedCodeBug335173_2() throws Exception {
912
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
913
		StringBuffer buf= new StringBuffer();
914
		buf.append("package test1;\n");
915
		buf.append("\n");
916
		buf.append("public class E1 {\n");
917
		buf.append("    public void foo(Integer n) {\n");
918
		buf.append("        int i = (((Integer) n)).intValue();\n");
919
		buf.append("    }\n");
920
		buf.append("}\n");
921
		ICompilationUnit cu1= pack1.createCompilationUnit("E1.java", buf.toString(), false, null);
922
923
		enable(CleanUpConstants.REMOVE_UNNECESSARY_CASTS);
924
925
		buf= new StringBuffer();
926
		buf.append("package test1;\n");
927
		buf.append("\n");
928
		buf.append("public class E1 {\n");
929
		buf.append("    public void foo(Integer n) {\n");
930
		buf.append("        int i = (n).intValue();\n");
931
		buf.append("    }\n");
932
		buf.append("}\n");
933
		String expected1= buf.toString();
934
935
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
936
	}
937
938
	public void testUnusedCodeBug335173_3() throws Exception {
939
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
940
		StringBuffer buf= new StringBuffer();
941
		buf.append("package test1;\n");
942
		buf.append("\n");
943
		buf.append("public class E1 {\n");
944
		buf.append("    public void foo(Integer n) {\n");
945
		buf.append("        int i = ((Integer) (n)).intValue();\n");
946
		buf.append("    }\n");
947
		buf.append("}\n");
948
		ICompilationUnit cu1= pack1.createCompilationUnit("E1.java", buf.toString(), false, null);
949
950
		enable(CleanUpConstants.REMOVE_UNNECESSARY_CASTS);
951
952
		buf= new StringBuffer();
953
		buf.append("package test1;\n");
954
		buf.append("\n");
955
		buf.append("public class E1 {\n");
956
		buf.append("    public void foo(Integer n) {\n");
957
		buf.append("        int i = (n).intValue();\n");
958
		buf.append("    }\n");
959
		buf.append("}\n");
960
		String expected1= buf.toString();
961
962
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
963
	}
964
880
	public void testJava5001() throws Exception {
965
	public void testJava5001() throws Exception {
881
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
966
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
882
		StringBuffer buf= new StringBuffer();
967
		StringBuffer buf= new StringBuffer();
Lines 5868-5874 Link Here
5868
		buf.append("package test1;\n");
5953
		buf.append("package test1;\n");
5869
		buf.append("public class E1 {\n");
5954
		buf.append("public class E1 {\n");
5870
		buf.append("    public void foo() {\n");
5955
		buf.append("    public void foo() {\n");
5871
		buf.append("        double d = 2.0 * 0.5 / 4.0;\n");
5956
		buf.append("        double d = 2.0 * (0.5 / 4.0);\n");
5872
		buf.append("        int spaceCount = 3;\n");
5957
		buf.append("        int spaceCount = 3;\n");
5873
		buf.append("        spaceCount = 2 * (spaceCount / 2);\n");
5958
		buf.append("        spaceCount = 2 * (spaceCount / 2);\n");
5874
		buf.append("    }\n");
5959
		buf.append("    }\n");
Lines 5937-5942 Link Here
5937
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { buf.toString() });
6022
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { buf.toString() });
5938
	}
6023
	}
5939
6024
6025
	public void testRemoveParenthesesBug335173_1() throws Exception {
6026
		//while loop's expression
6027
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6028
		StringBuffer buf= new StringBuffer();
6029
		buf.append("package test;\n");
6030
		buf.append("public class E {\n");
6031
		buf.append("    public void foo(boolean a) {\n");
6032
		buf.append("        while (((a))) {\n");
6033
		buf.append("        }\n");
6034
		buf.append("    }\n");
6035
		buf.append("    public void bar(int x) {\n");
6036
		buf.append("        while ((x > 2)) {\n");
6037
		buf.append("        }\n");
6038
		buf.append("    }\n");
6039
		buf.append("}\n");
6040
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6041
6042
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6043
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6044
6045
		buf= new StringBuffer();
6046
		buf.append("package test;\n");
6047
		buf.append("public class E {\n");
6048
		buf.append("    public void foo(boolean a) {\n");
6049
		buf.append("        while (a) {\n");
6050
		buf.append("        }\n");
6051
		buf.append("    }\n");
6052
		buf.append("    public void bar(int x) {\n");
6053
		buf.append("        while (x > 2) {\n");
6054
		buf.append("        }\n");
6055
		buf.append("    }\n");
6056
		buf.append("}\n");
6057
		String expected1= buf.toString();
6058
6059
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6060
	}
6061
6062
	public void testRemoveParenthesesBug335173_2() throws Exception {
6063
		//do while loop's expression
6064
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6065
		StringBuffer buf= new StringBuffer();
6066
		buf.append("package test;\n");
6067
		buf.append("public class E {\n");
6068
		buf.append("    public void foo(int x) {\n");
6069
		buf.append("        do {\n");
6070
		buf.append("        } while ((x > 2));\n");
6071
		buf.append("    }\n");
6072
		buf.append("}\n");
6073
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6074
6075
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6076
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6077
6078
		buf= new StringBuffer();
6079
		buf.append("package test;\n");
6080
		buf.append("public class E {\n");
6081
		buf.append("    public void foo(int x) {\n");
6082
		buf.append("        do {\n");
6083
		buf.append("        } while (x > 2);\n");
6084
		buf.append("    }\n");
6085
		buf.append("}\n");
6086
		String expected1= buf.toString();
6087
6088
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6089
	}
6090
6091
	public void testRemoveParenthesesBug335173_3() throws Exception {
6092
		//for loop's expression
6093
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6094
		StringBuffer buf= new StringBuffer();
6095
		buf.append("package test;\n");
6096
		buf.append("public class E {\n");
6097
		buf.append("    public void foo(int x) {\n");
6098
		buf.append("        for (int x = 0; (x > 2); x++) {\n");
6099
		buf.append("        }\n");
6100
		buf.append("    }\n");
6101
		buf.append("}\n");
6102
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6103
6104
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6105
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6106
6107
		buf= new StringBuffer();
6108
		buf.append("package test;\n");
6109
		buf.append("public class E {\n");
6110
		buf.append("    public void foo(int x) {\n");
6111
		buf.append("        for (int x = 0; x > 2; x++) {\n");
6112
		buf.append("        }\n");
6113
		buf.append("    }\n");
6114
		buf.append("}\n");
6115
		String expected1= buf.toString();
6116
6117
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6118
	}
6119
6120
	public void testRemoveParenthesesBug335173_4() throws Exception {
6121
		//switch statement expression
6122
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6123
		StringBuffer buf= new StringBuffer();
6124
		buf.append("package test;\n");
6125
		buf.append("public class E {\n");
6126
		buf.append("    public void foo(int x) {\n");
6127
		buf.append("        switch ((x - 2)) {\n");
6128
		buf.append("        }\n");
6129
		buf.append("    }\n");
6130
		buf.append("}\n");
6131
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6132
6133
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6134
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6135
6136
		buf= new StringBuffer();
6137
		buf.append("package test;\n");
6138
		buf.append("public class E {\n");
6139
		buf.append("    public void foo(int x) {\n");
6140
		buf.append("        switch (x - 2) {\n");
6141
		buf.append("        }\n");
6142
		buf.append("    }\n");
6143
		buf.append("}\n");
6144
		String expected1= buf.toString();
6145
6146
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6147
	}
6148
6149
	public void testRemoveParenthesesBug335173_5() throws Exception {
6150
		//switch case expression
6151
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6152
		StringBuffer buf= new StringBuffer();
6153
		buf.append("package test;\n");
6154
		buf.append("public class E {\n");
6155
		buf.append("    public void foo(int x) {\n");
6156
		buf.append("        switch (x) {\n");
6157
		buf.append("        case (1 + 2):\n");
6158
		buf.append("            break;\n");
6159
		buf.append("        }\n");
6160
		buf.append("    }\n");
6161
		buf.append("}\n");
6162
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6163
6164
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6165
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6166
6167
		buf= new StringBuffer();
6168
		buf.append("package test;\n");
6169
		buf.append("public class E {\n");
6170
		buf.append("    public void foo(int x) {\n");
6171
		buf.append("        switch (x) {\n");
6172
		buf.append("        case 1 + 2:\n");
6173
		buf.append("            break;\n");
6174
		buf.append("        }\n");
6175
		buf.append("    }\n");
6176
		buf.append("}\n");
6177
		String expected1= buf.toString();
6178
6179
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6180
	}
6181
6182
	public void testRemoveParenthesesBug335173_6() throws Exception {
6183
		//throw statement expression
6184
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6185
		StringBuffer buf= new StringBuffer();
6186
		buf.append("package test;\n");
6187
		buf.append("public class E {\n");
6188
		buf.append("    public void foo(int type) throws Exception {\n");
6189
		buf.append("        throw (type == 1 ? new IllegalArgumentException() : new Exception());\n");
6190
		buf.append("    }\n");
6191
		buf.append("}\n");
6192
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6193
6194
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6195
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6196
6197
		buf= new StringBuffer();
6198
		buf.append("package test;\n");
6199
		buf.append("public class E {\n");
6200
		buf.append("    public void foo(int type) throws Exception {\n");
6201
		buf.append("        throw type == 1 ? new IllegalArgumentException() : new Exception();\n");
6202
		buf.append("    }\n");
6203
		buf.append("}\n");
6204
		String expected1= buf.toString();
6205
6206
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6207
	}
6208
6209
	public void testRemoveParenthesesBug335173_7() throws Exception {
6210
		//synchronized statement expression
6211
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6212
		StringBuffer buf= new StringBuffer();
6213
		buf.append("package test;\n");
6214
		buf.append("public class E {\n");
6215
		buf.append("    private static final Object OBJECT = new Object();\n");
6216
		buf.append("    private static final String STRING = new String();\n");
6217
		buf.append("    \n");
6218
		buf.append("    public void foo(int x) {\n");
6219
		buf.append("        synchronized ((x == 1 ? STRING : OBJECT)) {\n");
6220
		buf.append("        }\n");
6221
		buf.append("    }\n");
6222
		buf.append("}\n");
6223
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6224
6225
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6226
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6227
6228
		buf= new StringBuffer();
6229
		buf.append("package test;\n");
6230
		buf.append("public class E {\n");
6231
		buf.append("    private static final Object OBJECT = new Object();\n");
6232
		buf.append("    private static final String STRING = new String();\n");
6233
		buf.append("    \n");
6234
		buf.append("    public void foo(int x) {\n");
6235
		buf.append("        synchronized (x == 1 ? STRING : OBJECT) {\n");
6236
		buf.append("        }\n");
6237
		buf.append("    }\n");
6238
		buf.append("}\n");
6239
		String expected1= buf.toString();
6240
6241
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6242
	}
6243
6244
	public void testRemoveParenthesesBug335173_8() throws Exception {
6245
		//assert statement expression
6246
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6247
		StringBuffer buf= new StringBuffer();
6248
		buf.append("package test;\n");
6249
		buf.append("public class E {\n");
6250
		buf.append("    public void foo(int x) {\n");
6251
		buf.append("        assert (x > 2);\n");
6252
		buf.append("    }\n");
6253
		buf.append("}\n");
6254
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6255
6256
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6257
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6258
6259
		buf= new StringBuffer();
6260
		buf.append("package test;\n");
6261
		buf.append("public class E {\n");
6262
		buf.append("    public void foo(int x) {\n");
6263
		buf.append("        assert x > 2;\n");
6264
		buf.append("    }\n");
6265
		buf.append("}\n");
6266
		String expected1= buf.toString();
6267
6268
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6269
	}
6270
6271
	public void testRemoveParenthesesBug335173_9() throws Exception {
6272
		//assert statement message expression
6273
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6274
		StringBuffer buf= new StringBuffer();
6275
		buf.append("package test;\n");
6276
		buf.append("public class E {\n");
6277
		buf.append("    public void foo(int x) {\n");
6278
		buf.append("        assert x > 2 : (x - 2);\n");
6279
		buf.append("    }\n");
6280
		buf.append("}\n");
6281
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6282
6283
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6284
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6285
6286
		buf= new StringBuffer();
6287
		buf.append("package test;\n");
6288
		buf.append("public class E {\n");
6289
		buf.append("    public void foo(int x) {\n");
6290
		buf.append("        assert x > 2 : x - 2;\n");
6291
		buf.append("    }\n");
6292
		buf.append("}\n");
6293
		String expected1= buf.toString();
6294
6295
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6296
	}
6297
6298
	public void testRemoveParenthesesBug335173_10() throws Exception {
6299
		//array access index expression
6300
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6301
		StringBuffer buf= new StringBuffer();
6302
		buf.append("package test;\n");
6303
		buf.append("public class E {\n");
6304
		buf.append("    public void foo(int a[], int x) {\n");
6305
		buf.append("        int i = a[(x + 2)];\n");
6306
		buf.append("    }\n");
6307
		buf.append("}\n");
6308
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6309
6310
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6311
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6312
6313
		buf= new StringBuffer();
6314
		buf.append("package test;\n");
6315
		buf.append("public class E {\n");
6316
		buf.append("    public void foo(int a[], int x) {\n");
6317
		buf.append("        int i = a[x + 2];\n");
6318
		buf.append("    }\n");
6319
		buf.append("}\n");
6320
		String expected1= buf.toString();
6321
6322
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6323
	}
6324
6325
	public void testRemoveParenthesesBug335173_11() throws Exception {
6326
		//conditional expression's then expression
6327
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6328
		StringBuffer buf= new StringBuffer();
6329
		buf.append("package test;\n");
6330
		buf.append("public class E {\n");
6331
		buf.append("    public void foo(int x) {\n");
6332
		buf.append("        int i = x > 10 ? (x > 5 ? x - 1 : x - 2): x;\n");
6333
		buf.append("    }\n");
6334
		buf.append("}\n");
6335
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6336
6337
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6338
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6339
6340
		buf= new StringBuffer();
6341
		buf.append("package test;\n");
6342
		buf.append("public class E {\n");
6343
		buf.append("    public void foo(int x) {\n");
6344
		buf.append("        int i = x > 10 ? x > 5 ? x - 1 : x - 2: x;\n");
6345
		buf.append("    }\n");
6346
		buf.append("}\n");
6347
		String expected1= buf.toString();
6348
6349
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6350
	}
6351
6352
	public void testRemoveParenthesesBug335173_12() throws Exception {
6353
		//conditional expression's else expression
6354
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6355
		StringBuffer buf= new StringBuffer();
6356
		buf.append("package test;\n");
6357
		buf.append("public class E {\n");
6358
		buf.append("    public void foo(int x) {\n");
6359
		buf.append("        int i = x > 10 ? x: (x > 5 ? x - 1 : x - 2);\n");
6360
		buf.append("    }\n");
6361
		buf.append("}\n");
6362
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6363
6364
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6365
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6366
6367
		buf= new StringBuffer();
6368
		buf.append("package test;\n");
6369
		buf.append("public class E {\n");
6370
		buf.append("    public void foo(int x) {\n");
6371
		buf.append("        int i = x > 10 ? x: x > 5 ? x - 1 : x - 2;\n");
6372
		buf.append("    }\n");
6373
		buf.append("}\n");
6374
		String expected1= buf.toString();
6375
6376
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6377
	}
6378
6379
	public void testRemoveParenthesesBug335173_13() throws Exception {
6380
		//conditional expression's then expression
6381
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6382
		StringBuffer buf= new StringBuffer();
6383
		buf.append("package test;\n");
6384
		buf.append("public class E {\n");
6385
		buf.append("    public void foo(int x) {\n");
6386
		buf.append("        int i = x > 10 ? (x = x - 2): x;\n");
6387
		buf.append("    }\n");
6388
		buf.append("}\n");
6389
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6390
6391
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6392
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6393
6394
		buf= new StringBuffer();
6395
		buf.append("package test;\n");
6396
		buf.append("public class E {\n");
6397
		buf.append("    public void foo(int x) {\n");
6398
		buf.append("        int i = x > 10 ? (x = x - 2): x;\n");
6399
		buf.append("    }\n");
6400
		buf.append("}\n");
6401
		String expected1= buf.toString();
6402
6403
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6404
	}
6405
6406
	public void testRemoveParenthesesBug335173_14() throws Exception {
6407
		//conditional expression's else expression
6408
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6409
		StringBuffer buf= new StringBuffer();
6410
		buf.append("package test;\n");
6411
		buf.append("public class E {\n");
6412
		buf.append("    public void foo(int x) {\n");
6413
		buf.append("        int i = x > 10 ? x: (x = x - 2);\n");
6414
		buf.append("    }\n");
6415
		buf.append("}\n");
6416
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6417
6418
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6419
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6420
6421
		buf= new StringBuffer();
6422
		buf.append("package test;\n");
6423
		buf.append("public class E {\n");
6424
		buf.append("    public void foo(int x) {\n");
6425
		buf.append("        int i = x > 10 ? x: (x = x - 2);\n");
6426
		buf.append("    }\n");
6427
		buf.append("}\n");
6428
		String expected1= buf.toString();
6429
6430
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6431
	}
6432
6433
	public void testRemoveParenthesesBug335173_15() throws Exception {
6434
		//shift operators
6435
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6436
		StringBuffer buf= new StringBuffer();
6437
		buf.append("package test;\n");
6438
		buf.append("public class E {\n");
6439
		buf.append("    public void foo(int x) {\n");
6440
		buf.append("        int m= (x >> 2) >> 1;\n");
6441
		buf.append("        m= x >> (2 >> 1);\n");
6442
		buf.append("        int n= (x << 2) << 1;\n");
6443
		buf.append("        n= x << (2 << 1);\n");
6444
		buf.append("    }\n");
6445
		buf.append("}\n");
6446
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6447
6448
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6449
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6450
6451
		buf= new StringBuffer();
6452
		buf.append("package test;\n");
6453
		buf.append("public class E {\n");
6454
		buf.append("    public void foo(int x) {\n");
6455
		buf.append("        int m= x >> 2 >> 1;\n");
6456
		buf.append("        m= x >> (2 >> 1);\n");
6457
		buf.append("        int n= x << 2 << 1;\n");
6458
		buf.append("        n= x << (2 << 1);\n");
6459
		buf.append("    }\n");
6460
		buf.append("}\n");
6461
		String expected1= buf.toString();
6462
6463
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6464
	}
6465
6466
	public void testRemoveParenthesesBug335173_16() throws Exception {
6467
		//integer multiplication
6468
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6469
		StringBuffer buf= new StringBuffer();
6470
		buf.append("package test;\n");
6471
		buf.append("public class E {\n");
6472
		buf.append("    public void foo(int x, long y) {\n");
6473
		buf.append("        int m= (4 * x) * 2;\n");
6474
		buf.append("        int n= 4 * (x * 2);\n");
6475
		buf.append("        int p= 4 * (x % 3);\n");
6476
		buf.append("        int q= 4 * (x / 3);\n");
6477
		buf.append("        int r= 4 * (x * y);\n");
6478
		buf.append("    }\n");
6479
		buf.append("}\n");
6480
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6481
6482
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6483
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6484
6485
		buf= new StringBuffer();
6486
		buf.append("package test;\n");
6487
		buf.append("public class E {\n");
6488
		buf.append("    public void foo(int x, long y) {\n");
6489
		buf.append("        int m= 4 * x * 2;\n");
6490
		buf.append("        int n= 4 * x * 2;\n");
6491
		buf.append("        int p= 4 * (x % 3);\n");
6492
		buf.append("        int q= 4 * (x / 3);\n");
6493
		buf.append("        int r= 4 * (x * y);\n");
6494
		buf.append("    }\n");
6495
		buf.append("}\n");
6496
		String expected1= buf.toString();
6497
6498
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6499
	}
6500
6501
	public void testRemoveParenthesesBug335173_17() throws Exception {
6502
		//floating point multiplication
6503
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6504
		StringBuffer buf= new StringBuffer();
6505
		buf.append("package test;\n");
6506
		buf.append("public class E {\n");
6507
		buf.append("    public void foo(double x) {\n");
6508
		buf.append("        int m= (4.0 * x) * 0.5;\n");
6509
		buf.append("        int n= 4.0 * (x * 0.5);\n");
6510
		buf.append("        int p= 4.0 * (x / 100);\n");
6511
		buf.append("        int q= 4.0 * (x % 3);\n");
6512
		buf.append("    }\n");
6513
		buf.append("}\n");
6514
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6515
6516
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6517
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6518
6519
		buf= new StringBuffer();
6520
		buf.append("package test;\n");
6521
		buf.append("public class E {\n");
6522
		buf.append("    public void foo(double x) {\n");
6523
		buf.append("        int m= 4.0 * x * 0.5;\n");
6524
		buf.append("        int n= 4.0 * (x * 0.5);\n");
6525
		buf.append("        int p= 4.0 * (x / 100);\n");
6526
		buf.append("        int q= 4.0 * (x % 3);\n");
6527
		buf.append("    }\n");
6528
		buf.append("}\n");
6529
		String expected1= buf.toString();
6530
6531
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6532
	}
6533
6534
	public void testRemoveParenthesesBug335173_18() throws Exception {
6535
		//integer addition
6536
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6537
		StringBuffer buf= new StringBuffer();
6538
		buf.append("package test;\n");
6539
		buf.append("public class E {\n");
6540
		buf.append("    public void foo(int x, long y) {\n");
6541
		buf.append("        int m= (4 + x) + 2;\n");
6542
		buf.append("        int n= 4 + (x + 2);\n");
6543
		buf.append("        int p= 4 + (x + y);\n");
6544
		buf.append("    }\n");
6545
		buf.append("}\n");
6546
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6547
6548
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6549
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6550
6551
		buf= new StringBuffer();
6552
		buf.append("package test;\n");
6553
		buf.append("public class E {\n");
6554
		buf.append("    public void foo(int x, long y) {\n");
6555
		buf.append("        int m= 4 + x + 2;\n");
6556
		buf.append("        int n= 4 + x + 2;\n");
6557
		buf.append("        int p= 4 + (x + y);\n");
6558
		buf.append("    }\n");
6559
		buf.append("}\n");
6560
		String expected1= buf.toString();
6561
6562
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6563
	}
6564
6565
	public void testRemoveParenthesesBug335173_19() throws Exception {
6566
		//floating point addition
6567
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6568
		StringBuffer buf= new StringBuffer();
6569
		buf.append("package test;\n");
6570
		buf.append("public class E {\n");
6571
		buf.append("    public void foo(double x) {\n");
6572
		buf.append("        int m= (4.0 + x) + 100.0;\n");
6573
		buf.append("        int n= 4.0 + (x + 100.0);\n");
6574
		buf.append("    }\n");
6575
		buf.append("}\n");
6576
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6577
6578
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6579
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6580
6581
		buf= new StringBuffer();
6582
		buf.append("package test;\n");
6583
		buf.append("public class E {\n");
6584
		buf.append("    public void foo(double x) {\n");
6585
		buf.append("        int m= 4.0 + x + 100.0;\n");
6586
		buf.append("        int n= 4.0 + (x + 100.0);\n");
6587
		buf.append("    }\n");
6588
		buf.append("}\n");
6589
		String expected1= buf.toString();
6590
6591
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6592
	}
6593
6594
	public void testRemoveParenthesesBug335173_20() throws Exception {
6595
		//string concatenation
6596
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
6597
		StringBuffer buf= new StringBuffer();
6598
		buf.append("package test;\n");
6599
		buf.append("public class E {\n");
6600
		buf.append("    public void foo(String s, String t, String u) {\n");
6601
		buf.append("        String a= (s + t) + u;\n");
6602
		buf.append("        String b= s + (t + u);\n");
6603
		buf.append("        String c= (1 + 2) + s;\n");
6604
		buf.append("        String d= 1 + (2 + s);\n");
6605
		buf.append("        String e= s + (1 + 2);\n");
6606
		buf.append("        String f= (s + 1) + 2;\n");
6607
		buf.append("        String g= (1 + s) + 2;\n");
6608
		buf.append("        String h= 1 + (s + 2);\n");
6609
		buf.append("        String i= s + (1 + t);\n");
6610
		buf.append("        String j= s + (t + 1);\n");
6611
		buf.append("        String k= s + (1 - 2);\n");
6612
		buf.append("        String l= s + (1 * 2);\n");
6613
		buf.append("    }\n");
6614
		buf.append("}\n");
6615
		ICompilationUnit cu1= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
6616
6617
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
6618
		enable(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER);
6619
6620
		buf= new StringBuffer();
6621
		buf.append("package test;\n");
6622
		buf.append("public class E {\n");
6623
		buf.append("    public void foo(String s, String t, String u) {\n");
6624
		buf.append("        String a= s + t + u;\n");
6625
		buf.append("        String b= s + t + u;\n");
6626
		buf.append("        String c= 1 + 2 + s;\n");
6627
		buf.append("        String d= 1 + (2 + s);\n");
6628
		buf.append("        String e= s + (1 + 2);\n");
6629
		buf.append("        String f= s + 1 + 2;\n");
6630
		buf.append("        String g= 1 + s + 2;\n");
6631
		buf.append("        String h= 1 + s + 2;\n");
6632
		buf.append("        String i= s + 1 + t;\n");
6633
		buf.append("        String j= s + t + 1;\n");
6634
		buf.append("        String k= s + (1 - 2);\n");
6635
		buf.append("        String l= s + 1 * 2;\n");
6636
		buf.append("    }\n");
6637
		buf.append("}\n");
6638
		String expected1= buf.toString();
6639
6640
		assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
6641
	}
6642
5940
	public void testRemoveQualifier01() throws Exception {
6643
	public void testRemoveQualifier01() throws Exception {
5941
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
6644
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
5942
		StringBuffer buf= new StringBuffer();
6645
		StringBuffer buf= new StringBuffer();
(-)ui/org/eclipse/jdt/ui/tests/quickfix/GetterSetterQuickFixTest.java (-1 / +233 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2010 IBM Corporation and others.
2
 * Copyright (c) 2007, 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 165-170 Link Here
165
		assertExpectedExistInProposals(proposals, expected);
165
		assertExpectedExistInProposals(proposals, expected);
166
	}
166
	}
167
167
168
	public void testInvisibleFieldToGetterSetterBug335173_1() throws Exception {
169
		IPackageFragment pack1= fSourceFolder.createPackageFragment("p", false, null);
170
		StringBuffer buf= new StringBuffer();
171
		buf.append("package p;\n");
172
		buf.append("\n");
173
		buf.append("public class C {\n");
174
		buf.append("    private int test;\n");
175
		buf.append("\n");
176
		buf.append("    public int getTest() {\n");
177
		buf.append("        return this.test;\n");
178
		buf.append("    }\n");
179
		buf.append("\n");
180
		buf.append("    public void setTest(int test) {\n");
181
		buf.append("        this.test = test;\n");
182
		buf.append("    }\n");
183
		buf.append("}\n");
184
		buf.append("\n");
185
		buf.append("class D {\n");
186
		buf.append("    public void foo(int x){\n");
187
		buf.append("        C c=new C();\n");
188
		buf.append("        c.test+= x;\n");
189
		buf.append("    }\n");
190
		buf.append("}\n");
191
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
192
193
		CompilationUnit astRoot= getASTRoot(cu);
194
		ArrayList proposals= collectCorrections(cu, astRoot);
195
196
		assertCorrectLabels(proposals);
197
		assertNumberOfProposals(proposals, 2);
198
199
		String[] expected= new String[1];
200
		buf= new StringBuffer();
201
		buf.append("package p;\n");
202
		buf.append("\n");
203
		buf.append("public class C {\n");
204
		buf.append("    private int test;\n");
205
		buf.append("\n");
206
		buf.append("    public int getTest() {\n");
207
		buf.append("        return this.test;\n");
208
		buf.append("    }\n");
209
		buf.append("\n");
210
		buf.append("    public void setTest(int test) {\n");
211
		buf.append("        this.test = test;\n");
212
		buf.append("    }\n");
213
		buf.append("}\n");
214
		buf.append("\n");
215
		buf.append("class D {\n");
216
		buf.append("    public void foo(int x){\n");
217
		buf.append("        C c=new C();\n");
218
		buf.append("        c.setTest(c.getTest() + x);\n");
219
		buf.append("    }\n");
220
		buf.append("}\n");
221
		expected[0]= buf.toString();
222
223
		assertExpectedExistInProposals(proposals, expected);
224
	}
225
226
	public void testInvisibleFieldToGetterSetterBug335173_2() throws Exception {
227
		IPackageFragment pack1= fSourceFolder.createPackageFragment("p", false, null);
228
		StringBuffer buf= new StringBuffer();
229
		buf.append("package p;\n");
230
		buf.append("\n");
231
		buf.append("public class C {\n");
232
		buf.append("    private int test;\n");
233
		buf.append("\n");
234
		buf.append("    public int getTest() {\n");
235
		buf.append("        return this.test;\n");
236
		buf.append("    }\n");
237
		buf.append("\n");
238
		buf.append("    public void setTest(int test) {\n");
239
		buf.append("        this.test = test;\n");
240
		buf.append("    }\n");
241
		buf.append("}\n");
242
		buf.append("\n");
243
		buf.append("class D {\n");
244
		buf.append("    public void foo(){\n");
245
		buf.append("        C c=new C();\n");
246
		buf.append("        c.test+= 1 + 2;\n");
247
		buf.append("    }\n");
248
		buf.append("}\n");
249
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
250
251
		CompilationUnit astRoot= getASTRoot(cu);
252
		ArrayList proposals= collectCorrections(cu, astRoot);
253
254
		assertCorrectLabels(proposals);
255
		assertNumberOfProposals(proposals, 2);
256
257
		String[] expected= new String[1];
258
		buf= new StringBuffer();
259
		buf.append("package p;\n");
260
		buf.append("\n");
261
		buf.append("public class C {\n");
262
		buf.append("    private int test;\n");
263
		buf.append("\n");
264
		buf.append("    public int getTest() {\n");
265
		buf.append("        return this.test;\n");
266
		buf.append("    }\n");
267
		buf.append("\n");
268
		buf.append("    public void setTest(int test) {\n");
269
		buf.append("        this.test = test;\n");
270
		buf.append("    }\n");
271
		buf.append("}\n");
272
		buf.append("\n");
273
		buf.append("class D {\n");
274
		buf.append("    public void foo(){\n");
275
		buf.append("        C c=new C();\n");
276
		buf.append("        c.setTest(c.getTest() + (1 + 2));\n");
277
		buf.append("    }\n");
278
		buf.append("}\n");
279
		expected[0]= buf.toString();
280
281
		assertExpectedExistInProposals(proposals, expected);
282
	}
283
284
	public void testInvisibleFieldToGetterSetterBug335173_3() throws Exception {
285
		IPackageFragment pack1= fSourceFolder.createPackageFragment("p", false, null);
286
		StringBuffer buf= new StringBuffer();
287
		buf.append("package p;\n");
288
		buf.append("\n");
289
		buf.append("public class C {\n");
290
		buf.append("    private int test;\n");
291
		buf.append("\n");
292
		buf.append("    public int getTest() {\n");
293
		buf.append("        return this.test;\n");
294
		buf.append("    }\n");
295
		buf.append("\n");
296
		buf.append("    public void setTest(int test) {\n");
297
		buf.append("        this.test = test;\n");
298
		buf.append("    }\n");
299
		buf.append("}\n");
300
		buf.append("\n");
301
		buf.append("class D {\n");
302
		buf.append("    public void foo(){\n");
303
		buf.append("        C c=new C();\n");
304
		buf.append("        c.test-= 1 + 2;\n");
305
		buf.append("    }\n");
306
		buf.append("}\n");
307
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
308
309
		CompilationUnit astRoot= getASTRoot(cu);
310
		ArrayList proposals= collectCorrections(cu, astRoot);
311
312
		assertCorrectLabels(proposals);
313
		assertNumberOfProposals(proposals, 2);
314
315
		String[] expected= new String[1];
316
		buf= new StringBuffer();
317
		buf.append("package p;\n");
318
		buf.append("\n");
319
		buf.append("public class C {\n");
320
		buf.append("    private int test;\n");
321
		buf.append("\n");
322
		buf.append("    public int getTest() {\n");
323
		buf.append("        return this.test;\n");
324
		buf.append("    }\n");
325
		buf.append("\n");
326
		buf.append("    public void setTest(int test) {\n");
327
		buf.append("        this.test = test;\n");
328
		buf.append("    }\n");
329
		buf.append("}\n");
330
		buf.append("\n");
331
		buf.append("class D {\n");
332
		buf.append("    public void foo(){\n");
333
		buf.append("        C c=new C();\n");
334
		buf.append("        c.setTest(c.getTest() - (1 + 2));\n");
335
		buf.append("    }\n");
336
		buf.append("}\n");
337
		expected[0]= buf.toString();
338
339
		assertExpectedExistInProposals(proposals, expected);
340
	}
341
342
	public void testInvisibleFieldToGetterSetterBug335173_4() throws Exception {
343
		IPackageFragment pack1= fSourceFolder.createPackageFragment("p", false, null);
344
		StringBuffer buf= new StringBuffer();
345
		buf.append("package p;\n");
346
		buf.append("\n");
347
		buf.append("public class C {\n");
348
		buf.append("    private int test;\n");
349
		buf.append("\n");
350
		buf.append("    public int getTest() {\n");
351
		buf.append("        return this.test;\n");
352
		buf.append("    }\n");
353
		buf.append("\n");
354
		buf.append("    public void setTest(int test) {\n");
355
		buf.append("        this.test = test;\n");
356
		buf.append("    }\n");
357
		buf.append("}\n");
358
		buf.append("\n");
359
		buf.append("class D {\n");
360
		buf.append("    public void foo(){\n");
361
		buf.append("        C c=new C();\n");
362
		buf.append("        c.test*= 1 + 2;\n");
363
		buf.append("    }\n");
364
		buf.append("}\n");
365
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
366
367
		CompilationUnit astRoot= getASTRoot(cu);
368
		ArrayList proposals= collectCorrections(cu, astRoot);
369
370
		assertCorrectLabels(proposals);
371
		assertNumberOfProposals(proposals, 2);
372
373
		String[] expected= new String[1];
374
		buf= new StringBuffer();
375
		buf.append("package p;\n");
376
		buf.append("\n");
377
		buf.append("public class C {\n");
378
		buf.append("    private int test;\n");
379
		buf.append("\n");
380
		buf.append("    public int getTest() {\n");
381
		buf.append("        return this.test;\n");
382
		buf.append("    }\n");
383
		buf.append("\n");
384
		buf.append("    public void setTest(int test) {\n");
385
		buf.append("        this.test = test;\n");
386
		buf.append("    }\n");
387
		buf.append("}\n");
388
		buf.append("\n");
389
		buf.append("class D {\n");
390
		buf.append("    public void foo(){\n");
391
		buf.append("        C c=new C();\n");
392
		buf.append("        c.setTest(c.getTest() * (1 + 2));\n");
393
		buf.append("    }\n");
394
		buf.append("}\n");
395
		expected[0]= buf.toString();
396
397
		assertExpectedExistInProposals(proposals, expected);
398
	}
399
168
	public void testCreateFieldUsingSef() throws Exception {
400
	public void testCreateFieldUsingSef() throws Exception {
169
		IPackageFragment pack1= fSourceFolder.createPackageFragment("", false, null);
401
		IPackageFragment pack1= fSourceFolder.createPackageFragment("", false, null);
170
		StringBuffer buf= new StringBuffer();
402
		StringBuffer buf= new StringBuffer();
(-)ui/org/eclipse/jdt/ui/tests/quickfix/LocalCorrectionsQuickFixTest.java (+66 lines)
Lines 4411-4416 Link Here
4411
		assertEqualString(preview, buf.toString());
4411
		assertEqualString(preview, buf.toString());
4412
	}
4412
	}
4413
4413
4414
	public void testUnnecessaryCastBug335173_1() throws Exception {
4415
		Hashtable hashtable= JavaCore.getOptions();
4416
		hashtable.put(JavaCore.COMPILER_PB_UNNECESSARY_TYPE_CHECK, JavaCore.ERROR);
4417
		JavaCore.setOptions(hashtable);
4418
4419
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
4420
		StringBuffer buf= new StringBuffer();
4421
		buf.append("package test1;\n");
4422
		buf.append("public class E {\n");
4423
		buf.append("    public void foo(Integer n) {\n");
4424
		buf.append("        int i = (((Integer) n)).intValue();\n");
4425
		buf.append("    }\n");
4426
		buf.append("}\n");
4427
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
4428
4429
		CompilationUnit astRoot= getASTRoot(cu);
4430
		ArrayList proposals= collectCorrections(cu, astRoot);
4431
		assertNumberOfProposals(proposals, 1);
4432
		assertCorrectLabels(proposals);
4433
4434
		CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(0);
4435
		String preview= getPreviewContent(proposal);
4436
4437
		buf= new StringBuffer();
4438
		buf.append("package test1;\n");
4439
		buf.append("public class E {\n");
4440
		buf.append("    public void foo(Integer n) {\n");
4441
		buf.append("        int i = (n).intValue();\n");
4442
		buf.append("    }\n");
4443
		buf.append("}\n");
4444
		assertEqualString(preview, buf.toString());
4445
	}
4446
4447
	public void testUnnecessaryCastBug335173_2() throws Exception {
4448
		Hashtable hashtable= JavaCore.getOptions();
4449
		hashtable.put(JavaCore.COMPILER_PB_UNNECESSARY_TYPE_CHECK, JavaCore.ERROR);
4450
		JavaCore.setOptions(hashtable);
4451
4452
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
4453
		StringBuffer buf= new StringBuffer();
4454
		buf.append("package test1;\n");
4455
		buf.append("public class E {\n");
4456
		buf.append("    public void foo(Integer n) {\n");
4457
		buf.append("        int i = ((Integer) (n)).intValue();\n");
4458
		buf.append("    }\n");
4459
		buf.append("}\n");
4460
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
4461
4462
		CompilationUnit astRoot= getASTRoot(cu);
4463
		ArrayList proposals= collectCorrections(cu, astRoot);
4464
		assertNumberOfProposals(proposals, 1);
4465
		assertCorrectLabels(proposals);
4466
4467
		CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(0);
4468
		String preview= getPreviewContent(proposal);
4469
4470
		buf= new StringBuffer();
4471
		buf.append("package test1;\n");
4472
		buf.append("public class E {\n");
4473
		buf.append("    public void foo(Integer n) {\n");
4474
		buf.append("        int i = (n).intValue();\n");
4475
		buf.append("    }\n");
4476
		buf.append("}\n");
4477
		assertEqualString(preview, buf.toString());
4478
	}
4479
4414
	public void testSuperfluousSemicolon() throws Exception {
4480
	public void testSuperfluousSemicolon() throws Exception {
4415
		Hashtable hashtable= JavaCore.getOptions();
4481
		Hashtable hashtable= JavaCore.getOptions();
4416
		hashtable.put(JavaCore.COMPILER_PB_EMPTY_STATEMENT, JavaCore.ERROR);
4482
		hashtable.put(JavaCore.COMPILER_PB_EMPTY_STATEMENT, JavaCore.ERROR);
(-)resources/ExtractConstant/canExtract/A_test39_in.java (+7 lines)
Added Link Here
1
//5, 21 -> 5, 26   AllowLoadtime == false
2
package p;
3
class A {
4
	void f() {
5
		int i= 1 - (2 + 3);
6
	}
7
}
(-)resources/ExtractConstant/canExtract/A_test39_out.java (+9 lines)
Added Link Here
1
//5, 21 -> 5, 26   AllowLoadtime == false
2
package p;
3
class A {
4
	private static final int CONSTANT= 2 + 3;
5
6
	void f() {
7
		int i= 1 - CONSTANT;
8
	}
9
}
(-)resources/ExtractConstant/canExtract/A_test40_in.java (+7 lines)
Added Link Here
1
//5, 20 -> 5, 27   AllowLoadtime == false
2
package p;
3
class A {
4
	void f() {
5
		int i= 1 - (2 + 3);
6
	}
7
}
(-)resources/ExtractConstant/canExtract/A_test40_out.java (+9 lines)
Added Link Here
1
//5, 20 -> 5, 27   AllowLoadtime == false
2
package p;
3
class A {
4
	private static final int CONSTANT= 2 + 3;
5
6
	void f() {
7
		int i= 1 - CONSTANT;
8
	}
9
}
(-)resources/ExtractConstant/canExtract/A_test41_in.java (+7 lines)
Added Link Here
1
//5, 22 -> 5, 27   AllowLoadtime == false
2
package p;
3
class A {
4
	void f() {
5
		int i= 1 - ((2 + 3));
6
	}
7
}
(-)resources/ExtractConstant/canExtract/A_test41_out.java (+9 lines)
Added Link Here
1
//5, 22 -> 5, 27   AllowLoadtime == false
2
package p;
3
class A {
4
	private static final int CONSTANT= 2 + 3;
5
6
	void f() {
7
		int i= 1 - (CONSTANT);
8
	}
9
}
(-)resources/ExtractConstant/canExtract/A_test42_in.java (+7 lines)
Added Link Here
1
//5, 21 -> 5, 28   AllowLoadtime == false
2
package p;
3
class A {
4
	void f() {
5
		int i= 1 - ((2 + 3));
6
	}
7
}
(-)resources/ExtractConstant/canExtract/A_test42_out.java (+9 lines)
Added Link Here
1
//5, 21 -> 5, 28   AllowLoadtime == false
2
package p;
3
class A {
4
	private static final int CONSTANT= 2 + 3;
5
6
	void f() {
7
		int i= 1 - CONSTANT;
8
	}
9
}
(-)resources/ExtractConstant/canExtract/A_test43_in.java (+7 lines)
Added Link Here
1
//5, 20 -> 5, 29   AllowLoadtime == false
2
package p;
3
class A {
4
	void f() {
5
		int i= 1 - ((2 + 3));
6
	}
7
}
(-)resources/ExtractConstant/canExtract/A_test43_out.java (+9 lines)
Added Link Here
1
//5, 20 -> 5, 29   AllowLoadtime == false
2
package p;
3
class A {
4
	private static final int CONSTANT= (2 + 3);
5
6
	void f() {
7
		int i= 1 - CONSTANT;
8
	}
9
}
(-)resources/ExtractMethodWorkSpace/ExtractMethodTests/expression_in/A_test625.java (+9 lines)
Added Link Here
1
package expression_in;
2
3
public class A_test625 {
4
5
	public void foo() {
6
		int i= 1 - (/*[*/2 + 3/*]*/);
7
		int j= 1 - (2 + 3);
8
	}	
9
}
(-)resources/ExtractMethodWorkSpace/ExtractMethodTests/expression_in/A_test626.java (+9 lines)
Added Link Here
1
package expression_in;
2
3
public class A_test626 {
4
5
	public void foo() {
6
		int i= 1 - /*[*/(2 + 3)/*]*/;
7
		int j= 1 - (2 + 3);
8
	}	
9
}
(-)resources/ExtractMethodWorkSpace/ExtractMethodTests/expression_in/A_test627.java (+9 lines)
Added Link Here
1
package expression_in;
2
3
public class A_test627 {
4
5
	public void foo() {
6
		int i= 1 - ((/*[*/2 + 3/*]*/));
7
		int j= 1 - ((2 + 3));
8
	}	
9
}
(-)resources/ExtractMethodWorkSpace/ExtractMethodTests/expression_in/A_test628.java (+9 lines)
Added Link Here
1
package expression_in;
2
3
public class A_test627 {
4
5
	public void foo() {
6
		int i= 1 - (/*[*/(2 + 3)/*]*/);
7
		int j= 1 - ((2 + 3));
8
	}	
9
}
(-)resources/ExtractMethodWorkSpace/ExtractMethodTests/expression_in/A_test629.java (+9 lines)
Added Link Here
1
package expression_in;
2
3
public class A_test627 {
4
5
	public void foo() {
6
		int i= 1 - /*[*/((2 + 3))/*]*/;
7
		int j= 1 - ((2 + 3));
8
	}	
9
}
(-)resources/ExtractMethodWorkSpace/ExtractMethodTests/expression_out/A_test625.java (+13 lines)
Added Link Here
1
package expression_out;
2
3
public class A_test625 {
4
5
	public void foo() {
6
		int i= 1 - extracted();
7
		int j= 1 - extracted();
8
	}
9
10
	protected int extracted() {
11
		return /*[*/2 + 3/*]*/;
12
	}	
13
}
(-)resources/ExtractMethodWorkSpace/ExtractMethodTests/expression_out/A_test626.java (+13 lines)
Added Link Here
1
package expression_out;
2
3
public class A_test626 {
4
5
	public void foo() {
6
		int i= 1 - extracted();
7
		int j= 1 - extracted();
8
	}
9
10
	protected int extracted() {
11
		return 2 + 3;
12
	}	
13
}
(-)resources/ExtractMethodWorkSpace/ExtractMethodTests/expression_out/A_test627.java (+13 lines)
Added Link Here
1
package expression_out;
2
3
public class A_test627 {
4
5
	public void foo() {
6
		int i= 1 - (extracted());
7
		int j= 1 - (extracted());
8
	}
9
10
	protected int extracted() {
11
		return /*[*/2 + 3/*]*/;
12
	}	
13
}
(-)resources/ExtractMethodWorkSpace/ExtractMethodTests/expression_out/A_test628.java (+13 lines)
Added Link Here
1
package expression_out;
2
3
public class A_test627 {
4
5
	public void foo() {
6
		int i= 1 - extracted();
7
		int j= 1 - extracted();
8
	}
9
10
	protected int extracted() {
11
		return 2 + 3;
12
	}	
13
}
(-)resources/ExtractMethodWorkSpace/ExtractMethodTests/expression_out/A_test629.java (+13 lines)
Added Link Here
1
package expression_out;
2
3
public class A_test627 {
4
5
	public void foo() {
6
		int i= 1 - extracted();
7
		int j= 1 - extracted();
8
	}
9
10
	protected int extracted() {
11
		return (2 + 3);
12
	}	
13
}
(-)resources/ExtractTemp/canExtract/A_test105_in.java (+7 lines)
Added Link Here
1
package p; //5, 21, 5, 26
2
3
class A {
4
	public void m() {
5
		int a= 1 - (2 + 3);
6
	}
7
}
(-)resources/ExtractTemp/canExtract/A_test105_out.java (+8 lines)
Added Link Here
1
package p; //5, 21, 5, 26
2
3
class A {
4
	public void m() {
5
		int temp= 2 + 3;
6
		int a= 1 - temp;
7
	}
8
}
(-)resources/ExtractTemp/canExtract/A_test106_in.java (+7 lines)
Added Link Here
1
package p; //5, 20, 5, 27
2
3
class A {
4
	public void m() {
5
		int a= 1 - (2 + 3);
6
	}
7
}
(-)resources/ExtractTemp/canExtract/A_test106_out.java (+8 lines)
Added Link Here
1
package p; //5, 20, 5, 27
2
3
class A {
4
	public void m() {
5
		int temp= 2 + 3;
6
		int a= 1 - temp;
7
	}
8
}
(-)resources/ExtractTemp/canExtract/A_test107_in.java (+7 lines)
Added Link Here
1
package p; //5, 22, 5, 27
2
3
class A {
4
	public void m() {
5
		int a= 1 - ((2 + 3));
6
	}
7
}
(-)resources/ExtractTemp/canExtract/A_test107_out.java (+8 lines)
Added Link Here
1
package p; //5, 22, 5, 27
2
3
class A {
4
	public void m() {
5
		int temp= 2 + 3;
6
		int a= 1 - (temp);
7
	}
8
}
(-)resources/ExtractTemp/canExtract/A_test108_in.java (+7 lines)
Added Link Here
1
package p; //5, 21, 5, 28
2
3
class A {
4
	public void m() {
5
		int a= 1 - ((2 + 3));
6
	}
7
}
(-)resources/ExtractTemp/canExtract/A_test108_out.java (+8 lines)
Added Link Here
1
package p; //5, 21, 5, 28
2
3
class A {
4
	public void m() {
5
		int temp= 2 + 3;
6
		int a= 1 - temp;
7
	}
8
}
(-)resources/ExtractTemp/canExtract/A_test109_in.java (+7 lines)
Added Link Here
1
package p; //5, 20, 5, 29
2
3
class A {
4
	public void m() {
5
		int a= 1 - ((2 + 3));
6
	}
7
}
(-)resources/ExtractTemp/canExtract/A_test109_out.java (+8 lines)
Added Link Here
1
package p; //5, 20, 5, 29
2
3
class A {
4
	public void m() {
5
		int temp= (2 + 3);
6
		int a= 1 - temp;
7
	}
8
}
(-)resources/InlineConstant/canInline/test24/out/A.java (-2 / +2 lines)
Lines 14-20 Link Here
14
				+ p.A.getCount() + A.getCount() + getCount2();
14
				+ p.A.getCount() + A.getCount() + getCount2();
15
		int ii= Consts.I + Consts.I + q.Consts.I
15
		int ii= Consts.I + Consts.I + q.Consts.I
16
				+ p.A.getCount() + A.getCount() + getCount2();
16
				+ p.A.getCount() + A.getCount() + getCount2();
17
		return (I + Consts.I + q.Consts.I
17
		return I + Consts.I + q.Consts.I
18
				+ p.A.getCount() + A.getCount() + getCount2()) + i + ii;
18
				+ p.A.getCount() + A.getCount() + getCount2() + i + ii;
19
	}
19
	}
20
}
20
}
(-)resources/InlineConstant/canInline/test33/out/A.java (-3 / +3 lines)
Lines 8-17 Link Here
8
        int f1= K - 1 - (B - 1);
8
        int f1= K - 1 - (B - 1);
9
        int f2= K - (B - 1) - (B - 1) - (B - 1);
9
        int f2= K - (B - 1) - (B - 1) - (B - 1);
10
10
11
        int x1= K + (B - 1);
11
        int x1= K + B - 1;
12
        int x2= K - (B - 1);
12
        int x2= K - (B - 1);
13
        int x3= K + 1 - (B - 1);
13
        int x3= K + 1 - (B - 1);
14
        int x4= K - 1 + (B - 1);
14
        int x4= K - 1 + B - 1;
15
        int x5= K + 1 + (B - 1) - (B - 1) - (B - 1);
15
        int x5= K + 1 + B - 1 - (B - 1) - (B - 1);
16
    }
16
    }
17
}
17
}
(-)resources/InlineConstant/canInline/test35/in/A.java (+8 lines)
Added Link Here
1
package p;
2
3
class A {
4
	static final int a = 10 + 20;
5
	void foo() {
6
		int b = 1 + a;
7
	}
8
}
(-)resources/InlineConstant/canInline/test35/out/A.java (+7 lines)
Added Link Here
1
package p;
2
3
class A {
4
	void foo() {
5
		int b = 1 + 10 + 20;
6
	}
7
}
(-)resources/InlineConstant/canInline/test36/in/A.java (+8 lines)
Added Link Here
1
package p;
2
3
class A {
4
	static final int a = 10 + 20;
5
	void foo() {
6
		int b = 1 - a;
7
	}
8
}
(-)resources/InlineConstant/canInline/test36/out/A.java (+7 lines)
Added Link Here
1
package p;
2
3
class A {
4
	void foo() {
5
		int b = 1 - (10 + 20);
6
	}
7
}
(-)resources/InlineConstant/canInline/test37/in/A.java (+7 lines)
Added Link Here
1
package p;
2
3
class A {
4
	static final long CONST = 2 * Integer.MAX_VALUE;
5
	
6
	long much = CONST * Integer.MAX_VALUE;
7
}
(-)resources/InlineConstant/canInline/test37/out/A.java (+5 lines)
Added Link Here
1
package p;
2
3
class A {
4
	long much = (long) (2 * Integer.MAX_VALUE) * Integer.MAX_VALUE;
5
}
(-)resources/InlineMethodWorkspace/TestCases/argument_out/TestArray.java (-1 / +1 lines)
Lines 7-12 Link Here
7
	}
7
	}
8
	
8
	
9
	public void main() {
9
	public void main() {
10
		int i= (new int[] {1})[0];
10
		int i= new int[] {1}[0];
11
	}
11
	}
12
}
12
}
(-)resources/InlineMethodWorkspace/TestCases/cast_in/TestInfixExpression1.java (+11 lines)
Added Link Here
1
package cast_in;
2
3
public class TestInfixExpression1 {	
4
	long x(long two) {
5
		return two * Integer.MAX_VALUE;
6
	}
7
8
	void foo() {
9
		long much = /*]*/x(1 + 1)/*[*/;
10
	}
11
}
(-)resources/InlineMethodWorkspace/TestCases/cast_in/TestInfixExpression2.java (+11 lines)
Added Link Here
1
package cast_in;
2
3
public class TestInfixExpression2 {	
4
	long x(long two) {
5
		return two * Integer.MAX_VALUE;
6
	}
7
8
	void foo() {
9
		long much = /*]*/x(2 * Integer.MAX_VALUE)/*[*/;
10
	}
11
}
(-)resources/InlineMethodWorkspace/TestCases/cast_out/TestInfixExpression1.java (+11 lines)
Added Link Here
1
package cast_out;
2
3
public class TestInfixExpression1 {	
4
	long x(long two) {
5
		return two * Integer.MAX_VALUE;
6
	}
7
8
	void foo() {
9
		long much = (long) (1 + 1) * Integer.MAX_VALUE;
10
	}
11
}
(-)resources/InlineMethodWorkspace/TestCases/cast_out/TestInfixExpression2.java (+11 lines)
Added Link Here
1
package cast_out;
2
3
public class TestInfixExpression2 {	
4
	long x(long two) {
5
		return two * Integer.MAX_VALUE;
6
	}
7
8
	void foo() {
9
		long much = (long) (2 * Integer.MAX_VALUE) * Integer.MAX_VALUE;
10
	}
11
}
(-)resources/InlineMethodWorkspace/TestCases/expression_out/TestConditionalExpression.java (-1 / +1 lines)
Lines 5-10 Link Here
5
		return k == 3 ? s.hashCode() : 3;
5
		return k == 3 ? s.hashCode() : 3;
6
	}
6
	}
7
	void f(int p) {
7
	void f(int p) {
8
		int u = (p == 3 ? this.hashCode() : 3);
8
		int u = p == 3 ? this.hashCode() : 3;
9
	}
9
	}
10
}
10
}
(-)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
}
(-)resources/InlineTemp/canInline/A_test36_in.java (-2 lines)
Lines 1-7 Link Here
1
package p;
1
package p;
2
2
3
import java.util.List;
4
5
class A {
3
class A {
6
	void x() {
4
	void x() {
7
		long two = 1 + 1;
5
		long two = 1 + 1;
(-)resources/InlineTemp/canInline/A_test36_out.java (-2 lines)
Lines 1-7 Link Here
1
package p;
1
package p;
2
2
3
import java.util.List;
4
5
class A {
3
class A {
6
	void x() {
4
	void x() {
7
		long much = (long) (1 + 1) * Integer.MAX_VALUE;
5
		long much = (long) (1 + 1) * Integer.MAX_VALUE;
(-)resources/InlineTemp/canInline/A_test41_in.java (+8 lines)
Added Link Here
1
package p;
2
3
class A {
4
	void foo() {
5
		int a = 10 + 20;
6
		int b = 1 + a;
7
	}
8
}
(-)resources/InlineTemp/canInline/A_test41_out.java (+7 lines)
Added Link Here
1
package p;
2
3
class A {
4
	void foo() {
5
		int b = 1 + 10 + 20;
6
	}
7
}
(-)resources/InlineTemp/canInline/A_test42_in.java (+8 lines)
Added Link Here
1
package p;
2
3
class A {
4
	void foo() {
5
		int a = 10 + 20;
6
		int b = 1 - a;
7
	}
8
}
(-)resources/InlineTemp/canInline/A_test42_out.java (+7 lines)
Added Link Here
1
package p;
2
3
class A {
4
	void foo() {
5
		int b = 1 - (10 + 20);
6
	}
7
}
(-)resources/InlineTemp/canInline/A_test43_in.java (+8 lines)
Added Link Here
1
package p;
2
3
class A {
4
	void x() {
5
		long two = 2 * Integer.MAX_VALUE;
6
		long much = two * Integer.MAX_VALUE;
7
	}
8
}
(-)resources/InlineTemp/canInline/A_test43_out.java (+7 lines)
Added Link Here
1
package p;
2
3
class A {
4
	void x() {
5
		long much = (long) (2 * Integer.MAX_VALUE) * Integer.MAX_VALUE;
6
	}
7
}
(-)resources/IntroduceParameter/simple/Expression1.java (+12 lines)
Added Link Here
1
//selection: 7, 21, 7, 26
2
//name: i -> first
3
package simple;
4
5
public class Expression1 {
6
	public void m() {
7
		int a= 1 - (2 + 3);
8
	}
9
	public void use() {
10
		m();
11
	}
12
}
(-)resources/IntroduceParameter/simple/Expression2.java (+12 lines)
Added Link Here
1
//selection: 7, 20, 7, 27
2
//name: i -> first
3
package simple;
4
5
public class Expression2 {
6
	public void m() {
7
		int a= 1 - (2 + 3);
8
	}
9
	public void use() {
10
		m();
11
	}
12
}
(-)resources/IntroduceParameter/simple/Expression3.java (+12 lines)
Added Link Here
1
//selection: 7, 22, 7, 27
2
//name: i -> first
3
package simple;
4
5
public class Expression3 {
6
	public void m() {
7
		int a= 1 - ((2 + 3));
8
	}
9
	public void use() {
10
		m();
11
	}
12
}
(-)resources/IntroduceParameter/simple/Expression4.java (+12 lines)
Added Link Here
1
//selection: 7, 21, 7, 28
2
//name: i -> first
3
package simple;
4
5
public class Expression4 {
6
	public void m() {
7
		int a= 1 - ((2 + 3));
8
	}
9
	public void use() {
10
		m();
11
	}
12
}
(-)resources/IntroduceParameter/simple/Expression5.java (+12 lines)
Added Link Here
1
//selection: 7, 20, 7, 29
2
//name: i -> first
3
package simple;
4
5
public class Expression5 {
6
	public void m() {
7
		int a= 1 - ((2 + 3));
8
	}
9
	public void use() {
10
		m();
11
	}
12
}
(-)resources/IntroduceParameter/simple/out/Expression1.java (+12 lines)
Added Link Here
1
//selection: 7, 21, 7, 26
2
//name: i -> first
3
package simple.out;
4
5
public class Expression1 {
6
	public void m(int first) {
7
		int a= 1 - first;
8
	}
9
	public void use() {
10
		m(2 + 3);
11
	}
12
}
(-)resources/IntroduceParameter/simple/out/Expression2.java (+12 lines)
Added Link Here
1
//selection: 7, 20, 7, 27
2
//name: i -> first
3
package simple.out;
4
5
public class Expression2 {
6
	public void m(int first) {
7
		int a= 1 - first;
8
	}
9
	public void use() {
10
		m(2 + 3);
11
	}
12
}
(-)resources/IntroduceParameter/simple/out/Expression3.java (+12 lines)
Added Link Here
1
//selection: 7, 22, 7, 27
2
//name: i -> first
3
package simple.out;
4
5
public class Expression3 {
6
	public void m(int first) {
7
		int a= 1 - (first);
8
	}
9
	public void use() {
10
		m(2 + 3);
11
	}
12
}
(-)resources/IntroduceParameter/simple/out/Expression4.java (+12 lines)
Added Link Here
1
//selection: 7, 21, 7, 28
2
//name: i -> first
3
package simple.out;
4
5
public class Expression4 {
6
	public void m(int first) {
7
		int a= 1 - first;
8
	}
9
	public void use() {
10
		m(2 + 3);
11
	}
12
}
(-)resources/IntroduceParameter/simple/out/Expression5.java (+12 lines)
Added Link Here
1
//selection: 7, 20, 7, 29
2
//name: i -> first
3
package simple.out;
4
5
public class Expression5 {
6
	public void m(int first) {
7
		int a= 1 - first;
8
	}
9
	public void use() {
10
		m((2 + 3));
11
	}
12
}
(-)resources/SefWorkSpace/SefTests/object_in/TestCompoundWrite4.java (+9 lines)
Added Link Here
1
package object_in;
2
3
public class TestCompoundWrite4 {
4
	int field;
5
	
6
	public void foo() {
7
		field+= 1 + 2;
8
	}
9
}
(-)resources/SefWorkSpace/SefTests/object_out/TestCompoundWrite4.java (+17 lines)
Added Link Here
1
package object_out;
2
3
public class TestCompoundWrite4 {
4
	private int field;
5
	
6
	public void foo() {
7
		setField(getField() + (1 + 2));
8
	}
9
10
	int getField() {
11
		return field;
12
	}
13
14
	void setField(int field) {
15
		this.field = field;
16
	}
17
}
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/ExtractConstantTests.java (-1 / +21 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 325-330 Link Here
325
		helper1(3, 19, 3, 24, true, false, "S_ALL", "ALL");
325
		helper1(3, 19, 3, 24, true, false, "S_ALL", "ALL");
326
	}
326
	}
327
327
328
	public void test39() throws Exception { // bug 335173
329
		helper1(5, 21, 5, 26, false, false, "CONSTANT", "INT");
330
	}
331
332
	public void test40() throws Exception { // bug 335173
333
		helper1(5, 20, 5, 27, false, false, "CONSTANT", "INT");
334
	}
335
336
	public void test41() throws Exception { // bug 335173
337
		helper1(5, 22, 5, 27, false, false, "CONSTANT", "INT");
338
	}
339
340
	public void test42() throws Exception { // bug 335173
341
		helper1(5, 21, 5, 28, false, false, "CONSTANT", "INT");
342
	}
343
344
	public void test43() throws Exception { // bug 335173
345
		helper1(5, 20, 5, 29, false, false, "CONSTANT", "INT");
346
	}
347
328
	public void testZeroLengthSelection0() throws Exception {
348
	public void testZeroLengthSelection0() throws Exception {
329
		helper1(5, 18, 5, 18, false, false, "CONSTANT", "_100");
349
		helper1(5, 18, 5, 18, false, false, "CONSTANT", "_100");
330
	}
350
	}
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/ExtractMethodTests.java (-1 / +21 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 1445-1450 Link Here
1445
		expressionTest();
1445
		expressionTest();
1446
	}
1446
	}
1447
1447
1448
	public void test625() throws Exception {
1449
		expressionTest();
1450
	}
1451
1452
	public void test626() throws Exception {
1453
		expressionTest();
1454
	}
1455
1456
	public void test627() throws Exception {
1457
		expressionTest();
1458
	}
1459
1460
	public void test628() throws Exception {
1461
		expressionTest();
1462
	}
1463
1464
	public void test629() throws Exception {
1465
		expressionTest();
1466
	}
1467
1448
	//---- Test nested methods and constructor
1468
	//---- Test nested methods and constructor
1449
1469
1450
	public void test650() throws Exception {
1470
	public void test650() throws Exception {
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/ExtractTempTests.java (-1 / +26 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 656-661 Link Here
656
		helper1(6, 17, 6, 24, true, false, "temp", "i");
656
		helper1(6, 17, 6, 24, true, false, "temp", "i");
657
	}
657
	}
658
658
659
	public void test105() throws Exception {
660
		//test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
661
		helper1(5, 21, 5, 26, true, false, "temp", "i");
662
	}
663
664
	public void test106() throws Exception {
665
		//test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
666
		helper1(5, 20, 5, 27, true, false, "temp", "i");
667
	}
668
669
	public void test107() throws Exception {
670
		//test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
671
		helper1(5, 22, 5, 27, true, false, "temp", "i");
672
	}
673
674
	public void test108() throws Exception {
675
		//test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
676
		helper1(5, 21, 5, 28, true, false, "temp", "i");
677
	}
678
679
	public void test109() throws Exception {
680
		//test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
681
		helper1(5, 20, 5, 29, true, false, "temp", "i");
682
	}
683
659
	public void testZeroLengthSelection0() throws Exception {
684
	public void testZeroLengthSelection0() throws Exception {
660
//		printTestDisabledMessage("test for bug 30146");
685
//		printTestDisabledMessage("test for bug 30146");
661
		helper1(4, 18, 4, 18, true, false, "temp", "j");
686
		helper1(4, 18, 4, 18, true, false, "temp", "j");
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/InlineConstantTests.java (-1 / +13 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 300-305 Link Here
300
		helper1("p.A", 4, 24, 4, 25, true, true);
300
		helper1("p.A", 4, 24, 4, 25, true, true);
301
	}
301
	}
302
	
302
	
303
	public void test35() throws Exception { // test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
304
		helper1("p.A", 4, 22, 4, 23, true, true);
305
	}
306
307
	public void test36() throws Exception { // test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
308
		helper1("p.A", 4, 22, 4, 23, true, true);
309
	}
310
311
	public void test37() throws Exception { // test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
312
		helper1("p.A", 4, 23, 4, 28, true, true);
313
	}
314
303
	// -- testing failing preconditions
315
	// -- testing failing preconditions
304
316
305
	public void testFail0() throws Exception {
317
	public void testFail0() throws Exception {
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java (-1 / +13 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 836-841 Link Here
836
		performCastTest();
836
		performCastTest();
837
	}
837
	}
838
838
839
	public void testInfixExpression1() throws Exception {
840
		performCastTest();
841
	}
842
843
	public void testInfixExpression2() throws Exception {
844
		performCastTest();
845
	}
846
839
	/* *********************** Enum Tests ******************************* */
847
	/* *********************** Enum Tests ******************************* */
840
848
841
	private void performEnumTest() throws Exception {
849
	private void performEnumTest() throws Exception {
Lines 956-961 Link Here
956
		performOperatorTest();
964
		performOperatorTest();
957
	}
965
	}
958
966
967
	public void testDiffPlus() throws Exception {
968
		performOperatorTest();
969
	}
970
959
	public void testTimesPlus() throws Exception {
971
	public void testTimesPlus() throws Exception {
960
		performOperatorTest();
972
		performOperatorTest();
961
	}
973
	}
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/InlineTempTests.java (-2 / +17 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 277-283 Link Here
277
	
277
	
278
	public void test36() throws Exception{
278
	public void test36() throws Exception{
279
		// parenthesize complex cast expression
279
		// parenthesize complex cast expression
280
		helper1(8, 21, 8, 24);
280
		helper1(6, 21, 6, 24);
281
	}
281
	}
282
	
282
	
283
	public void test37() throws Exception{
283
	public void test37() throws Exception{
Lines 300-305 Link Here
300
		helper1(5, 43, 5, 46);
300
		helper1(5, 43, 5, 46);
301
	}
301
	}
302
	
302
	
303
	public void test41() throws Exception {
304
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
305
		helper1(5, 13, 5, 14);
306
	}
307
308
	public void test42() throws Exception {
309
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
310
		helper1(5, 13, 5, 14);
311
	}
312
313
	public void test43() throws Exception {
314
		// parenthesize complex cast expression https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
315
		helper1(6, 21, 6, 24);
316
	}
317
303
	//------
318
	//------
304
319
305
	public void testFail0() throws Exception{
320
	public void testFail0() throws Exception{
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/IntroduceParameterTests.java (-1 / +21 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 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 153-158 Link Here
153
		performOK();
153
		performOK();
154
	}
154
	}
155
155
156
	public void testSimple_Expression1() throws Exception {
157
		performOK();
158
	}
159
160
	public void testSimple_Expression2() throws Exception {
161
		performOK();
162
	}
163
164
	public void testSimple_Expression3() throws Exception {
165
		performOK();
166
	}
167
168
	public void testSimple_Expression4() throws Exception {
169
		performOK();
170
	}
171
172
	public void testSimple_Expression5() throws Exception {
173
		performOK();
174
	}
175
156
	public void testSimple_NewInstance1() throws Exception {
176
	public void testSimple_NewInstance1() throws Exception {
157
		performOK();
177
		performOK();
158
	}
178
	}
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/SefTests.java (-1 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 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 238-243 Link Here
238
		objectTest("field");
238
		objectTest("field");
239
	}
239
	}
240
240
241
	public void testCompoundWrite4() throws Exception {
242
		objectTest("field");
243
	}
244
241
	public void testFinalField() throws Exception {
245
	public void testFinalField() throws Exception {
242
		objectTest("field");
246
		objectTest("field");
243
	}
247
	}

Return to bug 335173