diff --git a/docs/config_coding.html b/docs/config_coding.html new file mode 100644 index 000000000..26bef1019 --- /dev/null +++ b/docs/config_coding.html @@ -0,0 +1,416 @@ + + + + + Checks for Coding problems + + + + + + + + + + +

Checks for Coding problems

Checkstyle Logo
+ + + + + + + + +
+

EqualsHashCode

+

Description

+

+ Checks that classes that override equals() also + override hashCode(). +

+

+ Rationale: The contract of equals() and + hashCode() requires that equal objects have the same hashCode. Hence, + whenever you override equals() you must override + hashCode() to ensure that your class can be used in collections that are + hash based. +

+ +

Example

+

+ To configure the check: +

+
+<module name="EqualsHashCode"/>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+ +

HiddenField

Description

+

+ Checks that a local variable or a parameter does not shadow a field that is + defined in the same class. +

+

Properties

+ + + + + + + + + + + + + +
namedescriptiontypedefault value
tokenstokens to checksubset of tokens PARAMETER_DEF, VARIABLE_DEFPARAMETER_DEF, VARIABLE_DEF
+

Examples

+

+ To configure the check: +

+
+<module name="HiddenField"/>
+      
+

+ To configure the check so that it checks local variables but not parameters: +

+
+<module name="HiddenField">
+    <property name="tokens" value="VARIABLE_DEF"/>
+</module>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+ +

IllegalInstantiation

Description

+

+ Checks for illegal instantiations where a factory method is preferred. +

+

+ Rationale: Depending on the project, for some classes it might be preferable to + create instances through factory methods rather than calling the constructor. +

+

+ A simple example is the java.lang.Boolean class. In + order to save memory and CPU cycles, it is preferable to use the predefined + constants + TRUE and FALSE. Constructor invocations should + be replaced by calls to Boolean.valueOf(). +

+

+ Some extremely performance sensitive projects may require the use of factory + methods for other classes as well, to enforce the usage of number caches or + object pools. +

+

Notes

+

+ There is a limitation that it is currently not possible to specify array classes. +

+ + + + + + + + + + + + + +
namedescriptiontypedefault value
classesclasses that should not be instantiatedString Set{}
+

Example

+

+ To configure the check to find instantiations of java.lang.Boolean: +

+
+<module name="IllegalInstantiation">
+    <property name="classes" value="java.lang.Boolean"/>
+</module>
+      
+ +

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+

InnerAssignment

Description

+

+ Checks for assignments in subexpressions, such as in String s + = Integer.toString(i = 2);. +

+

+ Rationale: With the exception of for iterators, all + assignments should occur in their own toplevel statement to increase readability. + With inner assignments like the above it is difficult to see all places where a + variable is set. +

+

Properties

+ + + + + + + + + + + + + +
namedescriptiontypedefault value
tokensassignments to checksubset of tokens ASSIGN, BAND_ASSIGN, BOR_ASSIGN, BSR_ASSIGN, BXOR_ASSIGN, + DIV_ASSIGN, MINUS_ASSIGN, MOD_ASSIGN, PLUS_ASSIGN, SL_ASSIGN, SR_ASSIGN, + STAR_ASSIGNall tokens
+

Examples

+

+ To configure the check: +

+
+<module name="InnerAssignment"/>
+      
+

+ To configure the check for only =, + +=, and -= operators: +

+
+<module name="InnerAssignment">
+    <property name="tokens" value="ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN"/>
+</module>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+

MissingSwitchDefault

Description

+

+ Checks that switch statement has "default" clause. +

+

+ Rationale: It's usually a good idea to introduce a default case + in every switch statement. Even if the developer is sure that + all currently possible cases are covered, this should be + expressed in the default branch, e.g. by using an + assertion. This way the code is protected aginst later changes, + e.g. introduction of new types in an enumeration type. +

+

Examples

+

+ To configure the check: +

+
+<module name="MissingSwitchDefault"/>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+ +

SimplifyBooleanExpression

+

Description

+

+ Checks for overly complicated boolean expressions. Currently finds code like + if (b == true), b || true, !false, etc. +

+

+ Rationale: Complex boolean logic makes code hard to understand and maintain. +

+

Example

+

+ To configure the check: +

+
+<module name="SimplifyBooleanExpression"/>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+ +

SimplifyBooleanReturn

Description

+

+ Checks for overly complicated boolean return statements. For example the following code +

