Class AnnotatedMethodVisibilityModifierCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.modifier.AnnotatedMethodVisibilityModifierCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Checks that elements annotated with specified annotations
have only allowed visibility modifiers.
This check enforces consistency between annotation presence and declared visibility. If a configured annotation is found on a target element, its visibility modifier must match one of the allowed values.
- Since:
- 13.4.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final recordRecord holding visibility information.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionConfigured annotation canonical names.private StringCurrent package.private static final charDot.Set of non star imports.static final StringMessage key for violation.private static final String"package-private" visibility string.private static final String"private" visibility string.private static final String"protected" visibility string.private static final String"public" visibility string.Set of star imports.Allowed visibility values. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled before the starting to process a tree.private voidChecks the visibility of annotated elements.int[]The configurable token set.int[]Returns the default token a check is interested in.int[]The tokens that this check must be registered for.private static AnnotatedMethodVisibilityModifierCheck.VisibilityInfoExtracts visibility information from the AST node.private voidhandleImport(DetailAST ast) Processes import statements and records imported annotations.private voidhandlePackage(DetailAST ast) Handles package declarations and stores the current package name.private booleanDetermines whether the AST node contains a configured annotation.private static booleanDetermines whether the AST node is inside an interface.private StringresolveAnnotation(String name) Resolves the fully qualified name of an annotation.voidsetAnnotations(String... values) Setter for annotation canonical names.voidsetVisibility(String... values) Setter for allowed visibility modifiers.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
Message key for violation.- See Also:
-
PROTECTED
"protected" visibility string.- See Also:
-
PUBLIC
"public" visibility string.- See Also:
-
PRIVATE
"private" visibility string.- See Also:
-
PACKAGE_PRIVATE
"package-private" visibility string.- See Also:
-
DOT
Dot.- See Also:
-
annotations
Configured annotation canonical names. -
visibility
Allowed visibility values. Acceptable values: public, protected, package-private, private. -
importedAnnotations
Set of non star imports. -
starImports
Set of star imports. -
currentPackage
Current package.
-
-
Constructor Details
-
AnnotatedMethodVisibilityModifierCheck
-
-
Method Details
-
setAnnotations
Setter for annotation canonical names.- Parameters:
values- comma-separated fully qualified annotation names- Since:
- 13.4.0
-
setVisibility
Setter for allowed visibility modifiers. Allowed values: public, protected, private, package-private- Parameters:
values- comma-separated visibility names- Since:
- 13.4.0
-
beginTree
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
handlePackage
Handles package declarations and stores the current package name.- Parameters:
ast- package definition node
-
handleImport
Processes import statements and records imported annotations.- Parameters:
ast- import node
-
checkAnnotatedVisibility
Checks the visibility of annotated elements.- Parameters:
ast- AST node to inspect
-
hasConfiguredAnnotation
Determines whether the AST node contains a configured annotation.- Parameters:
ast- AST node to inspect- Returns:
- true if the annotation is present
-
resolveAnnotation
Resolves the fully qualified name of an annotation.- Parameters:
name- annotation name- Returns:
- resolved canonical name
-
getVisibilityInfo
private static AnnotatedMethodVisibilityModifierCheck.VisibilityInfo getVisibilityInfo(DetailAST ast) Extracts visibility information from the AST node.- Parameters:
ast- node to inspect- Returns:
- visibility information record
-
isInsideInterface
Determines whether the AST node is inside an interface.- Parameters:
ast- node to inspect- Returns:
- true if inside interface
-