Пример #1
0
  public static <T extends BeanWrapApi> T build(Class<T> beanClass, Object source) {
    Class basicClass = BasicEntity.class;
    Class sourceClass = source.getClass();
    T bean;

    try {
      Constructor constructor = beanClass.getConstructor(sourceClass);
      bean = (T) constructor.newInstance(source);
    } catch (Exception ignore) {
      try {
        bean = beanClass.newInstance();
      } catch (Exception e) {
        return null;
      }
    }

    bean.preHandle();

    for (Field field : beanClass.getFields()) {
      String name = field.getName();
      try {
        Field sourceField = sourceClass.getDeclaredField(name);
        sourceField.setAccessible(true);
        //                Field sourceField = sourceClass.getField(name);
        if (sourceField != null) {
          field.set(bean, sourceField.get(source));
        }
      } catch (Exception e) {
        // Logger.error(e, "Error in BeanUtils");
      }
    }

    BasicBean bb = (BasicBean) bean;
    BasicEntity be = (BasicEntity) source;

    bb.id = be.getId();
    bb.isDeleted = be.isDeleted();
    bb.createdTime = be.getCreatedTime();
    bb.updatedTime = be.getUpdatedTime();

    bean.postHandle(source);

    return bean;
  }