+
+if (valid())
+    return false;
+else
+    return true;
+      
+

+ could be written as +

+
+return !valid();
+      
+

+ The Idea for this Check has been shamelessly stolen + from the equivalent PMD rule. +

+

Example

+

+ To configure the check: +

+
+<module name="SimplifyBooleanReturn"/>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+ +

AvoidInlineConditionals

Description

+

+ Detects inline conditionals. + + An example inline conditional is this: +

+
+String a = getParameter("a");
+String b = (a==null || a.length<1) ? null : a.substring(1);
+      
+

+ Rationale: Some developers find inline conditionals hard to read, + so their company's coding standards forbids them. +

+

Examples

+

+ To configure the check: +

+
+<module name="AvoidInlineConditionals"/>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+ +

DoubleCheckedLocking

Description

+

+ The "double-checked locking" idiom (DCL) tries to avoid the runtime cost + of synchronization. An example that uses the DCL idiom is this: +

+
+public class MySingleton
+{
+    private static theInstance = null;
+
+    private MySingleton() {}
+
+    public MySingleton getInstance() {
+        if ( theInstance == null ) { // synchronize only if necessary
+            synchronized( MySingleton.class ) {
+                if ( theInstance == null ) {
+                    theInstance = new MySingleton();
+                }
+            }
+        }
+    }
+}
+      
+

+ The problem with the DCL idiom in Java is that it just does not work correctly. + Using it introduces bugs that are extremely hard to track down and reproduce. + The + "Double-Checked Locking is Broken" Declaration has an in depth explanation + of the exact problem which has to do with the semantics of the Java memory model. +

+

+ The DoubleCheckedLocking check will find source code where a test is wrapped in a + synchronized block that is wrapped in the same test, like in the example above. +

+

Examples

+

+ To configure the check: +

+
+<module name="DoubleCheckedLocking"/>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+ +
+
+ + + + diff --git a/docs/config_design.html b/docs/config_design.html new file mode 100644 index 000000000..56b6c6073 --- /dev/null +++ b/docs/config_design.html @@ -0,0 +1,297 @@ + + + + + Class Design Checks + + + + + + + + + + +

Class Design Checks

Checkstyle Logo
+ + + + + + + + +
+

VisibilityModifier

+

+ Checks visibility of class members. Only static final members + may be public; other class members must be private unless + property protectedAllowed or packageAllowed is set. +

+ +

+ Public members are not flagged if the name matches the public + member regular expression (contains "^serialVersionUID$" by default). Note: + Checkstyle 2 used to include "^f[A-Z][a-zA-Z0-9]*$" in the default + pattern to allow CMP for EJB 1.1 with the default settings. + With EJB 2.0 it is not longer necessary to have public access + for persistent fields, hence the default has been changed. +

+

+ Rationale: Enforce encapsulation. +

+ + +

Properties

+ + + + + + + + + + + + + + + + + + + + + + + + + +
namedescriptiontypedefault value
packageAllowedwhether package visible members are allowedbooleanfalse
protectedAllowedwhether protected members are allowedbooleanfalse
publicMemberPatternpattern for public members that should be ignoredregular expression^serialVersionUID$
+

Examples

+

+ To configure the check: +

+
+<module name="VisibilityModifier"/>
+      
+

+ To configure the check so that it allows package visible members: +

+
+<module name="VisibilityModifier">
+    <property name="packageAllowed" value="true"/>
+</module>
+      
+

+ To configure the check so that it allows no public members: +

+
+<module name="VisibilityModifier">
+    <property name="publicMemberPattern" value="^$"/>
+</module>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+ +

FinalClass

+ +

+ Checks that a class which has only private constructors is declared as final. +

+

Example

+

+ To configure the check: +

+
+<module name="FinalClass"/>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+

InterfaceIsType

Description

+

+ Implements Bloch, Effective Java, Item 17 - + Use Interfaces only to define types. +

+ +

+ According to Bloch, an interface should describe a type. + It is therefore inappropriate to define an interface that does not + contain any methods but only constants. The Standard class + javax.swing.SwingConstants + is an example of a class that would be flagged by this check. +

+ +

+ The check can be configured to also disallow marker interfaces like + java.io.Serializable, that do not contain methods or + constants at all. +

+ +

Properties

+ + + + + + + + + + + + + +
namedescriptiontypedefault value
allowMarkerInterfacesControls whether marker interfaces like Serializable are allowed.Booleantrue
+ +

Examples

+

