Package org.jboss.logging.processor.util
Class ElementHelper
- java.lang.Object
-
- org.jboss.logging.processor.util.ElementHelper
-
public final class ElementHelper extends java.lang.Object
An utility class to work with element.
-
-
Constructor Summary
Constructors Modifier Constructor Description private
ElementHelper()
Disable instantiation.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.Collection<javax.lang.model.element.AnnotationMirror>
getAnnotations(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> groupedAnnotation, java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Returns annotations that are associated with the element that match theannotation
parameter type.static javax.lang.model.element.TypeElement
getClassAnnotationValue(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Retrieves the first attribute value from the annotation and assumes it's aclass
type.static javax.lang.model.element.TypeElement
getClassAnnotationValue(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.lang.String attributeName)
Retrieves the attribute value from the annotation and assumes it's aclass
type.static java.util.List<javax.lang.model.type.TypeMirror>
getClassArrayAnnotationValue(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.lang.String attributeName)
Retrieves the attribute value from the annotation and assumes it's an arrayclasses
.private static java.util.Collection<javax.lang.model.element.AnnotationMirror>
getContainingAnnotations(javax.lang.model.element.AnnotationMirror annotation)
static java.util.List<? extends javax.lang.model.type.TypeMirror>
getTypeArguments(javax.lang.model.element.Element element)
Returns the type arguments for the element.static java.util.List<? extends javax.lang.model.type.TypeMirror>
getTypeArguments(javax.lang.model.type.TypeMirror type)
Returns the type arguments for the type.static boolean
hasConstructor(javax.lang.model.util.Types types, javax.lang.model.element.Element element, java.util.List<javax.lang.model.type.TypeMirror> args)
Checks whether or not a constructor matching the parameters exists.static boolean
isAnnotatedWith(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> clazz)
Check if an element is annotated with the given annotation.private static boolean
isSameType(java.lang.Class<?> c, javax.lang.model.type.TypeMirror type)
static javax.lang.model.type.TypeMirror
toType(javax.annotation.processing.ProcessingEnvironment processingEnv, java.lang.Class<?> type)
Returns the type as aTypeMirror
.static javax.lang.model.type.TypeMirror
toType(javax.lang.model.util.Elements elements, java.lang.Class<?> type)
Returns the type as aTypeMirror
.static javax.lang.model.element.TypeElement
toTypeElement(javax.annotation.processing.ProcessingEnvironment processingEnv, java.lang.Class<?> type)
Returns the type as aTypeMirror
.static javax.lang.model.element.TypeElement
toTypeElement(javax.lang.model.util.Elements elements, java.lang.Class<?> type)
Returns the type as aTypeMirror
.
-
-
-
Method Detail
-
getTypeArguments
public static java.util.List<? extends javax.lang.model.type.TypeMirror> getTypeArguments(javax.lang.model.element.Element element)
Returns the type arguments for the element. If the elements type is not aDeclaredType
or the element does not have any type arguments an empty list is returned.- Parameters:
element
- the element to get the type arguments for- Returns:
- the type arguments or an empty list
-
getTypeArguments
public static java.util.List<? extends javax.lang.model.type.TypeMirror> getTypeArguments(javax.lang.model.type.TypeMirror type)
Returns the type arguments for the type. If the type is not aDeclaredType
or the type does not have any type arguments an empty list is returned.- Parameters:
type
- the type to get the type arguments for- Returns:
- the type arguments or an empty list
-
isAnnotatedWith
public static boolean isAnnotatedWith(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> clazz)
Check if an element is annotated with the given annotation.- Parameters:
annotatedConstruct
- the object to look for the annotation on.clazz
- the annotation class- Returns:
true
if the element is annotated, otherwisefalse
- Throws:
java.lang.IllegalArgumentException
- if element parameter is null
-
getClassAnnotationValue
public static javax.lang.model.element.TypeElement getClassAnnotationValue(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Retrieves the first attribute value from the annotation and assumes it's aclass
type.- Parameters:
element
- the element the annotation is onannotation
- the annotation to get the value from- Returns:
- a
TypeElement
representing the value for the first annotation attribute ornull
if no attributes were found
-
getClassAnnotationValue
public static javax.lang.model.element.TypeElement getClassAnnotationValue(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.lang.String attributeName)
Retrieves the attribute value from the annotation and assumes it's aclass
type.- Parameters:
element
- the element the annotation is onannotation
- the annotation to get the value fromattributeName
- the name of the attribute to retrieve the class value for- Returns:
- a
TypeElement
representing the value for the annotation attribute ornull
if the attribute was not found
-
getClassArrayAnnotationValue
public static java.util.List<javax.lang.model.type.TypeMirror> getClassArrayAnnotationValue(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.lang.String attributeName)
Retrieves the attribute value from the annotation and assumes it's an arrayclasses
.- Parameters:
element
- the element the annotation is onannotation
- the annotation to get the value fromattributeName
- the name of the attribute to retrieve the class value array for- Returns:
- a list of
TypeMirror
representing the value for the annotation attribute or an empty list
-
getAnnotations
public static java.util.Collection<javax.lang.model.element.AnnotationMirror> getAnnotations(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> groupedAnnotation, java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Returns annotations that are associated with the element that match theannotation
parameter type. If thegroupedAnnotation
is notnull
then any repeated annotations that math theannotation
parameter type are also returned.The
groupedAnnotation
must have a value attribute that includes an array of annotations that math theannotation
parameter type.- Parameters:
element
- the element to search for annotationsgroupedAnnotation
- the grouped annotation, e.g. collector for repeatable annotations, ornull
if not a repeatable annotationannotation
- the annotation to search for- Returns:
- a collection matched annotations
-
hasConstructor
public static boolean hasConstructor(javax.lang.model.util.Types types, javax.lang.model.element.Element element, java.util.List<javax.lang.model.type.TypeMirror> args)
Checks whether or not a constructor matching the parameters exists.- Parameters:
types
- the type utility used to compare the type argumentselement
- the element that contains the constructorsargs
- the arguments the constructor should match- Returns:
true
if a matching constructor was found otherwisefalse
-
toTypeElement
public static javax.lang.model.element.TypeElement toTypeElement(javax.annotation.processing.ProcessingEnvironment processingEnv, java.lang.Class<?> type)
Returns the type as aTypeMirror
.- Parameters:
processingEnv
- the processing environment to get the elements utilitytype
- the type to create theTypeMirror
for- Returns:
- the type
-
toTypeElement
public static javax.lang.model.element.TypeElement toTypeElement(javax.lang.model.util.Elements elements, java.lang.Class<?> type)
Returns the type as aTypeMirror
.- Parameters:
elements
- the element utility used to generate the tye typetype
- the type to create theTypeMirror
for- Returns:
- the type
-
toType
public static javax.lang.model.type.TypeMirror toType(javax.annotation.processing.ProcessingEnvironment processingEnv, java.lang.Class<?> type)
Returns the type as aTypeMirror
.- Parameters:
processingEnv
- the processing environment to get the elements utilitytype
- the type to create theTypeMirror
for- Returns:
- the type
-
toType
public static javax.lang.model.type.TypeMirror toType(javax.lang.model.util.Elements elements, java.lang.Class<?> type)
Returns the type as aTypeMirror
.- Parameters:
elements
- the element utility used to generate the tye typetype
- the type to create theTypeMirror
for- Returns:
- the type
-
isSameType
private static boolean isSameType(java.lang.Class<?> c, javax.lang.model.type.TypeMirror type)
-
getContainingAnnotations
private static java.util.Collection<javax.lang.model.element.AnnotationMirror> getContainingAnnotations(javax.lang.model.element.AnnotationMirror annotation)
-
-