Class FinalClassCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.api.AutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck
- All Implemented Interfaces:
Configurable
,Contextualizable
Checks that a class which has only private constructors
is declared as final. Doesn't check for classes nested in interfaces
or annotations, as they are always final
there.
To configure the check:
<module name="FinalClass"/>
Example:
final class MyClass { // OK private MyClass() { } } class MyClass { // violation, class should be declared final private MyClass() { } } class MyClass { // OK, since it has a public constructor int field1; String field2; private MyClass(int value) { this.field1 = value; this.field2 = " "; } public MyClass(String value) { this.field2 = value; this.field1 = 0; } } interface CheckInterface { class MyClass { // OK, nested class in interface is always final private MyClass() {} } } public @interface Test { public boolean enabled() default true; class MyClass { // OK, class nested in an annotation is always final private MyClass() { } } } class TestAnonymousInnerClasses { // OK, class has an anonymous inner class. public static final TestAnonymousInnerClasses ONE = new TestAnonymousInnerClasses() { }; private TestAnonymousInnerClasses() { } }
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
final.class
- Since:
- 3.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Maintains information about class' ctors.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Deque
<FinalClassCheck.ClassDesc> Keeps ClassDesc objects for stack of declared classes.static final String
A key is pointing to the warning message text in "messages.properties" file.private static final String
Character separate package names in qualified name of java class.private String
Full qualified name of the package. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Called before the starting to process a tree.private static boolean
doesNameInExtendMatchSuperClassName
(String superClassQualifiedName, String superClassInExtendClause) Checks if given super class name in extend clause match super class qualified name.private static boolean
Check if class name matches with anonymous inner class name.private static String
Get name of class (with qualified package if specified) inast
.int[]
The configurable token set.private static String
getClassNameFromQualifiedName
(String qualifiedName) Get class name from qualified name.int[]
Returns the default token a check is interested in.private String
getQualifiedClassName
(DetailAST classAst) Get qualified class name from given class Ast.private static String
getQualifiedClassName
(String packageName, String outerClassQualifiedName, String className) Calculate qualified class name(package + class name) laying inside given outer class.int[]
The tokens that this check must be registered for.private static String
getSuperClassName
(DetailAST classAst) Get super class name of given class.void
leaveToken
(DetailAST ast) Called after all the child nodes have been process.private void
Register to outer super classes of given classAst that given classAst is extending them.void
visitToken
(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokens
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
PACKAGE_SEPARATOR
Character separate package names in qualified name of java class.- See Also:
-
classes
Keeps ClassDesc objects for stack of declared classes. -
packageName
Full qualified name of the package.
-
-
Constructor Details
-
FinalClassCheck
public FinalClassCheck()
-
-
Method Details
-
getDefaultTokens
public int[] getDefaultTokens()Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
public int[] getAcceptableTokens()Description copied from class:AbstractCheck
The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
public int[] getRequiredTokens()Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
-
beginTree
Description copied from class:AbstractCheck
Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTree
in classAbstractCheck
- Parameters:
rootAST
- the root of the tree
-
visitToken
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
leaveToken
Description copied from class:AbstractCheck
Called after all the child nodes have been process.- Overrides:
leaveToken
in classAbstractCheck
- Parameters:
ast
- the token leaving
-
extractQualifiedName
Get name of class (with qualified package if specified) inast
.- Parameters:
ast
- ast to extract class name from- Returns:
- qualified name
-
registerNestedSubclassToOuterSuperClasses
Register to outer super classes of given classAst that given classAst is extending them.- Parameters:
classAst
- class which outer super classes will be informed about nesting subclass
-
doesNameOfClassMatchAnonymousInnerClassName
private static boolean doesNameOfClassMatchAnonymousInnerClassName(DetailAST ast, FinalClassCheck.ClassDesc classDesc) Check if class name matches with anonymous inner class name.- Parameters:
ast
- current ast.classDesc
- class to match.- Returns:
- true if current class name matches anonymous inner class name.
-
getQualifiedClassName
Get qualified class name from given class Ast.- Parameters:
classAst
- class to get qualified class name- Returns:
- qualified class name of a class
-
getQualifiedClassName
private static String getQualifiedClassName(String packageName, String outerClassQualifiedName, String className) Calculate qualified class name(package + class name) laying inside given outer class.- Parameters:
packageName
- package name, empty string on default packageouterClassQualifiedName
- qualified name(package + class) of outer class, null if doesn't existclassName
- class name- Returns:
- qualified class name(package + class name)
-
getSuperClassName
Get super class name of given class.- Parameters:
classAst
- class- Returns:
- super class name or null if super class is not specified
-
doesNameInExtendMatchSuperClassName
private static boolean doesNameInExtendMatchSuperClassName(String superClassQualifiedName, String superClassInExtendClause) Checks if given super class name in extend clause match super class qualified name.- Parameters:
superClassQualifiedName
- super class qualified name (with package)superClassInExtendClause
- name in extend clause- Returns:
- true if given super class name in extend clause match super class qualified name, false otherwise
-
getClassNameFromQualifiedName
Get class name from qualified name.- Parameters:
qualifiedName
- qualified class name- Returns:
- class name
-