cn.hutool.core.bean.BeanUtil类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(5.6k)|赞(0)|评价(0)|浏览(1307)

本文整理了Java中cn.hutool.core.bean.BeanUtil类的一些代码示例,展示了BeanUtil类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BeanUtil类的具体详情如下:
包路径:cn.hutool.core.bean.BeanUtil
类名称:BeanUtil

BeanUtil介绍

[英]Bean工具类

把一个拥有对属性进行set和get方法的类,我们就可以称之为JavaBean。
[中]豆工具类
把一个拥有对属性进行设置和收到方法的类,我们就可以称之为JavaBean

代码示例

代码示例来源:origin: looly/hutool

/**
 * 对象转Map,不进行驼峰转下划线,不忽略值为空的字段
 * 
 * @param bean bean对象
 * @return Map
 */
public static Map<String, Object> beanToMap(Object bean) {
  return beanToMap(bean, false, false);
}

代码示例来源:origin: looly/hutool

/**
 * 复制Bean对象属性
 * 
 * @param source 源Bean对象
 * @param target 目标Bean对象
 */
public static void copyProperties(Object source, Object target) {
  copyProperties(source, target, CopyOptions.create());
}

代码示例来源:origin: looly/hutool

/**
 * 填充Value Object对象
 * 
 * @param <T> Bean类型
 * @param clazz Value Object(或者POJO)的类
 * @return vo
 */
public <T> T toBean(Class<T> clazz) {
  return BeanUtil.mapToBean(this, clazz, false);
}

代码示例来源:origin: looly/hutool

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Map<?, ?> convertInternal(Object value) {
  Map map = null;
  if(value instanceof Map){
    map = MapUtil.createMap(TypeUtil.getClass(this.mapType));
    convertMapToMap((Map)value, map);
  }else if(BeanUtil.isBean(value.getClass())){
    map = BeanUtil.beanToMap(value);
  }else{
    throw new UnsupportedOperationException(StrUtil.format("Unsupport toMap value type: {}", value.getClass().getName()));
  }
  return map;
}

代码示例来源:origin: looly/hutool

/**
 * 转换为Bean对象,并使用驼峰法模式转换
 * 
 * @param <T> Bean类型
 * @param bean Bean
 * @return Bean
 */
public <T> T toBeanWithCamelCase(T bean) {
  BeanUtil.fillBeanWithMap(this, bean, true, false);
  return bean;
}

代码示例来源:origin: looly/hutool

/**
 * 构造
 * 
 * @param bean Bean
 * @param ignoreCase 是否忽略字段大小写
 * @param ignoreError 是否忽略字段值读取错误
 */
public BeanValueProvider(Object bean, boolean ignoreCase, boolean ignoreError) {
  this.source = bean;
  this.ignoreError = ignoreError;
  sourcePdMap = BeanUtil.getBeanDesc(source.getClass()).getPropMap(ignoreCase);
}

代码示例来源:origin: looly/hutool

final Map<String, Object> map = BeanUtil.beanToMap(bean);
    MapUtil.getAny(map, unwrapedKeys);
return BeanUtil.getFieldValue(bean, expression);

代码示例来源:origin: looly/hutool

/**
 * ServletRequest 参数转Bean
 * 
 * @param <T> Bean类型
 * @param beanClass Bean Class
 * @param valueProvider 值提供者
 * @param copyOptions 拷贝选项,见 {@link CopyOptions}
 * @return Bean
 */
public static <T> T toBean(Class<T> beanClass, ValueProvider<String> valueProvider, CopyOptions copyOptions) {
  return fillBean(ReflectUtil.newInstance(beanClass), valueProvider, copyOptions);
}

代码示例来源:origin: looly/hutool

@Override
  public int hash(T t) {
    if (null == t || false == BeanUtil.isBean(t.getClass())) {
      // 非Bean放在同一子分组中
      return 0;
    }
    final Object value = ReflectUtil.getFieldValue(t, fieldName);
    int hash = fieldNameList.indexOf(value);
    if (hash < 0) {
      fieldNameList.add(value);
      return fieldNameList.size() - 1;
    } else {
      return hash;
    }
  }
});

代码示例来源:origin: looly/hutool

