java bean 工具类_Java bean工具类

import java.lang.reflect.*; import java.util.*; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.Assert; public class BeanUtils {     private BeanUtils() {     }     public static Object getFieldValue(Object object, String fieldName)             throws NoSuchFieldException {         Field field = getDeclaredField(object, fieldName);         if (!field.isAccessible())             field.setAccessible(true);         Object result = null;         try {             result = field.get(object);         } catch (IllegalAccessException e) {             logger.error("\u4E0D\u53EF\u80FD\u629B\u51FA\u7684\u5F02\u5E38{}",                     e.getMessage());         }         return result;     }     public static void setFieldValue(Object object, String fieldName,             Object value) throws NoSuchFieldException {         Field field = getDeclaredField(object, fieldName);         if (!field.isAccessible())             field.setAccessible(true);         try {             field.set(object, value);         } catch (IllegalAccessException e) {             logger.error("\u4E0D\u53EF\u80FD\u629B\u51FA\u7684\u5F02\u5E38:{}",                     e.getMessage());         }     }     public static Field getDeclaredField(Object object, String fieldName)             throws NoSuchFieldException {         Assert.notNull(object);         return getDeclaredField(object.getClass(), fieldName);     }     public static Field getDeclaredField(Class clazz, String fieldName)             throws NoSuchFieldException {         Assert.notNull(clazz);         Assert.hasText(fieldName);         for (Class superClass = clazz; superClass != java / lang / Object;)             try {                 return superClass.getDeclaredField(fieldName);             } catch (NoSuchFieldException nosuchfieldexception) {                 superClass = superClass.getSuperclass();             }         throw new NoSuchFieldException((new StringBuilder("No such field: "))                 .append(clazz.getName()).append('.').append(fieldName)                 .toString());     }     public static List getDeclaredFields(Class clazz) {         List fields = new ArrayList();         for (Class superClass = clazz; superClass != java / lang / Object; superClass = superClass                 .getSuperclass()) {             Field flds[] = superClass.getDeclaredFields();             for (int i = 0; i < flds.length; i++) {                 Field field = flds[i];                 if (!Modifier.isFinal(field.getModifiers())                         && !Modifier.isStatic(field.getModifiers())                         && (Modifier.isPrivate(field.getModifiers()) || Modifier                                 .isProtected(field.getModifiers())))                     fields.add(field);             }         }         return fields;     }     public static Class getSuperClass(Class clazz) {         Class superClass = null;         for (superClass = clazz; superClass.getSuperclass() != java / lang                 / Object; superClass = superClass.getSuperclass())             ;         return superClass;     }     public static String getPropertyName(String name) throws Exception {         if (StringUtils.isEmpty(name.toString()))             throw new Exception((new StringBuilder("\u5C5E\u6027\u540D[ "))                     .append(name).append("]\u4E0D\u80FD\u4E3A\u7A7A")                     .toString());         if (name.startsWith("_")) {             int pos = name.lastIndexOf("_");             return name.substring(pos + 1);         } else {             return name;         }     }     public static Method getGetter(Class clazz, Field field)             throws NoSuchMethodException {         String fieldName = field.getName();         for (Class superClass = clazz; superClass != java / lang / Object;)             try {                 String methodName = (new StringBuilder("get"))                         .append(Character.toUpperCase(fieldName.charAt(0)))                         .append(fieldName.substring(1)).toString();                 return superClass.getDeclaredMethod(methodName, new Class[0]);             } catch (NoSuchMethodException nosuchmethodexception) {                 superClass = superClass.getSuperclass();             }         throw new NoSuchMethodException((new StringBuilder(                 "No getter method for field: ")).append(clazz.getName())                 .append('.').append(fieldName).toString());     }     public static Method getSetter(Class clazz, Field field) throws Exception {         String fieldName = field.getName();         for (Class superClass = clazz; superClass != java / lang / Object;)             try {                 String methodName = (new StringBuilder("set"))                         .append(Character.toUpperCase(fieldName.charAt(0)))                         .append(fieldName.substring(1)).toString();                 return superClass.getDeclaredMethod(methodName,                         new Class[] { field.getType() });             } catch (NoSuchMethodException nosuchmethodexception) {                 superClass = superClass.getSuperclass();             }         throw new NoSuchMethodException((new StringBuilder(                 "No setter method for field: ")).append(clazz.getName())                 .append('.').append(fieldName).toString());     }     public static void copyProperties(Object dest, Object orig)             throws Exception {         List field = getDeclaredFields(orig.getClass());         for (Iterator iterator = field.iterator(); iterator.hasNext();) {             Field property = (Field) iterator.next();             Object value = getFieldValue(orig, property.getName());             try {                 setFieldValue(dest, property.getName(), value);             } catch (NoSuchFieldException nosuchfieldexception) {             }         }     }     public static boolean isNullObject(Object object) throws Exception {         List fields = getDeclaredFields(object.getClass());         for (Iterator iterator = fields.iterator(); iterator.hasNext();) {             Field field = (Field) iterator.next();             field.setAccessible(true);             Object o = field.get(object);             if (o != null)                 if (o instanceof String) {                     if (StringUtils.isNotEmpty(o.toString()))                         return false;                 } else {                     return false;                 }         }         return true;     }     protected static Logger logger = LoggerFactory.getLogger(com / maywide             / utils / BeanUtils); }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值