IDEA-55747 (Reverse direction of for loop produce wrong code)
authorBas Leijdekkers <leijdekkers@carp-technologies.nl>
Sun, 12 Sep 2010 07:26:43 +0000 (09:26 +0200)
committerBas Leijdekkers <leijdekkers@carp-technologies.nl>
Sun, 12 Sep 2010 07:26:43 +0000 (09:26 +0200)
plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReverseForLoopDirectionIntention.java

index 0cfbd4ed5ec2ac36b5b3a4898b300b23b6357c21..6f5219fbf12a23dfaa2762b36509678c01cf1caf 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2009 Bas Leijdekkers
+ * Copyright 2009-2010 Bas Leijdekkers
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -112,17 +112,23 @@ public class ReverseForLoopDirectionIntention extends Intention {
             conditionText.append(negatedSign);
             if (sign == JavaTokenType.GE) {
                 conditionText.append(incrementExpression(initializer, true));
+            } else if (sign == JavaTokenType.LE) {
+                conditionText.append(incrementExpression(initializer, false));
             } else {
                 conditionText.append(initializer.getText());
             }
             if (sign == JavaTokenType.LT) {
                 newInitializerText.append(incrementExpression(rhs, false));
+            } else if (sign == JavaTokenType.GT) {
+                newInitializerText.append(incrementExpression(rhs, true));
             } else {
                 newInitializerText.append(rhs.getText());
             }
         } else if (VariableAccessUtils.evaluatesToVariable(rhs, variable)) {
             if (sign == JavaTokenType.LE) {
                 conditionText.append(incrementExpression(initializer, true));
+            } else if (sign == JavaTokenType.GE) {
+                conditionText.append(incrementExpression(initializer, false));
             } else {
                 conditionText.append(initializer.getText());
             }
@@ -130,6 +136,8 @@ public class ReverseForLoopDirectionIntention extends Intention {
             conditionText.append(variableName);
             if (sign == JavaTokenType.GT) {
                 newInitializerText.append(incrementExpression(lhs, false));
+            } else if (sign == JavaTokenType.LT) {
+                newInitializerText.append(incrementExpression(lhs, true));
             } else {
                 newInitializerText.append(lhs.getText());
             }
@@ -154,11 +162,22 @@ public class ReverseForLoopDirectionIntention extends Intention {
                 return null;
             }
             if (positive) {
-                return String.valueOf(value.longValue() + 1);
+                return String.valueOf(value.longValue() + 1L);
             } else {
-                return String.valueOf(value.longValue() - 1);
+                return String.valueOf(value.longValue() - 1L);
             }
         } else {
+            if (expression instanceof PsiBinaryExpression) {
+                final PsiBinaryExpression binaryExpression =
+                        (PsiBinaryExpression) expression;
+                final IElementType tokenType =
+                        binaryExpression.getOperationTokenType();
+                if (tokenType == JavaTokenType.MINUS && positive)  {
+                    return binaryExpression.getLOperand().getText();
+                } else if (tokenType == JavaTokenType.PLUS && !positive) {
+                    return binaryExpression.getLOperand().getText();
+                }
+            }
             final String expressionText;
             if (ParenthesesUtils.getPrecedence(expression) >
                     ParenthesesUtils.ADDITIVE_PRECEDENCE) {