Class ClassUtils

java.lang.Object
org.apache.dubbo.common.utils.ClassUtils

public class ClassUtils extends Object
  • Field Details

  • Constructor Details

    • ClassUtils

      public ClassUtils()
  • Method Details

    • forNameWithThreadContextClassLoader

      public static Class<?> forNameWithThreadContextClassLoader(String name) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • forNameWithCallerClassLoader

      public static Class<?> forNameWithCallerClassLoader(String name, Class<?> caller) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • getCallerClassLoader

      public static ClassLoader getCallerClassLoader(Class<?> caller)
    • getClassLoader

      public static ClassLoader getClassLoader(Class<?> clazz)
      get class loader
      Parameters:
      clazz -
      Returns:
      class loader
    • getClassLoader

      public static ClassLoader getClassLoader()
      Return the default ClassLoader to use: typically the thread context ClassLoader, if available; the ClassLoader that loaded the ClassUtils class will be used as fallback.

      Call this method if you intend to use the thread context ClassLoader in a scenario where you absolutely need a non-null ClassLoader reference: for example, for class path resource loading (but not necessarily for Class.forName, which accepts a null ClassLoader reference as well).

      Returns:
      the default ClassLoader (never null)
      See Also:
    • forName

      public static Class<?> forName(String name) throws ClassNotFoundException
      Same as Class.forName(), except that it works for primitive types.
      Throws:
      ClassNotFoundException
    • forNameAndTryCatch

      public static Class<?> forNameAndTryCatch(String name)
      find class and don`t expect to throw exception
      Parameters:
      name -
      Returns:
    • forName

      public static Class<?> forName(String name, ClassLoader classLoader) throws ClassNotFoundException, LinkageError
      Replacement for Class.forName() that also returns Class instances for primitives (like "int") and array class names (like "String[]").
      Parameters:
      name - the name of the Class
      classLoader - the class loader to use (may be null, which indicates the default class loader)
      Returns:
      Class instance for the supplied name
      Throws:
      ClassNotFoundException - if the class was not found
      LinkageError - if the class file could not be loaded
      See Also:
    • resolvePrimitiveClassName

      public static Class<?> resolvePrimitiveClassName(String name)
      Resolve the given class name as primitive class, if appropriate, according to the JVM's naming rules for primitive classes.

      Also supports the JVM's internal class names for primitive arrays. Does not support the "[]" suffix notation for primitive arrays; this is only supported by forName(java.lang.String).

      Parameters:
      name - the name of the potentially primitive class
      Returns:
      the primitive class, or null if the name does not denote a primitive class or primitive array class
    • toShortString

      public static String toShortString(Object obj)
    • simpleClassName

      public static String simpleClassName(Class<?> clazz)
    • isPrimitive

      public static boolean isPrimitive(Class<?> type)
      Deprecated.
      as 2.7.6, use Class.isPrimitive() plus isSimpleType(Class) instead
      The specified type is primitive type or simple type
      Parameters:
      type - the type to test
      Returns:
    • isSimpleType

      public static boolean isSimpleType(Class<?> type)
      The specified type is simple type or not
      Parameters:
      type - the type to test
      Returns:
      if type is one element of SIMPLE_TYPES, return true, or false
      Since:
      2.7.6
      See Also:
    • convertPrimitive

      public static Object convertPrimitive(Class<?> type, String value)
    • convertPrimitive

      public static Object convertPrimitive(FrameworkModel frameworkModel, Class<?> type, String value)
    • isTypeMatch

      public static boolean isTypeMatch(Class<?> type, String value)
      We only check boolean value at this moment.
      Parameters:
      type -
      value -
      Returns:
    • getAllSuperClasses

      public static Set<Class<?>> getAllSuperClasses(Class<?> type, Predicate<Class<?>>... classFilters)
      Get all super classes from the specified type
      Parameters:
      type - the specified type
      classFilters - the filters for classes
      Returns:
      non-null read-only Set
      Since:
      2.7.6
    • getAllInterfaces

      public static Set<Class<?>> getAllInterfaces(Class<?> type, Predicate<Class<?>>... interfaceFilters)
      Get all interfaces from the specified type
      Parameters:
      type - the specified type
      interfaceFilters - the filters for interfaces
      Returns:
      non-null read-only Set
      Since:
      2.7.6
    • getAllInheritedTypes

      public static Set<Class<?>> getAllInheritedTypes(Class<?> type, Predicate<Class<?>>... typeFilters)
      Get all inherited types from the specified type
      Parameters:
      type - the specified type
      typeFilters - the filters for types
      Returns:
      non-null read-only Set
      Since:
      2.7.6
    • isAssignableFrom

      public static boolean isAssignableFrom(Class<?> superType, Class<?> targetType)
      the semantics is same as Class.isAssignableFrom(Class)
      Parameters:
      superType - the super type
      targetType - the target type
      Returns:
      see Class.isAssignableFrom(Class)
      Since:
      2.7.6
    • isPresent

      public static boolean isPresent(String className, ClassLoader classLoader)
      Test the specified class name is present in the ClassLoader
      Parameters:
      className - the name of Class
      classLoader - ClassLoader
      Returns:
      If found, return true
      Since:
      2.7.6
    • resolveClass

      public static Class<?> resolveClass(String className, ClassLoader classLoader)
      Resolve the Class by the specified name and ClassLoader
      Parameters:
      className - the name of Class
      classLoader - ClassLoader
      Returns:
      If can't be resolved , return null
      Since:
      2.7.6
    • isGenericClass

      public static boolean isGenericClass(Class<?> type)
      Is generic class or not?
      Parameters:
      type - the target type
      Returns:
      if the target type is not null or void or Void.class, return true, or false
      Since:
      2.7.6
    • hasMethods

      public static boolean hasMethods(Method[] methods)
    • getMethodNames

      public static String[] getMethodNames(Class<?> tClass)
      get method name array.
      Returns:
      method name array.
    • isMatch

      public static boolean isMatch(Class<?> from, Class<?> to)
    • getDeclaredMethodNames

      public static String[] getDeclaredMethodNames(Class<?> tClass)
      get method name array.
      Returns:
      method name array.