|
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.Assignment; |
| 19 |
import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; |
| 20 |
import org.eclipse.jdt.core.dom.ConditionalExpression; |
| 21 |
import org.eclipse.jdt.core.dom.EnhancedForStatement; |
| 22 |
import org.eclipse.jdt.core.dom.Expression; |
| 23 |
import org.eclipse.jdt.core.dom.ITypeBinding; |
| 24 |
import org.eclipse.jdt.core.dom.IfStatement; |
| 25 |
import org.eclipse.jdt.core.dom.InfixExpression; |
| 26 |
import org.eclipse.jdt.core.dom.InfixExpression.Operator; |
| 27 |
import org.eclipse.jdt.core.dom.ParenthesizedExpression; |
| 28 |
import org.eclipse.jdt.core.dom.ReturnStatement; |
| 29 |
import org.eclipse.jdt.core.dom.SingleVariableDeclaration; |
| 30 |
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; |
| 31 |
import org.eclipse.jdt.core.dom.SwitchCase; |
| 32 |
import org.eclipse.jdt.core.dom.SwitchStatement; |
| 33 |
import org.eclipse.jdt.core.dom.VariableDeclarationFragment; |
| 34 |
|
| 35 |
import org.eclipse.jdt.internal.corext.refactoring.code.OperatorPrecedence; |
| 36 |
|
| 37 |
public class MissingNecessaryParenthesesChecker { |
| 38 |
|
| 39 |
/* |
| 40 |
* Get the expression wrapped by the parentheses |
| 41 |
* i.e. ((((expression)))) -> expression |
| 42 |
*/ |
| 43 |
private static Expression getExpression(ParenthesizedExpression node) { |
| 44 |
Expression expression= node.getExpression(); |
| 45 |
while (expression instanceof ParenthesizedExpression) { |
| 46 |
expression= ((ParenthesizedExpression)expression).getExpression(); |
| 47 |
} |
| 48 |
return expression; |
| 49 |
} |
| 50 |
|
| 51 |
private static boolean expressionTypeNeedsParentheses(Expression expression) { |
| 52 |
//TODO: is this list correct ? is this list complete ? |
| 53 |
int type= expression.getNodeType(); |
| 54 |
return type == ASTNode.INFIX_EXPRESSION || type == ASTNode.CONDITIONAL_EXPRESSION || |
| 55 |
type == ASTNode.PREFIX_EXPRESSION || type == ASTNode.POSTFIX_EXPRESSION || |
| 56 |
type == ASTNode.CAST_EXPRESSION || type == ASTNode.INSTANCEOF_EXPRESSION || |
| 57 |
type == ASTNode.ARRAY_CREATION; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* Do all operands in expression have same type |
| 62 |
*/ |
| 63 |
private static boolean isAllOperandsHaveSameType(InfixExpression expression) { |
| 64 |
ITypeBinding binding= expression.getLeftOperand().resolveTypeBinding(); |
| 65 |
if (binding == null) |
| 66 |
return false; |
| 67 |
|
| 68 |
ITypeBinding current= expression.getRightOperand().resolveTypeBinding(); |
| 69 |
if (binding != current) |
| 70 |
return false; |
| 71 |
|
| 72 |
for (Iterator iterator= expression.extendedOperands().iterator(); iterator.hasNext();) { |
| 73 |
Expression operand= (Expression) iterator.next(); |
| 74 |
current= operand.resolveTypeBinding(); |
| 75 |
if (binding != current) |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Is the given expression associative? |
| 84 |
* <p> |
| 85 |
* This is true if and only if:<br> |
| 86 |
* <code>left operator (right) == (right) operator left == right operator left</code> |
| 87 |
* </p> |
| 88 |
* |
| 89 |
* @param expression the expression to inspect |
| 90 |
* @return true if expression is associative |
| 91 |
*/ |
| 92 |
private static boolean isAssociative(InfixExpression expression) { |
| 93 |
Operator operator= expression.getOperator(); |
| 94 |
|
| 95 |
if (operator == InfixExpression.Operator.PLUS) |
| 96 |
return isAllOperandsHaveSameType(expression); |
| 97 |
|
| 98 |
if (operator == InfixExpression.Operator.CONDITIONAL_AND) |
| 99 |
return true; |
| 100 |
|
| 101 |
if (operator == InfixExpression.Operator.CONDITIONAL_OR) |
| 102 |
return true; |
| 103 |
|
| 104 |
if (operator == InfixExpression.Operator.AND) |
| 105 |
return true; |
| 106 |
|
| 107 |
if (operator == InfixExpression.Operator.OR) |
| 108 |
return true; |
| 109 |
|
| 110 |
if (operator == InfixExpression.Operator.XOR) |
| 111 |
return true; |
| 112 |
|
| 113 |
if (operator == InfixExpression.Operator.TIMES) |
| 114 |
return true; |
| 115 |
|
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
private static boolean isIntegerNumber(String name) { |
| 120 |
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$ |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Can the parentheses be removed from the parenthesized expression ? |
| 125 |
* |
| 126 |
* <p> |
| 127 |
* The parenthesized expression must not be an unparented node. |
| 128 |
* </p> |
| 129 |
* |
| 130 |
* @param node the parenthesized expression |
| 131 |
* @return <code>true</code> if parentheses can be removed, <code>false</code> otherwise. |
| 132 |
*/ |
| 133 |
public static boolean canRemoveParentheses(Expression node) { |
| 134 |
ASTNode parent= node.getParent(); |
| 135 |
return canRemoveParentheses(node, parent, node.getLocationInParent()); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Can the parentheses be removed from the parenthesized expression ? |
| 140 |
* |
| 141 |
* <p> |
| 142 |
* The parenthesized expression can be an unparented node. |
| 143 |
* </p> |
| 144 |
* |
| 145 |
* @param node the parenthesized expression |
| 146 |
* @param parent node's parent node |
| 147 |
* @param locationInParent location of node in the parent |
| 148 |
* @return <code>true</code> if parentheses can be removed, <code>false</code> otherwise. |
| 149 |
*/ |
| 150 |
public static boolean canRemoveParentheses(Expression node, ASTNode parent, StructuralPropertyDescriptor locationInParent) { |
| 151 |
if (!(node instanceof ParenthesizedExpression)) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
return !isParenthesesNeeded(getExpression((ParenthesizedExpression)node), parent, locationInParent); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Does the node need parentheses ? |
| 159 |
* |
| 160 |
* <p> |
| 161 |
* The node must not be an unparented node. |
| 162 |
* </p> |
| 163 |
* |
| 164 |
* @param node the node |
| 165 |
* @return <code>true</code> if node needs parentheses, <code>false</code> otherwise. |
| 166 |
*/ |
| 167 |
public static boolean isParenthesesNeeded(Expression node) { |
| 168 |
ASTNode parent= node.getParent(); |
| 169 |
return isParenthesesNeeded(node, parent, node.getLocationInParent()); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Does the node need parentheses ? |
| 174 |
* |
| 175 |
* <p> |
| 176 |
* The node can be an unparented node. |
| 177 |
* </p> |
| 178 |
* |
| 179 |
* @param node the node |
| 180 |
* @param parent node's parent node |
| 181 |
* @param locationInParent location of node in the parent |
| 182 |
* @return <code>true</code> if node needs parentheses, <code>false</code> otherwise. |
| 183 |
*/ |
| 184 |
public static boolean isParenthesesNeeded(Expression node, ASTNode parent, StructuralPropertyDescriptor locationInParent) { |
| 185 |
if (node instanceof Assignment) //for esthetic reasons |
| 186 |
return true; |
| 187 |
|
| 188 |
if (!expressionTypeNeedsParentheses(node)) |
| 189 |
return false; |
| 190 |
|
| 191 |
if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) { |
| 192 |
// e.g. argument lists of MethodInvocation, ClassInstanceCreation, ... |
| 193 |
return false; |
| 194 |
} else if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY) { |
| 195 |
return false; |
| 196 |
} else if (locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY) { |
| 197 |
return false; |
| 198 |
} else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) { |
| 199 |
return false; |
| 200 |
} else if (locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY) { |
| 201 |
return false; |
| 202 |
} else if (locationInParent == IfStatement.EXPRESSION_PROPERTY) { |
| 203 |
return false; |
| 204 |
} else if (locationInParent == SwitchStatement.EXPRESSION_PROPERTY) { |
| 205 |
return false; |
| 206 |
} else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY) { |
| 207 |
return false; |
| 208 |
} else if (locationInParent == ArrayAccess.INDEX_PROPERTY) { |
| 209 |
return false; |
| 210 |
} else if (parent instanceof Expression) { |
| 211 |
Expression parentExpression= (Expression)parent; |
| 212 |
if (parentExpression instanceof ParenthesizedExpression) |
| 213 |
return false; |
| 214 |
|
| 215 |
Expression expression= node; |
| 216 |
|
| 217 |
int expressionPrecedence= OperatorPrecedence.getExpressionPrecedence(expression); |
| 218 |
int parentPrecedence= OperatorPrecedence.getExpressionPrecedence(parentExpression); |
| 219 |
|
| 220 |
if (expressionPrecedence > parentPrecedence) |
| 221 |
//(opEx) opParent and opEx binds more -> can safely remove |
| 222 |
return false; |
| 223 |
|
| 224 |
if (expressionPrecedence < parentPrecedence) |
| 225 |
//(opEx) opParent and opEx binds less -> do not remove |
| 226 |
return true; |
| 227 |
|
| 228 |
//(opEx) opParent binds equal |
| 229 |
|
| 230 |
if (parentExpression instanceof InfixExpression) { |
| 231 |
InfixExpression parentInfix= (InfixExpression)parentExpression; |
| 232 |
if (locationInParent == InfixExpression.LEFT_OPERAND_PROPERTY) { |
| 233 |
//we have (expr op expr) op expr |
| 234 |
//infix expressions are evaluated from left to right -> can safely remove |
| 235 |
return false; |
| 236 |
} else if (isAssociative(parentInfix)) { |
| 237 |
//we have parent op (expr op expr) and op is associative |
| 238 |
//left op (right) == (right) op left == right op left |
| 239 |
if (expression instanceof InfixExpression) { |
| 240 |
InfixExpression infixExpression= (InfixExpression)expression; |
| 241 |
Operator operator= infixExpression.getOperator(); |
| 242 |
if (parentInfix.getOperator() != InfixExpression.Operator.TIMES) |
| 243 |
return false; |
| 244 |
|
| 245 |
if (operator == InfixExpression.Operator.TIMES) |
| 246 |
// x * (y * z) == x * y * z |
| 247 |
return false; |
| 248 |
|
| 249 |
if (operator == InfixExpression.Operator.REMAINDER) |
| 250 |
// x * (y % z) != x * y % z |
| 251 |
return true; |
| 252 |
|
| 253 |
//x * (y / z) == z * y / z iff no rounding |
| 254 |
ITypeBinding binding= infixExpression.resolveTypeBinding(); |
| 255 |
if (binding == null) |
| 256 |
return true; |
| 257 |
|
| 258 |
if (!binding.isPrimitive()) |
| 259 |
return true; |
| 260 |
|
| 261 |
String name= binding.getName(); |
| 262 |
if (isIntegerNumber(name)) |
| 263 |
//rounding involved |
| 264 |
return true; |
| 265 |
|
| 266 |
return false; |
| 267 |
} |
| 268 |
return false; |
| 269 |
} else { |
| 270 |
return true; |
| 271 |
} |
| 272 |
} else if (parentExpression instanceof ConditionalExpression) { |
| 273 |
if (locationInParent != ConditionalExpression.ELSE_EXPRESSION_PROPERTY) { |
| 274 |
return true; |
| 275 |
} |
| 276 |
} |
| 277 |
return false; |
| 278 |
} |
| 279 |
return true; |
| 280 |
} |
| 281 |
} |