Class CheckstyleApiFixer


  • public class CheckstyleApiFixer
    extends Object
    The public API of the Checkstyle tool changes frequently, and many releases of Checkstyle are incompatible with their predecessors. This class uses reflection in order to allow the Checkstyle Addons code to work with all dependency configurations. It simply provides a stable interface with workarounds for all supported Checkstyle versions.
    • Constructor Detail

      • CheckstyleApiFixer

        public CheckstyleApiFixer​(@Nonnull
                                  AbstractCheck pCheck)
        Constructor.
        Parameters:
        pCheck - the check for which this instance of the API fixer shall run
      • CheckstyleApiFixer

        public CheckstyleApiFixer​(@Nonnull
                                  AbstractCheck pCheck,
                                  @Nullable
                                  String pCurrentFileNameMockFile)
        Constructor.
        Parameters:
        pCheck - the check for which this instance of the API fixer shall run
        pCurrentFileNameMockFile - file to use as result of getCurrentFileName() in unit tests
    • Method Detail

      • getCurrentFileName

        @CheckForNull
        public File getCurrentFileName()
        Wrapper for FileContents.getFileName() (Checkstyle issue #1205).
        Returns:
        the currently analyzed file as returned by Checkstyle
        Throws:
        UnsupportedOperationException - no known variant of FileContents.getFileName() could be found
      • getTokenName

        @Nonnull
        public String getTokenName​(int pTokenId)
        Returns the name of a token for a given ID.
        Parameters:
        pTokenId - the ID of the token name to get
        Returns:
        a token name as returned by Checkstyle
        Throws:
        UnsupportedOperationException - no known variant of getTokenName() could be found
      • isRootToken

        public boolean isRootToken​(int pToken)
        Determine if the given token is the root token of Checkstyle's AST. This means it's either an EOF token or a COMPILATION_UNIT token. This must be performed reflectively, because in Checkstyle 9.0, the EOF token was replaced with COMPILATION_UNIT.
        Parameters:
        pToken - the token to check
        Returns:
        flag indicating root token
        Throws:
        IllegalStateException - TokenTypes class has neither an EOF nor a COMPILATION_UNIT field