/plugins/InspectionGadgets/src/com/siyeh/ig/visibility/MethodOverridesPrivateMethodInspection.java

https://bitbucket.org/nbargnesi/idea · Java · 93 lines · 67 code · 11 blank · 15 comment · 10 complexity · 0a56e10fb86471675fc52c18e75bc4d7 MD5 · raw file

  1. /*
  2. * Copyright 2003-2007 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.visibility;
  17. import com.intellij.psi.PsiClass;
  18. import com.intellij.psi.PsiMethod;
  19. import com.intellij.psi.PsiModifier;
  20. import com.siyeh.InspectionGadgetsBundle;
  21. import com.siyeh.ig.BaseInspection;
  22. import com.siyeh.ig.BaseInspectionVisitor;
  23. import com.siyeh.ig.InspectionGadgetsFix;
  24. import com.siyeh.ig.fixes.RenameFix;
  25. import org.jetbrains.annotations.NotNull;
  26. import java.util.HashSet;
  27. import java.util.Set;
  28. public class MethodOverridesPrivateMethodInspection extends BaseInspection {
  29. @NotNull
  30. public String getID() {
  31. return "MethodOverridesPrivateMethodOfSuperclass";
  32. }
  33. @NotNull
  34. public String getDisplayName() {
  35. return InspectionGadgetsBundle.message(
  36. "method.overrides.private.display.name");
  37. }
  38. @NotNull
  39. public String buildErrorString(Object... infos) {
  40. return InspectionGadgetsBundle.message(
  41. "method.overrides.private.display.name.problem.descriptor");
  42. }
  43. protected InspectionGadgetsFix buildFix(Object... infos) {
  44. return new RenameFix();
  45. }
  46. protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
  47. return true;
  48. }
  49. public BaseInspectionVisitor buildVisitor() {
  50. return new MethodOverridesPrivateMethodVisitor();
  51. }
  52. private static class MethodOverridesPrivateMethodVisitor
  53. extends BaseInspectionVisitor {
  54. @Override
  55. public void visitMethod(@NotNull PsiMethod method) {
  56. final PsiClass aClass = method.getContainingClass();
  57. if (aClass == null) {
  58. return;
  59. }
  60. if (method.getNameIdentifier() == null) {
  61. return;
  62. }
  63. PsiClass ancestorClass = aClass.getSuperClass();
  64. final Set<PsiClass> visitedClasses = new HashSet<PsiClass>();
  65. while (ancestorClass != null) {
  66. if (!visitedClasses.add(ancestorClass)) {
  67. return;
  68. }
  69. final PsiMethod overridingMethod =
  70. ancestorClass.findMethodBySignature(method, false);
  71. if (overridingMethod != null) {
  72. if (overridingMethod.hasModifierProperty(
  73. PsiModifier.PRIVATE)) {
  74. registerMethodError(method);
  75. return;
  76. }
  77. }
  78. ancestorClass = ancestorClass.getSuperClass();
  79. }
  80. }
  81. }
  82. }