Exemple #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);
    }
  }
Exemple #4
0
  /**
   * Test DynaClass of a RemoteBean.
   *
   * @throws Exception
   */
  public void testGetDynaClass() throws Exception {
    MyBean sampleBean = new MyBean();

    ServerContext serverContext =
        new ServerContextImpl(sampleBean, sm, new OurHierarchicalRegistry());

    OddjobMBean test =
        new OddjobMBean(
            sampleBean, OddjobMBeanFactory.objectName(0), new OurServerSession(), serverContext);

    DynaClass dc = (DynaClass) test.invoke("getDynaClass", new Object[] {}, new String[] {});

    assertNotNull(dc);
    DynaProperty dp = dc.getDynaProperty("fruit");
    assertEquals(String.class, dp.getType());
  }
  /** {@inheritDoc} */
  public String toString() {
    StringBuffer result = new StringBuffer();
    DynaClass type = getDynaClass();
    DynaProperty[] props = type.getDynaProperties();

    result.append(type.getName());
    result.append(": ");
    for (int idx = 0; idx < props.length; idx++) {
      if (idx > 0) {
        result.append(", ");
      }
      result.append(props[idx].getName());
      result.append(" = ");
      result.append(get(props[idx].getName()));
    }
    return result.toString();
  }
  /**
   * 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;
  }