Class JandexUtil


  • public class JandexUtil
    extends Object
    Some utility methods for working with Jandex objects.
    Author:
    [email protected]
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  JandexUtil.RefType
      Simple enum to indicate the type of a $ref being read/written.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Optional<Boolean> booleanValue​(org.jboss.jandex.AnnotationInstance annotation, String propertyName)
      Reads a Boolean property value from the given annotation instance.
      static boolean booleanValueWithDefault​(org.jboss.jandex.AnnotationInstance annotation, String propertyName)
      Reads a Boolean property from the given annotation instance.
      static String createUniqueMethodReference​(org.jboss.jandex.MethodInfo methodInfo)  
      static <T extends Enum<?>>
      T
      enumValue​(String strVal, Class<T> clazz)
      Converts a string value to the given enum type.
      static <T extends Enum<?>>
      T
      enumValue​(org.jboss.jandex.AnnotationInstance annotation, String propertyName, Class<T> clazz)
      Reads a String property value from the given annotation instance.
      static boolean equals​(org.jboss.jandex.AnnotationTarget t1, org.jboss.jandex.AnnotationTarget t2)  
      static boolean equals​(org.jboss.jandex.ClassInfo c1, org.jboss.jandex.ClassInfo c2)  
      static boolean equals​(org.jboss.jandex.FieldInfo f1, org.jboss.jandex.FieldInfo f2)  
      static boolean equals​(org.jboss.jandex.MethodParameterInfo p1, org.jboss.jandex.MethodParameterInfo p2)  
      static org.jboss.jandex.AnnotationInstance getAnnotation​(org.jboss.jandex.FieldInfo field, Collection<org.jboss.jandex.DotName> names)
      Gets a single annotation from the given field.
      static org.jboss.jandex.AnnotationInstance getAnnotation​(org.jboss.jandex.FieldInfo field, org.jboss.jandex.DotName name)  
      static org.jboss.jandex.AnnotationInstance getAnnotation​(org.jboss.jandex.MethodInfo mi, Collection<org.jboss.jandex.DotName> names)
      Gets a single annotation from the given method.
      static org.jboss.jandex.AnnotationInstance getAnnotation​(org.jboss.jandex.MethodInfo mi, org.jboss.jandex.DotName... names)  
      static org.jboss.jandex.AnnotationInstance getClassAnnotation​(org.jboss.jandex.ClassInfo ct, Collection<org.jboss.jandex.DotName> names)
      Gets a single class annotation from the given class.
      static org.jboss.jandex.AnnotationInstance getClassAnnotation​(org.jboss.jandex.ClassInfo ct, org.jboss.jandex.DotName name)
      Gets a single class annotation from the given class.
      static org.jboss.jandex.AnnotationInstance getMethodParameterAnnotation​(org.jboss.jandex.MethodInfo method, int parameterIndex, org.jboss.jandex.DotName annotationName)
      Finds an annotation (if present) with the given name, on a particular parameter of a method.Returns null if not found.
      static org.jboss.jandex.Type getMethodParameterType​(org.jboss.jandex.MethodInfo method, short position)
      Returns the class type of the method parameter at the given position.
      static org.jboss.jandex.Type getMethodParameterType​(org.jboss.jandex.MethodParameterInfo parameter)
      Returns the class type of the method parameter.
      static List<org.jboss.jandex.AnnotationInstance> getParameterAnnotations​(org.jboss.jandex.MethodInfo method, short paramPosition)
      Returns all annotations configured for a single parameter of a method.
      static List<org.jboss.jandex.AnnotationInstance> getRepeatableAnnotation​(org.jboss.jandex.AnnotationTarget target, org.jboss.jandex.DotName singleAnnotationName, org.jboss.jandex.DotName repeatableAnnotationName)
      Many OAI annotations can either be found singly or as a wrapped array.
      static boolean hasAnyOneOfAnnotation​(org.jboss.jandex.MethodInfo method, Collection<org.jboss.jandex.DotName> annotations)
      Return if any one of the listed annotations exist
      static boolean hasAnyOneOfAnnotation​(org.jboss.jandex.MethodInfo method, org.jboss.jandex.DotName... annotations)
      Return if any one of the listed annotations exist
      static boolean hasImplementation​(org.jboss.jandex.AnnotationInstance annotation)
      Returns true if the given @Schema annotation has defined an "implementation" field.
      static Map<org.jboss.jandex.ClassInfo,​org.jboss.jandex.Type> inheritanceChain​(org.jboss.jandex.IndexView index, org.jboss.jandex.ClassInfo klazz, org.jboss.jandex.Type type)
      Builds an insertion-order map of a class's inheritance chain, starting with the klazz argument.
      static Integer intValue​(org.jboss.jandex.AnnotationInstance annotation, String propertyName)
      Reads a Integer property value from the given annotation instance.
      static boolean isArraySchema​(org.jboss.jandex.AnnotationInstance annotation)
      Returns true if the given @Schema annotation is an array schema.
      static boolean isEmpty​(org.jboss.jandex.AnnotationInstance annotation)
      Returns true if the given annotation is void of any values (and thus is "empty").
      static boolean isRef​(org.jboss.jandex.AnnotationInstance annotation)
      Returns true if the given annotation instance is a "ref".
      static boolean isSimpleArraySchema​(org.jboss.jandex.AnnotationInstance annotation)
      Returns true if the given @Schema annotation is a simple array schema.
      static boolean isSimpleClassSchema​(org.jboss.jandex.AnnotationInstance annotation)
      Returns true if the given @Schema annotation is a simple class schema.
      static String nameFromRef​(org.jboss.jandex.AnnotationInstance annotation)
      Gets the name of an item from its ref.
      static Optional<String> optionalStringValue​(org.jboss.jandex.AnnotationInstance annotation, String propertyName)  
      static String refValue​(org.jboss.jandex.AnnotationInstance annotation, JandexUtil.RefType refType)
      Reads a string property named "ref" value from the given annotation and converts it to a value appropriate for setting on a model's "$ref" property.
      static List<org.jboss.jandex.AnnotationValue> schemaDisplayValues​(org.jboss.jandex.AnnotationInstance annotation)  
      static Optional<List<String>> stringListValue​(org.jboss.jandex.AnnotationInstance annotation, String propertyName)
      Reads a String array property value from the given annotation instance.
      static String stringValue​(org.jboss.jandex.AnnotationInstance annotation, String propertyName)
      Reads a String property value from the given annotation instance.
      static <T> T value​(org.jboss.jandex.AnnotationInstance annotation, String name)
      Convenience method to retrieve the named parameter from an annotation.
      static <T> T value​(org.jboss.jandex.AnnotationInstance annotation, String name, T defaultValue)
      Convenience method to retrieve the named parameter from an annotation.
    • Method Detail

      • createUniqueMethodReference

        public static String createUniqueMethodReference​(org.jboss.jandex.MethodInfo methodInfo)
      • refValue

        public static String refValue​(org.jboss.jandex.AnnotationInstance annotation,
                                      JandexUtil.RefType refType)
        Reads a string property named "ref" value from the given annotation and converts it to a value appropriate for setting on a model's "$ref" property.
        Parameters:
        annotation - AnnotationInstance
        refType - RefType
        Returns:
        String value
      • value

        public static <T> T value​(org.jboss.jandex.AnnotationInstance annotation,
                                  String name)
        Convenience method to retrieve the named parameter from an annotation. The value will be unwrapped from its containing AnnotationValue.
        Type Parameters:
        T - the type of the parameter being retrieved
        Parameters:
        annotation - the annotation from which to fetch the parameter
        name - the name of the parameter
        Returns:
        an unwrapped annotation parameter value
      • value

        public static <T> T value​(org.jboss.jandex.AnnotationInstance annotation,
                                  String name,
                                  T defaultValue)
        Convenience method to retrieve the named parameter from an annotation. The value will be unwrapped from its containing AnnotationValue.
        Type Parameters:
        T - the type of the parameter being retrieved
        Parameters:
        annotation - the annotation from which to fetch the parameter
        name - the name of the parameter
        defaultValue - a default value to return if the parameter is not defined
        Returns:
        an unwrapped annotation parameter value
      • stringValue

        public static String stringValue​(org.jboss.jandex.AnnotationInstance annotation,
                                         String propertyName)
        Reads a String property value from the given annotation instance. If no value is found this will return null.
        Parameters:
        annotation - AnnotationInstance
        propertyName - String
        Returns:
        String value
      • optionalStringValue

        public static Optional<String> optionalStringValue​(org.jboss.jandex.AnnotationInstance annotation,
                                                           String propertyName)
      • booleanValue

        public static Optional<Boolean> booleanValue​(org.jboss.jandex.AnnotationInstance annotation,
                                                     String propertyName)
        Reads a Boolean property value from the given annotation instance. If no value is found this will return null.
        Parameters:
        annotation - AnnotationInstance
        propertyName - String
        Returns:
        Boolean value
      • booleanValueWithDefault

        public static boolean booleanValueWithDefault​(org.jboss.jandex.AnnotationInstance annotation,
                                                      String propertyName)
        Reads a Boolean property from the given annotation instance. If no value is found this will return false.
        Parameters:
        annotation - AnnotationInstance
        propertyName - String
        Returns:
        Boolean value
      • intValue

        public static Integer intValue​(org.jboss.jandex.AnnotationInstance annotation,
                                       String propertyName)
        Reads a Integer property value from the given annotation instance. If no value is found this will return null.
        Parameters:
        annotation - AnnotationInstance
        propertyName - String
        Returns:
        Integer value
      • stringListValue

        public static Optional<List<String>> stringListValue​(org.jboss.jandex.AnnotationInstance annotation,
                                                             String propertyName)
        Reads a String array property value from the given annotation instance. If no value is found this will return null.
        Parameters:
        annotation - AnnotationInstance
        propertyName - String
        Returns:
        List of Strings
      • enumValue

        public static <T extends Enum<?>> T enumValue​(org.jboss.jandex.AnnotationInstance annotation,
                                                      String propertyName,
                                                      Class<T> clazz)
        Reads a String property value from the given annotation instance. If no value is found this will return null.
        Type Parameters:
        T - Type parameter
        Parameters:
        annotation - AnnotationInstance
        propertyName - String
        clazz - Class type of the Enum
        Returns:
        Value of property
      • enumValue

        public static <T extends Enum<?>> T enumValue​(String strVal,
                                                      Class<T> clazz)
        Converts a string value to the given enum type. If the string does not match one of the the enum's values name (case-insensitive) or toString value, null will be returned.
        Type Parameters:
        T - Type parameter
        Parameters:
        strVal - String
        clazz - Class type of the Enum
        Returns:
        Value of property
      • isRef

        public static boolean isRef​(org.jboss.jandex.AnnotationInstance annotation)
        Returns true if the given annotation instance is a "ref". An annotation is a ref if it has a non-null value for the "ref" property.
        Parameters:
        annotation - AnnotationInstance
        Returns:
        Whether it's a "ref"
      • isEmpty

        public static boolean isEmpty​(org.jboss.jandex.AnnotationInstance annotation)
        Returns true if the given annotation is void of any values (and thus is "empty"). An example of this would be if a jax-rs method were annotated with @Tag()
        Parameters:
        annotation - AnnotationInstance
        Returns:
        Whether it's empty
      • getClassAnnotation

        public static org.jboss.jandex.AnnotationInstance getClassAnnotation​(org.jboss.jandex.ClassInfo ct,
                                                                             org.jboss.jandex.DotName name)
        Gets a single class annotation from the given class. Returns null if no matching annotation is found.
        Parameters:
        ct - ClassInfo
        name - DotName
        Returns:
        AnnotationInstance
      • getClassAnnotation

        public static org.jboss.jandex.AnnotationInstance getClassAnnotation​(org.jboss.jandex.ClassInfo ct,
                                                                             Collection<org.jboss.jandex.DotName> names)
        Gets a single class annotation from the given class. Returns null if no matching annotation is found.
        Parameters:
        ct - ClassInfo
        names - List of DotNames
        Returns:
        AnnotationInstance
      • getAnnotation

        public static org.jboss.jandex.AnnotationInstance getAnnotation​(org.jboss.jandex.MethodInfo mi,
                                                                        org.jboss.jandex.DotName... names)
      • getAnnotation

        public static org.jboss.jandex.AnnotationInstance getAnnotation​(org.jboss.jandex.FieldInfo field,
                                                                        org.jboss.jandex.DotName name)
      • getAnnotation

        public static org.jboss.jandex.AnnotationInstance getAnnotation​(org.jboss.jandex.FieldInfo field,
                                                                        Collection<org.jboss.jandex.DotName> names)
        Gets a single annotation from the given field. Returns null if no matching annotation is found.
        Parameters:
        field - FieldInfo
        names - DotName
        Returns:
        AnnotationInstance
      • getAnnotation

        public static org.jboss.jandex.AnnotationInstance getAnnotation​(org.jboss.jandex.MethodInfo mi,
                                                                        Collection<org.jboss.jandex.DotName> names)
        Gets a single annotation from the given method. Returns null if no matching annotation is found.
        Parameters:
        mi - MethodInfo
        names - DotName
        Returns:
        AnnotationInstance
      • hasAnyOneOfAnnotation

        public static boolean hasAnyOneOfAnnotation​(org.jboss.jandex.MethodInfo method,
                                                    org.jboss.jandex.DotName... annotations)
        Return if any one of the listed annotations exist
        Parameters:
        method -
        annotations -
        Returns:
      • hasAnyOneOfAnnotation

        public static boolean hasAnyOneOfAnnotation​(org.jboss.jandex.MethodInfo method,
                                                    Collection<org.jboss.jandex.DotName> annotations)
        Return if any one of the listed annotations exist
        Parameters:
        method -
        annotations -
        Returns:
      • getParameterAnnotations

        public static List<org.jboss.jandex.AnnotationInstance> getParameterAnnotations​(org.jboss.jandex.MethodInfo method,
                                                                                        short paramPosition)
        Returns all annotations configured for a single parameter of a method.
        Parameters:
        method - MethodInfo
        paramPosition - parameter position
        Returns:
        List of AnnotationInstance's
      • nameFromRef

        public static String nameFromRef​(org.jboss.jandex.AnnotationInstance annotation)
        Gets the name of an item from its ref. For example, the ref might be "#/components/parameters/departureDate" which would result in a name of "departureDate".
        Parameters:
        annotation - AnnotationInstance
        Returns:
        Name of item from ref
      • getRepeatableAnnotation

        public static List<org.jboss.jandex.AnnotationInstance> getRepeatableAnnotation​(org.jboss.jandex.AnnotationTarget target,
                                                                                        org.jboss.jandex.DotName singleAnnotationName,
                                                                                        org.jboss.jandex.DotName repeatableAnnotationName)
        Many OAI annotations can either be found singly or as a wrapped array. This method will look for both and return a list of all found. Both the single and wrapper annotation names must be provided.
        Parameters:
        target - the annotated target (e.g. ClassInfo, MethodInfo)
        singleAnnotationName - DotName
        repeatableAnnotationName - DotName
        Returns:
        List of AnnotationInstance's
      • getMethodParameterType

        public static org.jboss.jandex.Type getMethodParameterType​(org.jboss.jandex.MethodInfo method,
                                                                   short position)
        Returns the class type of the method parameter at the given position.
        Parameters:
        method - MethodInfo
        position - parameter position
        Returns:
        Type
      • getMethodParameterType

        public static org.jboss.jandex.Type getMethodParameterType​(org.jboss.jandex.MethodParameterInfo parameter)
        Returns the class type of the method parameter.
        Parameters:
        parameter - the parameter
        Returns:
        Type
      • getMethodParameterAnnotation

        public static org.jboss.jandex.AnnotationInstance getMethodParameterAnnotation​(org.jboss.jandex.MethodInfo method,
                                                                                       int parameterIndex,
                                                                                       org.jboss.jandex.DotName annotationName)
        Finds an annotation (if present) with the given name, on a particular parameter of a method.Returns null if not found.
        Parameters:
        method - the method
        parameterIndex - the parameter index
        annotationName - name of annotation we are looking for
        Returns:
        the Annotation instance
      • schemaDisplayValues

        public static List<org.jboss.jandex.AnnotationValue> schemaDisplayValues​(org.jboss.jandex.AnnotationInstance annotation)
      • isSimpleClassSchema

        public static boolean isSimpleClassSchema​(org.jboss.jandex.AnnotationInstance annotation)
        Returns true if the given @Schema annotation is a simple class schema. This means that the annotation only has one field defined, and that field is "implementation".
        Parameters:
        annotation - AnnotationInstance
        Returns:
        Is it a simple class @Schema
      • isSimpleArraySchema

        public static boolean isSimpleArraySchema​(org.jboss.jandex.AnnotationInstance annotation)
        Returns true if the given @Schema annotation is a simple array schema. This is defined as a schema with only a "type" field and "implementation" field defined *and* the type must be array.
        Parameters:
        annotation - AnnotationInstance
        Returns:
        Is it a simple array @Schema
      • isArraySchema

        public static boolean isArraySchema​(org.jboss.jandex.AnnotationInstance annotation)
        Returns true if the given @Schema annotation is an array schema. This is defined as a schema with a "type" field and "implementation" field defined *and* the type must be array.
        Parameters:
        annotation - AnnotationInstance
        Returns:
        Is it an array @Schema
      • hasImplementation

        public static boolean hasImplementation​(org.jboss.jandex.AnnotationInstance annotation)
        Returns true if the given @Schema annotation has defined an "implementation" field.
        Parameters:
        annotation - AnnotationInstance
        Returns:
        true if the annotation defines an implementation, otherwise false
      • inheritanceChain

        public static Map<org.jboss.jandex.ClassInfo,​org.jboss.jandex.Type> inheritanceChain​(org.jboss.jandex.IndexView index,
                                                                                                   org.jboss.jandex.ClassInfo klazz,
                                                                                                   org.jboss.jandex.Type type)
        Builds an insertion-order map of a class's inheritance chain, starting with the klazz argument.
        Parameters:
        index - index for superclass retrieval
        klazz - the class to retrieve inheritance
        type - type of the klazz
        Returns:
        map of a class's inheritance chain/ancestry
      • equals

        public static boolean equals​(org.jboss.jandex.AnnotationTarget t1,
                                     org.jboss.jandex.AnnotationTarget t2)
      • equals

        public static boolean equals​(org.jboss.jandex.ClassInfo c1,
                                     org.jboss.jandex.ClassInfo c2)
      • equals

        public static boolean equals​(org.jboss.jandex.FieldInfo f1,
                                     org.jboss.jandex.FieldInfo f2)
      • equals

        public static boolean equals​(org.jboss.jandex.MethodParameterInfo p1,
                                     org.jboss.jandex.MethodParameterInfo p2)