Package io.smallrye.openapi.runtime.util
Class JandexUtil
- java.lang.Object
-
- io.smallrye.openapi.runtime.util.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<?>>
TenumValue(String strVal, Class<T> clazz)
Converts a string value to the given enum type.static <T extends Enum<?>>
TenumValue(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 existstatic boolean
hasAnyOneOfAnnotation(org.jboss.jandex.MethodInfo method, org.jboss.jandex.DotName... annotations)
Return if any one of the listed annotations existstatic 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
- AnnotationInstancerefType
- 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 containingAnnotationValue
.- Type Parameters:
T
- the type of the parameter being retrieved- Parameters:
annotation
- the annotation from which to fetch the parametername
- 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 containingAnnotationValue
.- Type Parameters:
T
- the type of the parameter being retrieved- Parameters:
annotation
- the annotation from which to fetch the parametername
- the name of the parameterdefaultValue
- 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
- AnnotationInstancepropertyName
- 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
- AnnotationInstancepropertyName
- 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
- AnnotationInstancepropertyName
- 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
- AnnotationInstancepropertyName
- 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
- AnnotationInstancepropertyName
- 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
- AnnotationInstancepropertyName
- Stringclazz
- 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
- Stringclazz
- 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
- ClassInfoname
- 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
- ClassInfonames
- 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
- FieldInfonames
- 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
- MethodInfonames
- 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
- MethodInfoparamPosition
- 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
- DotNamerepeatableAnnotationName
- 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
- MethodInfoposition
- 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
- theparameter
- 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 methodparameterIndex
- the parameter indexannotationName
- 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 retrievalklazz
- the class to retrieve inheritancetype
- 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)
-
-