Class ElementHelper


  • public final class ElementHelper
    extends Object
    An utility class to work with element.
    Author:
    Kevin Pollet - SERLI - (kevin.pollet@serli.com), James R. Perkins
    • Method Detail

      • getTypeArguments

        public static List<? extends TypeMirror> getTypeArguments​(Element element)
        Returns the type arguments for the element. If the elements type is not a DeclaredType 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 List<? extends TypeMirror> getTypeArguments​(TypeMirror type)
        Returns the type arguments for the type. If the type is not a DeclaredType 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​(AnnotatedConstruct annotatedConstruct,
                                              Class<? extends 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, otherwise false
        Throws:
        IllegalArgumentException - if element parameter is null
      • getClassAnnotationValue

        public static TypeElement getClassAnnotationValue​(Element element,
                                                          Class<? extends Annotation> annotation)
        Retrieves the first attribute value from the annotation and assumes it's a class type.
        Parameters:
        element - the element the annotation is on
        annotation - the annotation to get the value from
        Returns:
        a TypeElement representing the value for the first annotation attribute or null if no attributes were found
      • getClassAnnotationValue

        public static TypeElement getClassAnnotationValue​(Element element,
                                                          Class<? extends Annotation> annotation,
                                                          String attributeName)
        Retrieves the attribute value from the annotation and assumes it's a class type.
        Parameters:
        element - the element the annotation is on
        annotation - the annotation to get the value from
        attributeName - the name of the attribute to retrieve the class value for
        Returns:
        a TypeElement representing the value for the annotation attribute or null if the attribute was not found
      • getClassArrayAnnotationValue

        public static List<TypeMirror> getClassArrayAnnotationValue​(Element element,
                                                                    Class<? extends Annotation> annotation,
                                                                    String attributeName)
        Retrieves the attribute value from the annotation and assumes it's an array classes.
        Parameters:
        element - the element the annotation is on
        annotation - the annotation to get the value from
        attributeName - 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 Collection<AnnotationMirror> getAnnotations​(Element element,
                                                                  Class<? extends Annotation> groupedAnnotation,
                                                                  Class<? extends Annotation> annotation)
        Returns annotations that are associated with the element that match the annotation parameter type. If the groupedAnnotation is not null then any repeated annotations that math the annotation parameter type are also returned.

        The groupedAnnotation must have a value attribute that includes an array of annotations that math the annotation parameter type.

        Parameters:
        element - the element to search for annotations
        groupedAnnotation - the grouped annotation, e.g. collector for repeatable annotations, or null if not a repeatable annotation
        annotation - the annotation to search for
        Returns:
        a collection matched annotations
      • hasConstructor

        public static boolean hasConstructor​(Types types,
                                             Element element,
                                             List<TypeMirror> args)
        Checks whether or not a constructor matching the parameters exists.
        Parameters:
        types - the type utility used to compare the type arguments
        element - the element that contains the constructors
        args - the arguments the constructor should match
        Returns:
        true if a matching constructor was found otherwise false
      • toTypeElement

        public static TypeElement toTypeElement​(ProcessingEnvironment processingEnv,
                                                Class<?> type)
        Returns the type as a TypeMirror.
        Parameters:
        processingEnv - the processing environment to get the elements utility
        type - the type to create the TypeMirror for
        Returns:
        the type
      • toTypeElement

        public static TypeElement toTypeElement​(Elements elements,
                                                Class<?> type)
        Returns the type as a TypeMirror.
        Parameters:
        elements - the element utility used to generate the tye type
        type - the type to create the TypeMirror for
        Returns:
        the type
      • toType

        public static TypeMirror toType​(Elements elements,
                                        Class<?> type)
        Returns the type as a TypeMirror.
        Parameters:
        elements - the element utility used to generate the tye type
        type - the type to create the TypeMirror for
        Returns:
        the type