Class AlphabeticallyOrderedDefinitionsCheck

java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.atlassian.bitbucket.checkstyle.AlphabeticallyOrderedDefinitionsCheck
All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

public class AlphabeticallyOrderedDefinitionsCheck extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
Checks that definitions with the same modifiers are alphabetically ordered (with case insensitivity).

The configuration checks the ordering of variable and method definitions under classes and interfaces.

 <module name="AlphabeticallyOrderedDefinitions"/>
 
This check will error on a definition's line if it should be alphabetically before the last definition that has the same modifiers. For a definition that depends on a previous definition, if there are any ordering errors with definitions that come after the most recently declared definition that it depends on, the check will error. However, if the definition is a special variable, the check will pretend it's not there, so it won't be checked for alphabetical ordering, and will not affect variables declared after it. You can configure special variables by specifying the variable's type and identifier, separated by a space.
 <module name="AlphabeticallyOrderedDefinitions">
    <property name="specialVariables" value="Logger log, SpecialVariableType specialVariableName"/>
 </module>
 

To specify non-access modifiers to ignore, you can configure the ignoredModifiers property with final, static, transient, native, synchronized, volatile, abstract, strictfp, default, and annotation. For example, the following configuration will check that definitions that have the same modifiers, but ignoring whether they have static or final modifiers in common, are alphabetically ordered.

 <module name="AlphabeticallyOrderedDefinitions">
    <property name="ignoredModifiers" value="static, final"/>
 </module>
 

For simplicity regarding the annotation modifier, the current implementation only checks whether a definition is annotated and does not check what annotations the definition has, so if two definitions have the same modifiers and one is annotated with @Override and the other with @Nonnull, they'll still be compared.

You can configure the ignoreVariables or ignoreMethods properties to not check variables or methods respectively. For example, the following configuration doesn't check the ordering of methods:

 <module name="AlphabeticallyOrderedDefinitions">
     <property name="ignoreMethods" value="true"/>
 </module>
 
  • Nested Class Summary

    Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean

    com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int[]
     
    int[]
     
    int[]
     
    void
     
    void
    setIgnoreMethods(boolean shouldIgnore)
     
    void
    setIgnoreVariables(boolean shouldIgnore)
     
    void
     
    void
    visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
     

    Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

    beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, 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.AbstractAutomaticBean

    configure, contextualize, getConfiguration, setupChild

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AlphabeticallyOrderedDefinitionsCheck

      public AlphabeticallyOrderedDefinitionsCheck()
  • Method Details

    • getDefaultTokens

      public int[] getDefaultTokens()
      Specified by:
      getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • getAcceptableTokens

      public int[] getAcceptableTokens()
      Specified by:
      getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • getRequiredTokens

      public int[] getRequiredTokens()
      Specified by:
      getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • setIgnoredModifiers

      public void setIgnoredModifiers(String... modifiers)
    • setIgnoreMethods

      public void setIgnoreMethods(boolean shouldIgnore)
    • setIgnoreVariables

      public void setIgnoreVariables(boolean shouldIgnore)
    • setSpecialVariables

      public void setSpecialVariables(String... variables)
    • visitToken

      public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
      Overrides:
      visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck