Skip to content

Instantly share code, notes, and snippets.

@chhsiao90
Last active August 2, 2018 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chhsiao90/b777d0468852837bac8060d5c4d7f81a to your computer and use it in GitHub Desktop.
Save chhsiao90/b777d0468852837bac8060d5c4d7f81a to your computer and use it in GitHub Desktop.
public <T extends CustomParserConfig> T getConfig(Map<String,Object> configObject, Class<T> classType) {
ModelMapper modelMapper = new ModelMapper();
modelMapper.createTypeMap(configObject, classType).addMappings(new PropertyMap<Map<String,Object>, T>() {
@Override
protected void configure() {
map(source("fields"), destination("fields"));
}
}).map(configObject);
}
@irshadgit
Copy link

irshadgit commented Aug 1, 2018

@chhsiao90 : It gives me compile error first time near PropertyMap<Map<String,Object>>() saying wrong number of type arguments. When I replaced it as below

public static <T extends CustomParserConfig> T getConfig(Map<String, Object> configObject, Class<T> classType) {
        ModelMapper modelMapper = new ModelMapper();

        return modelMapper.createTypeMap(configObject, classType).addMappings(new PropertyMap<Map<String,Object>, T>() {
            @Override
            protected void configure() {
                map(source("fields"), destination("fields"));
            }
        }).map(configObject);
}

compile error goes but I get another error while executing it as below.

1) Failed to instantiate instance of destination java.util.List. Ensure that java.util.List has a non-private no-argument constructor.

1 error

	at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:380)
	at org.modelmapper.internal.TypeMapImpl.map(TypeMapImpl.java:190)
	at com.prevalent.parsers.utils.parserconfig.CustomParserConfigUtils.getConfig(CustomParserConfigUtils.java:17)
	at com.prevalent.parsers.ad.ADRegxParser.configure(ADRegxParser.java:79)
	at com.prevalent.parsers.ad.ADRegxParserTest.parseProperConfig(ADRegxParserTest.java:65)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.NoSuchMethodException: java.util.List.<init>()
	at java.lang.Class.getConstructor0(Class.java:3082)
	at java.lang.Class.getDeclaredConstructor(Class.java:2178)
	at org.modelmapper.internal.MappingEngineImpl.instantiate(MappingEngineImpl.java:326)
	at org.modelmapper.internal.MappingEngineImpl.createDestination(MappingEngineImpl.java:341)
	at org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:131)
	at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:113)
	at org.modelmapper.internal.MappingEngineImpl.setDestinationValue(MappingEngineImpl.java:238)
	at org.modelmapper.internal.MappingEngineImpl.propertyMap(MappingEngineImpl.java:184)
	at org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:148)
	at org.modelmapper.internal.TypeMapImpl.map(TypeMapImpl.java:185)
	... 26 more


Process finished with exit code 255

@chhsiao90
Copy link
Author

chhsiao90 commented Aug 2, 2018

hi @irshadgit - You are right, the workaround didn't work with v2.1.0 because some other issues.

@chhsiao90
Copy link
Author

After more investigation, the workaround is not working.

For more detail:
map(source("fields"), destination("fields")) will create a mapping Map[fields] -> CustomParserConfig[fields] (Object -> List) and ModelMapper failed to map because it think Map.fields is an object instead of a list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment