/** {@inheritDoc} */ public Object populate(String entityName, Map<String, Object> params) { Type type = Model.getType(entityName); if (null == type) { throw new RuntimeException(entityName + " was not configured!"); } else { return populate(type.newInstance(), type.getName(), params); } }
/** * {@inheritDoc} 初始化对象指定路径的属性。<br> * 例如给定属性a.b.c,方法会依次检查a a.b a.b.c是否已经初始化 */ public ObjectAndType initProperty(final Object target, String entityName, final String attr) { Object propObj = target; Object property = null; int index = 0; String[] attrs = Strings.split(attr, "."); Type type = Model.getType(entityName); while (index < attrs.length) { try { property = PropertyUtils.getProperty(propObj, attrs[index]); Type propertyType = type.getPropertyType(attrs[index]); // 初始化 if (null == propertyType) { logger.error("Cannot find property type [{}] of {}", attrs[index], propObj.getClass()); throw new RuntimeException( "Cannot find property type " + attrs[index] + " of " + propObj.getClass().getName()); } if (null == property) { property = propertyType.newInstance(); try { PropertyUtils.setProperty(propObj, attrs[index], property); } catch (NoSuchMethodException e) { // Try fix jdk error for couldn't find correct setter when object's Set required type is // diffent with Get's return type declared in interface. Method setter = Reflections.getSetter(propObj.getClass(), attrs[index]); if (null != setter) setter.invoke(propObj, property); else throw e; } } index++; propObj = property; type = propertyType; } catch (Exception e) { throw new RuntimeException(e); } } return new ObjectAndType(property, type); }