Class PublicReferenceToPrivateTypeCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.github.sevntu.checkstyle.checks.design.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.AbstractCheckThis 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(); }Rationale: it is possible to return
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
}
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
-
-
Constructor Summary
Constructors Constructor Description PublicReferenceToPrivateTypeCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeginTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)voidfinishTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAst)int[]getAcceptableTokens()int[]getDefaultTokens()int[]getRequiredTokens()static booleanhasModifier(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.voidvisitToken(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
-
-
-
-
Field Detail
-
MSG_KEY
public static final String MSG_KEY
Check message key for private types.- See Also:
- Constant Field Values
-
-
Method Detail
-
getDefaultTokens
public int[] getDefaultTokens()
- Specified by:
getDefaultTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
beginTree
public void beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)
- Overrides:
beginTreein classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST defAst)
- Overrides:
visitTokenin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
finishTree
public void finishTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAst)
- Overrides:
finishTreein classcom.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 typedefAst- definition ast (METHOD_DEF, FIELD_DEF, etc.)- Returns:
- true if class, interface, enumeration, method or field definition has an access modifier of specified type
-
-