some binaryexpression usages removed

This commit is contained in:
Alexey Kudravtsev
2011-07-15 14:10:14 +04:00
parent ccf07d6686
commit a2c55164f2
18 changed files with 117 additions and 160 deletions
@@ -296,28 +296,6 @@ public class EvaluatorBuilderImpl implements EvaluatorBuilder {
}
}
@Override
public void visitBinaryExpression(PsiBinaryExpression expression) {
if (LOG.isDebugEnabled()) {
LOG.debug("visitBinaryExpression " + expression);
}
final PsiExpression lOperand = expression.getLOperand();
lOperand.accept(this);
Evaluator lResult = myResult;
final PsiExpression rOperand = expression.getROperand();
if(rOperand == null) {
throwEvaluateException(DebuggerBundle.message("evaluation.error.invalid.expression", expression.getText())); return;
}
rOperand.accept(this);
Evaluator rResult = myResult;
IElementType opType = expression.getOperationTokenType();
PsiType expressionExpectedType = expression.getType();
if (expressionExpectedType == null) {
throwEvaluateException(DebuggerBundle.message("evaluation.error.unknown.expression.type", expression.getText()));
}
myResult = createBinaryEvaluator(lResult, lOperand.getType(), rResult, rOperand.getType(), opType, expressionExpectedType);
}
@Override
public void visitPolyadicExpression(PsiPolyadicExpression wideExpression) {
if (LOG.isDebugEnabled()) {
@@ -336,12 +314,13 @@ public class EvaluatorBuilderImpl implements EvaluatorBuilder {
expression.accept(this);
Evaluator rResult = myResult;
IElementType opType = wideExpression.getOperationTokenType();
PsiType expressionExpectedType = expression.getType();
if (expressionExpectedType == null) {
PsiType rType = expression.getType();
if (rType == null) {
throwEvaluateException(DebuggerBundle.message("evaluation.error.unknown.expression.type", expression.getText()));
}
myResult = createBinaryEvaluator(result, lType, rResult, expression.getType(), opType, expressionExpectedType);
lType = TypeConversionUtil.calcTypeForBinaryExpression(lType, expressionExpectedType, opType, true);
PsiType typeForBinOp = TypeConversionUtil.calcTypeForBinaryExpression(lType, rType, opType, true);
myResult = createBinaryEvaluator(result, lType, rResult, rType, opType, typeForBinOp);
lType = typeForBinOp;
result = myResult;
}
}
@@ -148,7 +148,7 @@ public class ConcatenationToMessageFormatAction implements IntentionAction {
while (true) {
final PsiElement parent = binaryExpression.getParent();
if (!(parent instanceof PsiPolyadicExpression)) return binaryExpression;
PsiPolyadicExpression parentBinaryExpression = (PsiBinaryExpression)parent;
PsiPolyadicExpression parentBinaryExpression = (PsiPolyadicExpression)parent;
if (!stringType.equals(parentBinaryExpression.getType())) return binaryExpression;
binaryExpression = parentBinaryExpression;
}
@@ -213,7 +213,7 @@ public class DfaUtil {
public static boolean allOperandsAreLiterals(@Nullable final PsiExpression expression) {
if (expression == null) return false;
if (expression instanceof PsiLiteralExpression) return true;
if (expression instanceof PsiBinaryExpression) {
if (expression instanceof PsiPolyadicExpression) {
Stack<PsiExpression> stack = new Stack<PsiExpression>();
stack.add(expression);
while (!stack.isEmpty()) {
@@ -76,7 +76,7 @@ public class DfaValueFactory {
if (!(psiSource instanceof PsiField)) {
PsiExpression initializer = variable.getInitializer();
if (initializer instanceof PsiBinaryExpression && variable.hasModifierProperty(PsiModifier.FINAL)) {
if (initializer instanceof PsiPolyadicExpression && variable.hasModifierProperty(PsiModifier.FINAL)) {
PsiType type = initializer.getType();
if (type != null && type.equalsToText("java.lang.String")) {
return getNotNullFactory().create(type);
@@ -261,16 +261,13 @@ public class NullityInferrer {
PsiExpression elseExpression,
PsiExpression rOperand,
PsiExpression lOperand) {
if (rOperand instanceof PsiLiteralExpression && "null".equals(rOperand.getText())) {
if (lOperand instanceof PsiReferenceExpression) {
final PsiElement resolve = ((PsiReferenceExpression)lOperand).resolve();
if (resolve instanceof PsiVariable) {
if (((PsiBinaryExpression)condition).getOperationTokenType() == JavaTokenType.EQEQ) {
if (elseExpression instanceof PsiReferenceExpression && ((PsiReferenceExpression)elseExpression).resolve() == resolve) {
return true;
}
}
}
if (rOperand instanceof PsiLiteralExpression && "null".equals(rOperand.getText()) && lOperand instanceof PsiReferenceExpression) {
final PsiElement resolve = ((PsiReferenceExpression)lOperand).resolve();
if (resolve instanceof PsiVariable &&
((PsiBinaryExpression)condition).getOperationTokenType() == JavaTokenType.EQEQ &&
elseExpression instanceof PsiReferenceExpression &&
((PsiReferenceExpression)elseExpression).resolve() == resolve) {
return true;
}
}
return false;
@@ -546,11 +543,10 @@ public class NullityInferrer {
opposite = lOperand;
}
if (opposite != null && opposite.getType() == PsiType.NULL) {
if (parent.getParent() instanceof PsiAssertStatement) {
if (((PsiBinaryExpression)parent).getOperationTokenType() == JavaTokenType.NE) {
registerNotNullAnnotation(parameter);
return true;
}
if (parent.getParent() instanceof PsiAssertStatement &&
((PsiBinaryExpression)parent).getOperationTokenType() == JavaTokenType.NE) {
registerNotNullAnnotation(parameter);
return true;
}
registerNullableAnnotation(parameter);
return true;
@@ -561,7 +557,8 @@ public class NullityInferrer {
if (qualifierExpression == expr) {
registerNotNullAnnotation(parameter);
return true;
} else {
}
else {
PsiElement exprParent = expr.getParent();
while (exprParent instanceof PsiTypeCastExpression || exprParent instanceof PsiParenthesizedExpression) {
if (qualifierExpression == exprParent) {
@@ -31,7 +31,7 @@ public class ExcludeSillyAssignment implements ElementFilter {
PsiElement each = context;
while (each != null && !(each instanceof PsiFile)) {
if (each instanceof PsiExpressionList || each instanceof PsiPrefixExpression || each instanceof PsiBinaryExpression) {
if (each instanceof PsiExpressionList || each instanceof PsiPrefixExpression || each instanceof PsiPolyadicExpression) {
return true;
}
@@ -18,7 +18,6 @@ package com.intellij.psi.impl.source.tree.java;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiBinaryExpression;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiPolyadicExpression;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
@@ -49,7 +48,7 @@ public class ReplaceExpressionUtil {
else if (i == JavaElementType.BINARY_EXPRESSION || i == JavaElementType.POLYADIC_EXPRESSION) {
if (priority < parentPriority) return true;
PsiElement element = SourceTreeToPsiMap.treeElementToPsi(oldParent);
IElementType opType = i == JavaElementType.BINARY_EXPRESSION ? ((PsiBinaryExpression)element).getOperationTokenType() : ((PsiPolyadicExpression)element).getOperationTokenType();
IElementType opType = ((PsiPolyadicExpression)element).getOperationTokenType();
return ((CompositeElement)oldParent).getChildRole(oldExpr) != ChildRole.LOPERAND &&
opType != JavaTokenType.PLUS &&
opType != JavaTokenType.ASTERISK &&
@@ -105,7 +104,7 @@ public class ReplaceExpressionUtil {
}
else if (i == JavaElementType.BINARY_EXPRESSION || i == JavaElementType.POLYADIC_EXPRESSION) {
PsiElement element = SourceTreeToPsiMap.treeElementToPsi(expr);
IElementType opType = i == JavaElementType.BINARY_EXPRESSION ? ((PsiBinaryExpression)element).getOperationTokenType() : ((PsiPolyadicExpression)element).getOperationTokenType();
IElementType opType = ((PsiPolyadicExpression)element).getOperationTokenType();
if (opType == JavaTokenType.OROR) {
return 2;
}
@@ -224,7 +224,7 @@ public class SliceNullnessAnalyzer {
if (method != null && AnnotationUtil.isNotNull(method)) return DfaUtil.Nullness.NOT_NULL;
if (method != null && AnnotationUtil.isNullable(method)) return DfaUtil.Nullness.NULL;
}
if (value instanceof PsiBinaryExpression && ((PsiBinaryExpression)value).getOperationTokenType() == JavaTokenType.PLUS) {
if (value instanceof PsiPolyadicExpression && ((PsiPolyadicExpression)value).getOperationTokenType() == JavaTokenType.PLUS) {
return DfaUtil.Nullness.NOT_NULL; // "xxx" + var
}
@@ -42,7 +42,7 @@ public class CodeInsightServicesUtil {
for (int i = 0; i < ourTokenMap.length; i++) {
IElementType tokenType = ourTokenMap[i];
if (operationSign == tokenType) {
expression = (PsiBinaryExpression)expression.copy();
expression = (PsiPolyadicExpression)expression.copy();
PsiExpression[] operands = expression.getOperands();
for (int o = 0; o < operands.length; o++) {
PsiExpression op = operands[o];
@@ -80,7 +80,7 @@ public class CodeInsightServicesUtil {
}
PsiPrefixExpression result = (PsiPrefixExpression)factory.createExpressionFromText("!(a)", null);
if (!(booleanExpression instanceof PsiBinaryExpression)) {
if (!(booleanExpression instanceof PsiPolyadicExpression)) {
result.getOperand().replace(booleanExpression);
}
else {
@@ -122,14 +122,16 @@ public class TestDataReferenceCollector {
@Nullable
private String evaluate(PsiExpression expression, Map<String, Computable<String>> arguments) {
if (expression instanceof PsiBinaryExpression) {
PsiBinaryExpression binaryExpression = (PsiBinaryExpression)expression;
if (expression instanceof PsiPolyadicExpression) {
PsiPolyadicExpression binaryExpression = (PsiPolyadicExpression)expression;
if (binaryExpression.getOperationTokenType() == JavaTokenType.PLUS) {
String lhs = evaluate(binaryExpression.getLOperand(), arguments);
String rhs = evaluate(binaryExpression.getROperand(), arguments);
if (lhs != null && rhs != null) {
return lhs + rhs;
String r = "";
for (PsiExpression op : binaryExpression.getOperands()) {
String lhs = evaluate(op, arguments);
if (lhs == null) return null;
r += lhs;
}
return r;
}
}
else if (expression instanceof PsiLiteralExpression) {
@@ -407,7 +407,7 @@ public class ConcatenationInjector implements ConcatenationAwareInjector {
return true;
}
}
else if (parent instanceof PsiBinaryExpression || parent instanceof PsiPolyadicExpression) {
else if (parent instanceof PsiPolyadicExpression) {
return true;
}
return false;
@@ -76,12 +76,6 @@ public class ContextComputationProcessor {
collectOperands(((PsiConditionalExpression)expression).getThenExpression(), result, unparsable);
collectOperands(((PsiConditionalExpression)expression).getElseExpression(), result, unparsable);
}
else if (expression instanceof PsiBinaryExpression &&
((PsiBinaryExpression)expression).getOperationTokenType() == JavaTokenType.PLUS) {
final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)expression;
collectOperands(binaryExpression.getLOperand(), result, unparsable);
collectOperands(binaryExpression.getROperand(), result, unparsable);
}
else if (expression instanceof PsiPolyadicExpression &&
((PsiPolyadicExpression)expression).getOperationTokenType() == JavaTokenType.PLUS) {
final PsiPolyadicExpression binaryExpression = (PsiPolyadicExpression)expression;
@@ -16,8 +16,8 @@
package com.siyeh.ipp.bool;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiBinaryExpression;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiPolyadicExpression;
import com.intellij.psi.tree.IElementType;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ErrorUtil;
@@ -25,10 +25,10 @@ import com.siyeh.ipp.psiutils.ErrorUtil;
class ConjunctionPredicate implements PsiElementPredicate{
public boolean satisfiedBy(PsiElement element){
if(!(element instanceof PsiBinaryExpression)){
if(!(element instanceof PsiPolyadicExpression)){
return false;
}
final PsiBinaryExpression expression = (PsiBinaryExpression) element;
final PsiPolyadicExpression expression = (PsiPolyadicExpression) element;
final IElementType tokenType = expression.getOperationTokenType();
if (!tokenType.equals(JavaTokenType.ANDAND) &&
!tokenType.equals(JavaTokenType.OROR)) {
@@ -18,19 +18,19 @@ package com.siyeh.ipp.bool;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.BoolUtils;
import com.siyeh.ipp.psiutils.ComparisonUtils;
import com.siyeh.ipp.psiutils.ParenthesesUtils;
import com.siyeh.IntentionPowerPackBundle;
import org.jetbrains.annotations.NotNull;
public class DemorgansIntention extends MutablyNamedIntention {
protected String getTextForElement(PsiElement element) {
final PsiBinaryExpression binaryExpression =
(PsiBinaryExpression)element;
final PsiPolyadicExpression binaryExpression =
(PsiPolyadicExpression)element;
final IElementType tokenType = binaryExpression.getOperationTokenType();
if (tokenType.equals(JavaTokenType.ANDAND)) {
return IntentionPowerPackBundle.message("demorgans.intention.name1");
@@ -44,14 +44,14 @@ public class DemorgansIntention extends MutablyNamedIntention {
return new ConjunctionPredicate();
}
public void processIntention(PsiElement element)
public void processIntention(@NotNull PsiElement element)
throws IncorrectOperationException {
PsiBinaryExpression exp =
(PsiBinaryExpression)element;
PsiPolyadicExpression exp =
(PsiPolyadicExpression)element;
final IElementType tokenType = exp.getOperationTokenType();
PsiElement parent = exp.getParent();
while (isConjunctionExpression(parent, tokenType)) {
exp = (PsiBinaryExpression)parent;
exp = (PsiPolyadicExpression)parent;
assert exp != null;
parent = exp.getParent();
}
@@ -61,33 +61,20 @@ public class DemorgansIntention extends MutablyNamedIntention {
exp);
}
private static String convertConjunctionExpression(PsiBinaryExpression exp,
private static String convertConjunctionExpression(PsiPolyadicExpression exp,
IElementType tokenType) {
final PsiExpression lhs = exp.getLOperand();
final String lhsText;
if (isConjunctionExpression(lhs, tokenType)) {
lhsText = convertConjunctionExpression((PsiBinaryExpression)lhs,
tokenType);
} else {
lhsText = convertLeafExpression(lhs);
}
final PsiExpression rhs = exp.getROperand();
final String rhsText;
if (isConjunctionExpression(rhs, tokenType)) {
rhsText = convertConjunctionExpression((PsiBinaryExpression)rhs,
tokenType);
} else {
rhsText = convertLeafExpression(rhs);
}
final String flippedConjunction;
if (tokenType.equals(JavaTokenType.ANDAND)) {
flippedConjunction = "||";
} else {
flippedConjunction = "&&";
}
return lhsText + flippedConjunction + rhsText;
final String flippedConjunction;
if (tokenType.equals(JavaTokenType.ANDAND)) {
flippedConjunction = "||";
} else {
flippedConjunction = "&&";
}
String result = null;
for (PsiExpression expression : exp.getOperands()) {
String lhsText = convertLeafExpression(expression);
result = result == null ? lhsText : result + flippedConjunction + lhsText;
}
return result;
}
private static String convertLeafExpression(PsiExpression condition) {
@@ -120,11 +107,11 @@ public class DemorgansIntention extends MutablyNamedIntention {
private static boolean isConjunctionExpression(PsiElement exp,
IElementType conjunctionType) {
if (!(exp instanceof PsiBinaryExpression)) {
if (!(exp instanceof PsiPolyadicExpression)) {
return false;
}
final PsiBinaryExpression binExp = (PsiBinaryExpression)exp;
final PsiPolyadicExpression binExp = (PsiPolyadicExpression)exp;
final IElementType tokenType = binExp.getOperationTokenType();
return tokenType.equals(conjunctionType);
}
}
}
@@ -18,17 +18,18 @@ package com.siyeh.ipp.bool;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.IntentionPowerPackBundle;
import org.jetbrains.annotations.NotNull;
public class FlipConjunctionIntention extends MutablyNamedIntention {
protected String getTextForElement(PsiElement element) {
final PsiBinaryExpression binaryExpression =
(PsiBinaryExpression)element;
final PsiJavaToken sign = binaryExpression.getOperationSign();
final PsiPolyadicExpression binaryExpression =
(PsiPolyadicExpression)element;
PsiExpression op = binaryExpression.getOperands()[1];
final PsiJavaToken sign = binaryExpression.getTokenBeforeOperand(op);
return IntentionPowerPackBundle.message("flip.smth.intention.name",
sign.getText());
}
@@ -38,11 +39,10 @@ public class FlipConjunctionIntention extends MutablyNamedIntention {
return new ConjunctionPredicate();
}
public void processIntention(PsiElement element)
public void processIntention(@NotNull PsiElement element)
throws IncorrectOperationException {
PsiExpression exp = (PsiExpression)element;
final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)exp;
assert binaryExpression != null;
final PsiPolyadicExpression binaryExpression = (PsiPolyadicExpression)exp;
final IElementType conjunctionType = binaryExpression.getOperationTokenType();
PsiElement parent = exp.getParent();
while (isConjunctionExpression(parent, conjunctionType)) {
@@ -56,33 +56,35 @@ public class FlipConjunctionIntention extends MutablyNamedIntention {
private static String flipExpression(PsiExpression expression,
IElementType conjunctionType) {
if (isConjunctionExpression(expression, conjunctionType)) {
final PsiBinaryExpression andExpression =
(PsiBinaryExpression)expression;
final PsiExpression rhs = andExpression.getROperand();
final PsiExpression lhs = andExpression.getLOperand();
final String conjunctionSign;
if (conjunctionType.equals(JavaTokenType.ANDAND)) {
conjunctionSign = "&&";
} else {
conjunctionSign = "||";
}
return flipExpression(rhs, conjunctionType) + ' ' +
conjunctionSign + ' ' +
flipExpression(lhs, conjunctionType);
} else {
return expression.getText();
}
if (!isConjunctionExpression(expression, conjunctionType)) {
return expression.getText();
}
final PsiPolyadicExpression andExpression =
(PsiPolyadicExpression)expression;
final String conjunctionSign;
if (conjunctionType.equals(JavaTokenType.ANDAND)) {
conjunctionSign = "&&";
} else {
conjunctionSign = "||";
}
String r = null;
PsiExpression[] operands = andExpression.getOperands();
for (int i = operands.length - 1; i >= 0; i--) {
PsiExpression op = operands[i];
String flip = flipExpression(op, conjunctionType);
r = r == null ? flip : r + ' ' + conjunctionSign + ' ' + flip;
}
return r;
}
private static boolean isConjunctionExpression(
PsiElement element, IElementType conjunctionType) {
if (!(element instanceof PsiBinaryExpression)) {
if (!(element instanceof PsiPolyadicExpression)) {
return false;
}
final PsiBinaryExpression binaryExpression =
(PsiBinaryExpression)element;
final PsiPolyadicExpression binaryExpression =
(PsiPolyadicExpression)element;
final IElementType tokenType = binaryExpression.getOperationTokenType();
return tokenType.equals(conjunctionType);
}
}
}
@@ -47,9 +47,9 @@ class CharToStringPredicate implements PsiElementPredicate{
private static boolean isInConcatenationContext(PsiElement element){
final PsiElement parent = element.getParent();
if(parent instanceof PsiBinaryExpression){
final PsiBinaryExpression parentExpression =
(PsiBinaryExpression) parent;
if(parent instanceof PsiPolyadicExpression){
final PsiPolyadicExpression parentExpression =
(PsiPolyadicExpression) parent;
final PsiType parentType = parentExpression.getType();
if(parentType == null){
return false;
@@ -116,4 +116,4 @@ class CharToStringPredicate implements PsiElementPredicate{
}
return false;
}
}
}
@@ -17,6 +17,7 @@ package com.siyeh.ipp.chartostring;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.ArrayUtil;
import com.siyeh.ipp.base.PsiElementPredicate;
import org.jetbrains.annotations.NonNls;
@@ -45,9 +46,9 @@ class StringToCharPredicate implements PsiElementPredicate{
private static boolean isInConcatenationContext(PsiElement element){
final PsiElement parent = element.getParent();
if(parent instanceof PsiBinaryExpression){
final PsiBinaryExpression parentExpression =
(PsiBinaryExpression) parent;
if(parent instanceof PsiPolyadicExpression){
final PsiPolyadicExpression parentExpression =
(PsiPolyadicExpression) parent;
final PsiType parentType = parentExpression.getType();
if(parentType == null){
return false;
@@ -56,25 +57,21 @@ class StringToCharPredicate implements PsiElementPredicate{
if(!"java.lang.String".equals(parentTypeText)){
return false;
}
final PsiExpression lhs = parentExpression.getLOperand();
final PsiExpression rhs = parentExpression.getROperand();
if(rhs == null){
return false;
}
final PsiExpression otherOperand;
if(lhs.equals(element)){
otherOperand = rhs;
} else{
otherOperand = lhs;
}
if (parentExpression.getOperationTokenType() != JavaTokenType.PLUS) return false;
PsiExpression[] operands = parentExpression.getOperands();
int n = ArrayUtil.indexOf(operands, element);
if (n == -1) return false;
for (int i=0; i<=n+1 && i<operands.length;i++) {
if (i==n) continue;
PsiExpression otherOperand = operands[i];
final PsiType otherOperandType = otherOperand.getType();
if(otherOperandType == null){
return false;
}
final String otherOperandTypeText =
otherOperandType.getCanonicalText();
return "java.lang.String".equals(otherOperandTypeText);
} else if(parent instanceof PsiAssignmentExpression){
if ("java.lang.String".equals(otherOperandTypeText)) return true;
}
return false;
}
else if(parent instanceof PsiAssignmentExpression){
final PsiAssignmentExpression parentExpression =
(PsiAssignmentExpression) parent;
final IElementType tokenType = parentExpression.getOperationTokenType();
@@ -138,4 +135,4 @@ class StringToCharPredicate implements PsiElementPredicate{
return false;
}
}
}
}
@@ -126,9 +126,9 @@ public class ParenthesesUtils{
if(expression instanceof PsiPostfixExpression){
return POSTFIX_PRECEDENCE;
}
if(expression instanceof PsiBinaryExpression){
final PsiBinaryExpression binaryExpression =
(PsiBinaryExpression)expression;
if(expression instanceof PsiPolyadicExpression){
final PsiPolyadicExpression binaryExpression =
(PsiPolyadicExpression)expression;
return getPrecedenceForBinaryOperator(binaryExpression.getOperationTokenType());
}
if(expression instanceof PsiInstanceOfExpression){
@@ -530,4 +530,4 @@ public class ParenthesesUtils{
}
return false;
}
}
}