Skip to content

Commit 686d009

Browse files
committed
Added enum processing to TypeNameCheck
1 parent f1cff12 commit 686d009

5 files changed

Lines changed: 107 additions & 11 deletions

File tree

src/checkstyle/com/puppycrawl/tools/checkstyle/checks/naming/AbstractNameCheck.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
public abstract class AbstractNameCheck
3333
extends AbstractFormatCheck
3434
{
35+
/**
36+
* Message key for invalid pattern error.
37+
*/
38+
public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
39+
3540
/**
3641
* Creates a new <code>AbstractNameCheck</code> instance.
3742
* @param aFormat format to check with
@@ -61,7 +66,7 @@ public void visitToken(DetailAST aAST)
6166
if (!getRegexp().matcher(nameAST.getText()).find()) {
6267
log(nameAST.getLineNo(),
6368
nameAST.getColumnNo(),
64-
"name.invalidPattern",
69+
MSG_INVALID_PATTERN,
6570
nameAST.getText(),
6671
getFormat());
6772
}

src/checkstyle/com/puppycrawl/tools/checkstyle/checks/naming/TypeNameCheck.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,26 @@ public class TypeNameCheck
4848
extends AbstractAccessControlNameCheck
4949
{
5050

51+
/**
52+
* default pattern for type name.
53+
*/
54+
public static final String DEFAULT_PATTERN = "^[A-Z][a-zA-Z0-9]*$";
55+
5156
/**
5257
* Creates a new <code>TypeNameCheck</code> instance.
5358
*/
5459
public TypeNameCheck()
5560
{
56-
super("^[A-Z][a-zA-Z0-9]*$");
61+
super(DEFAULT_PATTERN);
5762
}
5863

5964
/** {@inheritDoc} */
6065
@Override
6166
public int[] getDefaultTokens()
6267
{
6368
return new int[] {TokenTypes.CLASS_DEF,
64-
TokenTypes.INTERFACE_DEF, };
69+
TokenTypes.INTERFACE_DEF,
70+
TokenTypes.ENUM_DEF,
71+
};
6572
}
6673
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.puppycrawl.tools.checkstyle.naming;
2+
3+
class inputHeaderClass {
4+
5+
public interface inputHeaderInterface {};
6+
7+
public enum inputHeaderEnum { one, two };
8+
9+
}

src/tests/com/puppycrawl/tools/checkstyle/checks/naming/TypeNameCheckTest.java

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,108 @@
1818
////////////////////////////////////////////////////////////////////////////////
1919
package com.puppycrawl.tools.checkstyle.checks.naming;
2020

21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.text.MessageFormat;
24+
25+
import org.junit.Test;
26+
2127
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
2228
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
23-
import org.junit.Test;
2429

2530
public class TypeNameCheckTest
2631
extends BaseCheckTestSupport
2732
{
33+
34+
/**
35+
* Localized error message from @link {@link TypeNameCheck}.
36+
*/
37+
private final String msg = getCheckMessage(AbstractNameCheck.MSG_INVALID_PATTERN);
38+
39+
private final String inputFilename;
40+
41+
public TypeNameCheckTest() throws IOException
42+
{
43+
inputFilename = getPath("naming" + File.separator
44+
+ "InputTypeName.java");
45+
}
46+
2847
@Test
2948
public void testSpecified()
3049
throws Exception
3150
{
3251
final DefaultConfiguration checkConfig =
33-
createCheckConfig(TypeNameCheck.class);
52+
createCheckConfig(TypeNameCheck.class);
3453
checkConfig.addAttribute("format", "^inputHe");
3554
final String[] expected = {
3655
};
37-
verify(checkConfig, getPath("inputHeader.java"), expected);
56+
verify(checkConfig, inputFilename, expected);
3857
}
3958

4059
@Test
4160
public void testDefault()
4261
throws Exception
62+
{
63+
final DefaultConfiguration checkConfig =
64+
createCheckConfig(TypeNameCheck.class);
65+
final String[] expected = {
66+
buildMesssage(3, 7, "inputHeaderClass",
67+
TypeNameCheck.DEFAULT_PATTERN),
68+
buildMesssage(5, 22, "inputHeaderInterface",
69+
TypeNameCheck.DEFAULT_PATTERN),
70+
buildMesssage(7, 17, "inputHeaderEnum",
71+
TypeNameCheck.DEFAULT_PATTERN),
72+
};
73+
verify(checkConfig, inputFilename, expected);
74+
}
75+
76+
@Test
77+
public void testClassSpecific()
78+
throws Exception
4379
{
4480
final DefaultConfiguration checkConfig =
4581
createCheckConfig(TypeNameCheck.class);
82+
checkConfig.addAttribute("tokens", "CLASS_DEF");
4683
final String[] expected = {
47-
"1:48: Name 'inputHeader' must match pattern '^[A-Z][a-zA-Z0-9]*$'.",
84+
buildMesssage(3, 7, "inputHeaderClass",
85+
TypeNameCheck.DEFAULT_PATTERN),
4886
};
49-
verify(checkConfig, getPath("inputHeader.java"), expected);
87+
verify(checkConfig, inputFilename, expected);
5088
}
89+
90+
@Test
91+
public void testInterfaceSpecific()
92+
throws Exception
93+
{
94+
final DefaultConfiguration checkConfig =
95+
createCheckConfig(TypeNameCheck.class);
96+
checkConfig.addAttribute("tokens", "INTERFACE_DEF");
97+
final String[] expected = {
98+
buildMesssage(5, 22, "inputHeaderInterface",
99+
TypeNameCheck.DEFAULT_PATTERN),
100+
};
101+
verify(checkConfig, inputFilename, expected);
102+
}
103+
104+
@Test
105+
public void testEnumSpecific()
106+
throws Exception
107+
{
108+
final DefaultConfiguration checkConfig =
109+
createCheckConfig(TypeNameCheck.class);
110+
checkConfig.addAttribute("tokens", "ENUM_DEF");
111+
final String[] expected = {
112+
buildMesssage(7, 17, "inputHeaderEnum",
113+
TypeNameCheck.DEFAULT_PATTERN),
114+
};
115+
verify(checkConfig, inputFilename, expected);
116+
}
117+
118+
private String buildMesssage(int lineNumber, int colNumber, String name,
119+
String pattern)
120+
{
121+
return lineNumber + ":" + colNumber + ": "
122+
+ MessageFormat.format(msg, name, pattern);
123+
}
124+
51125
}

src/xdocs/config_naming.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
</tr>
112112
<tr>
113113
<td><code>TypeName</code></td>
114-
<td>classes and interfaces</td>
114+
<td>classes, interfaces and enums</td>
115115
<td><code>^[A-Z][a-zA-Z0-9]*$</code></td>
116116
</tr>
117117
</table>
@@ -142,8 +142,9 @@
142142
<p>
143143
Module <code>TypeName</code> also has property
144144
<code>tokens</code> which can be used to control whether the
145-
check applies to classes or interfaces through tokens
146-
<code>CLASS_DEF</code> and <code>INTERFACE_DEF</code>. For
145+
check applies to classes, interfaces and enums through tokens
146+
<code>CLASS_DEF</code>, <code>INTERFACE_DEF</code> and
147+
<code>ENUM_DEF</code>. For
147148
example, the following configuration element ensures that
148149
interface names begin with <code>"I_"</code>, followed by
149150
letters and digits:

0 commit comments

Comments
 (0)