+ To configure the check: +

+
+<module name="InterfaceIsType"/>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+ +

HideUtilityClassConstructor

Description

+

+ Make sure that utility classes (classes that contain only static methods) + do not have a public constructor. +

+

+ Rationale: Instantiating utility classes does not make sense. + Hence the constructors should either be private or (if you + want to allow subclassing) protected. A common mistake is forgetting + to hide the default constructor. +

+

+ If you make the constructor protected you may want to consider + the following constructor implementation technique to disallow + instantiating subclasses: +

+
+public class StringUtils // not final to allow subclassing
+{
+    protected StringUtils() {
+        throw new UnsupportedOperationException(); // prevents calls from subclass
+    }
+
+    public int count(char c, String s) {
+        // ...
+    }
+}
+      
+

Examples

+

+ To configure the check: +

+
+<module name="HideUtilityClassConstructor"/>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+ +

DesignForInheritance

Description

+

+ Checks that classes are designed for inheritance. + More specifically, it enforces a programming style + where superclasses provide empty "hooks" that can be + implemented by subclasses. +

+ +

+ The exact rule is that nonprivate, nonstatic methods of classes that + can be subclassed must either be +

+
    +
  • abstract or
  • +
  • final or
  • +
  • have an empty implementation
  • +
+ +

+ Rationale: This API design style protects superclasses against beeing broken by + subclasses. The downside is that subclasses are limited in their flexibility, + in particular they cannot prevent execution of code in the superclass, but that also + means that subclasses cannot corrupt the state of the superclass by forgetting to + call the super method. +

+ +

Properties

+ None. + +

Examples

+

+ To configure the check: +

+
+<module name="DesignForInheritance"/>
+      
+

Package

+

+ com.puppycrawl.tools.checkstyle.checks +

+

Parent Module

+

+ TreeWalker +

+ + +
+
+ + + + diff --git a/docs/config_misc.html b/docs/config_misc.html index 012ad5081..71e02442b 100644 --- a/docs/config_misc.html +++ b/docs/config_misc.html @@ -22,33 +22,12 @@ -

EqualsHashCode

-

Description

-

- Checks that classes that override equals() also - override hashCode(). -

-

- Rationale: The contract of equals() and - hashCode() requires that equal objects have the same hashCode. Hence, - whenever you override equals() you must override - hashCode() to ensure that your class can be used in collections that are - hash based. -

+ -

Example

-

- To configure the check: -

-
-<module name="EqualsHashCode"/>
-      
-

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

GenericIllegalRegexp

Description

A generic check for code problems - the user can search for any pattern. This is @@ -144,182 +81,6 @@ <module name="GenericIllegalRegexp"> <property name="format" value="System\.out\.println"/> </module> - -

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

-

HiddenField

Description

-

- Checks that a local variable or a parameter does not shadow a field that is - defined in the same class. -

-

Properties

- - - - - - - - - - - - - -
namedescriptiontypedefault value
tokenstokens to checksubset of tokens PARAMETER_DEF, VARIABLE_DEFPARAMETER_DEF, VARIABLE_DEF
-

Examples

-

- To configure the check: -

-
-<module name="HiddenField"/>
-      
-

- To configure the check so that it checks local variables but not parameters: -

-
-<module name="HiddenField">
-    <property name="tokens" value="VARIABLE_DEF"/>
-</module>
-      
-

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

-

IllegalInstantiation

Description

-

- Checks for illegal instantiations where a factory method is preferred. -

-

- Rationale: Depending on the project, for some classes it might be preferable to - create instances through factory methods rather than calling the constructor. -

-

- A simple example is the java.lang.Boolean class. In - order to save memory and CPU cycles, it is preferable to use the predefined - constants - TRUE and FALSE. Constructor invocations should - be replaced by calls to Boolean.valueOf(). -

-

- Some extremely performance sensitive projects may require the use of factory - methods for other classes as well, to enforce the usage of number caches or - object pools. -

-

Notes

-

- There is a limitation that it is currently not possible to specify array classes. -

- - - - - - - - - - - - - -
namedescriptiontypedefault value
classesclasses that should not be instantiatedString Set{}
-

Example

-

- To configure the check: -

-
-<module name="IllegalInstantiation"/>
-      
- -

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

-

InnerAssignment

Description

-

- Checks for assignments in subexpressions, such as in String s - = Integer.toString(i = 2);. -

-