/**
 * 转换为Bean对象
 * 
 * @param <T> Bean类型
 * @param bean Bean
 * @return Bean
 * @since 3.3.1
 */
public <T> T toBeanIgnoreCase(T bean) {
  BeanUtil.fillBeanWithMapIgnoreCase(this, bean, false);
  return bean;
}

代码示例来源:origin: looly/hutool

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Map<?, ?> convertInternal(Object value) {
  Map map = null;
  if(value instanceof Map){
    map = MapUtil.createMap(TypeUtil.getClass(this.mapType));
    convertMapToMap((Map)value, map);
  }else if(BeanUtil.isBean(value.getClass())){
    map = BeanUtil.beanToMap(value);
  }else{
    throw new UnsupportedOperationException(StrUtil.format("Unsupport toMap value type: {}", value.getClass().getName()));
  }
  return map;
}

代码示例来源:origin: looly/hutool

/**
 * 使用Map填充Bean对象
 * 
 * @param <T> Bean类型
 * @param map Map
 * @param bean Bean
 * @param isIgnoreError 是否忽略注入错误
 * @return Bean
 */
public static <T> T fillBeanWithMap(Map<?, ?> map, T bean, boolean isIgnoreError) {
  return fillBeanWithMap(map, bean, false, isIgnoreError);
}

代码示例来源:origin: looly/hutool

/**
 * 构造
 * 
 * @param bean Bean
 * @param ignoreCase 是否忽略字段大小写
 * @param ignoreError 是否忽略字段值读取错误
 */
public BeanValueProvider(Object bean, boolean ignoreCase, boolean ignoreError) {
  this.source = bean;
  this.ignoreError = ignoreError;
  sourcePdMap = BeanUtil.getBeanDesc(source.getClass()).getPropMap(ignoreCase);
}

代码示例来源:origin: looly/hutool

final Map<String, Object> map = BeanUtil.beanToMap(bean);
    MapUtil.getAny(map, unwrapedKeys);
return BeanUtil.getFieldValue(bean, expression);

代码示例来源:origin: looly/hutool

/**
 * ServletRequest 参数转Bean
 * 
 * @param <T> Bean类型
 * @param beanClass Bean Class
 * @param valueProvider 值提供者
 * @param copyOptions 拷贝选项,见 {@link CopyOptions}
 * @return Bean
 */
public static <T> T toBean(Class<T> beanClass, ValueProvider<String> valueProvider, CopyOptions copyOptions) {
  return fillBean(ReflectUtil.newInstance(beanClass), valueProvider, copyOptions);
}

代码示例来源:origin: looly/hutool

@Override
  public int hash(T t) {
    if (null == t || false == BeanUtil.isBean(t.getClass())) {
      // 非Bean放在同一子分组中
      return 0;
    }
    final Object value = ReflectUtil.getFieldValue(t, fieldName);
    int hash = fieldNameList.indexOf(value);
    if (hash < 0) {
      fieldNameList.add(value);
      return fieldNameList.size() - 1;
    } else {
      return hash;
    }
  }
});

代码示例来源:origin: looly/hutool

/**
 * 转换为Bean对象
 * 
 * @param <T> Bean类型
 * @param bean Bean
 * @return Bean
 * @since 3.3.1
 */
public <T> T toBeanIgnoreCase(T bean) {
  BeanUtil.fillBeanWithMapIgnoreCase(this, bean, false);
  return bean;
}

代码示例来源:origin: looly/hutool

/**
 * 填充Value Object对象
 * 
 * @param <T> Bean类型
 * @param clazz Value Object(或者POJO)的类
 * @return vo
 */
public <T> T toBean(Class<T> clazz) {
  return BeanUtil.mapToBean(this, clazz, false);
}

代码示例来源:origin: looly/hutool

/**
 * 复制Bean对象属性
 * 
 * @param source 源Bean对象
 * @param target 目标Bean对象
 */
public static void copyProperties(Object source, Object target) {
  copyProperties(source, target, CopyOptions.create());
}

代码示例来源:origin: looly/hutool

/**
 * 对象转Map,不进行驼峰转下划线,不忽略值为空的字段
 * 
 * @param bean bean对象
 * @return Map
 */
public static Map<String, Object> beanToMap(Object bean) {
  return beanToMap(bean, false, false);
}

相关文章