Class AnnotationUtil
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.utils.AnnotationUtil
-
public final class AnnotationUtil extends java.lang.Object
Contains utility methods designed to work with annotations.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringFQ_OVERRIDEFully-qualifiedOverrideannotation name.private static java.lang.StringOVERRIDEOverrideannotation name.private static java.util.List<java.lang.String>OVERRIDE_ANNOTATIONSList of simple and fully-qualifiedOverrideannotation names.private static java.lang.StringTHE_AST_IS_NULLCommon message.
-
Constructor Summary
Constructors Modifier Constructor Description privateAnnotationUtil()Private utility constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancontainsAnnotation(DetailAST ast)Checks if the AST is annotated with any annotation.static booleancontainsAnnotation(DetailAST ast, java.lang.String annotation)Checks if the AST is annotated with the passed in annotation.static booleancontainsAnnotation(DetailAST ast, java.util.List<java.lang.String> annotations)Checks if the given AST element is annotated with any of the specified annotations.private static DetailASTfindFirstAnnotation(DetailAST ast, java.util.function.Predicate<DetailAST> predicate)Checks if the given AST is annotated with at least one annotation that matches the given predicate and returns the AST representing the first matching annotation.static DetailASTgetAnnotation(DetailAST ast, java.lang.String annotation)Checks if the AST is annotated with the passed in annotation and returns the AST representing that annotation.static DetailASTgetAnnotationHolder(DetailAST ast)Gets the AST that holds a series of annotations for the potentially annotated AST.static booleanhasOverrideAnnotation(DetailAST ast)Checks if the AST is annotated withOverrideorjava.lang.Overrideannotation.
-
-
-
Field Detail
-
THE_AST_IS_NULL
private static final java.lang.String THE_AST_IS_NULL
Common message.- See Also:
- Constant Field Values
-
OVERRIDE
private static final java.lang.String OVERRIDE
Overrideannotation name.- See Also:
- Constant Field Values
-
FQ_OVERRIDE
private static final java.lang.String FQ_OVERRIDE
Fully-qualifiedOverrideannotation name.- See Also:
- Constant Field Values
-
OVERRIDE_ANNOTATIONS
private static final java.util.List<java.lang.String> OVERRIDE_ANNOTATIONS
List of simple and fully-qualifiedOverrideannotation names.
-
-
Constructor Detail
-
AnnotationUtil
private AnnotationUtil()
Private utility constructor.- Throws:
java.lang.UnsupportedOperationException- if called
-
-
Method Detail
-
containsAnnotation
public static boolean containsAnnotation(DetailAST ast, java.lang.String annotation)
Checks if the AST is annotated with the passed in annotation.This method will not look for imports or package statements to detect the passed in annotation.
To check if an AST contains a passed in annotation taking into account fully-qualified names (ex: java.lang.Override, Override) this method will need to be called twice. Once for each name given.
- Parameters:
ast- the current nodeannotation- the annotation name to check for- Returns:
- true if contains the annotation
-
containsAnnotation
public static boolean containsAnnotation(DetailAST ast)
Checks if the AST is annotated with any annotation.- Parameters:
ast- the current node- Returns:
trueif the AST contains at least one annotation- Throws:
java.lang.IllegalArgumentException- when ast is null
-
containsAnnotation
public static boolean containsAnnotation(DetailAST ast, java.util.List<java.lang.String> annotations)
Checks if the given AST element is annotated with any of the specified annotations.This method accepts both simple and fully-qualified names, e.g. "Override" will match both java.lang.Override and Override.
- Parameters:
ast- The type or method definition.annotations- A collection of annotations to look for.- Returns:
trueif the given AST element is annotated with at least one of the specified annotations;falseotherwise.- Throws:
java.lang.IllegalArgumentException- when ast or annotations are null
-
hasOverrideAnnotation
public static boolean hasOverrideAnnotation(DetailAST ast)
Checks if the AST is annotated withOverrideorjava.lang.Overrideannotation.- Parameters:
ast- the current node- Returns:
trueif the AST contains Override annotation- Throws:
java.lang.IllegalArgumentException- when ast is null
-
getAnnotationHolder
public static DetailAST getAnnotationHolder(DetailAST ast)
Gets the AST that holds a series of annotations for the potentially annotated AST. Returnsnullif the passed in AST does not have an Annotation Holder.- Parameters:
ast- the current node- Returns:
- the Annotation Holder
- Throws:
java.lang.IllegalArgumentException- when ast is null
-
getAnnotation
public static DetailAST getAnnotation(DetailAST ast, java.lang.String annotation)
Checks if the AST is annotated with the passed in annotation and returns the AST representing that annotation.This method will not look for imports or package statements to detect the passed in annotation.
To check if an AST contains a passed in annotation taking into account fully-qualified names (ex: java.lang.Override, Override) this method will need to be called twice. Once for each name given.
- Parameters:
ast- the current nodeannotation- the annotation name to check for- Returns:
- the AST representing that annotation
- Throws:
java.lang.IllegalArgumentException- when ast or annotations are null; when annotation is blank
-
findFirstAnnotation
private static DetailAST findFirstAnnotation(DetailAST ast, java.util.function.Predicate<DetailAST> predicate)
Checks if the given AST is annotated with at least one annotation that matches the given predicate and returns the AST representing the first matching annotation.This method will not look for imports or package statements to detect the passed in annotation.
- Parameters:
ast- the current nodepredicate- The predicate which decides if an annotation matches- Returns:
- the AST representing that annotation
-
-