Class FinalClassCheck
- All Implemented Interfaces:
Configurable,Contextualizable
- Private classes with no declared constructors.
- Classes with any modifier, and contains only private constructors.
Classes are skipped if:
- Class is Super class of some Anonymous inner class.
- Class is extended by another class in the same file.
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
final.class
- Since:
- 3.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classMaintains information about the class.private static classMaintains information about the type of declaration.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionMaps anonymous inner class'sTokenTypes.LITERAL_NEWnode to the outer type declaration's fully qualified name.private Map<String,FinalClassCheck.ClassDesc> Keeps ClassDesc objects for all inner classes.static final StringA key is pointing to the warning message text in "messages.properties" file.private static final StringCharacter separate package names in qualified name of java class.private StringFull qualified name of the package.Keeps TypeDeclarationDescription object for stack of declared type descriptions. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled before the starting to process a tree.private StringextractQualifiedTypeName(DetailAST typeDeclarationAst) Extract the qualified type declaration name from given type declaration Ast.int[]The configurable token set.private static intgetAnonSuperTypeMatchingCount(String patternTypeDeclaration, String typeDeclarationToBeMatched) Calculates and returns the type declaration matching count whenclassToBeMatchedis considered to be super class of an anonymous inner class.int[]Returns the default token a check is interested in.private Optional<FinalClassCheck.ClassDesc>getNearestClassWithSameName(String className, ToIntFunction<FinalClassCheck.ClassDesc> countProvider) Get the nearest class with same name.int[]The tokens that this check must be registered for.private static StringgetSuperClassName(DetailAST classAst) Get super class name of given class.voidleaveToken(DetailAST ast) Called after all the child nodes have been process.private voidregisterAnonymousInnerClassToSuperClass(DetailAST literalNewAst, String outerTypeDeclName) Register to the super class of anonymous inner class that the given class is instantiated by an anonymous inner class.private voidregisterExtendedClass(String qualifiedClassName, FinalClassCheck.ClassDesc currentClass) Register to outer super class of given classAst that given classAst is extending them.private static booleanshouldBeDeclaredAsFinal(FinalClassCheck.ClassDesc classDesc) Checks whether a class should be declared as final or not.private voidvisitClass(DetailAST ast) Called to process a type definition.private voidCalled 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, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
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:
-
innerClasses
Keeps ClassDesc objects for all inner classes. -
anonInnerClassToOuterTypeDecl
Maps anonymous inner class'sTokenTypes.LITERAL_NEWnode to the outer type declaration's fully qualified name. -
typeDeclarations
Keeps TypeDeclarationDescription object for stack of declared type descriptions. -
packageName
Full qualified name of the package.
-
-
Constructor Details
-
FinalClassCheck
public FinalClassCheck()
-
-
Method Details
-
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:
-
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:
-
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:
-
beginTree
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
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
visitClass
Called to process a type definition.- Parameters:
ast- the token to process
-
visitCtor
Called to process a constructor definition.- Parameters:
ast- the token to process
-
leaveToken
Description copied from class:AbstractCheckCalled after all the child nodes have been process.- Overrides:
leaveTokenin classAbstractCheck- Parameters:
ast- the token leaving
-
shouldBeDeclaredAsFinal
Checks whether a class should be declared as final or not.- Parameters:
classDesc- description of the class- Returns:
- true if given class should be declared as final otherwise false
-
registerExtendedClass
private void registerExtendedClass(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
-
registerAnonymousInnerClassToSuperClass
private void registerAnonymousInnerClassToSuperClass(DetailAST literalNewAst, String outerTypeDeclName) Register to the super class of anonymous inner class that the given class is instantiated by an anonymous inner class.- Parameters:
literalNewAst- ast node ofTokenTypes.LITERAL_NEWrepresenting anonymous inner classouterTypeDeclName- Fully qualified name of the outer type declaration of anonymous inner class
-
getNearestClassWithSameName
private Optional<FinalClassCheck.ClassDesc> getNearestClassWithSameName(String className, ToIntFunction<FinalClassCheck.ClassDesc> countProvider) Get the nearest class with same name.The parameter
countProviderexists because if the class being searched is the super class of anonymous inner class, the rules of evaluation are a bit different, consider the following example-
If thepublic class Main { static class One { static class Two { } } class Three { One.Two object = new One.Two() { // Object of Main.Three.One.Two // and not of Main.One.Two }; static class One { static class Two { } } } }FunctioncountProviderhadn't usedgetAnonSuperTypeMatchingCount(java.lang.String, java.lang.String)to calculate the matching count then the logic would have falsely evaluatedMain.One.Twoto be the super class of the anonymous inner class.- Parameters:
className- name of the classcountProvider- the function to apply to calculate the name matching count- Returns:
OptionalofFinalClassCheck.ClassDescobject of the nearest class with the same name.
-
extractQualifiedTypeName
Extract the qualified type declaration name from given type declaration Ast.- Parameters:
typeDeclarationAst- type declaration for which qualified name is being fetched- Returns:
- qualified name of a type declaration
-
getSuperClassName
Get super class name of given class.- Parameters:
classAst- class- Returns:
- super class name or null if super class is not specified
-
getAnonSuperTypeMatchingCount
private static int getAnonSuperTypeMatchingCount(String patternTypeDeclaration, String typeDeclarationToBeMatched) Calculates and returns the type declaration matching count whenclassToBeMatchedis considered to be super class of an anonymous inner class.Suppose our pattern class is
Main.ClassOneand class to be matched isMain.ClassOne.ClassTwo.ClassThreethen type declaration name matching count would be calculated by comparing every character, and updating main counter when we hit "." or when it is the last character of the pattern class and certain conditions are met. This is done so that matching count is 13 instead of 5. This is due to the fact that pattern class can contain anonymous inner class object of a nested class which isn't true in case of extending classes as you can't extend nested classes.- Parameters:
patternTypeDeclaration- type declaration against which the given type declaration has to be matchedtypeDeclarationToBeMatched- type declaration to be matched- Returns:
- type declaration matching count
-