Class ReflectUtils

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

public final class ReflectUtils extends Object
ReflectUtils
  • Field Details

  • Method Details

    • isPrimitives

      public static boolean isPrimitives(Class<?> cls)
    • isPrimitive

      public static boolean isPrimitive(Class<?> cls)
    • getBoxedClass

      public static Class<?> getBoxedClass(Class<?> c)
    • isCompatible

      public static boolean isCompatible(Class<?> c, Object o)
      is compatible.
      Parameters:
      c - class.
      o - instance.
      Returns:
      compatible or not.
    • isCompatible

      public static boolean isCompatible(Class<?>[] cs, Object[] os)
      is compatible.
      Parameters:
      cs - class array.
      os - object array.
      Returns:
      compatible or not.
    • getCodeBase

      public static String getCodeBase(Class<?> cls)
    • getName

      public static String getName(Class<?> c)
      get name. java.lang.Object[][].class => "java.lang.Object[][]"
      Parameters:
      c - class.
      Returns:
      name.
    • getGenericClass

      public static Class<?> getGenericClass(Class<?> cls)
    • getGenericClass

      public static Class<?> getGenericClass(Class<?> cls, int i)
    • getName

      public static String getName(Method m)
      get method name. "void do(int)", "void do()", "int do(java.lang.String,boolean)"
      Parameters:
      m - method.
      Returns:
      name.
    • getSignature

      public static String getSignature(String methodName, Class<?>[] parameterTypes)
    • getName

      public static String getName(Constructor<?> c)
      get constructor name. "()", "(java.lang.String,int)"
      Parameters:
      c - constructor.
      Returns:
      name.
    • getDesc

      public static String getDesc(Class<?> c)
      get class desc. boolean[].class => "[Z" Object.class => "Ljava/lang/Object;"
      Parameters:
      c - class.
      Returns:
      desc.
      Throws:
      javassist.NotFoundException
    • getDesc

      public static String getDesc(Class<?>[] cs)
      get class array desc. [int.class, boolean[].class, Object.class] => "I[ZLjava/lang/Object;"
      Parameters:
      cs - class array.
      Returns:
      desc.
      Throws:
      javassist.NotFoundException
    • getDesc

      public static String getDesc(Method m)
      get method desc. int do(int arg1) => "do(I)I" void do(String arg1,boolean arg2) => "do(Ljava/lang/String;Z)V"
      Parameters:
      m - method.
      Returns:
      desc.
    • getDescArray

      public static String[] getDescArray(Method m)
    • getDesc

      public static String getDesc(Constructor<?> c)
      get constructor desc. "()V", "(Ljava/lang/String;I)V"
      Parameters:
      c - constructor.
      Returns:
      desc
    • getDescWithoutMethodName

      public static String getDescWithoutMethodName(Method m)
      get method desc. "(I)I", "()V", "(Ljava/lang/String;Z)V"
      Parameters:
      m - method.
      Returns:
      desc.
    • getDesc

      public static String getDesc(javassist.CtClass c) throws javassist.NotFoundException
      get class desc. Object.class => "Ljava/lang/Object;" boolean[].class => "[Z"
      Parameters:
      c - class.
      Returns:
      desc.
      Throws:
      javassist.NotFoundException
    • getDesc

      public static String getDesc(javassist.CtMethod m) throws javassist.NotFoundException
      get method desc. "do(I)I", "do()V", "do(Ljava/lang/String;Z)V"
      Parameters:
      m - method.
      Returns:
      desc.
      Throws:
      javassist.NotFoundException
    • getDesc

      public static String getDesc(javassist.CtConstructor c) throws javassist.NotFoundException
      get constructor desc. "()V", "(Ljava/lang/String;I)V"
      Parameters:
      c - constructor.
      Returns:
      desc
      Throws:
      javassist.NotFoundException
    • getDescWithoutMethodName

      public static String getDescWithoutMethodName(javassist.CtMethod m) throws javassist.NotFoundException
      get method desc. "(I)I", "()V", "(Ljava/lang/String;Z)V".
      Parameters:
      m - method.
      Returns:
      desc.
      Throws:
      javassist.NotFoundException
    • name2desc

      public static String name2desc(String name)
      name to desc. java.util.Map[][] => "[[Ljava/util/Map;"
      Parameters:
      name - name.
      Returns:
      desc.
    • desc2name

      public static String desc2name(String desc)
      desc to name. "[[I" => "int[][]"
      Parameters:
      desc - desc.
      Returns:
      name.
    • forName

      public static Class<?> forName(String name)
    • forName

      public static Class<?> forName(ClassLoader cl, String name)
    • name2class

      public static Class<?> name2class(String name) throws ClassNotFoundException
      name to class. "boolean" => boolean.class "java.util.Map[][]" => java.util.Map[][].class
      Parameters:
      name - name.
      Returns:
      Class instance.
      Throws:
      ClassNotFoundException
    • desc2class

      public static Class<?> desc2class(String desc) throws ClassNotFoundException
      desc to class. "[Z" => boolean[].class "[[Ljava/util/Map;" => java.util.Map[][].class
      Parameters:
      desc - desc.
      Returns:
      Class instance.
      Throws:
      ClassNotFoundException
    • desc2classArray

      public static Class<?>[] desc2classArray(String desc) throws ClassNotFoundException
      get class array instance.
      Parameters:
      desc - desc.
      Returns:
      Class class array.
      Throws:
      ClassNotFoundException
    • findMethodByMethodSignature

      @Deprecated public static Method findMethodByMethodSignature(Class<?> clazz, String methodName, String[] parameterTypes) throws NoSuchMethodException, ClassNotFoundException
      Find method from method signature
      Parameters:
      clazz - Target class to find method
      methodName - Method signature, e.g.: method1(int, String). It is allowed to provide method name only, e.g.: method2
      Returns:
      target method
      Throws:
      NoSuchMethodException
      ClassNotFoundException
      IllegalStateException - when multiple methods are found (overridden method when parameter info is not provided)
    • findMethodByMethodName

      @Deprecated public static Method findMethodByMethodName(Class<?> clazz, String methodName) throws NoSuchMethodException, ClassNotFoundException
      Parameters:
      clazz - Target class to find method
      methodName - Method signature, e.g.: method1(int, String). It is allowed to provide method name only, e.g.: method2
      Returns:
      target method
      Throws:
      NoSuchMethodException
      ClassNotFoundException
      IllegalStateException - when multiple methods are found (overridden method when parameter info is not provided)
    • findConstructor

      public static Constructor<?> findConstructor(Class<?> clazz, Class<?> paramType) throws NoSuchMethodException
      Throws:
      NoSuchMethodException
    • isInstance

      public static boolean isInstance(Object obj, String interfaceClazzName)
      Check if one object is the implementation for a given interface.

      This method will not trigger classloading for the given interface, therefore it will not lead to error when the given interface is not visible by the classloader

      Parameters:
      obj - Object to examine
      interfaceClazzName - The given interface
      Returns:
      true if the object implements the given interface, otherwise return false
    • getEmptyObject

      public static Object getEmptyObject(Class<?> returnType)
    • defaultReturn

      public static Object defaultReturn(Method m)
    • defaultReturn

      public static Object defaultReturn(Class<?> classType)
    • isBeanPropertyReadMethod

      public static boolean isBeanPropertyReadMethod(Method method)
    • getPropertyNameFromBeanReadMethod

      public static String getPropertyNameFromBeanReadMethod(Method method)
    • isBeanPropertyWriteMethod

      public static boolean isBeanPropertyWriteMethod(Method method)
    • getPropertyNameFromBeanWriteMethod

      public static String getPropertyNameFromBeanWriteMethod(Method method)
    • isPublicInstanceField

      public static boolean isPublicInstanceField(Field field)
    • getBeanPropertyFields

      public static Map<String,Field> getBeanPropertyFields(Class cl)
    • getBeanPropertyReadMethods

      public static Map<String,Method> getBeanPropertyReadMethods(Class cl)
    • getReturnTypes

      public static Type[] getReturnTypes(Method method)
    • findParameterizedTypes

      public static Set<ParameterizedType> findParameterizedTypes(Class<?> sourceClass)
      Parameters:
      sourceClass - the source class
      Returns:
      non-null read-only Set
      Since:
      2.7.5
    • findHierarchicalTypes

      public static <T> Set<Class<T>> findHierarchicalTypes(Class<?> sourceClass, Class<T> matchType)
      Find the hierarchical types from the source class by specified type.
      Type Parameters:
      T - the type to match
      Parameters:
      sourceClass - the source class
      matchType - the type to match
      Returns:
      non-null read-only Set
      Since:
      2.7.5
    • getProperty

      public static <T> T getProperty(Object bean, String methodName)
      Get the value from the specified bean and its getter method.
      Type Parameters:
      T - the type of property value
      Parameters:
      bean - the bean instance
      methodName - the name of getter
      Returns:
      Since:
      2.7.5
    • hasMethod

      public static boolean hasMethod(Class<?> beanClass, String methodName)
      Check target bean class whether has specify method
      Parameters:
      beanClass -
      methodName -
      Returns:
    • resolveTypes

      public static Class[] resolveTypes(Object... values)
      Resolve the types of the specified values
      Parameters:
      values - the values
      Returns:
      If can't be resolved, return empty class array
      Since:
      2.7.6
    • checkZeroArgConstructor

      public static boolean checkZeroArgConstructor(Class clazz)
    • isJdk

      public static boolean isJdk(Class clazz)
    • makeAccessible

      public static void makeAccessible(Method method)
      Copy from org.springframework.util.ReflectionUtils. Make the given method accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).
      Parameters:
      method - the method to make accessible
      See Also:
    • getAllFieldNames

      public static Set<String> getAllFieldNames(Class<?> type)
      Get all field names of target type
      Parameters:
      type -
      Returns:
    • getFieldValue

      public static <T> T getFieldValue(Object obj, String fieldName) throws RuntimeException
      Throws:
      RuntimeException