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
public class FinalClassCheck extends AbstractCheck
Checks that a class that has only private constructors and has no descendant classes is declared as final.
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; } } class TestAnonymousInnerClasses { // OK, class has an anonymous inner class. public static final TestAnonymousInnerClasses ONE = new TestAnonymousInnerClasses() { }; private TestAnonymousInnerClasses() { } }Parent is
com.puppycrawl.tools.checkstyle.TreeWalkerViolation Message Keys:
-
final.class
- Since:
- 3.1
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classFinalClassCheck.ClassDescMaintains information about class' ctors.-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Deque<FinalClassCheck.ClassDesc>classesKeeps ClassDesc objects for stack of declared classes.private java.util.Map<java.lang.String,FinalClassCheck.ClassDesc>innerClassesKeeps ClassDesc objects for all inner classes.static java.lang.StringMSG_KEYA key is pointing to the warning message text in "messages.properties" file.private static java.lang.StringPACKAGE_SEPARATORCharacter separate package names in qualified name of java class.private java.lang.StringpackageNameFull qualified name of the package.
-
Constructor Summary
Constructors Constructor Description FinalClassCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeginTree(DetailAST rootAST)Called before the starting to process a tree.private static booleandoesNameOfClassMatchAnonymousInnerClassName(DetailAST ast, FinalClassCheck.ClassDesc classDesc)Check if class name matches with anonymous inner class name.int[]getAcceptableTokens()The configurable token set.private static intgetClassDeclarationNameMatchingCountDiff(java.lang.String superClassName, FinalClassCheck.ClassDesc firstClass, FinalClassCheck.ClassDesc secondClass)Get the difference between class declaration name matching count.private static java.lang.StringgetClassNameFromQualifiedName(java.lang.String qualifiedName)Get class name from qualified name.int[]getDefaultTokens()Returns the default token a check is interested in.private FinalClassCheck.ClassDescgetNearestClassWithSameName(java.lang.String className, java.lang.String superClassName)Checks if there is a class with same name.private java.lang.StringgetQualifiedClassName(DetailAST classAst)Get qualified class name from given class Ast.int[]getRequiredTokens()The tokens that this check must be registered for.private static java.lang.StringgetSuperClassName(DetailAST classAst)Get super class name of given class.voidleaveToken(DetailAST ast)Called after all the child nodes have been process.private voidregisterNestedSubclassToOuterSuperClasses(java.lang.String qualifiedClassName, FinalClassCheck.ClassDesc currentClass)Register to outer super class of given classAst that given classAst is extending them.private static booleanshouldBeDeclaredAsFinal(FinalClassCheck.ClassDesc desc)Checks whether a class should be declared as final or not.private voidvisitClass(DetailAST ast)Called to process a type definition.private voidvisitCtor(DetailAST ast)Called to process a constructor definition.voidvisitToken(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 Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
PACKAGE_SEPARATOR
private static final java.lang.String PACKAGE_SEPARATOR
Character separate package names in qualified name of java class.- See Also:
- Constant Field Values
-
innerClasses
private java.util.Map<java.lang.String,FinalClassCheck.ClassDesc> innerClasses
Keeps ClassDesc objects for all inner classes.
-
classes
private java.util.Deque<FinalClassCheck.ClassDesc> classes
Keeps ClassDesc objects for stack of declared classes.
-
packageName
private java.lang.String packageName
Full qualified name of the package.
-
-
Constructor Detail
-
FinalClassCheck
public FinalClassCheck()
-
-
Method Detail
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheckThe 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:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
beginTree
public void beginTree(DetailAST rootAST)
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
visitClass
private void visitClass(DetailAST ast)
Called to process a type definition.- Parameters:
ast- the token to process
-
visitCtor
private void visitCtor(DetailAST ast)
Called to process a constructor definition.- Parameters:
ast- the token to process
-
leaveToken
public void leaveToken(DetailAST ast)
Description copied from class:AbstractCheckCalled after all the child nodes have been process.- Overrides:
leaveTokenin classAbstractCheck- Parameters:
ast- the token leaving
-
shouldBeDeclaredAsFinal
private static boolean shouldBeDeclaredAsFinal(FinalClassCheck.ClassDesc desc)
Checks whether a class should be declared as final or not.- Parameters:
desc- description of the class- Returns:
- true if given class should be declared as final otherwise false
-
registerNestedSubclassToOuterSuperClasses
private void registerNestedSubclassToOuterSuperClasses(java.lang.String qualifiedClassName, FinalClassCheck.ClassDesc currentClass)
Register to outer super class of given classAst that given classAst is extending them.- Parameters:
qualifiedClassName- qualifies class name(with package) of the current classcurrentClass- class which outer super class will be informed about nesting subclass
-
getNearestClassWithSameName
private FinalClassCheck.ClassDesc getNearestClassWithSameName(java.lang.String className, java.lang.String superClassName)
Checks if there is a class with same name.- Parameters:
className- name of the classsuperClassName- name of the super class- Returns:
- true if there is another class with same name.
-
getClassDeclarationNameMatchingCountDiff
private static int getClassDeclarationNameMatchingCountDiff(java.lang.String superClassName, FinalClassCheck.ClassDesc firstClass, FinalClassCheck.ClassDesc secondClass)
Get the difference between class declaration name matching count. If the difference between them is zero, then their depth is compared to obtain the result.- Parameters:
superClassName- name of the super classfirstClass- first input classsecondClass- second input class- Returns:
- difference between class declaration name matching count
-
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
private java.lang.String getQualifiedClassName(DetailAST classAst)
Get qualified class name from given class Ast.- Parameters:
classAst- class to get qualified class name- Returns:
- qualified class name of a class
-
getSuperClassName
private static java.lang.String getSuperClassName(DetailAST classAst)
Get super class name of given class.- Parameters:
classAst- class- Returns:
- super class name or null if super class is not specified
-
getClassNameFromQualifiedName
private static java.lang.String getClassNameFromQualifiedName(java.lang.String qualifiedName)
Get class name from qualified name.- Parameters:
qualifiedName- qualified class name- Returns:
- class name
-
-