Package io.swagger.v3.core.util
Class ReflectionUtils
- java.lang.Object
-
- io.swagger.v3.core.util.ReflectionUtils
-
public class ReflectionUtils extends Object
-
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static FieldfindField(String name, Class<?> cls)Searches the field name in given class cls.static MethodfindMethod(Method methodToFind, Class<?> cls)Searches the method methodToFind in given class cls.static <A extends Annotation>
AgetAnnotation(Class<?> cls, Class<A> annotationClass)static <A extends Annotation>
AgetAnnotation(Method method, Class<A> annotationClass)Returns an annotation by type from a method.static List<Field>getDeclaredFields(Class<?> cls)Returns the list of declared fields from the classclsand its superclasses excludingObjectclass.static MethodgetOverriddenMethod(Method method)Returns overridden method from superclass if it exists.static Annotation[][]getParameterAnnotations(Method method)static <A extends Annotation>
List<A>getRepeatableAnnotations(Class<?> cls, Class<A> annotationClass)static <A extends Annotation>
List<A>getRepeatableAnnotations(Method method, Class<A> annotationClass)Returns a List of repeatable annotations by type from a method.static <A extends Annotation>
A[]getRepeatableAnnotationsArray(Class<?> cls, Class<A> annotationClass)static booleanhasOverriddenMethods(Method methodToFind, Class<?> cls)static booleanisConstructorCompatible(Constructor<?> constructor)static booleanisInject(List<Annotation> annotations)static booleanisOverriddenMethod(Method methodToFind, Class<?> cls)Checks if the method methodToFind is the overridden method from the superclass or superinterface.static booleanisSystemType(com.fasterxml.jackson.databind.JavaType type)static booleanisSystemTypeNotArray(com.fasterxml.jackson.databind.JavaType type)static booleanisVoid(Type type)Checks if the type is void.static Class<?>loadClassByName(String className)Load Class by class name.static Optional<Object>safeGet(Field field, Object obj)A utility method to get an optional containing value of field or empty optional if unable to accessstatic Optional<Object>safeInvoke(Method method, Object obj, Object... args)A utility method to get an optional containing result from method or empty optional if unable to accessstatic TypetypeFromString(String type)
-
-
-
Method Detail
-
loadClassByName
public static Class<?> loadClassByName(String className) throws ClassNotFoundException
Load Class by class name. If class not found in it's Class loader or one of the parent class loaders - delegate to the Thread's ContextClassLoader- Parameters:
className- Canonical class name- Returns:
- Class definition of className
- Throws:
ClassNotFoundException
-
isOverriddenMethod
public static boolean isOverriddenMethod(Method methodToFind, Class<?> cls)
Checks if the method methodToFind is the overridden method from the superclass or superinterface.- Parameters:
methodToFind- is method to checkcls- is method class- Returns:
- true if the method is overridden method
-
getOverriddenMethod
public static Method getOverriddenMethod(Method method)
Returns overridden method from superclass if it exists. If method was not found returns null.- Parameters:
method- is method to find- Returns:
- overridden method from superclass
-
findField
public static Field findField(String name, Class<?> cls)
Searches the field name in given class cls. If the field is found returns it, else return null.- Parameters:
name- is the field to searchcls- is the class or interface where to search- Returns:
- field if it is found
-
findMethod
public static Method findMethod(Method methodToFind, Class<?> cls)
Searches the method methodToFind in given class cls. If the method is found returns it, else return null.- Parameters:
methodToFind- is the method to searchcls- is the class or interface where to search- Returns:
- method if it is found
-
isInject
public static boolean isInject(List<Annotation> annotations)
-
isConstructorCompatible
public static boolean isConstructorCompatible(Constructor<?> constructor)
-
getDeclaredFields
public static List<Field> getDeclaredFields(Class<?> cls)
Returns the list of declared fields from the classclsand its superclasses excludingObjectclass. If the field from child class hides the field from superclass, the field from superclass won't be added to the result list. The list is sorted by name to make the output of this method deterministic. See https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getFields--- Parameters:
cls- is the processing class- Returns:
- list of Fields
-
getAnnotation
public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationClass)
Returns an annotation by type from a method.- Type Parameters:
A- is the type of annotation- Parameters:
method- is the method to findannotationClass- is the type of annotation- Returns:
- annotation if it is found
-
getAnnotation
public static <A extends Annotation> A getAnnotation(Class<?> cls, Class<A> annotationClass)
-
getRepeatableAnnotations
public static <A extends Annotation> List<A> getRepeatableAnnotations(Method method, Class<A> annotationClass)
Returns a List of repeatable annotations by type from a method.- Type Parameters:
A- is the type of annotation- Parameters:
method- is the method to findannotationClass- is the type of annotation- Returns:
- List of repeatable annotations if it is found
-
getRepeatableAnnotations
public static <A extends Annotation> List<A> getRepeatableAnnotations(Class<?> cls, Class<A> annotationClass)
-
getRepeatableAnnotationsArray
public static <A extends Annotation> A[] getRepeatableAnnotationsArray(Class<?> cls, Class<A> annotationClass)
-
getParameterAnnotations
public static Annotation[][] getParameterAnnotations(Method method)
-
isVoid
public static boolean isVoid(Type type)
Checks if the type is void.- Parameters:
type- is the type to check- Returns:
- true if the type is void
-
isSystemType
public static boolean isSystemType(com.fasterxml.jackson.databind.JavaType type)
-
isSystemTypeNotArray
public static boolean isSystemTypeNotArray(com.fasterxml.jackson.databind.JavaType type)
-
safeInvoke
public static Optional<Object> safeInvoke(Method method, Object obj, Object... args)
A utility method to get an optional containing result from method or empty optional if unable to access- Parameters:
method- from reflect, a method of a class or interfaceobj- the class object in which the method existsargs- varags of the parameters passed to the method- Returns:
- the result of the method, or empty conditional
-
safeGet
public static Optional<Object> safeGet(Field field, Object obj)
A utility method to get an optional containing value of field or empty optional if unable to access- Parameters:
field- from reflect, a field of a class or interfaceobj- the class object in which the field exists- Returns:
- optional containing the value of the field on the specified object, or empty optional
-
-