@Override public Map<String, Object> toConfigNode( Map<Class<?>, Object> object, AnnotatedConfigurableProperty property, BeanVitalizer vitalizer) throws ConfigurationException { Map<String, Object> extensionsMapNode = new TreeMap<String, Object>(); for (Map.Entry<Class<?>, Object> classObjectEntry : object.entrySet()) { Object extensionNode = vitalizer.createConfigNodeFromInstance( classObjectEntry.getValue(), classObjectEntry.getKey()); extensionsMapNode.put(classObjectEntry.getKey().getSimpleName(), extensionNode); } return extensionsMapNode; }
@Override public Map<Class<?>, Object> fromConfigNode( Map<String, Object> configNode, AnnotatedConfigurableProperty property, BeanVitalizer vitalizer, Object parent) throws ConfigurationException { // figure out base extension class Class<Object> extensionBaseClass = null; try { extensionBaseClass = (Class<Object>) property.getTypeForGenericsParameter(1); } catch (ClassCastException e) { throw new ConfigurationException( "Incorrectly annotated extensions field, parameter 1 must be an extension class", e); } Map<Class<?>, Object> extensionsMap = new HashMap<Class<?>, Object>(); for (Map.Entry<String, Object> entry : configNode.entrySet()) { try { // figure out current extension class List<Class<?>> extensionClasses = vitalizer .getContext(ConfigurationManager.class) .getExtensionClassesByBaseClass(extensionBaseClass); Class<?> extensionClass = Extensions.getExtensionClassBySimpleName(entry.getKey(), extensionClasses); // create empty extension bean Object extension = vitalizer.newInstance(extensionClass); // set parent so this field is accessible for use in extension bean's setters SetParentIntoField setParentIntoField = extensionBaseClass.getAnnotation(SetParentIntoField.class); if (setParentIntoField != null) try { PropertyUtils.setSimpleProperty(extension, setParentIntoField.value(), parent); } catch (Exception e) { throw new ConfigurationException( "Could not 'inject' parent object into field specified by 'SetParentIntoField' annotation. Field '" + setParentIntoField.value() + "'", e); } // proceed with deserialization vitalizer.configureInstance( extension, (Map<String, Object>) entry.getValue(), extensionClass); extensionsMap.put(extensionClass, extension); } catch (ClassNotFoundException e) { // noop log.debug("Extension class {} not found", entry.getKey()); } } return extensionsMap; }