/plugins/InspectionGadgets/src/com/siyeh/ig/security/PublicStaticArrayFieldInspection.java

https://bitbucket.org/nbargnesi/idea · Java · 68 lines · 46 code · 7 blank · 15 comment · 4 complexity · 4d7bc037d4b864def21de5577882e4af 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.security;
  17. import com.intellij.psi.PsiArrayType;
  18. import com.intellij.psi.PsiField;
  19. import com.intellij.psi.PsiModifier;
  20. import com.intellij.psi.PsiType;
  21. import com.siyeh.InspectionGadgetsBundle;
  22. import com.siyeh.ig.BaseInspection;
  23. import com.siyeh.ig.BaseInspectionVisitor;
  24. import com.siyeh.ig.psiutils.CollectionUtils;
  25. import org.jetbrains.annotations.NotNull;
  26. public class PublicStaticArrayFieldInspection extends BaseInspection {
  27. @NotNull
  28. public String getDisplayName() {
  29. return InspectionGadgetsBundle.message(
  30. "public.static.array.field.display.name");
  31. }
  32. @NotNull
  33. protected String buildErrorString(Object... infos) {
  34. return InspectionGadgetsBundle.message(
  35. "public.static.array.field.problem.descriptor");
  36. }
  37. public BaseInspectionVisitor buildVisitor() {
  38. return new PublicStaticArrayFieldVisitor();
  39. }
  40. private static class PublicStaticArrayFieldVisitor
  41. extends BaseInspectionVisitor {
  42. @Override
  43. public void visitField(@NotNull PsiField field) {
  44. super.visitField(field);
  45. if (!field.hasModifierProperty(PsiModifier.PUBLIC)) {
  46. return;
  47. }
  48. if (!field.hasModifierProperty(PsiModifier.STATIC)) {
  49. return;
  50. }
  51. final PsiType type = field.getType();
  52. if (!(type instanceof PsiArrayType)) {
  53. return;
  54. }
  55. if (CollectionUtils.isConstantEmptyArray(field)) {
  56. return;
  57. }
  58. registerFieldError(field);
  59. }
  60. }
  61. }