IDEADEV-31824 (Incorrect "manual array copy" warning)
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / naming / ConstantNamingConventionInspection.java
blob36f343697f81375a5b364d432ffa72dab6e1fa7f
1 /*
2 * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.siyeh.ig.naming;
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.BaseInspectionVisitor;
23 import com.siyeh.ig.InspectionGadgetsFix;
24 import com.siyeh.ig.fixes.RenameFix;
25 import com.siyeh.ig.psiutils.ClassUtils;
26 import org.jetbrains.annotations.NotNull;
28 public class ConstantNamingConventionInspection extends ConventionInspection {
30 private static final int DEFAULT_MIN_LENGTH = 5;
31 private static final int DEFAULT_MAX_LENGTH = 32;
33 @NotNull
34 public String getDisplayName() {
35 return InspectionGadgetsBundle.message(
36 "constant.naming.convention.display.name");
39 protected InspectionGadgetsFix buildFix(Object... infos) {
40 return new RenameFix();
43 protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
44 return true;
47 @NotNull
48 public String buildErrorString(Object... infos) {
49 final String fieldName = (String)infos[0];
50 if (fieldName.length() < getMinLength()) {
51 return InspectionGadgetsBundle.message(
52 "constant.name.convention.problem.descriptor.short");
54 else if (fieldName.length() > getMaxLength()) {
55 return InspectionGadgetsBundle.message(
56 "constant.name.convention.problem.descriptor.long");
58 return InspectionGadgetsBundle.message(
59 "constant.name.convention.problem.descriptor.regex.mismatch",
60 getRegex());
63 protected String getDefaultRegex() {
64 return "[A-Z_\\d]*";
67 protected int getDefaultMinLength() {
68 return DEFAULT_MIN_LENGTH;
71 protected int getDefaultMaxLength() {
72 return DEFAULT_MAX_LENGTH;
75 public BaseInspectionVisitor buildVisitor() {
76 return new NamingConventionsVisitor();
79 private class NamingConventionsVisitor extends BaseInspectionVisitor {
81 @Override public void visitField(@NotNull PsiField field) {
82 super.visitField(field);
83 if (!field.hasModifierProperty(PsiModifier.STATIC) ||
84 !field.hasModifierProperty(PsiModifier.FINAL)) {
85 return;
87 final String name = field.getName();
88 if (name == null) {
89 return;
91 final PsiType type = field.getType();
92 if (!ClassUtils.isImmutable(type)) {
93 return;
95 if (isValid(name)) {
96 return;
98 registerFieldError(field, name);