Class AnnotatedDeclarationVisibilityCheck
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.AnnotatedDeclarationVisibilityCheck
- 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.9.0
-
Nested Class Summary
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.Set of star imports.private AccessModifierOption[]Allowed visibility values. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newAnnotatedDeclarationVisibilityCheckinstance. -
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 StringgetVisibilityName(AccessModifierOption accessModifier) Gets the visibility value to display in violation messages.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 booleanisAllowedVisibility(AccessModifierOption accessModifier) Checks whether the given visibility value is allowed.private StringresolveAnnotation(String name) Resolves the fully qualified name of an annotation.voidsetAnnotations(String... values) Setter for annotation canonical names.voidsetVisibility(AccessModifierOption... 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:
-
DOT
Dot.- See Also:
-
annotations
Configured annotation canonical names. -
importedAnnotations
Set of non star imports. -
starImports
Set of star imports. -
visibility
Allowed visibility values. -
currentPackage
Current package.
-
-
Constructor Details
-
AnnotatedDeclarationVisibilityCheck
public AnnotatedDeclarationVisibilityCheck()Creates a newAnnotatedDeclarationVisibilityCheckinstance.
-
-
Method Details
-
setAnnotations
Setter for annotation canonical names.- Parameters:
values- comma-separated fully qualified annotation names- Since:
- 13.9.0
-
setVisibility
Setter for allowed visibility modifiers. Allowed values: public, protected, package, private.- Parameters:
values- allowed visibility values- Since:
- 13.9.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
-
isAllowedVisibility
Checks whether the given visibility value is allowed.- Parameters:
accessModifier- visibility value- Returns:
- true if given visibility value is allowed
-
getVisibilityName
Gets the visibility value to display in violation messages.- Parameters:
accessModifier- visibility value- Returns:
- display name of the visibility value
-