+-
首页 专栏 java 文章详情
关注作者
关注作者
0
反射判断对象所有字段是空及获取对象字段名及字段值
一片秋叶一树春 发布于 2 月 25 日
说明:使用反射判断或者获取的对象中值或者字段
示例:
1.获取带某注解的对象字段及属性名称
//注解定义
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ModifyField {
/**
* 字段名称 * @return
*/
String name() default "";
}
/**
* 获取对象中所有带@ModifyField注解的字段列表map * @param
* @return
*/
public static Map<String, String> getAllFieldMap(Class<?> clazz) {
Map<String, String> map = new HashMap<>();
if (clazz == null) {
return map;
}
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
ModifyField modifyField = field.getAnnotation(ModifyField.class);
if (modifyField != null) {
map.put(field.getName(), modifyField.name());
}
}
return map;
}
2.检查某对象属性是否全是空
/**
* 检验对象属性是否全是空 * @param object
* @return
*/
public static boolean checkAllFieldsIsNull(Object object) {
if (null == object) {
return true;
}
try {
for (Field f : object.getClass().getDeclaredFields()) {
f.setAccessible(true);
if (f.get(object) != null && StringUtils.isNotBlank(f.get(object).toString())) {
return false;
}
}
} catch (Exception e) {
log.error("checkAllFieldsIsNull err {}",e);
}
return true;
}
3.获取某对象中的所有有值的字段及值内容
/**
* 获取对象中的所有有值的字段及值内容 * @param obj
* @return
*/
public static Map<String, Object> getAllFieldNotNull(Object obj) {
Map<String, Object> map = new HashMap<>();
if (obj == null) {
return map;
}
try {
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(obj);
if (value != null) {
map.put(field.getName(), value);
}
}
} catch (IllegalAccessException e) {
log.error("getAllFieldNotNull err {}",e);
}
return map;
}
java 后端 springboot
阅读 36 发布于 2 月 25 日
赞
收藏
分享
本作品系原创, 采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议
一片秋叶一树春
贪君子之财,好美景之色,行正义之事,了前生之愿,爱此生之人!!!!!
37 声望
2 粉丝
0 条评论
得票 时间
提交评论
一片秋叶一树春
贪君子之财,好美景之色,行正义之事,了前生之愿,爱此生之人!!!!!
37 声望
2 粉丝
宣传栏
目录
▲
说明:使用反射判断或者获取的对象中值或者字段
示例:
1.获取带某注解的对象字段及属性名称
//注解定义
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ModifyField {
/**
* 字段名称 * @return
*/
String name() default "";
}
/**
* 获取对象中所有带@ModifyField注解的字段列表map * @param
* @return
*/
public static Map<String, String> getAllFieldMap(Class<?> clazz) {
Map<String, String> map = new HashMap<>();
if (clazz == null) {
return map;
}
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
ModifyField modifyField = field.getAnnotation(ModifyField.class);
if (modifyField != null) {
map.put(field.getName(), modifyField.name());
}
}
return map;
}
2.检查某对象属性是否全是空
/**
* 检验对象属性是否全是空 * @param object
* @return
*/
public static boolean checkAllFieldsIsNull(Object object) {
if (null == object) {
return true;
}
try {
for (Field f : object.getClass().getDeclaredFields()) {
f.setAccessible(true);
if (f.get(object) != null && StringUtils.isNotBlank(f.get(object).toString())) {
return false;
}
}
} catch (Exception e) {
log.error("checkAllFieldsIsNull err {}",e);
}
return true;
}
3.获取某对象中的所有有值的字段及值内容
/**
* 获取对象中的所有有值的字段及值内容 * @param obj
* @return
*/
public static Map<String, Object> getAllFieldNotNull(Object obj) {
Map<String, Object> map = new HashMap<>();
if (obj == null) {
return map;
}
try {
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(obj);
if (value != null) {
map.put(field.getName(), value);
}
}
} catch (IllegalAccessException e) {
log.error("getAllFieldNotNull err {}",e);
}
return map;
}