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 Object getBeanValue(T t) { BeanMap bm = new BeanMap(t); for (Object propertyName : bm.keySet()) { System.out.println( "Property: " + propertyName + " value : " + bm.get(propertyName) + " property type = " + bm.getType(propertyName.toString())); Class clazz = bm.getType(propertyName.toString()); System.out.println(clazz.getName() + " " + clazz.getSimpleName()); writeCell(null, bm.get(propertyName), clazz.getSimpleName(), ""); } System.out.println(bm); return null; }
/** * 将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; }
/** * Copies all properties from source object to target object. When a Set object is encountered, * though, a new instance of the type {@link HashSet} is created and the content from the target * object is moved to that one before being set into target. * * @param source * @param target */ public static void copyValuesAndSetsFromBeanToBean(Object source, Object target) { BeanMap sourceMap = new BeanMap(source); BeanMap targetMap = new BeanMap(target); new BeanMap(target).putAllWriteable(new BeanMap(source)); for (Object key : targetMap.keySet()) { Object value = sourceMap.get(key); if (targetMap.getWriteMethod((String) key) != null) { if (value instanceof Set) { Set newSet = new HashSet(); Set oldSet = (Set) value; newSet.addAll(oldSet); value = newSet; } targetMap.put(key, value); } } }
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; }