Class ClassUtils


  • public class ClassUtils
    extends Object
    • Constructor Detail

      • ClassUtils

        public ClassUtils()
    • Method Detail

      • getAnnotatedMethods

        public static Set<Method> getAnnotatedMethods​(Class<?> type,
                                                      Class<? extends Annotation> annotation)
        Retrieves all public methods on the given class (same as Class.getMethods()) annotated by the given annotation
        Parameters:
        type - The class to scan
        annotation - 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 as Class.getFields()) annotated by the given annotation
        Parameters:
        type - The class to scan
        annotation - 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 resolved
        declaringType - 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 resolved
        declaringType - The declaring annotated type against which to resolve the field type
        Returns:
        The resolved annotated field type
      • getEnumConstantField

        public static Field getEnumConstantField​(Enum<?> constant)
      • 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 resolved
        declaringType - 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
      • getRawType

        public static <T> Class<T> getRawType​(Type type)
      • 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)
      • getFieldNameFromGetter

        public static String getFieldNameFromGetter​(Method getter)
      • getFieldNameFromSetter

        public static String getFieldNameFromSetter​(Method setter)
      • getFieldValue

        public static <T> T getFieldValue​(Object source,
                                          String fieldName)
      • findImplementations

        @Deprecated
        public static List<AnnotatedType> findImplementations​(AnnotatedType superType,
                                                              String... packages)
        Deprecated.
        Use ClassFinder directly as that enables caching of the search results
        Searches for the implementations/subtypes of the given AnnotatedType. Only the matching classes are loaded.
        Parameters:
        superType - The type the implementations/subtypes of which are to be searched for
        packages - The packages to limit the search to
        Returns:
        A collection of AnnotatedTypes discovered that implementation/extend superType
      • isAbstract

        public static boolean isAbstract​(AnnotatedType type)
      • isAbstract

        public static boolean isAbstract​(Class<?> type)
      • isAssignable

        public static boolean isAssignable​(Type superType,
                                           Type subType)
      • isSuperClass

        public static boolean isSuperClass​(Class<?> superClass,
                                           AnnotatedType subType)
      • isSuperClass

        public static boolean isSuperClass​(AnnotatedType superType,
                                           Class<?> subClass)
      • isSubPackage

        public static boolean isSubPackage​(Package pkg,
                                           String prefix)
      • hasAnnotation

        public static boolean hasAnnotation​(AnnotatedElement element,
                                            Class<? extends Annotation> annotation)
        Checks if an annotation is present either directly on the element, or as a 1st level meta-annotation
        Parameters:
        element - The element to search the annotation on
        annotation - The type of the annotation to search for
        Returns:
        true if the annotation of type annotation is found, false otherwise
      • hasMetaAnnotation

        public static boolean hasMetaAnnotation​(AnnotatedElement element,
                                                Class<? extends Annotation> annotation)
        Checks if an annotation is present either directly on the element, or recursively as a meta-annotation, at any level
        Parameters:
        element - The element to search the annotation on
        annotation - The type of the annotation to search for
        Returns:
        true if the annotation of type annotation is found, false otherwise
      • addAnnotations

        public static <T extends AnnotatedType> T addAnnotations​(T type,
                                                                 Annotation... annotations)
      • eraseBounds

        public static AnnotatedType eraseBounds​(AnnotatedType type,
                                                AnnotatedType replacement)
        Recursively replaces all bounded types found within the structure of the given AnnotatedType with 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
      • 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) a TypeMappingException is 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 fallback or throws a TypeMappingException if fallback is null.

        Parameters:
        types - Types whose most specific super types is to be found
        fallback - 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)
      • getSuperTypes

        public static Set<Class<?>> getSuperTypes​(Class<?> clazz)
      • 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:
        true if the given class is a known proxy, false otherwise
      • getDefaultValueForType

        public static Object getDefaultValueForType​(Class<?> type)
      • isPrimitive

        public static boolean isPrimitive​(AnnotatedType type)