Java Code Examples for cn.hutool.core.util.ReflectUtil#getFields()

The following examples show how to use cn.hutool.core.util.ReflectUtil#getFields() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NoBootTest.java    From zuihou-admin-boot with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
    TestModel obj = new TestModel();
    obj.setStation(new RemoteData<>(101L));
    obj.setOrg2(new RemoteData<>(101L));

    Field[] fields = ReflectUtil.getFields(obj.getClass());
    for (Field field : fields) {
        InjectionField anno = field.getDeclaredAnnotation(InjectionField.class);
        if (anno == null) {
            continue;
        }
        field.setAccessible(true);

        String api = anno.api();
        Class<?> feign = anno.feign();

        if (StrUtil.isEmpty(api) && Object.class.equals(feign)) {
            log.warn("忽略注入字段: {}.{}", field.getType(), field.getName());
            continue;
        }


    }
}
 
Example 2
Source File: BaseDao.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 获取字段列表 {@code 过滤数据库中不存在的字段,以及自增列}
 *
 * @param t          对象
 * @param ignoreNull 是否忽略空值
 * @return 字段列表
 */
private List<Field> getField(T t, Boolean ignoreNull) {
	// 获取所有字段,包含父类中的字段
	Field[] fields = ReflectUtil.getFields(t.getClass());

	// 过滤数据库中不存在的字段,以及自增列
	List<Field> filterField;
	Stream<Field> fieldStream = CollUtil.toList(fields).stream().filter(field -> ObjectUtil.isNull(field.getAnnotation(Ignore.class)) || ObjectUtil.isNull(field.getAnnotation(Pk.class)));

	// 是否过滤字段值为null的字段
	if (ignoreNull) {
		filterField = fieldStream.filter(field -> ObjectUtil.isNotNull(ReflectUtil.getFieldValue(t, field))).collect(Collectors.toList());
	} else {
		filterField = fieldStream.collect(Collectors.toList());
	}
	return filterField;
}
 
Example 3
Source File: MessageController.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 判断Bean是否为空对象或者空白字符串,空对象表示本身为<code>null</code>或者所有属性都为<code>null</code>
 *
 * @param bean Bean对象
 * @return 是否为空,<code>true</code> - 空 / <code>false</code> - 非空
 */
private boolean isBlank(Object bean) {
    if (null != bean) {
        for (Field field : ReflectUtil.getFields(bean.getClass())) {
            Object fieldValue = ReflectUtil.getFieldValue(bean, field);
            if (null != fieldValue) {
                if (fieldValue instanceof String && StrUtil.isNotBlank((String) fieldValue)) {
                    return false;
                } else if (!(fieldValue instanceof String)) {
                    return false;
                }
            }
        }
    }
    return true;
}
 
Example 4
Source File: NoBootTest.java    From zuihou-admin-cloud with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
    TestModel obj = new TestModel();
    obj.setStation(new RemoteData<>(101L));
    obj.setOrg2(new RemoteData<>(101L));

    Field[] fields = ReflectUtil.getFields(obj.getClass());
    for (Field field : fields) {
        InjectionField anno = field.getDeclaredAnnotation(InjectionField.class);
        if (anno == null) {
            continue;
        }
        field.setAccessible(true);

        String api = anno.api();
        Class<?> feign = anno.feign();

        if (StrUtil.isEmpty(api) && Object.class.equals(feign)) {
            log.warn("忽略注入字段: {}.{}", field.getType(), field.getName());
            continue;
        }


    }
}
 
Example 5
Source File: BaseDao.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 获取字段列表 {@code 过滤数据库中不存在的字段,以及自增列}
 *
 * @param t          对象
 * @param ignoreNull 是否忽略空值
 * @return 字段列表
 */
private List<Field> getField(T t, Boolean ignoreNull) {
	// 获取所有字段,包含父类中的字段
	Field[] fields = ReflectUtil.getFields(t.getClass());

	// 过滤数据库中不存在的字段,以及自增列
	List<Field> filterField;
	Stream<Field> fieldStream = CollUtil.toList(fields).stream().filter(field -> ObjectUtil.isNull(field.getAnnotation(Ignore.class)) || ObjectUtil.isNull(field.getAnnotation(Pk.class)));

	// 是否过滤字段值为null的字段
	if (ignoreNull) {
		filterField = fieldStream.filter(field -> ObjectUtil.isNotNull(ReflectUtil.getFieldValue(t, field))).collect(Collectors.toList());
	} else {
		filterField = fieldStream.collect(Collectors.toList());
	}
	return filterField;
}
 
