/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/ForLoopReplaceableByWhileInspection.java

https://bitbucket.org/nbargnesi/idea · Java · 141 lines · 108 code · 15 blank · 18 comment · 16 complexity · e7482143db8b729bb23a6c240f1aaa68 MD5 · raw file

  1. /*
  2. * Copyright 2003-2009 Dave Griffith, Bas Leijdekkers
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.siyeh.ig.controlflow;
  17. import com.intellij.codeInspection.ProblemDescriptor;
  18. import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
  19. import com.intellij.openapi.project.Project;
  20. import com.intellij.psi.*;
  21. import com.intellij.util.IncorrectOperationException;
  22. import com.siyeh.InspectionGadgetsBundle;
  23. import com.siyeh.ig.BaseInspection;
  24. import com.siyeh.ig.BaseInspectionVisitor;
  25. import com.siyeh.ig.InspectionGadgetsFix;
  26. import org.jetbrains.annotations.NonNls;
  27. import org.jetbrains.annotations.NotNull;
  28. import javax.swing.*;
  29. public class ForLoopReplaceableByWhileInspection extends BaseInspection {
  30. /**
  31. * @noinspection PublicField
  32. */
  33. public boolean m_ignoreLoopsWithoutConditions = false;
  34. @Override
  35. @NotNull
  36. public String getDisplayName() {
  37. return InspectionGadgetsBundle.message(
  38. "for.loop.replaceable.by.while.display.name");
  39. }
  40. @Override
  41. @NotNull
  42. public String getID() {
  43. return "ForLoopReplaceableByWhile";
  44. }
  45. @Override
  46. @NotNull
  47. protected String buildErrorString(Object... infos) {
  48. return InspectionGadgetsBundle.message(
  49. "for.loop.replaceable.by.while.problem.descriptor");
  50. }
  51. @Override
  52. public JComponent createOptionsPanel() {
  53. return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message(
  54. "for.loop.replaceable.by.while.ignore.option"),
  55. this, "m_ignoreLoopsWithoutConditions");
  56. }
  57. @Override
  58. public InspectionGadgetsFix buildFix(Object... infos) {
  59. return new ReplaceForByWhileFix();
  60. }
  61. private static class ReplaceForByWhileFix extends InspectionGadgetsFix {
  62. @NotNull
  63. public String getName() {
  64. return InspectionGadgetsBundle.message(
  65. "for.loop.replaceable.by.while.replace.quickfix");
  66. }
  67. @Override
  68. public void doFix(Project project, ProblemDescriptor descriptor)
  69. throws IncorrectOperationException {
  70. final PsiElement forKeywordElement = descriptor.getPsiElement();
  71. final PsiForStatement forStatement =
  72. (PsiForStatement)forKeywordElement.getParent();
  73. assert forStatement != null;
  74. final PsiExpression condition = forStatement.getCondition();
  75. final PsiStatement body = forStatement.getBody();
  76. final String bodyText;
  77. if (body == null) {
  78. bodyText = "";
  79. }
  80. else {
  81. bodyText = body.getText();
  82. }
  83. @NonNls final String whileStatement;
  84. if (condition == null) {
  85. whileStatement = "while(true)" + bodyText;
  86. }
  87. else {
  88. whileStatement = "while(" + condition.getText() + ')' +
  89. bodyText;
  90. }
  91. replaceStatement(forStatement, whileStatement);
  92. }
  93. }
  94. @Override
  95. public BaseInspectionVisitor buildVisitor() {
  96. return new ForLoopReplaceableByWhileVisitor();
  97. }
  98. private class ForLoopReplaceableByWhileVisitor
  99. extends BaseInspectionVisitor {
  100. @Override
  101. public void visitForStatement(
  102. @NotNull PsiForStatement statement) {
  103. super.visitForStatement(statement);
  104. final PsiStatement initialization = statement.getInitialization();
  105. if (initialization != null &&
  106. !(initialization instanceof PsiEmptyStatement)) {
  107. return;
  108. }
  109. final PsiStatement update = statement.getUpdate();
  110. if (update != null && !(update instanceof PsiEmptyStatement)) {
  111. return;
  112. }
  113. if (m_ignoreLoopsWithoutConditions) {
  114. final PsiExpression condition = statement.getCondition();
  115. if (condition == null) {
  116. return;
  117. }
  118. final String conditionText = condition.getText();
  119. if (PsiKeyword.TRUE.equals(conditionText)) {
  120. return;
  121. }
  122. }
  123. registerStatementError(statement);
  124. }
  125. }
  126. }