- Rationale: With the exception of for iterators, all - assignments should occur in their own toplevel statement to increase readability. - With inner assignments like the above it is difficult to see all places where a - variable is set. -

-

Properties

- - - - - - - - - - - - - -
namedescriptiontypedefault value
tokensassignments to checksubset of tokens ASSIGN, BAND_ASSIGN, BOR_ASSIGN, BSR_ASSIGN, BXOR_ASSIGN, - DIV_ASSIGN, MINUS_ASSIGN, MOD_ASSIGN, PLUS_ASSIGN, SL_ASSIGN, SR_ASSIGN, - STAR_ASSIGNall tokens
-

Examples

-

- To configure the check: -

-
-<module name="InnerAssignment"/>
-      
-

- To configure the check for only =, - +=, and -= operators: -

-
-<module name="InnerAssignment">
-    <property name="tokens" value="ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN"/>
-</module>
-      
-

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

-

MissingSwitchDefault

Description

-

- Checks that switch statement has "default" clause. -

-

- Rationale: It's usually a good idea to introduce a default case - in every switch statement. Even if the developer is sure that - all currently possible cases are covered, this should be - expressed in the default branch, e.g. by using an - assertion. This way the code is protected aginst later changes, - e.g. introduction of new types in an enumeration type. -

-

Examples

-

- To configure the check: -

-
-<module name="MissingSwitchDefault"/>
       

Package

@@ -376,65 +137,6 @@

Checker

-

SimplifyBooleanExpression

-

Description

-

- Checks for overly complicated boolean expressions. Currently finds code like - if (b == true), b || true, !false, etc. -

-

- Rationale: Complex boolean logic makes code hard to understand and maintain. -

-

Example

-

- To configure the check: -

-
-<module name="SimplifyBooleanExpression"/>
-      
-

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

-

SimplifyBooleanReturn

Description

-

- Checks for overly complicated boolean return statements. For example the following code -

-
-if (valid())
-    return false;
-else
-    return true;
-      
-

- could be written as -

-
-return !valid();
-      
-

- The Idea for this Check has been shamelessly stolen - from the equivalent PMD rule. -

-

Example

-

- To configure the check: -

-
-<module name="SimplifyBooleanReturn"/>
-      
-

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

TodoComment

Description

A check for TODO: comments. Actually it is a generic regular @@ -579,198 +281,6 @@ like 1.

-

AvoidInlineConditionals

Description

-

- Detects inline conditionals. - - An example inline conditional is this: -

-
-String a = getParameter("a");
-String b = (a==null || a.length<1) ? null : a.substring(1);
-      
-

- Rationale: Some developers find inline conditionals hard to read, - so their company's coding standards forbids them. -

-

Examples

-

- To configure the check: -

-
-<module name="AvoidInlineConditionals"/>
-      
-

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

- -

InterfaceIsType

Description

-

- Implements Bloch, Effective Java, Item 17 - - Use Interfaces only to define types. -

- -

- According to Bloch, an interface should describe a type. - It is therefore inappropriate to define an interface that does not - contain any methods but only constants. The Standard class - javax.swing.SwingConstants - is an example of a class that would be flagged by this check. -

- -

- The check can be configured to also disallow marker interfaces like - java.io.Serializable, that do not contain methods or - constants at all. -

- -

Properties

- - - - - - - - - - - - - -
namedescriptiontypedefault value
allowMarkerInterfacesControls whether marker interfaces like Serializable are allowed.Booleantrue
- -

Examples

-

- To configure the check: -

-
-<module name="InterfaceIsType"/>
-      
-

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

- -

HideUtilityClassConstructor

Description

-

- Make sure that utility classes (classes that contain only static methods) - do not have a public constructor. -

-

- Rationale: Instantiating utility classes does not make sense. - Hence the constructors should either be private or (if you - want to allow subclassing) protected. A common mistake is forgetting - to hide the default constructor. -

-

- If you make the constructor protected you may want to consider - the following constructor implementation technique to disallow - instantiating subclasses: -

-
-public class StringUtils // not final to allow subclassing
-{
-    protected StringUtils() {
-        throw new UnsupportedOperationException(); // prevents calls from subclass
-    }
-
-    public int count(char c, String s) {
-        // ...
-    }
-}
-      
-

Examples

-

- To configure the check: -

-
-<module name="HideUtilityClassConstructor"/>
-      
-

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

- -

DesignForInheritance

Description

-

