Пример #1
0
 /**
  * 返回字段的类型, 支持多级字段
  *
  * @param clazz
  * @param propertyName
  * @return
  */
 public static Class getFieldType(Class clazz, String propertyName) {
   List<String> props = StringUtil.split(propertyName, ".");
   Class cls = clazz;
   try {
     for (String prop : props) {
       Method m = cls.getMethod(getGetterMethodName(prop), new Class[] {});
       cls = m.getReturnType();
     }
   } catch (Exception ex) {
     ex.printStackTrace();
     cls = null;
   }
   return cls;
 }
Пример #2
0
  /**
   * 获取对象的字段的值,支持多级字段链,如:getFeildValue(party, "parentParty.name") 返回party对象的parentParty字段的name的值
   *
   * @param obj
   * @param fieldChain
   * @return
   */
  public static Object getFieldValue(Object obj, String fieldChain) {

    List<String> fieldNames = StringUtil.split(fieldChain, ".");

    Object retObj = obj;
    for (String fieldName : fieldNames) {
      if (retObj == null) {
        return null;
      }
      retObj = getValue(retObj, fieldName);
    }

    return retObj;
  }
Пример #3
0
  /**
   * 设置某个对象字段的值,支持多级字段链 如:setFieldValue(party, "parentParty.name", "microsoft")
   *
   * @param obj
   * @param fieldChain
   * @param fieldValue
   */
  public static Object setFieldValue(Object obj, String fieldChain, Object fieldValue) {

    List<String> fieldNames = StringUtil.split(fieldChain, ".");

    Object retObj = obj;
    Object lastObj = obj;
    int size = fieldNames.size();

    Class clazz = null;
    String fieldName = null;
    Field field = null;
    Class fieldClass = null;

    for (int i = 0; i < size - 1; i++) {
      fieldName = fieldNames.get(i);
      // log.fine("handle field value: " + fieldName + " of " + lastObj);
      retObj = getValue(lastObj, fieldName);

      // 如果retObj为空,则构造一个新的实例
      if (retObj == null) {
        clazz = lastObj.getClass();
        try {
          field = getField(clazz, fieldName);
          field.setAccessible(true);
          fieldClass = field.getType();
          retObj = fieldClass.newInstance();
          setValue(lastObj, fieldName, retObj);
        } catch (SecurityException e) {
          e.printStackTrace();
          return null;
        } catch (InstantiationException e) {
          e.printStackTrace();
          return null;
        } catch (IllegalAccessException e) {
          e.printStackTrace();
          return null;
        }
      }
      lastObj = retObj;
    }
    if (retObj != null) {
      return setValue(retObj, fieldNames.get(size - 1), fieldValue);
    } else {
      return null;
    }
  }
Пример #4
0
 /**
  * 返回指定类的某个属性的getter方法
  *
  * @param clazz
  * @param propertyName 属性名, 可以是多级属性, 如: partyLocation.location
  * @return
  */
 public static Method getGetter(Class clazz, String propertyName) {
   List<String> props = StringUtil.split(propertyName, ".");
   Class cls = clazz;
   Method m = null;
   for (String prop : props) {
     try {
       m = cls.getMethod(getGetterMethodName(prop), new Class[] {});
       cls = m.getReturnType();
     } catch (NoSuchMethodException ex) {
       try {
         m = cls.getMethod(getBooleanGetterMethodName(prop), new Class[] {});
         cls = m.getReturnType();
       } catch (NoSuchMethodException ne) {
         System.out.println("【ObjectUtil.getGetter】没有这个方法:" + ne.getMessage());
       }
     } catch (SecurityException ex) {
       ex.printStackTrace();
     }
   }
   return m;
 }