Package io.leangen.graphql.util
Class ClassUtils
- java.lang.Object
-
- io.leangen.graphql.util.ClassUtils
-
public class ClassUtils extends Object
-
-
Constructor Summary
Constructors Constructor Description ClassUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T extends AnnotatedType>
TaddAnnotations(T type, Annotation... annotations)static AnnotatedTypecompleteGenerics(AnnotatedType type, AnnotatedType replacement)static booleancontainsTypeAnnotation(AnnotatedType type, Class<? extends Annotation> annotation)static AnnotatedTypeeraseBounds(AnnotatedType type, AnnotatedType replacement)Recursively replaces all bounded types found within the structure of the givenAnnotatedTypewith their first bound.static <T extends Annotation>
Optional<T>findApplicableAnnotation(AnnotatedElement element, Class<T> annotation)static Optional<Field>findField(Class<?> type, String fieldName)static Optional<Field>findField(Class<?> type, Predicate<Field> condition)static Optional<Field>findFieldByGetter(Method getter)static Optional<Field>findFieldBySetter(Method setter)static Optional<Method>findGetter(Class<?> type, String fieldName)static List<AnnotatedType>findImplementations(AnnotatedType superType, String... packages)Deprecated.UseClassFinderdirectly as that enables caching of the search resultsstatic Optional<Method>findMethod(Class<?> type, String methodName, Class<?>... parameterTypes)static Optional<Method>findSetter(Class<?> type, String fieldName, Class<?> fieldType)static Class<?>forName(String className)static Annotation[]getAllAnnotations(Stream<AnnotatedType> types)static Set<Field>getAnnotatedFields(Class<?> type, Class<? extends Annotation> annotation)Retrieves all public fields on the given class (same asClass.getFields()) annotated by the given annotationstatic Set<Method>getAnnotatedMethods(Class<?> type, Class<? extends Annotation> annotation)Retrieves all public methods on the given class (same asClass.getMethods()) annotated by the given annotationstatic List<Method>getAnnotationFields(Class<? extends Annotation> annotation)static AnnotatedTypegetCommonSuperType(List<AnnotatedType> types)Finds the most specific common super type of all the given types, merging the original annotations at each level.static AnnotatedTypegetCommonSuperType(List<AnnotatedType> types, AnnotatedType fallback)Finds the most specific common super type of all the given types, merging the original annotations at each level.static List<Class<?>>getCommonSuperTypes(List<Class<?>> classes)static ObjectgetDefaultValueForType(Class<?> type)static FieldgetEnumConstantField(Enum<?> constant)static StringgetFieldNameFromGetter(Method getter)static StringgetFieldNameFromSetter(Method setter)static AnnotatedTypegetFieldType(Field field, AnnotatedType declaringType)Returns the exact annotated type of the field declared by the given type, with type variables resolved (if possible)static <T> TgetFieldValue(Object source, String fieldName)static AnnotatedType[]getParameterTypes(Executable executable, AnnotatedType declaringType)Returns the exact annotated parameter types of the executable declared by the given type, with type variables resolved (if possible)static Set<Property>getProperties(Class<?> type)static <T extends Member & AnnotatedElement>
List<AnnotatedElement>getPropertyMembers(T member)static <T> Class<T>getRawType(Type type)static AnnotatedTypegetReturnType(Method method, AnnotatedType declaringType)Returns the exact annotated return type of the method declared by the given type, with type variables resolved (if possible)static Set<Class<?>>getSuperTypes(Class<?> clazz)static booleanhasAnnotation(AnnotatedElement element, Class<? extends Annotation> annotation)Checks if an annotation is present either directly on theelement, or as a 1st level meta-annotationstatic booleanhasMetaAnnotation(AnnotatedElement element, Class<? extends Annotation> annotation)Checks if an annotation is present either directly on theelement, or recursively as a meta-annotation, at any levelstatic <T> Tinstance(Class<T> clazz)static <T> Tinstance(AnnotatedType type)static <T> TinstanceWithOptionalInjection(Class<T> clazz, Class<?>[] parameterTypes, Object[] arguments)static <T> TinstanceWithOptionalInjection(Class<T> clazz, Class<?> parameterType, Object argument)static <T> TinstanceWithOptionalInjection(Class<T> clazz, Object... arguments)static booleanisAbstract(Class<?> type)static booleanisAbstract(AnnotatedType type)static booleanisAssignable(Type superType, Type subType)static booleanisGetter(Method getter)Checks whether the given method is a JavaBean property getterstatic booleanisMissingTypeParameters(Type type)static booleanisPrimitive(AnnotatedType type)static booleanisProxy(Class<?> clazz)Attempts to discover if the given class is a dynamically generated proxy class.static booleanisReal(AnnotatedElement element)static booleanisReal(Field field)static booleanisReal(Member member)static booleanisReal(Method method)static booleanisReal(Parameter parameter)static booleanisSetter(Method setter)Checks whether the given method is a JavaBean property setterstatic booleanisSubPackage(Package pkg, String prefix)static booleanisSuperClass(Class<?> superClass, AnnotatedType subType)static booleanisSuperClass(AnnotatedType superType, Class<?> subClass)static <T extends AnnotatedType>
Tnormalize(T type)static <T extends AnnotatedType>
TremoveAnnotations(T type, Set<Class<? extends Annotation>> toRemove)static StringtoString(AnnotatedElement element)static StringtoString(AnnotatedType type)static <T extends AnnotatedType>
TtransformType(T type, UnaryOperator<T> transformer)
-
-
-
Method Detail
-
getAnnotatedMethods
public static Set<Method> getAnnotatedMethods(Class<?> type, Class<? extends Annotation> annotation)
Retrieves all public methods on the given class (same asClass.getMethods()) annotated by the given annotation- Parameters:
type- The class to scanannotation- The annotation to look for- Returns:
- All annotated methods
-
getAnnotatedFields
public static Set<Field> getAnnotatedFields(Class<?> type, Class<? extends Annotation> annotation)
Retrieves all public fields on the given class (same asClass.getFields()) annotated by the given annotation- Parameters:
type- The class to scanannotation- The annotation to look for- Returns:
- All annotated fields
-
getReturnType
public static AnnotatedType getReturnType(Method method, AnnotatedType declaringType)
Returns the exact annotated return type of the method declared by the given type, with type variables resolved (if possible)- Parameters:
method- The method whose return type is to be resolveddeclaringType- The declaring annotated type against which to resolve the return type- Returns:
- The resolved annotated return type
-
getFieldType
public static AnnotatedType getFieldType(Field field, AnnotatedType declaringType)
Returns the exact annotated type of the field declared by the given type, with type variables resolved (if possible)- Parameters:
field- The field whose type is to be resolveddeclaringType- The declaring annotated type against which to resolve the field type- Returns:
- The resolved annotated field type
-
getParameterTypes
public static AnnotatedType[] getParameterTypes(Executable executable, AnnotatedType declaringType)
Returns the exact annotated parameter types of the executable declared by the given type, with type variables resolved (if possible)- Parameters:
executable- The executable whose parameter types are to be resolveddeclaringType- The declaring annotated type against which to resolve the types of the parameters of the given executable- Returns:
- The resolved annotated types of the parameters of the given executable
-
isMissingTypeParameters
public static boolean isMissingTypeParameters(Type type)
-
normalize
public static <T extends AnnotatedType> T normalize(T type)
-
instance
public static <T> T instance(AnnotatedType type)
-
instance
public static <T> T instance(Class<T> clazz)
-
instanceWithOptionalInjection
public static <T> T instanceWithOptionalInjection(Class<T> clazz, Object... arguments)
-
instanceWithOptionalInjection
public static <T> T instanceWithOptionalInjection(Class<T> clazz, Class<?> parameterType, Object argument)
-
instanceWithOptionalInjection
public static <T> T instanceWithOptionalInjection(Class<T> clazz, Class<?>[] parameterTypes, Object[] arguments)
-
isGetter
public static boolean isGetter(Method getter)
Checks whether the given method is a JavaBean property getter- Parameters:
getter- The method to be checked- Returns:
- Boolean indicating whether the method is a getter
- See Also:
isSetter(Method)
-
isSetter
public static boolean isSetter(Method setter)
Checks whether the given method is a JavaBean property setter- Parameters:
setter- The method to be checked- Returns:
- Boolean indicating whether the method is a setter
- See Also:
isGetter(Method)
-
isReal
public static boolean isReal(Method method)
-
isReal
public static boolean isReal(Field field)
-
isReal
public static boolean isReal(Parameter parameter)
-
isReal
public static boolean isReal(Member member)
-
isReal
public static boolean isReal(AnnotatedElement element)
-
getPropertyMembers
public static <T extends Member & AnnotatedElement> List<AnnotatedElement> getPropertyMembers(T member)
-
findSetter
public static Optional<Method> findSetter(Class<?> type, String fieldName, Class<?> fieldType)
-
findMethod
public static Optional<Method> findMethod(Class<?> type, String methodName, Class<?>... parameterTypes)
-
findImplementations
@Deprecated public static List<AnnotatedType> findImplementations(AnnotatedType superType, String... packages)
Deprecated.UseClassFinderdirectly as that enables caching of the search resultsSearches for the implementations/subtypes of the givenAnnotatedType. Only the matching classes are loaded.- Parameters:
superType- The type the implementations/subtypes of which are to be searched forpackages- The packages to limit the search to- Returns:
- A collection of
AnnotatedTypes discovered that implementation/extendsuperType
-
isAbstract
public static boolean isAbstract(AnnotatedType type)
-
isAbstract
public static boolean isAbstract(Class<?> type)
-
isSuperClass
public static boolean isSuperClass(Class<?> superClass, AnnotatedType subType)
-
isSuperClass
public static boolean isSuperClass(AnnotatedType superType, Class<?> subClass)
-
toString
public static String toString(AnnotatedType type)
-
toString
public static String toString(AnnotatedElement element)
-
hasAnnotation
public static boolean hasAnnotation(AnnotatedElement element, Class<? extends Annotation> annotation)
Checks if an annotation is present either directly on theelement, or as a 1st level meta-annotation- Parameters:
element- The element to search the annotation onannotation- The type of the annotation to search for- Returns:
trueif the annotation of typeannotationis found,falseotherwise
-
hasMetaAnnotation
public static boolean hasMetaAnnotation(AnnotatedElement element, Class<? extends Annotation> annotation)
Checks if an annotation is present either directly on theelement, or recursively as a meta-annotation, at any level- Parameters:
element- The element to search the annotation onannotation- The type of the annotation to search for- Returns:
trueif the annotation of typeannotationis found,falseotherwise
-
findApplicableAnnotation
public static <T extends Annotation> Optional<T> findApplicableAnnotation(AnnotatedElement element, Class<T> annotation)
-
getAnnotationFields
public static List<Method> getAnnotationFields(Class<? extends Annotation> annotation)
-
containsTypeAnnotation
public static boolean containsTypeAnnotation(AnnotatedType type, Class<? extends Annotation> annotation)
-
getAllAnnotations
public static Annotation[] getAllAnnotations(Stream<AnnotatedType> types)
-
addAnnotations
public static <T extends AnnotatedType> T addAnnotations(T type, Annotation... annotations)
-
removeAnnotations
public static <T extends AnnotatedType> T removeAnnotations(T type, Set<Class<? extends Annotation>> toRemove)
-
eraseBounds
public static AnnotatedType eraseBounds(AnnotatedType type, AnnotatedType replacement)
Recursively replaces all bounded types found within the structure of the givenAnnotatedTypewith their first bound. I.e.- All
AnnotatedWildcardTypes are replaced with their first lower bound if it exists, or their first upper bound otherwise. All annotations are preserved. - All
AnnotatedTypeVariables are replaced with their first bound. All annotations are preserved. - Other types are kept as they are.
- Parameters:
type- A potentially bounded type- Returns:
- The type of the same structure as the given type but with bounds erased, or the unchanged type itself if it contained no bounds
- All
-
completeGenerics
public static AnnotatedType completeGenerics(AnnotatedType type, AnnotatedType replacement)
-
transformType
public static <T extends AnnotatedType> T transformType(T type, UnaryOperator<T> transformer)
-
getCommonSuperType
public static AnnotatedType getCommonSuperType(List<AnnotatedType> types)
Finds the most specific common super type of all the given types, merging the original annotations at each level. If no common ancestors are found (except Object) aTypeMappingExceptionis thrown.- Parameters:
types- Types whose most specific super types is to be found- Returns:
- The most specific super type
-
getCommonSuperType
public static AnnotatedType getCommonSuperType(List<AnnotatedType> types, AnnotatedType fallback)
Finds the most specific common super type of all the given types, merging the original annotations at each level.If no common ancestors are found (except Object) returns
fallbackor throws aTypeMappingExceptioniffallbackisnull.- Parameters:
types- Types whose most specific super types is to be foundfallback- The type to return as the result when no common ancestors except Object are found (at any level)- Returns:
- The most specific super type
-
getCommonSuperTypes
public static List<Class<?>> getCommonSuperTypes(List<Class<?>> classes)
- Parameters:
classes- Types whose most specific super types is to be found- Returns:
- The most specific super type
- See Also:
getCommonSuperType(List)
-
isProxy
public static boolean isProxy(Class<?> clazz)
Attempts to discover if the given class is a dynamically generated proxy class. Standard Java proxies, cglib and Javassist proxies are detected.- Parameters:
clazz- The class to test- Returns:
trueif the given class is a known proxy,falseotherwise
-
forName
public static Class<?> forName(String className) throws ClassNotFoundException
- Throws:
ClassNotFoundException
-
isPrimitive
public static boolean isPrimitive(AnnotatedType type)
-
-