- Checks that classes are designed for inheritance. - More specifically, it enforces a programming style - where superclasses provide empty "hooks" that can be - implemented by subclasses. -

- -

- The exact rule is that nonprivate, nonstatic methods of classes that - can be subclassed must either be -

- - -

- Rationale: This API design style protects superclasses against beeing broken by - subclasses. The downside is that subclasses are limited in their flexibility, - in particular they cannot prevent execution of code in the superclass, but that also - means that subclasses cannot corrupt the state of the superclass by forgetting to - call the super method. -

- -

Properties

- - - - - - - - - - - - - -
namedescriptiontypedefault value
javaStyleControls whether to enforce Java style (true) or C style (false).Booleantrue
- -

Examples

-

- To configure the check to enforce Java style: -

-
-<module name="ArrayTypeStyle"/>
-      
-

- To configure the check to enforce C style: -

-
-<module name="ArrayTypeStyle">
-    <property name="javaStyle" value="false"/>
-</module>
-      
-

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

-

ArrayTypeStyle

Description

Checks the style of array type definitions. @@ -871,51 +381,6 @@ public class StringUtils // not final to allow subclassing TreeWalker

-

DoubleCheckedLocking

Description

-

- The "double-checked locking" idiom (DCL) tries to avoid the runtime cost - of synchronization. An example that uses the DCL idiom is this: -

-
-public class MySingleton
-{
-    private theInstance = null;
-
-    private MySingleton() {}
-
-    public MySingleton getInstance() {
-        if ( theInstance == null ) { // synchronize only if necessary
-            synchronized( MySingleton.class ) {
-                if ( s_singleton == null ) {
-                    theInstance = new MySingleton();
-                }
-            }
-        }
-    }
-}
-      
-

- The problem with the DCL idiom in Java is that it just does not work correctly. - Using it introduces bugs that are extremely hard to track down and reproduce. - The - "Double-Checked Locking is Broken" Declaration has an in depth explanation - of the exact problem which has to do with the semantics of the Java memory model. -

-

Examples

-

- To configure the check: -

-
-<module name="DoubleCheckedLocking"/>
-      
-

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

diff --git a/docs/config_modifiers.html b/docs/config_modifiers.html index 7572c044b..cb6e7428d 100644 --- a/docs/config_modifiers.html +++ b/docs/config_modifiers.html @@ -22,42 +22,18 @@ -

FinalClass

+ -

- Checks that a class which has only private constructors is declared as final. -

-

Example

-

- To configure the check: -

-
-<module name="FinalClass"/>
-      
-

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

-

ModifierOrder

+

ModifierOrder

Description

@@ -178,87 +154,6 @@ Language specification, section 9.3).

-

VisibilityModifier

-

- Checks visibility of class members. Only static final members - may be public; other class members must be private unless - property protectedAllowed or packageAllowed is set. -

- -

- Public members are not flagged if the name matches the public - member regular expression (contains "^serialVersionUID$" by default). Note: - Checkstyle 2 used to include "^f[A-Z][a-zA-Z0-9]*$" in the default - pattern to allow CMP for EJB 1.1 with the default settings. - With EJB 2.0 it is not longer necessary to have public access - for persistent fields, hence the default has been changed. -

-

- Rationale: Enforce encapsulation. -

- - -

Properties

- - - - - - - - - - - - - - - - - - - - - - - - - -
namedescriptiontypedefault value
packageAllowedwhether package visible members are allowedbooleanfalse
protectedAllowedwhether protected members are allowedbooleanfalse
publicMemberPatternpattern for public members that should be ignoredregular expression^serialVersionUID$
-

Examples

-

- To configure the check: -

-
-<module name="VisibilityModifier"/>
-      
-

- To configure the check so that it allows package visible members: -

-
-<module name="VisibilityModifier">
-    <property name="packageAllowed" value="true"/>
-</module>
-      
-

- To configure the check so that it allows no public members: -

-
-<module name="VisibilityModifier">
-    <property name="publicMemberPattern" value="^$"/>
-</module>
-      
-

Package

-

- com.puppycrawl.tools.checkstyle.checks -

-

Parent Module

-

- TreeWalker -

diff --git a/docs/index.html b/docs/index.html index addc5d729..074943a09 100644 --- a/docs/index.html +++ b/docs/index.html @@ -122,6 +122,8 @@
  • Whitespace
  • Modifiers
  • Blocks
  • +
  • Coding Problems
  • +
  • Class Design
  • Miscellaneous Checks