Пример #1
0
 public static Class getNestedPropertyType(DynaClass bean, String name)
     throws IllegalAccessException, InvocationTargetException, NoSuchMethodException,
         ClassNotFoundException, NoSuchFieldException {
   // Resolve nested references
   while (resolver.hasNested(name)) {
     String next = resolver.next(name);
     if (resolver.isIndexed(next) || resolver.isMapped(next)) {
       String property = resolver.getProperty(next);
       Class<?> clazz = Class.forName(bean.getName());
       Class<?> detectTypeParameter =
           detectTypeParameter(clazz, property, resolver.isIndexed(name) ? 0 : 1);
       bean = WrapDynaClass.createDynaClass(detectTypeParameter);
       return getNestedPropertyType(bean, resolver.remove(name));
     }
     DynaProperty db = bean.getDynaProperty(next);
     bean = WrapDynaClass.createDynaClass(db.getType());
     name = resolver.remove(name);
   }
   if (resolver.isMapped(name) || resolver.isIndexed(name)) {
     String property = resolver.getProperty(name);
     Class<?> clazz = Class.forName(bean.getName());
     return detectTypeParameter(clazz, property, resolver.isIndexed(name) ? 0 : 1);
   }
   Class<?> type = bean.getDynaProperty(name).getType();
   return type;
 }
  /**
   * Populate the target object with the source object
   *
   * @param targetObject the target object
   * @param sourceObject the source object
   */
  public static void buildObjectWithoutReferenceFields(Object targetObject, Object sourceObject) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(targetObject.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();

    for (DynaProperty property : properties) {
      ObjectUtil.setProperty(targetObject, sourceObject, property, true);
    }
  }
  /**
   * Populate the target object with the source object
   *
   * @param targetObject the target object
   * @param sourceObject the source object
   */
  public static void buildObject(Object targetObject, Object sourceObject) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(targetObject.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();

    for (DynaProperty property : properties) {
      ObjectUtil.setProperty(targetObject, sourceObject, property, false);
    }
  }
  /**
   * determine if the source object has a field with null as its value
   *
   * @param sourceObject the source object
   */
  public static boolean hasNullValueField(Object sourceObject) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(sourceObject.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();

    for (DynaProperty property : properties) {
      String propertyName = property.getName();

      if (PropertyUtils.isReadable(sourceObject, propertyName)) {
        try {
          Object propertyValue = PropertyUtils.getProperty(sourceObject, propertyName);
          if (propertyValue == null) {
            return true;
          }
        } catch (Exception e) {
          LOG.info(e);
          return false;
        }
      }
    }
    return false;
  }
  /**
   * build a map of business object with its specified property names and corresponding values
   *
   * @param businessObject the given business object
   * @param the specified fields that need to be included in the return map
   * @return the map of business object with its property names and values
   */
  public static Map<String, Object> buildPropertyMap(Object object, List<String> keyFields) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(object.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();
    Map<String, Object> propertyMap = new LinkedHashMap<String, Object>();

    for (DynaProperty property : properties) {
      String propertyName = property.getName();

      if (PropertyUtils.isReadable(object, propertyName) && keyFields.contains(propertyName)) {
        try {
          Object propertyValue = PropertyUtils.getProperty(object, propertyName);

          if (propertyValue != null && !StringUtils.isEmpty(propertyValue.toString())) {
            propertyMap.put(propertyName, propertyValue);
          }
        } catch (Exception e) {
          LOG.info(e);
        }
      }
    }
    return propertyMap;
  }
Пример #6
0
 private WrapDynaClass getDynaClass(Object reference) {
   if (dynaClass == null && reference != null) {
     dynaClass = WrapDynaClass.createDynaClass(reference.getClass());
   }
   return dynaClass;
 }
Пример #7
0
 public ListContainer(Class<T> type) {
   backingList = new ArrayList<T>();
   dynaClass = WrapDynaClass.createDynaClass(type);
 }
Пример #8
0
 public ListContainer(Class<T> type, Collection<T> backingList) {
   dynaClass = WrapDynaClass.createDynaClass(type);
   setCollection(backingList);
 }