Пример #1
0
  protected void displayResults(Object result, int level) {
    if (null == result) return;

    String prefix = StringUtils.repeat(" ", level * 2) + " * ";

    if (Collection.class.isAssignableFrom(result.getClass())) {
      @SuppressWarnings("unchecked")
      Collection<Object> coll = Collection.class.cast(result);

      for (Object o : coll) displayResults(o, 1 + level);

      return;
    } else if ("java.lang".equals(result.getClass().getPackage().getName())) {
      getLog()
          .info(
              prefix
                  + CredentialsUtil.redact("" + result)
                  + " [class: "
                  + result.getClass().getSimpleName()
                  + "]");

      return;
    }

    BeanMap beanMap = new BeanMap(result);

    for (Iterator<?> itProperty = beanMap.keyIterator(); itProperty.hasNext(); ) {
      String propertyName = "" + itProperty.next();
      Object propertyValue = beanMap.get(propertyName);

      if ("class".equals(propertyName)) continue;

      if (null == propertyValue) continue;

      Class<?> propertyClass = null;

      try {
        propertyClass = beanMap.getType(propertyName);
      } catch (Exception e) {
        getLog().warn("Failure on property " + propertyName, e);
      }

      if (null == propertyClass) {
        getLog().info(prefix + propertyName + ": " + CredentialsUtil.redact("" + propertyValue));
      } else {
        getLog()
            .info(
                prefix
                    + propertyName
                    + ": "
                    + CredentialsUtil.redact("" + propertyValue)
                    + " [class: "
                    + propertyClass.getSimpleName()
                    + "]");
      }
    }
  }
  public CaseXml mapFromDomainObject(T careCase) {
    CaseXml ccCase = new CaseXml();
    try {
      BeanUtils.copyProperties(ccCase, careCase);

      BeanMap beanMap = new BeanMap(careCase);
      removeStaticProperties(beanMap);

      Map<String, String> valueMap = new HashMap<String, String>();
      while (beanMap.keyIterator().hasNext()) {
        valueMap.put(
            (String) beanMap.keyIterator().next(),
            (String) beanMap.get((String) beanMap.keyIterator().next()));
      }
      ccCase.setFieldValues(valueMap);

    } catch (Exception e) {
      LOG.error(e.getMessage(), e);
    }

    return ccCase;
  }
Пример #3
0
 /**
  * 将Bean对象转换成Map对象,将忽略掉值为null或size=0的属性
  *
  * @param obj 对象
  * @return 若给定对象为null则返回size=0的map对象
  */
 public static Map<String, Object> toMap(Object obj) {
   Map<String, Object> map = new HashMap<String, Object>();
   if (obj == null) {
     return map;
   }
   BeanMap beanMap = new BeanMap(obj);
   Iterator<String> it = beanMap.keyIterator();
   while (it.hasNext()) {
     String name = it.next();
     Object value = beanMap.get(name);
     // 转换时会将类名也转换成属性,此处去掉
     if (value != null && !name.equals("class")) {
       map.put(name, value);
     }
   }
   return map;
 }