Class PublicReferenceToPrivateTypeCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class PublicReferenceToPrivateTypeCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
    This Check warns on propagation of inner private types to outer classes:
    - Externally accessible method if it returns private inner type.
    - Externally accessible field if its type is a private inner type.
    These types could be private inner classes, interfaces or enumerations.

    Examples:
     class OuterClass {
      public InnerClass innerFromMain = new InnerClass(); //WARNING
      private class InnerClass { ... }
      public InnerClass  getValue() { //WARNING
          return new InnerClass();
      }
      
    private interface InnerInterface { ... } public Set<InnerInterface> getValue() { //WARNING return new TreeSet<InnerInterface> }
    private Enum Fruit {Apple, Pear} public Fruit getValue() { //WARNING return Fruit.Apple; }
    public someMethod(InnerClass innerClass) { ... } //WARNING
    }
    Rationale: it is possible to return
    private inner type or use it as the parameter of non-private method, but it is impossible
    to use it in other classes (besides inner classes)
    unless it extends or implements at least one non-private class or interface.
    Such situation usually happens after bulk refactoring and usually means dead/useless code

    Since:
    1.12.0
    Author:
    Aleksey Nesterenko
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      Check message key for private types.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void beginTree​(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)  
      void finishTree​(com.puppycrawl.tools.checkstyle.api.DetailAST rootAst)  
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      static boolean hasModifier​(int modifierType, com.puppycrawl.tools.checkstyle.api.DetailAST defAst)
      Checks if class, interface, enumeration, method or field definition has an access modifier of specified type.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST defAst)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        clearViolations, destroy, 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.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Constructor Detail

      • PublicReferenceToPrivateTypeCheck

        public PublicReferenceToPrivateTypeCheck()
    • Method Detail

      • 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
      • beginTree

        public void beginTree​(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)
        Overrides:
        beginTree in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST defAst)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • finishTree

        public void finishTree​(com.puppycrawl.tools.checkstyle.api.DetailAST rootAst)
        Overrides:
        finishTree in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • hasModifier

        public static boolean hasModifier​(int modifierType,
                                          com.puppycrawl.tools.checkstyle.api.DetailAST defAst)
        Checks if class, interface, enumeration, method or field definition has an access modifier of specified type.
        Parameters:
        modifierType - modifier type
        defAst - definition ast (METHOD_DEF, FIELD_DEF, etc.)
        Returns:
        true if class, interface, enumeration, method or field definition has an access modifier of specified type