IDEA-97330 (Change default pattern of "Enumerated constant naming")

This commit is contained in:
Bas Leijdekkers
2012-12-13 17:34:50 +01:00
parent 9bedc6a656
commit b4a47f95eb
8 changed files with 80 additions and 27 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2009 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -61,29 +61,23 @@ public class ConstantNamingConventionInspection extends ConventionInspection {
public String buildErrorString(Object... infos) {
final String fieldName = (String)infos[0];
if (fieldName.length() < getMinLength()) {
return InspectionGadgetsBundle.message(
"constant.naming.convention.problem.descriptor.short");
return InspectionGadgetsBundle.message("constant.naming.convention.problem.descriptor.short");
}
else if (fieldName.length() > getMaxLength()) {
return InspectionGadgetsBundle.message(
"constant.naming.convention.problem.descriptor.long");
return InspectionGadgetsBundle.message("constant.naming.convention.problem.descriptor.long");
}
return InspectionGadgetsBundle.message(
"constant.naming.convention.problem.descriptor.regex.mismatch",
getRegex());
return InspectionGadgetsBundle.message("constant.naming.convention.problem.descriptor.regex.mismatch", getRegex());
}
@Override
public Collection<? extends JComponent> createExtraOptions() {
return Arrays.asList(
new CheckBox(InspectionGadgetsBundle.message(
"constant.naming.convention.immutables.option"), this,
"onlyCheckImmutables"));
new CheckBox(InspectionGadgetsBundle.message("constant.naming.convention.immutables.option"), this, "onlyCheckImmutables"));
}
@Override
protected String getDefaultRegex() {
return "[A-Z_\\d]*";
return "[A-Z][A-Z_\\d]*";
}
@Override
@@ -109,8 +103,7 @@ public class ConstantNamingConventionInspection extends ConventionInspection {
if (field instanceof PsiEnumConstant) {
return;
}
if (!field.hasModifierProperty(PsiModifier.STATIC) ||
!field.hasModifierProperty(PsiModifier.FINAL)) {
if (!field.hasModifierProperty(PsiModifier.STATIC) || !field.hasModifierProperty(PsiModifier.FINAL)) {
return;
}
final String name = field.getName();
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2009 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,8 +22,7 @@ import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import org.jetbrains.annotations.NotNull;
public class EnumeratedConstantNamingConventionInspection
extends ConventionInspection {
public class EnumeratedConstantNamingConventionInspection extends ConventionInspection {
private static final int DEFAULT_MIN_LENGTH = 5;
private static final int DEFAULT_MAX_LENGTH = 32;
@@ -31,8 +30,7 @@ public class EnumeratedConstantNamingConventionInspection
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"enumerated.constant.naming.convention.display.name");
return InspectionGadgetsBundle.message("enumerated.constant.naming.convention.display.name");
}
@Override
@@ -50,21 +48,17 @@ public class EnumeratedConstantNamingConventionInspection
public String buildErrorString(Object... infos) {
final String fieldName = (String)infos[0];
if (fieldName.length() < getMinLength()) {
return InspectionGadgetsBundle.message(
"enumerated.constant.naming.convention.problem.descriptor.short");
return InspectionGadgetsBundle.message("enumerated.constant.naming.convention.problem.descriptor.short");
}
else if (fieldName.length() > getMaxLength()) {
return InspectionGadgetsBundle.message(
"enumerated.constant.naming.convention.problem.descriptor.long");
return InspectionGadgetsBundle.message("enumerated.constant.naming.convention.problem.descriptor.long");
}
return InspectionGadgetsBundle.message(
"enumerated.constant.naming.convention.problem.descriptor.regex.mismatch",
getRegex());
return InspectionGadgetsBundle.message("enumerated.constant.naming.convention.problem.descriptor.regex.mismatch", getRegex());
}
@Override
protected String getDefaultRegex() {
return "[A-Z][A-Za-z\\d]*";
return "[A-Z][A-Z_\\d]*";
}
@Override
@@ -0,0 +1,7 @@
package com.siyeh.igtest.naming.constant_naming_convention;
class ConstantNamingConvention {
static final String A_B_C_D3 = "";
static final String a = "";
static final String aaaaaa = "";
}
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>ConstantNamingConvention.java</file>
<line>5</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Constant naming convention</problem_class>
<description>Constant name &lt;code&gt;a&lt;/code&gt; is too short #loc</description>
</problem>
<problem>
<file>ConstantNamingConvention.java</file>
<line>6</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Constant naming convention</problem_class>
<description>Constant &lt;code&gt;aaaaaa&lt;/code&gt; doesn't match regex '[A-Z][A-Z_\d]*' #loc</description>
</problem>
</problems>
@@ -0,0 +1,7 @@
package com.siyeh.igtest.naming.enumerated_constant_naming_convention;
enum EnumeratedConstantNamingConvention {
A_B_C,
A,
aaaaaa
}
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>EnumeratedConstantNamingConvention.java</file>
<line>5</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Enumerated constant naming convention</problem_class>
<description>Enumerated constant name &lt;code&gt;A&lt;/code&gt; is too short #loc</description>
</problem>
<problem>
<file>EnumeratedConstantNamingConvention.java</file>
<line>6</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Enumerated constant naming convention</problem_class>
<description>Enumerated constant &lt;code&gt;aaaaaa&lt;/code&gt; doesn't match regex '[A-Z][A-Z_\d]*' #loc</description>
</problem>
</problems>
@@ -0,0 +1,10 @@
package com.siyeh.ig.naming;
import com.siyeh.ig.IGInspectionTestCase;
public class ConstantNamingConventionInspectionTest extends IGInspectionTestCase {
public void test() throws Exception {
doTest("com/siyeh/igtest/naming/constant_naming_convention", new ConstantNamingConventionInspection());
}
}
@@ -0,0 +1,10 @@
package com.siyeh.ig.naming;
import com.siyeh.ig.IGInspectionTestCase;
public class EnumeratedConstantNamingConventionInspectionTest extends IGInspectionTestCase {
public void test() throws Exception {
doTest("com/siyeh/igtest/naming/enumerated_constant_naming_convention", new EnumeratedConstantNamingConventionInspection());
}
}