Class ClassUtil

java.lang.Object
com.landawn.abacus.util.ClassUtil

public final class ClassUtil extends Object
Since:
0.9
Author:
Haiyang Li
  • Field Details

    • CLASS_MASK

      @Deprecated @Internal public static final Class<?> CLASS_MASK
      Deprecated.
      for internal only.
      The Constant CLASS_MASK.
    • METHOD_MASK

      @Deprecated @Internal public static final Method METHOD_MASK
      Deprecated.
      for internal only.
      The Constant METHOD_MASK.
    • FIELD_MASK

      @Deprecated @Internal public static final Field FIELD_MASK
      Deprecated.
      for internal only.
      The Constant FIELD_MASK.
  • Method Details

    • registerNonBeanClass

      public static void registerNonBeanClass(Class<?> cls)
      Register non bean class.
      Parameters:
      cls -
    • registerNonPropGetSetMethod

      public static void registerNonPropGetSetMethod(Class<?> cls, String propName)
      Register non prop get set method.
      Parameters:
      cls -
      propName -
    • registerPropGetSetMethod

      public static void registerPropGetSetMethod(String propName, Method method)
      Register prop get set method.
      Parameters:
      propName -
      method -
    • registerXMLBindingClass

      public static void registerXMLBindingClass(Class<?> cls)
      The property maybe only has get method if its type is collection or map by xml binding specification Otherwise, it will be ignored if not registered by calling this method.
      Parameters:
      cls -
    • isRegisteredXMLBindingClass

      public static boolean isRegisteredXMLBindingClass(Class<?> cls)
      Parameters:
      cls -
      Returns:
    • createMethodHandle

      public static MethodHandle createMethodHandle(Method method)
      Parameters:
      method -
      Returns:
    • distanceOfInheritance

      public static int distanceOfInheritance(Class<?> child, Class<?> parent)

      Returns the number of inheritance hops between two classes.

      Parameters:
      child - the child class, may be null
      parent - the parent class, may be null
      Returns:
      the number of generations between the child and parent; 0 if the same class; -1 if the classes are not related as child and parent (includes where either class is null)
      Since:
      3.2
    • forClass

      public static <T> Class<T> forClass(String clsName) throws IllegalArgumentException
      Supports primitive types: boolean, char, byte, short, int, long, float, double. And array type with format java.lang.String[]
      Type Parameters:
      T -
      Parameters:
      clsName -
      Returns:
      Throws:
      IllegalArgumentException - if class not found.
    • formalizePropName

      public static String formalizePropName(String propName)
      It's designed for field/method/class/column/table names. and source and target Strings will be cached.
      Parameters:
      propName -
      Returns:
    • formatParameterizedTypeName

      public static String formatParameterizedTypeName(String parameterizedTypeName)
      Format parameterized type name.
      Parameters:
      parameterizedTypeName -
      Returns:
    • getCanonicalClassName

      public static String getCanonicalClassName(Class<?> cls)
      Gets the canonical class name.
      Parameters:
      cls -
      Returns:
    • getClassName

      public static String getClassName(Class<?> cls)
      Gets the class name.
      Parameters:
      cls -
      Returns:
    • getSimpleClassName

      public static String getSimpleClassName(Class<?> cls)
      Gets the simple class name.
      Parameters:
      cls -
      Returns:
    • getPackage

      public static Package getPackage(Class<?> cls)
      Gets the package.
      Parameters:
      cls -
      Returns:
      null if it's primitive type or no package defined for the class.
    • getPackageName

      public static String getPackageName(Class<?> cls)
      Gets the package name.
      Parameters:
      cls -
      Returns:
      null if it's primitive type or no package defined for the class.
    • getClassesByPackage

      public static List<Class<?>> getClassesByPackage(String pkgName, boolean isRecursive, boolean skipClassLoaddingException) throws UncheckedIOException
      Gets the classes by package.
      Parameters:
      pkgName -
      isRecursive -
      skipClassLoaddingException -
      Returns:
      Throws:
      UncheckedIOException - the unchecked IO exception
    • getClassesByPackage

      public static <E extends Exception> List<Class<?>> getClassesByPackage(String pkgName, boolean isRecursive, boolean skipClassLoaddingException, Throwables.Predicate<? super Class<?>,E> predicate) throws UncheckedIOException, E
      Gets the classes by package.
      Type Parameters:
      E -
      Parameters:
      pkgName -
      isRecursive -
      skipClassLoaddingException -
      predicate -
      Returns:
      Throws:
      UncheckedIOException - the unchecked IO exception
      E - the e
    • getAllInterfaces

      public static Set<Class<?>> getAllInterfaces(Class<?> cls)
      Copied from Apache Commons Lang under Apache License v2.

      Gets a List of all interfaces implemented by the given class and its super classes.

      The order is determined by looking through each interface in turn as declared in the source file and following its hierarchy up. Then each superclass is considered in the same way. Later duplicates are ignored, so the order is maintained.

      Parameters:
      cls - the class to look up.
      Returns:
    • getAllSuperclasses

      public static List<Class<?>> getAllSuperclasses(Class<?> cls)
      Copied from Apache Commons Lang under Apache License v2.

      Gets a List of super classes for the given class, excluding Object.class.

      Parameters:
      cls - the class to look up.
      Returns:
    • getAllSuperTypes

      public static Set<Class<?>> getAllSuperTypes(Class<?> cls)
      Returns all the interfaces and super classes the specified class implements or extends, excluding Object.class.
      Parameters:
      cls -
      Returns:
    • getEnclosingClass

      public static Class<?> getEnclosingClass(Class<?> cls)
      Gets the enclosing class.
      Parameters:
      cls -
      Returns:
    • getDeclaredConstructor

      @SafeVarargs public static <T> Constructor<T> getDeclaredConstructor(Class<T> cls, Class<?>... parameterTypes)
      Returns the constructor declared in the specified cls with the specified parameterTypes.
      Type Parameters:
      T -
      Parameters:
      cls -
      parameterTypes -
      Returns:
      null if no constructor is found
    • getDeclaredMethod

      @SafeVarargs public static Method getDeclaredMethod(Class<?> cls, String methodName, Class<?>... parameterTypes)
      Returns the method declared in the specified cls with the specified methodName and parameterTypes.
      Parameters:
      cls -
      methodName -
      parameterTypes -
      Returns:
      null if no method is found
    • getParameterizedTypeNameByField

      public static String getParameterizedTypeNameByField(Field field)
      Gets the parameterized type name by method.
      Parameters:
      field -
      Returns:
    • getParameterizedTypeNameByMethod

      public static String getParameterizedTypeNameByMethod(Method method)
      Gets the parameterized type name by method.
      Parameters:
      method -
      Returns:
    • getPropNameByMethod

      public static String getPropNameByMethod(Method getSetMethod)
      Gets the prop name by method.
      Parameters:
      getSetMethod -
      Returns:
    • getPropNameList

      public static ImmutableList<String> getPropNameList(Class<?> cls)
      Returns an immutable bean property name List by the specified class.
      Parameters:
      cls -
      Returns:
    • getPropNames

      public static <E extends Exception> List<String> getPropNames(Object bean, Throwables.BiPredicate<Object,com.landawn.abacus.type.Type<Object>,E> propValueFilter) throws E
      Type Parameters:
      E -
      Parameters:
      bean -
      propValueFilter - first parameter is property value, second parameter is property type.
      Returns:
      Throws:
      E
    • getPropNames

      public static <E extends Exception> List<String> getPropNames(Object bean, Throwables.Predicate<Object,E> propValueFilter) throws E
      Type Parameters:
      E -
      Parameters:
      bean -
      propValueFilter -
      Returns:
      Throws:
      E
    • getPropNamesExclusively

      public static List<String> getPropNamesExclusively(Class<?> cls, Collection<String> propNameToExcluded)
      Gets the prop name list exclusively.
      Parameters:
      cls -
      propNameToExcluded -
      Returns:
    • getPropNamesExclusively

      public static List<String> getPropNamesExclusively(Class<?> cls, Set<String> propNameToExcluded)
      Gets the prop name list exclusively.
      Parameters:
      cls -
      propNameToExcluded -
      Returns:
    • getNonNullPropNames

      public static List<String> getNonNullPropNames(Object bean)
      Parameters:
      bean -
      Returns:
    • getPropField

      public static Field getPropField(Class<?> cls, String propName)
      Gets the prop field.
      Parameters:
      cls -
      propName -
      Returns:
    • getPropFields

      public static ImmutableMap<String,Field> getPropFields(Class<?> cls)
      Parameters:
      cls -
      Returns:
    • getPropGetMethod

      public static Method getPropGetMethod(Class<?> cls, String propName)
      Returns the property get method declared in the specified cls with the specified property name propName. null is returned if no method is found. Call registerXMLBindingClassForPropGetSetMethod first to retrieve the property getter/setter method for the class/bean generated/wrote by JAXB specification
      Parameters:
      cls -
      propName -
      Returns:
    • getPropGetMethods

      public static ImmutableMap<String,Method> getPropGetMethods(Class<?> cls)
      Call registerXMLBindingClassForPropGetSetMethod first to retrieve the property getter/setter method for the class/bean generated/wrote by JAXB specification.
      Parameters:
      cls -
      Returns:
    • getPropSetMethod

      public static Method getPropSetMethod(Class<?> cls, String propName)
      Returns the property set method declared in the specified cls with the specified property name propName. null is returned if no method is found.
      Parameters:
      cls -
      propName -
      Returns:
    • getPropSetMethods

      public static ImmutableMap<String,Method> getPropSetMethods(Class<?> cls)
      Gets the prop set method list.
      Parameters:
      cls -
      Returns:
    • getPropValue

      public static <T> T getPropValue(Object bean, Method propGetMethod)
      Return the specified propValue got by the specified method propSetMethod in the specified bean.
      Type Parameters:
      T -
      Parameters:
      bean - MapEntity is not supported
      propGetMethod -
      Returns:
    • getPropValue

      public static <T> T getPropValue(Object bean, String propName)
      Refer to getPropValue(Method, Object).
      Type Parameters:
      T -
      Parameters:
      bean -
      propName - is case insensitive
      Returns:
      getPropValue(Object, Method)
    • getPropValue

      public static <T> T getPropValue(Object bean, String propName, boolean ignoreUnmatchedProperty)
      Gets the prop value.
      Type Parameters:
      T -
      Parameters:
      bean -
      propName -
      ignoreUnmatchedProperty -
      Returns:
      Throws:
      IllegalArgumentException - if the specified property can't be gotten and ignoreUnmatchedProperty is false.
    • setPropValue

      public static void setPropValue(Object bean, Method propSetMethod, Object propValue)
      Set the specified propValue to bean by the specified method propSetMethod. This method will try to convert propValue to appropriate type and set again if fails to set in the first time. The final value which is set to the property will be returned if property is set successfully finally. it could be the input propValue or converted property value, otherwise, exception will be threw if the property value is set unsuccessfully.
      Parameters:
      bean - MapEntity is not supported
      propSetMethod -
      propValue -
    • setPropValue

      @Deprecated public static void setPropValue(Object bean, String propName, Object propValue)
      Refer to setPropValue(Method, Object, Object).
      Parameters:
      bean -
      propName - is case insensitive
      propValue -
    • setPropValue

      @Deprecated public static boolean setPropValue(Object bean, String propName, Object propValue, boolean ignoreUnmatchedProperty)
      Sets the prop value.
      Parameters:
      bean -
      propName -
      propValue -
      ignoreUnmatchedProperty -
      Returns:
      true if the property value has been set.
      Throws:
      IllegalArgumentException - if the specified property can't be set and ignoreUnmatchedProperty is false.
    • setPropValueByGet

      public static void setPropValueByGet(Object bean, Method propGetMethod, Object propValue)
      Sets the prop value by get.
      Parameters:
      bean -
      propGetMethod -
      propValue -
    • getBuilderInfo

      public static Tuple.Tuple3<Class<?>,Supplier<Object>,Function<Object,Object>> getBuilderInfo(Class<?> cls)
      Parameters:
      cls -
      Returns:
    • getTypeName

      public static String getTypeName(Type type)
      Parameters:
      type -
      Returns:
    • hierarchy

      public static ObjIterator<Class<?>> hierarchy(Class<?> type)
      Gets an Iterator that can iterate over a class hierarchy in ascending (subclass to superclass) order, excluding interfaces.
      Parameters:
      type - the type to get the class hierarchy from
      Returns:
      Iterator an Iterator over the class hierarchy of the given class
      Since:
      3.2
    • hierarchy

      public static ObjIterator<Class<?>> hierarchy(Class<?> type, boolean includeInterface)
      Gets an Iterator that can iterate over a class hierarchy in ascending (subclass to superclass) order.
      Parameters:
      type - the type to get the class hierarchy from
      includeInterface - switch indicating whether to include or exclude interfaces
      Returns:
      Iterator an Iterator over the class hierarchy of the given class
      Since:
      3.2
    • invokeConstructor

      @SafeVarargs public static <T> T invokeConstructor(Constructor<T> constructor, Object... args)
      Type Parameters:
      T -
      Parameters:
      constructor -
      args -
      Returns:
    • invokeMethod

      @SafeVarargs public static <T> T invokeMethod(Method method, Object... args)
      Type Parameters:
      T -
      Parameters:
      method -
      args -
      Returns:
    • invokeMethod

      @SafeVarargs public static <T> T invokeMethod(Object instance, Method method, Object... args)
      Type Parameters:
      T -
      Parameters:
      instance -
      method -
      args -
      Returns:
    • setAccessible

      public static void setAccessible(AccessibleObject accessibleObject, boolean flag)
      Parameters:
      accessibleObject -
      flag -
    • setAccessibleQuietly

      public static boolean setAccessibleQuietly(AccessibleObject accessibleObject, boolean flag)
      Parameters:
      accessibleObject -
      flag -
      Returns:
      true if no error happens, otherwise false is returned.
    • toCamelCase

      public static void toCamelCase(Map<String,Object> props)
      To camel case.
      Parameters:
      props -
    • toCamelCase

      public static String toCamelCase(String propName)
      It's designed for field/method/class/column/table names. and source and target Strings will be cached.
      Parameters:
      propName -
      Returns:
    • toLowerCaseKeyWithUnderscore

      public static void toLowerCaseKeyWithUnderscore(Map<String,Object> props)
      To lower case key with underscore.
      Parameters:
      props -
    • toLowerCaseWithUnderscore

      public static String toLowerCaseWithUnderscore(String str)
      It's designed for field/method/class/column/table names. and source and target Strings will be cached.
      Parameters:
      str -
      Returns:
    • toUpperCaseWithUnderscore

      public static String toUpperCaseWithUnderscore(String str)
      It's designed for field/method/class/column/table names. and source and target Strings will be cached.
      Parameters:
      str -
      Returns:
    • toUpperCaseKeyWithUnderscore

      public static void toUpperCaseKeyWithUnderscore(Map<String,Object> props)
      To upper case key with underscore.
      Parameters:
      props -
    • isBeanClass

      public static boolean isBeanClass(Class<?> cls)
      Checks if is bean.
      Parameters:
      cls -
      Returns:
      true, if is bean
    • isRecordClass

      public static boolean isRecordClass(Class<?> cls)
      Parameters:
      cls -
      Returns:
    • isAnonymousClass

      public static boolean isAnonymousClass(Class<?> cls)
      Parameters:
      cls -
      Returns:
    • isMemberClass

      public static boolean isMemberClass(Class<?> cls)
      Parameters:
      cls -
      Returns:
    • isAnonymousOrMemeberClass

      public static boolean isAnonymousOrMemeberClass(Class<?> cls)
      Parameters:
      cls -
      Returns: