AbstractClassName

Since Checkstyle 3.2

Description

Ensures that the names of abstract classes conforming to some pattern and check that abstract modifier exists.

Rationale: Abstract classes are convenience base class implementations of interfaces. For this reason, it should be made obvious that a given class is abstract by prefacing the class name with 'Abstract'.

Properties

name description type default value since
format Specify valid identifiers. Pattern "^Abstract.+$" 3.2
ignoreModifier Control whether to ignore checking for the abstract modifier on classes that match the name. boolean false 5.3
ignoreName Control whether to ignore checking the name. Realistically only useful if using the check to identify that match name and do not have the abstract modifier. boolean false 5.3

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="AbstractClassName"/>
  </module>
</module>
        

Example:

class Example1 {
  abstract class AbstractFirst {}
  abstract class Second {} // violation 'must match pattern'
  class AbstractThird {} // violation 'must be declared as 'abstract''
  class Fourth {}
  abstract class GeneratorFifth {}
  // violation above 'must match pattern'
  class GeneratorSixth {}
}
        

To configure the check so that it check name but ignore abstract modifier:

<module name="Checker">
  <module name="TreeWalker">
    <module name="AbstractClassName">
      <property name="ignoreModifier" value="true"/>
    </module>
  </module>
</module>
        

Example:

class Example2 {
  abstract class AbstractFirst {}
  abstract class Second {} // violation 'must match pattern'
  class AbstractThird {}
  class Fourth {}
  abstract class GeneratorFifth {}
  // violation above 'must match pattern'
  class GeneratorSixth {}
}
        

To configure the check to ignore name validation when class declared as 'abstract'

<module name="Checker">
  <module name="TreeWalker">
    <module name="AbstractClassName">
      <property name="ignoreName" value="true"/>
    </module>
  </module>
</module>
        

Example:

class Example3 {
  abstract class AbstractFirst {}
  abstract class Second {}
  class AbstractThird {} // violation 'must be declared as 'abstract''
  class Fourth {}
  abstract class GeneratorFifth {}
  class GeneratorSixth {}
}
        

To configure the check with format:

<module name="Checker">
  <module name="TreeWalker">
    <module name="AbstractClassName">
      <property name="format" value="^Generator.+$"/>
    </module>
  </module>
</module>
        

Example:

class Example4 {
  // violation below 'must match pattern'
  abstract class AbstractFirst {}
  abstract class Second {} // violation 'must match pattern'
  class AbstractThird {}
  class Fourth {}
  abstract class GeneratorFifth {}
  class GeneratorSixth {} // violation 'must be declared as 'abstract''
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.naming

Parent Module

TreeWalker