Example 6
Source File: MessageController.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 判断Bean是否为空对象或者空白字符串,空对象表示本身为<code>null</code>或者所有属性都为<code>null</code>
 *
 * @param bean Bean对象
 * @return 是否为空,<code>true</code> - 空 / <code>false</code> - 非空
 */
private boolean isBlank(Object bean) {
    if (null != bean) {
        for (Field field : ReflectUtil.getFields(bean.getClass())) {
            Object fieldValue = ReflectUtil.getFieldValue(bean, field);
            if (null != fieldValue) {
                if (fieldValue instanceof String && StrUtil.isNotBlank((String) fieldValue)) {
                    return false;
                } else if (!(fieldValue instanceof String)) {
                    return false;
                }
            }
        }
    }
    return true;
}
 
Example 7
Source File: BaseDao.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 获取字段列表 {@code 过滤数据库中不存在的字段,以及自增列}
 *
 * @param t          对象
 * @param ignoreNull 是否忽略空值
 * @return 字段列表
 */
private List<Field> getField(T t, Boolean ignoreNull) {
	// 获取所有字段,包含父类中的字段
	Field[] fields = ReflectUtil.getFields(t.getClass());

	// 过滤数据库中不存在的字段,以及自增列
	List<Field> filterField;
	Stream<Field> fieldStream = CollUtil.toList(fields).stream().filter(field -> ObjectUtil.isNull(field.getAnnotation(Ignore.class)) || ObjectUtil.isNull(field.getAnnotation(Pk.class)));

	// 是否过滤字段值为null的字段
	if (ignoreNull) {
		filterField = fieldStream.filter(field -> ObjectUtil.isNotNull(ReflectUtil.getFieldValue(t, field))).collect(Collectors.toList());
	} else {
		filterField = fieldStream.collect(Collectors.toList());
	}
	return filterField;
}
 
Example 8
Source File: MessageController.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 判断Bean是否为空对象或者空白字符串,空对象表示本身为<code>null</code>或者所有属性都为<code>null</code>
 *
 * @param bean Bean对象
 * @return 是否为空,<code>true</code> - 空 / <code>false</code> - 非空
 */
private boolean isBlank(Object bean) {
    if (null != bean) {
        for (Field field : ReflectUtil.getFields(bean.getClass())) {
            Object fieldValue = ReflectUtil.getFieldValue(bean, field);
            if (null != fieldValue) {
                if (fieldValue instanceof String && StrUtil.isNotBlank((String) fieldValue)) {
                    return false;
                } else if (!(fieldValue instanceof String)) {
                    return false;
                }
            }
        }
    }
    return true;
}
 
Example 9
Source File: QueryWrapperUtil.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static <T> QueryWrapper<T> getWrapper(Object query) {
	QueryWrapper<T> entityWrapper = Wrappers.query();
	if (query == null) {
		return entityWrapper;
	}
	Field[] fields = ReflectUtil.getFields(query.getClass());
	try {
		for (Field field : fields) {
			boolean accessible = field.isAccessible();
			field.setAccessible(true);
			Query q = field.getAnnotation(Query.class);
			if (q != null) {
				String propName = q.propName();
				String blurry = q.blurry();
				String attributeName = StringUtil.isEmpty(propName) ? StringUtil.toRevertCamelCase(field.getName(), CharUtil.UNDERLINE) : propName;
				Object val = field.get(query);
				if (cn.hutool.core.util.ObjectUtil.isNull(val) || "".equals(val)) {
					continue;
				}
				// 模糊多字段
				if (cn.hutool.core.util.ObjectUtil.isNotEmpty(blurry)) {
					String[] blurrys = blurry.split(",");
					entityWrapper.and(i -> {
						for (String s : blurrys) {
							i.or().like(s, val.toString());
						}
					});
					continue;
				}
				parseWarpper(entityWrapper, q, attributeName, val);
			}
			field.setAccessible(accessible);
		}
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	}
	return entityWrapper;
}