/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/classmetrics/FieldCountInspection.java

http://github.com/JetBrains/intellij-community · Java · 154 lines · 120 code · 19 blank · 15 comment · 10 complexity · 3c64e5e0e2f720318399459ea296e3b9 MD5 · raw file

  1. /*
  2. * Copyright 2003-2013 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.classmetrics;
  17. import com.intellij.psi.PsiClass;
  18. import com.intellij.psi.PsiEnumConstant;
  19. import com.intellij.psi.PsiField;
  20. import com.intellij.psi.PsiModifier;
  21. import com.intellij.util.ui.CheckBox;
  22. import com.intellij.util.ui.UIUtil;
  23. import com.siyeh.InspectionGadgetsBundle;
  24. import com.siyeh.ig.BaseInspectionVisitor;
  25. import com.siyeh.ig.psiutils.ClassUtils;
  26. import org.jetbrains.annotations.NotNull;
  27. import javax.swing.*;
  28. import java.awt.*;
  29. public class FieldCountInspection extends ClassMetricInspection {
  30. private static final int FIELD_COUNT_LIMIT = 10;
  31. @SuppressWarnings("PublicField")
  32. public boolean m_countConstantFields = false;
  33. @SuppressWarnings("PublicField")
  34. public boolean m_considerStaticFinalFieldsConstant = false;
  35. @SuppressWarnings("PublicField")
  36. public boolean myCountEnumConstants = false;
  37. @Override
  38. @NotNull
  39. public String getID() {
  40. return "ClassWithTooManyFields";
  41. }
  42. @Override
  43. @NotNull
  44. public String buildErrorString(Object... infos) {
  45. return InspectionGadgetsBundle.message("too.many.fields.problem.descriptor", infos[0]);
  46. }
  47. @Override
  48. protected int getDefaultLimit() {
  49. return FIELD_COUNT_LIMIT;
  50. }
  51. @Override
  52. protected String getConfigurationLabel() {
  53. return InspectionGadgetsBundle.message("too.many.fields.count.limit.option");
  54. }
  55. @Override
  56. public JComponent createOptionsPanel() {
  57. final String configurationLabel = getConfigurationLabel();
  58. final JLabel label = new JLabel(configurationLabel);
  59. final JFormattedTextField valueField = prepareNumberEditor("m_limit");
  60. final CheckBox includeCheckBox =
  61. new CheckBox(InspectionGadgetsBundle.message("field.count.inspection.include.constant.fields.in.count.checkbox"),
  62. this, "m_countConstantFields");
  63. final CheckBox considerCheckBox =
  64. new CheckBox(InspectionGadgetsBundle.message("field.count.inspection.static.final.fields.count.as.constant.checkbox"),
  65. this, "m_considerStaticFinalFieldsConstant");
  66. final CheckBox enumConstantCheckBox =
  67. new CheckBox(InspectionGadgetsBundle.message("field.count.inspection.include.enum.constants.in.count"),
  68. this, "myCountEnumConstants");
  69. final GridBagConstraints constraints = new GridBagConstraints();
  70. constraints.gridx = 0;
  71. constraints.gridy = 0;
  72. constraints.weightx = 0.0;
  73. constraints.weighty = 0.0;
  74. constraints.insets.right = UIUtil.DEFAULT_HGAP;
  75. constraints.anchor = GridBagConstraints.WEST;
  76. constraints.fill = GridBagConstraints.NONE;
  77. final JPanel panel = new JPanel(new GridBagLayout());
  78. panel.add(label, constraints);
  79. constraints.gridx = 1;
  80. constraints.gridy = 0;
  81. constraints.gridwidth = 1;
  82. constraints.weightx = 1.0;
  83. constraints.insets.right = 0;
  84. constraints.anchor = GridBagConstraints.NORTHWEST;
  85. panel.add(valueField, constraints);
  86. constraints.gridx = 0;
  87. constraints.gridy = 1;
  88. constraints.gridwidth = 2;
  89. panel.add(includeCheckBox, constraints);
  90. constraints.gridy = 2;
  91. panel.add(considerCheckBox, constraints);
  92. constraints.gridy = 3;
  93. constraints.weighty = 1;
  94. panel.add(enumConstantCheckBox, constraints);
  95. return panel;
  96. }
  97. @Override
  98. public BaseInspectionVisitor buildVisitor() {
  99. return new FieldCountVisitor();
  100. }
  101. private class FieldCountVisitor extends BaseInspectionVisitor {
  102. @Override
  103. public void visitClass(@NotNull PsiClass aClass) {
  104. final int totalFields = countFields(aClass);
  105. if (totalFields <= getLimit()) {
  106. return;
  107. }
  108. registerClassError(aClass, Integer.valueOf(totalFields));
  109. }
  110. private int countFields(PsiClass aClass) {
  111. int totalFields = 0;
  112. final PsiField[] fields = aClass.getFields();
  113. for (final PsiField field : fields) {
  114. if (field instanceof PsiEnumConstant) {
  115. if (myCountEnumConstants) {
  116. totalFields++;
  117. }
  118. }
  119. else if (m_countConstantFields || !fieldIsConstant(field)) {
  120. totalFields++;
  121. }
  122. }
  123. return totalFields;
  124. }
  125. private boolean fieldIsConstant(PsiField field) {
  126. if (!field.hasModifierProperty(PsiModifier.STATIC) || !field.hasModifierProperty(PsiModifier.FINAL)) {
  127. return false;
  128. }
  129. if (m_considerStaticFinalFieldsConstant) {
  130. return true;
  131. }
  132. return ClassUtils.isImmutable(field.getType());
  133. }
  134. }
  135. }