/Map2BeanTransformer.patch Secret
Last active
August 29, 2015 14:20
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/magnolia-configuration/src/main/java/info/magnolia/config/map2bean/Map2BeanTransformer.java b/magnolia-configuration/src/main/java/info/magnolia/config/map2bean/Map2BeanTransformer.java | |
| index 1e28f7c..a86aef4 100644 | |
| --- a/magnolia-configuration/src/main/java/info/magnolia/config/map2bean/Map2BeanTransformer.java | |
| +++ b/magnolia-configuration/src/main/java/info/magnolia/config/map2bean/Map2BeanTransformer.java | |
| @@ -58,7 +58,8 @@ import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| /** | |
| - * Transforms a map to a bean graph and does some special features like sub-classing over declarations. | |
| + * Transforms a map to a bean graph. Resolves types through {@link TypeMapping} and {@link PreConfiguredBeanUtils} | |
| + * to mimic what Node2BeanTransformer does. | |
| */ | |
| public class Map2BeanTransformer { | |
| @@ -133,16 +134,16 @@ public class Map2BeanTransformer { | |
| return (T) readOutList((Collection<Object>) source, mapping.getTypeDescriptor(Object.class)); | |
| } | |
| Map<String, Object> sourceMap = (Map<String, Object>) source; | |
| - TypeDescriptor targetTypeDescriptor = findClass(sourceMap, defaultTargetTypeDescriptor); | |
| + TypeDescriptor targetTypeDescriptor = resolveType(sourceMap, defaultTargetTypeDescriptor); | |
| + targetTypeDescriptor = resolveType(sourceMap, targetTypeDescriptor); | |
| if (targetTypeDescriptor.getType() == Object.class || targetTypeDescriptor.isMap()) { | |
| return (T) readOutMap(sourceMap, mapping.getTypeDescriptor(Object.class)); | |
| } else { | |
| - return (T) readOutObject(sourceMap, targetTypeDescriptor.getType()); | |
| + return (T) readOutObject(sourceMap, targetTypeDescriptor); | |
| } | |
| } | |
| - private <T> T readOutObject(Map<String, Object> sourceMap, Class<T> defaultTargetType) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException, ConfigurationParsingException { | |
| - TypeDescriptor targetType = findClass(sourceMap, mapping.getTypeDescriptor(defaultTargetType)); | |
| + private <T> T readOutObject(Map<String, Object> sourceMap, TypeDescriptor targetType) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException, ConfigurationParsingException { | |
| T targetObj = (T) componentProvider.newInstance(targetType.getType()); | |
| Map<String, String> targetDescription = beanUtils.describe(targetObj); | |
| @@ -156,6 +157,7 @@ public class Map2BeanTransformer { | |
| } | |
| if (!targetDescription.containsKey(sourcePropertyName)) { | |
| + // TODO is this the correct exception ? | |
| throw new RuntimeException("Property " + sourcePropertyName + " not found in bean " + targetObj); | |
| } | |
| @@ -211,39 +213,38 @@ public class Map2BeanTransformer { | |
| return targetMap; | |
| } | |
| - private TypeDescriptor findClass(Map<String, Object> map, TypeDescriptor defaultType) throws ConfigurationParsingException { | |
| - TypeDescriptor targetType = null; | |
| - | |
| - // 1. Resolve target type from configured class property | |
| + private TypeDescriptor resolveType(Map<String, Object> map, TypeDescriptor defaultType) throws ConfigurationParsingException { | |
| + final TypeDescriptor targetType; | |
| + // 1. Resolve target type from configured class property in the map, fallback to defaultType otherwise | |
| if (map.containsKey("class")) { | |
| String className = (String) map.get("class"); | |
| ClassFactory classFactory = Classes.getClassFactory(); | |
| try { | |
| Class<?> loadClass = classFactory.forName(className); | |
| - loadClass.isInterface(); | |
| targetType = mapping.getTypeDescriptor(loadClass); | |
| } catch (ClassNotFoundException e) { | |
| throw new ConfigurationParsingException("The classname [" + className + "] in the class attribute could not be found in the classpath", e); | |
| } | |
| - } | |
| - | |
| - // 2. Or from defaultType if no class property is configured | |
| - if (targetType == null) { | |
| + } else { | |
| targetType = defaultType; | |
| } | |
| - // 3. Get implementation according to type mappings | |
| + // 2. Get implementation according to type mappings | |
| + final TypeDescriptor mappedType; | |
| try { | |
| - targetType = mapping.getTypeDescriptor(componentProvider.getImplementation(targetType.getType())); | |
| + // ComponentProvider.getImplementation is actually where the type mapping happens (based on info from module descriptors) | |
| + // TypeMapping.getTypeDescriptor() _just_ gets us a descriptor of the given class, unlike its name suggests | |
| + final Class<?> implClass = componentProvider.getImplementation(targetType.getType()); | |
| + mappedType = mapping.getTypeDescriptor(implClass); | |
| } catch (ClassNotFoundException e) { | |
| - throw new ConfigurationParsingException("The classname [" + defaultType.getType() + "] in the class attribute could not be found in the classpath", e); | |
| + throw new ConfigurationParsingException("We don't know the implementation type to be used for [" + targetType.getType() + "]", e); | |
| } | |
| - // 4. If componentProvider has no mapping, we have a few defaults here ... | |
| - if (DEFAULT_TYPES.containsKey(targetType.getType())) { | |
| - return mapping.getTypeDescriptor(DEFAULT_TYPES.get(targetType.getType())); | |
| + // 3. If componentProvider has no mapping, we have a few defaults here ... | |
| + if (DEFAULT_TYPES.containsKey(mappedType.getType())) { | |
| + return mapping.getTypeDescriptor(DEFAULT_TYPES.get(mappedType.getType())); | |
| } | |
| - return targetType; | |
| + return mappedType; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment