Class Beans

java.lang.Object
cn.xuanyuanli.core.util.Beans

public class Beans extends Object
关于类操作的,都在这里
     用到工具类BeanUtils,把抛出的异常屏蔽了
     其他类操作工具类,参考:FieldUtils、MethodUtils等
     泛型操作这里是自己写了,也可以考虑使用Spring的ResolvableType
     类型转换使用了ConvertUtilsBean,可以考虑Spring的DefaultConversionService
 
Author:
John Li Email:jujubeframework@163.com
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
      日志记录器
    • PROPERTY_DESCRIPTOR_CACHE

      private static final ConcurrentMap<String,AtomicReference<PropertyDescriptor>> PROPERTY_DESCRIPTOR_CACHE
      PropertyDescriptor的缓存。key为classname+fieldName,value使用AtomicReference包装,因为ConcurrentMap不支持空值
    • METHOD_CACHE

      private static final ConcurrentMap<String,AtomicReference<Method>> METHOD_CACHE
      ConcurrentMap.computeIfAbsent不能递归调用,所以建立三个方法的缓存
    • DECLARED_METHOD_CACHE

      private static final ConcurrentMap<String,AtomicReference<Method>> DECLARED_METHOD_CACHE
      声明方法缓存
    • SELF_DECLARED_METHOD_CACHE

      private static final ConcurrentMap<String,AtomicReference<Method>> SELF_DECLARED_METHOD_CACHE
      自我声明方法缓存
    • BEANINFO_CACHE

      private static final ConcurrentMap<String,BeanInfo> BEANINFO_CACHE
      beaninfo缓存
    • FIELDNAMES_CACHE

      private static final ConcurrentMap<String,List<String>> FIELDNAMES_CACHE
      缓存字段名
    • CLASSGENERICTYPE_CACHE

      private static final ConcurrentMap<String,Class<?>> CLASSGENERICTYPE_CACHE
      classgenerictype缓存
    • FIELD_CACHE

      private static final ConcurrentMap<String,AtomicReference<Field>> FIELD_CACHE
      字段缓存
    • SELF_FIELD_CACHE

      private static final ConcurrentMap<String,AtomicReference<Field>> SELF_FIELD_CACHE
      自我字段缓存
    • DISCOVERER

      private static final org.springframework.core.ParameterNameDiscoverer DISCOVERER
      ParameterNameDiscoverer的对象
    • BASIC_TYPE

      private static final List<Class<?>> BASIC_TYPE
      基本类型封装类列表
    • CONVERT_UTILS_BEAN

      private static final org.apache.commons.beanutils.ConvertUtilsBean CONVERT_UTILS_BEAN
      转换工具类
    • errlog

      private static int errlog
      errlog
  • Constructor Details

    • Beans

      public Beans()
  • Method Details

    • beanToMap

      public static Map<String,Object> beanToMap(Object obj)
      把对象转换为map
      Parameters:
      obj - obj
      Returns:
      Mapinvalid input: '<'String, Object>
    • beanToMap

      public static Map<String,Object> beanToMap(Object obj, boolean filterNull)
      把对象转换为map(Cglib的BeanMap性能最高)
      Parameters:
      obj - obj
      filterNull - 滤波器零
      Returns:
      Mapinvalid input: '<'String, Object>
    • getInstance

      public static <T> T getInstance(Class<T> cl)
      根据类获得实例
      Type Parameters:
      T - 泛型
      Parameters:
      cl - cl
      Returns:
      Beans
    • forName

      public static Class<?> forName(String className)
      根据Class的完整限定名装配Class
      Parameters:
      className - 类名
      Returns:
      Classinvalid input: '<'
      invalid @link
      {@link ?
      }>
    • setProperty

      public static void setProperty(Object bean, String name, Object value)
      自己实现的set方法(解决链式调用后setProperty不管用的情况)
      Parameters:
      bean - 豆
      name - 名字
      value - 价值
    • convert

      private static <T> T convert(Object value, Class<T> type)
      值类型转换,copy自BeanUtils.convert
      Parameters:
      value - 价值
      type - 类型
      Returns:
      Beans
    • getProperty

      public static Object getProperty(Object bean, String name)
      自己实现的getter方法(解决字段第二个字母为大写的情况)
      Parameters:
      bean - 豆
      name - 名字
      Returns:
      Object
    • getPropertyAsString

      public static String getPropertyAsString(Object bean, String name)
      通过getter方法来获取转换为String后的指
      Parameters:
      bean - 豆
      name - 名字
      Returns:
      String
    • getMethod

      public static Method getMethod(Class<?> cl, String methodName, Class<?>... parameterTypes)
      获得所有的public方法
      Parameters:
      cl - cl
      methodName - 方法名称
      parameterTypes - 参数类型
      Returns:
      Method
    • getDeclaredMethod

      public static Method getDeclaredMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
      获得类的所有声明方法,包括父类和接口中的
      Parameters:
      clazz - clazz
      methodName - 方法名称
      parameterTypes - 参数类型
      Returns:
      Method
    • getAllInterfacesAndParentClass

      public static List<Class<?>> getAllInterfacesAndParentClass(Class<?> clazz)
      获得所有接口和父类
      Parameters:
      clazz - clazz
      Returns:
      Listinvalid input: '<'Classinvalid input: '<'
      invalid @link
      {@link ?
      }>>
    • getInterfaces

      private static void getInterfaces(Class<?> clazz, Set<Class<?>> classes)
      获得接口
      Parameters:
      clazz - clazz
      classes - 类
    • getSelfDeclaredMethod

      public static Method getSelfDeclaredMethod(Class<?> cl, String methodName, Class<?>... parameterTypes)
      获得类的所有声明方法,不包括父类中的
      Parameters:
      cl - cl
      methodName - 方法名称
      parameterTypes - 参数类型
      Returns:
      Method
    • invoke

      public static Object invoke(Method method, Object obj, Object... args)
      反射调用方法
      Parameters:
      method - 方法
      obj - obj
      args - arg游戏
      Returns:
      Object
    • invokeDefaultMethod

      public static Object invokeDefaultMethod(Method method, Object... args)
      反射调用default方法(不支持default方法中再次调用本接口的其他default方法)
      Parameters:
      method - 方法
      args - 方法参数
      Returns:
      Object
    • invokeDefaultMethod

      public static Object invokeDefaultMethod(Object proxy, Method method, Object... args) throws Throwable
      反射调用default方法
      Parameters:
      proxy - 一般为接口子对象
      method - 方法
      args - 方法参数
      Returns:
      Object
      Throws:
      Throwable - throwable
    • getDeclaredField

      public static Field getDeclaredField(Class<?> clazz, String fieldName)
      获得类的所有声明字段,包括父类中的
      Parameters:
      clazz - clazz
      fieldName - 字段名
      Returns:
      Field
    • getSelfDeclaredField

      public static Field getSelfDeclaredField(Class<?> cl, String fieldName)
      获得类的所有声明字段,不包括父类中的
      Parameters:
      cl - cl
      fieldName - 字段名
      Returns:
      Field
    • getBeanInfo

      private static BeanInfo getBeanInfo(Class<?> targetClass)
      根据Class获得类信息
      Parameters:
      targetClass - 目标类
      Returns:
      BeanInfo
    • getPropertyDescriptor

      public static PropertyDescriptor getPropertyDescriptor(Class<?> targetClass, String fieldName)
      获得类的某个字段属性描述
      Parameters:
      targetClass - 目标类
      fieldName - 字段名
      Returns:
      PropertyDescriptor
    • getPropertyDescriptorFromBeanInfo

      private static PropertyDescriptor getPropertyDescriptorFromBeanInfo(BeanInfo beanInfo, String fieldName)
      从BeanInfo中获取字段属性描述器
      Parameters:
      beanInfo - bean信息
      fieldName - 字段名
      Returns:
      PropertyDescriptor
    • getFormalParamSimpleMapping

      public static Map<String,Object> getFormalParamSimpleMapping(Method method, Object... args)
      获得形参名和形参值的简单对照表(name-value)
      Parameters:
      method - 方法
      args - 实参集合(可为空,MethodParam的value也为空)
      Returns:
      Mapinvalid input: '<'String, Object>
    • getMethodParamNames

      public static String[] getMethodParamNames(Method method)
      获取方法的形参名集合
      Parameters:
      method - 方法
      Returns:
      String
    • getAllDeclaredFieldNames

      public static List<String> getAllDeclaredFieldNames(Class<?> clazz)
      获得所有可访问的字段名(包括父类)集合
      Parameters:
      clazz - clazz
      Returns:
      Listinvalid input: '<'String>
    • contrastObject

      public static List<Beans.FieldDidderence> contrastObject(Object oldObject, Object newObject)
      对比两个对象,获取差异字段集合
      Parameters:
      oldObject - 旧对象
      newObject - 新对象
      Returns:
      Listinvalid input: '<'Beans.FieldDidderence>
    • getClassGenericType

      public static <T> Class<T> getClassGenericType(Class<?> clazz)
      通过反射, 获得Class定义中声明的泛型参数的类型(先找父类,后找接口)。如无法找到, 返回Object.class.
      Type Parameters:
      T - 泛型
      Parameters:
      clazz - The class to introspect
      Returns:
      the first generic declaration, or Object.class if you cannot be determined
    • getClassGenericType

      public static Class<?> getClassGenericType(Class<?> clazz, int index)
      通过反射, 获得Class定义中声明的父类(或接口,如果是接口的话,默认获得第一个泛型接口)的泛型参数的类型。如无法找到, 返回Object.class.
      Parameters:
      clazz - clazz The class to introspect
      index - the Index of the generic ddeclaration,start from 0.
      Returns:
      the index generic declaration, or Object.class if you cannot be determined
    • getDefaultClassLoader

      public static ClassLoader getDefaultClassLoader()
      获得当前项目(jar)的ClassLoader
      Returns:
      ClassLoader
    • isBasicType

      public static boolean isBasicType(Class<?> cl)
      是否是基本数据类型
      Parameters:
      cl - cl
      Returns:
      boolean
    • getObjcetFromMethodArgs

      public static <T> T getObjcetFromMethodArgs(Object[] methodArgs, Class<T> clazz)
      从方法实参中获得对应类型的对象
      Type Parameters:
      T - 泛型
      Parameters:
      methodArgs - 方法参数
      clazz - clazz
      Returns:
      Beans
    • getExpectTypeValue

      public static <T> T getExpectTypeValue(Object o, Class<T> returnType)
      获得预期类型的值
      Type Parameters:
      T - 泛型
      Parameters:
      o - o
      returnType - 返回类型
      Returns:
      Beans
    • getMethodReturnParameterizedTypeFirst

      public static Class<?> getMethodReturnParameterizedTypeFirst(Method method, Class<?> defaultClass)
      获得方法参数化返回值的第一个类型
      Parameters:
      method - 方法
      defaultClass - 默认类型(在获取不到参数化类型时默认取这个值)
      Returns:
      Classinvalid input: '<'
      invalid @link
      {@link ?
      }>