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 (-1 / +2 lines)
Lines 37-42 Link Here
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 336-342 Link Here
336
			castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
337
			castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
337
		if (castTo != null) {
338
		if (castTo != null) {
338
			CastExpression cast= ast.newCastExpression();
339
			CastExpression cast= ast.newCastExpression();
339
			if (ASTNodes.needsParentheses(expression)) {
340
			if (NecessaryParenthesesChecker.needsParentheses(expression)) {
340
				ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
341
				ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
341
				parenthesized.setExpression(expression);
342
				parenthesized.setExpression(expression);
342
				cast.setExpression(parenthesized);
343
				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 (+292 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(InfixExpression 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
	/*
139
	 * Is the given expression associative?
140
	 * 
141
	 * This is true if and only if:<br>
142
	 * <code>left operator (right) == (right) operator left == right operator left</code>
143
	 */
144
	private static boolean isAssociative(InfixExpression expression) {
145
		Operator operator= expression.getOperator();
146
147
		if (operator == InfixExpression.Operator.PLUS || operator == InfixExpression.Operator.TIMES)
148
			return isExpressionIntegerType(expression) && isAllOperandsHaveSameType(expression);
149
150
		if (operator == InfixExpression.Operator.CONDITIONAL_AND
151
				|| operator == InfixExpression.Operator.CONDITIONAL_OR
152
				|| operator == InfixExpression.Operator.AND
153
				|| operator == InfixExpression.Operator.OR
154
				|| operator == InfixExpression.Operator.XOR)
155
			return true;
156
	
157
		return false;
158
	}
159
160
	private static boolean isIntegerNumber(String name) {
161
		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$
162
	}
163
164
	private static boolean needsParenthesesInInfixExpression(Expression expression, InfixExpression parentInfix, StructuralPropertyDescriptor locationInParent) {
165
		if (locationInParent == InfixExpression.LEFT_OPERAND_PROPERTY) {
166
			//we have (expr op expr) op expr
167
			//infix expressions are evaluated from left to right -> parentheses not needed
168
			return false;
169
		} else if (isAssociative(parentInfix)) {
170
			//we have parent op (expr op expr) and op is associative
171
			//left op (right) == (right) op left == right op left
172
			if (expression instanceof InfixExpression) {
173
				InfixExpression infixExpression= (InfixExpression)expression;
174
				Operator operator= infixExpression.getOperator();
175
				if (parentInfix.getOperator() != InfixExpression.Operator.TIMES)
176
					return false;
177
	
178
				if (operator == InfixExpression.Operator.TIMES)
179
					// x * (y * z) == x * y * z
180
					return false;
181
	
182
				if (operator == InfixExpression.Operator.REMAINDER || operator == InfixExpression.Operator.DIVIDE)
183
					// x * (y % z) != x * y % z , x * (y / z) == x * y / z rounding involved
184
					return true;
185
	
186
				return false;
187
			}
188
			return false;
189
		} else {
190
			return true;
191
		}
192
	}
193
194
	/**
195
	 * Can the parentheses be removed from the parenthesized expression ?
196
	 * 
197
	 * <p>
198
	 * <b>Note:</b> The parenthesized expression must not be an unparented node.
199
	 * </p>
200
	 * 
201
	 * @param expression the parenthesized expression
202
	 * @return <code>true</code> if parentheses can be removed, <code>false</code> otherwise.
203
	 */
204
	public static boolean canRemoveParentheses(Expression expression) {
205
		return canRemoveParentheses(expression, expression.getParent(), expression.getLocationInParent());
206
	}
207
208
	/**
209
	 * Can the parentheses be removed from the parenthesized expression ?
210
	 * 
211
	 * <p>
212
	 * <b>Note:</b> The parenthesized expression can be an unparented node.
213
	 * </p>
214
	 * 
215
	 * @param expression the parenthesized expression
216
	 * @param parent the parent node
217
	 * @param locationInParent location of expression in the parent
218
	 * @return <code>true</code> if parentheses can be removed, <code>false</code> otherwise.
219
	 */
220
	public static boolean canRemoveParentheses(Expression expression, ASTNode parent, StructuralPropertyDescriptor locationInParent) {
221
		if (!(expression instanceof ParenthesizedExpression)) {
222
			return false;
223
		}
224
		return !needsParentheses(getExpression((ParenthesizedExpression)expression), parent, locationInParent);
225
	}
226
227
	/**
228
	 * Does the expression need parentheses ?
229
	 * 
230
	 * <p>
231
	 * <b>Note:</b> The expression must not be an unparented node.
232
	 * </p>
233
	 * 
234
	 * @param expression the expression
235
	 * @return <code>true</code> if the expression needs parentheses, <code>false</code> otherwise.
236
	 */
237
	public static boolean needsParentheses(Expression expression) {
238
		return needsParentheses(expression, expression.getParent(), expression.getLocationInParent());
239
	}
240
241
	/**
242
	 * Does the expression need parentheses ?
243
	 * 
244
	 * <p>
245
	 * <b>Note:</b> The expression can be an unparented node.
246
	 * </p>
247
	 * 
248
	 * @param expression the expression
249
	 * @param parent the parent node
250
	 * @param locationInParent location of expression in the parent
251
	 * @return <code>true</code> if the expression needs parentheses, <code>false</code> otherwise.
252
	 */
253
	public static boolean needsParentheses(Expression expression, ASTNode parent, StructuralPropertyDescriptor locationInParent) {
254
		if (!expressionTypeNeedsParentheses(expression))
255
			return false;
256
257
		if (!locationNeedsParentheses(locationInParent)) {
258
			return false;
259
		}
260
261
		if (parent instanceof Expression) {
262
			Expression parentExpression= (Expression)parent;
263
			if (parentExpression instanceof ParenthesizedExpression)
264
				return false;
265
266
			int expressionPrecedence= OperatorPrecedence.getExpressionPrecedence(expression);
267
			int parentPrecedence= OperatorPrecedence.getExpressionPrecedence(parentExpression);
268
269
			if (expressionPrecedence > parentPrecedence)
270
				//(opEx) opParent and opEx binds more -> parentheses not needed
271
				return false;
272
273
			if (expressionPrecedence < parentPrecedence)
274
				//(opEx) opParent and opEx binds less -> parentheses needed
275
				return true;
276
277
			//(opEx) opParent binds equal
278
279
			if (parentExpression instanceof InfixExpression) {
280
				return needsParenthesesInInfixExpression(expression, (InfixExpression)parentExpression, locationInParent);
281
			}
282
283
			if (parentExpression instanceof ConditionalExpression && locationInParent == ConditionalExpression.EXPRESSION_PROPERTY) {
284
				return true;
285
			}
286
287
			return false;
288
		}
289
290
		return true;
291
	}
292
}
(-)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)) {
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)) {
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/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, reference.getParent(), reference.getLocationInParent())) { //TODO: this check is not really needed, and can be safely removed... i think
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 (-3 / +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-339 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
		boolean brackets= NecessaryParenthesesChecker.needsParentheses(copy, reference.getParent(), reference.getLocationInParent());
337
		if (brackets) {
338
		if (brackets) {
338
			ParenthesizedExpression parentExpr= rewrite.getAST().newParenthesizedExpression();
339
			ParenthesizedExpression parentExpr= rewrite.getAST().newParenthesizedExpression();
339
			parentExpr.setExpression(copy);
340
			parentExpr.setExpression(copy);
Lines 367-373 Link Here
367
		ITypeBinding explicitCast= ASTNodes.getExplicitCast(initializer, reference);
368
		ITypeBinding explicitCast= ASTNodes.getExplicitCast(initializer, reference);
368
		if (explicitCast != null) {
369
		if (explicitCast != null) {
369
			CastExpression cast= ast.newCastExpression();
370
			CastExpression cast= ast.newCastExpression();
370
			if (ASTNodes.substituteMustBeParenthesized(copy, cast)) {
371
			if (NecessaryParenthesesChecker.needsParentheses(copy, reference.getParent(), reference.getLocationInParent())) { //TODO: this definitley does not work, and is like the case in InlineConstantRefactoring
371
				ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
372
				ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
372
				parenthesized.setExpression(copy);
373
				parenthesized.setExpression(copy);
373
				copy= parenthesized;
374
				copy= parenthesized;
(-)core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/SourceProvider.java (-16 / +7 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(newExpression, element.getParent(), element.getLocationInParent())) { //TODO: this definitley does not work, and is like the case in InlineConstantRefactoring
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
						newExpression= createParenthesizedExpression(cast, ast);
465
					} else if (argumentNeedsParenthesis(expression, parameter)) {
456
					} else if (NecessaryParenthesesChecker.needsParentheses(expression, element.getParent(), element.getLocationInParent())) {
466
						newExpression= createParenthesizedExpression(newExpression, ast);
457
						newExpression= createParenthesizedExpression(newExpression, ast);
467
					}
458
					}
468
					rewriter.replace(element, newExpression, null);
459
					rewriter.replace(element, newExpression, null);
(-)core refactoring/org/eclipse/jdt/internal/corext/refactoring/sef/AccessAnalyzer.java (-1 / +2 lines)
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
				boolean needsParentheses= NecessaryParenthesesChecker.needsParentheses((node.getRightHandSide()));
142
				InfixExpression exp= ast.newInfixExpression();
143
				InfixExpression exp= ast.newInfixExpression();
143
				exp.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
144
				exp.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
144
				MethodInvocation getter= ast.newMethodInvocation();
145
				MethodInvocation getter= ast.newMethodInvocation();
(-)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 / +655 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
5940
	public void testRemoveQualifier01() throws Exception {
6594
	public void testRemoveQualifier01() throws Exception {
5941
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
6595
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
5942
		StringBuffer buf= new StringBuffer();
6596
		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/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/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/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_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/SefWorkSpace/SefTests/object_out/TestCompoundWrite2.java (-1 / +1 lines)
Lines 4-10 Link Here
4
	private String field;
4
	private String field;
5
	
5
	
6
	public void foo() {
6
	public void foo() {
7
		setField(getField() + ("d" + "e"));
7
		setField(getField() + "d" + "e");
8
	}
8
	}
9
9
10
	String getField() {
10
	String getField() {
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/InlineConstantTests.java (-1 / +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 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
303
	// -- testing failing preconditions
311
	// -- testing failing preconditions
304
312
305
	public void testFail0() throws Exception {
313
	public void testFail0() throws Exception {
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTestSetup.java (-1 / +1 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
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java (-1 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 956-961 Link Here
956
		performOperatorTest();
956
		performOperatorTest();
957
	}
957
	}
958
958
959
	public void testDiffPlus() throws Exception {
960
		performOperatorTest();
961
	}
962
959
	public void testTimesPlus() throws Exception {
963
	public void testTimesPlus() throws Exception {
960
		performOperatorTest();
964
		performOperatorTest();
961
	}
965
	}
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/InlineTempTests.java (-1 / +11 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(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
303
	//------
313
	//------
304
314
305
	public void testFail0() throws Exception{
315
	public void testFail0() throws Exception{

Return to bug 335173