Class 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.TreeWalker

    Violation Message Keys:

    • final.class
    Since:
    3.1
    • Method Detail

      • 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 class AbstractCheck
        Returns:
        the token set this check is designed for.
        See Also:
        TokenTypes
      • beginTree

        public void beginTree​(DetailAST rootAST)
        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 class AbstractCheck
        Parameters:
        rootAST - the root of the tree
      • 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
      • 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 class
        currentClass - 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 class
        superClassName - 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 class
        firstClass - first input class
        secondClass - second input class
        Returns:
        difference between class declaration name matching count
      • 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