Checks the style of array type definitions. Some like Java style:
public static void main(String[] args)
and some like
C style: public static void main(String args[])
name | description | type | default value |
---|---|---|---|
javaStyle | Controls whether to enforce Java style (true) or C style (false). | Boolean | true |
To configure the check to enforce Java style:
To configure the check to enforce C style:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks
Restrict using Unicode escapes (e.g. \u221e). It is possible to allow using escapes for non-printable(control) characters. Also, this check can be configured to allow using escapes if trail comment is present. By the option it is possible to allow using escapes if literal contains only them.
name | description | type | default value |
---|---|---|---|
allowEscapesForControlCharacters | Allow use escapes for non-printable(control) characters. | Boolean | false |
allowByTailComment | Allow use escapes if trail comment is present. | Boolean | false |
allowIfAllCharactersEscaped | Allow if all characters in literal are escaped. | Boolean | false |
allowNonPrintableEscapes | Allow non-printable escapes. | Boolean | false |
Examples of using Unicode:
An example of how to configure the check is:
An example of non-printable(control) characters.
An example of how to configure the check to allow using escapes for non-printable(control) characters:
Example of using escapes with trail comment:
An example of how to configure the check to allow using escapes if trail comment is present:
Example of using escapes if literal contains only them:
An example of how to configure the check to allow escapes if literal contains only them:
An example of how to configure the check to allow non-printable escapes:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks
Controls the indentation between comments and surrounding code. Comments are indented at the same level as the surrounding code. Detailed info about such convention can be found here
Please take a look at the following examples to understand how the check works:
Example #1: Block comments.
Example #2: Comment is placed at the end of the block and has previous statement.
Example #3: Comment is used as a single line border to separate groups of methods.
Example #4: Comment has destributed previous statement.
Example #5: Single line block comment is placed within an empty code block. Note, if comment is placed at the end of the empty code block, we have Checkstyle's limitations to clearly detect user intention of explanation target - above or below. The only case we can assume as a violation is when a single line comment within the empty code block has indentation level that is lower than the indentation level of the closing right curly brace.
Example #6: 'fallthrough' comments and similar.
Example #7: Comment is placed within a destributed statement.
Examnple #8: Comment is placed within an empty case block. Note, if comment is placed at the end of the empty case block, we have Checkstyle's limitations to clearly detect user intention of explanation target - above or below. The only case we can assume as a violation is when a single line comment within the empty case block has indentation level that is lower than the indentation level of the next case token.
Example #9: Single line block comment has previous and next statement.
Example #10: Comment within the block tries to describe the next code block.
name | description | type | default value |
---|---|---|---|
tokens | tokens to check | subset of tokens SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN. | SINGLE_LINE_COMMENT, BLOCK_COMMENT_BEGIN. |
To configure the Check:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.indentation
Checks for restricted tokens beneath other tokens.
WARNING: This is a very powerful and flexible check, but, at the same time, it is low-level and very implementation-dependent because its results depend on the grammar we use to build abstract syntax trees. Thus we recommend using other checks when they provide the desired functionality. Essentially, this check just works on the level of an abstract syntax tree and knows nothing about language structures.
name | description | type | default value |
---|---|---|---|
limitedTokens | set of tokens with limited occurrences as descendants | subset of tokens TokenTypes | empty set |
minimumDepth | the minimum depth for descendant counts | Integer | 0 |
maximumDepth | the maximum depth for descendant counts | Integer | java.lang.Integer.MAX_VALUE |
minimumNumber | a minimum count for descendants | Integer | 0 |
maximumNumber | a maximum count for descendants | Integer | java.lang.Integer.MAX_VALUE |
sumTokenCounts | whether the number of tokens found should be calculated from the sum of the individual token counts | Boolean | false |
minimumMessage | error message when the minimum count is not reached | String | "descendant.token.min" |
maximumMessage | error message when the maximum count is exceeded | String | "descendant.token.max" |
Switch with no default:
The condition in for
performs no check:
Comparing this
with null
(i.e. this ==
null
and this != null
):
String literal equality check:
Assert statement may have side effects (formatted for browser display):
The initialiser in for
performs no setup (where a while
statement could be used instead):
A switch within a switch:
A return statement from within a catch or finally block:
A try statement within a catch or finally block:
Too many cases within a switch:
Too many local variables within a method:
Too many returns from within a method:
Too many fields within an interface:
Limits the number of exceptions a method can throw:
Limits the number of expressions in a method:
Disallows empty statements:
Too many fields within a class:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks
Holds the current file contents for global access when configured as a TreeWalker sub-module. For example,a filter can access the current file contents through this module.
This check should not be used by itself, as it does no reporting. It is to be used in conjuction with other checks like SuppressionCommentFilter and SuppressWithNearbyCommentFilter.
com.puppycrawl.tools.checkstyle.checks
Check that parameters for methods, constructors, and catch blocks are final. Interface, abstract, and native methods are not checked: the final keyword does not make sense for interface, abstract, and native method parameters as there is no code that could modify the parameter.
Rationale: Changing the value of parameters during the execution of the method's algorithm can be confusing and should be avoided. A great way to let the Java compiler prevent this coding style is to declare parameters final.
name | description | type | default value |
---|---|---|---|
ignorePrimitiveTypes | ignore primitive types as parameters | Boolean | false |
tokens | tokens to check | subset of tokens METHOD_DEF, CTOR_DEF, LITERAL_CATCH, FOR_EACH_CLAUSE. | METHOD_DEF, CTOR_DEF. |
To configure the check to enforce final parameters for methods and constructors:
To configure the check to enforce final parameters only for constructors:
To configure the check to allow ignoring primitive datatypes as parameters:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks
Checks correct indentation of Java code.
The idea behind this is that while pretty printers are sometimes convenient for bulk reformats of legacy code, they often either aren't configurable enough or just can't anticipate how format should be done. Sometimes this is personal preference, other times it is practical experience. In any case, this check should just ensure that a minimal set of indentation rules is followed.
name | description | type | default value |
---|---|---|---|
basicOffset | how far new indentation level should be indented when on the next line | Integer | 4 |
braceAdjustment | how far a braces should be indented when on the next line | Integer | 0 |
caseIndent | how far a case label should be indented when on next line | Integer | 4 |
throwsIndent | how far a throws clause should be indented when on next line | Integer | 4 |
arrayInitIndent | how far an array initialisation should be indented when on next line | Integer | 4 |
lineWrappingIndentation | how far continuation line should be indented when line-wrapping is present | Integer | 4 |
forceStrictCondition | force strict indent level in line wrapping case. If value is true, line wrap indent have to be same as lineWrappingIndentation parameter. If value is false, line wrap indent could be bigger on any value user would like. | Boolean | false |
To configure the check:
To configure the check to enforce the indentation style recommended by Oracle:
To configure the Check to enforce strict condition in line-wrapping validation.
Such config doesn't allow next cases:
But if forceStrictCondition = false, this code is valid:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.indentation
Checks whether files end with a line separator.
Rationale: Any source files and text files in general should
end with a line separator to let other easily add new content
at the end of file and "diff" command does not show previous lines as changed.
Example (line 36 should not be in diff):
Old Ratinale: CVS source control management systems will even print a warning when it encounters a file that doesn't end with a line separator.
name | description | type | default value |
---|---|---|---|
lineSeparator | type of line separator | One of "system" (system default), "crlf" (Windows-style), "cr" (Mac-style), "lf" (Unix-style) and "lf_cr_crlf" (lf, cr or crlf). | "system" |
fileExtensions | file type extension of the files to check. | String Set | all files |
To configure the check:
To configure the check to always use Unix-style line separators:
To configure the check to work only on Java, XML and Python files:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks
Checks that the outer type name and the file name match. For example,
the class Foo
must be in a file named
Foo.java
.
To configure the check:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks
A check for TODO:
comments. Actually
it is a generic regular
expression matcher on Java comments. To check for other
patterns in Java comments, set the format
property.
name | description | type | default value |
---|---|---|---|
format | Pattern to match comments against | regular expression | TODO: |
Using TODO:
comments is a great way
to keep track of tasks that need to be done. Having them
reported by Checkstyle makes it very hard to forget about
them.
To configure the check:
To configure the check for comments that contain TODO
and FIXME
:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks
The check to ensure that requires that comments be the only thing on
a line. For the case of //
comments that means that the only thing
that should precede it is whitespace. It doesn't check comments if
they do not end a line; for example, it accepts the following:
Thread.sleep( 10 <some comment here> );
Format
property is intended to deal with the "} // while" example.
Rationale: Steve McConnel in Code Complete suggests that endline comments are a bad practice. An end line comment would be one that is on the same line as actual code. For example:
Quoting Code Complete for the justification:
McConnel's comments on being hard to maintain when the size of the line changes are even more important in the age of automated refactorings.
name | description | type | default value |
---|---|---|---|
format | pattern for strings allowed before the comment | regular expression | ^[\\s\\}\\);]*$ |
legalComment | pattern for text allowed in trailing comments. (This pattern will not be applied to multiline comments and the text of the comment will be trimmed before matching.) | regular expression | (not set) |
To configure the check:
To configure the check so it enforces only comment on a line:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks
A FileSetCheck that ensures the correct translation of code by checking property files for consistency regarding their keys. Two property files describing one and the same context are consistent if they contain the same keys. TranslationCheck also can check an existence of required translations which must exist in project, if 'requiredTranslations' option is used.
Consider the following properties file in the same directory:
The Translation check will find the typo in the German hello
key, the missing ok
key in the default resource file and the
missing cancel
key in the German resource file:
name | description | type | default value |
---|---|---|---|
fileExtensions |
File type extension to identify translation files. Setting
this property is typically only required if your
translation files are preprocessed and the original files
do not have the extension .properties
|
String Set | properties |
baseName | Base name of resource bundles which contain message resources. It helps the check to distinguish config and localization resources. | regular expression | ^messages.*$ |
requiredTranslations | Allows to specify language codes of required translations which must exist in project. Language code is composed of the lowercase, two-letter codes as defined by ISO 639-1. Default value is empty String Set which means that only the existence of default translation is checked. Note, if you specify language codes (or just one language code) of required translations the check will also check for existence of default translation files in project. ATTENTION: the check will perform the validation of ISO codes if the option is used. So, if you specify, for example, "mm" for language code, TranslationCheck will rise violation that the language code is incorrect. | String Set | empty String Set |
To configure the check to check only files which have '.properties' and '.translations' extensions:
Note, that files with the same path and base name but which have different extensions will be considered as files that belong to different resource bundles.
An example of how to configure the check to validate only bundles which base names start with "ButtonLabels":
To configure the check to check existence of Japanese and French translations:
The following example shows how the check works if there is a message bundle which element name contains language code, county code, platform name. Consider that we have the below configuration:
As we can see from the configuration, the TranslationCheck was configured to check an existence of 'es', 'fr' and 'de' translations. Lets assume that we have the resource bunbdle:
Than the check will rise the following violation: "0: Properties file 'messages_home_de.properties' is missing."
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks
Checks for uncommented main() methods.
Rationale: A main() method is often used for debugging purposes. When debugging is finished, developers often forget to remove the method, which changes the API and increases the size of the resulting class or JAR file. With the exception of the real program entry points, all main() methods should be removed or commented out of the sources.
name | description | type | default value |
---|---|---|---|
excludedClasses | Pattern for qualified names of classes which are allowed to have a main method. | regular expression | ^$ |
To configure the check:
To configure the check to allow the main
method for all classes
with "Main" name:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks
Checks properties files for duplicated properties.
Rationale: Multiple property keys usually appear after merge or rebase of several branches. While there are no errors in runtime, there can be a confusion due to having different values for the duplicated properties.
name | description | type | default value |
---|---|---|---|
fileExtensions | file type extension of the files to check. | String Set | properties |
To configure the check:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks
Checks that long constants are defined with an upper ell. That
is ' L'
and not 'l'
. This is in accordance with the Java
Language Specification,
Section 3.10.1.
The capital L looks a lot like 1
.
To configure the check:
All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks