vadbch / orika

Automatically exported from code.google.com/p/orika
0 stars 0 forks source link

Usage of .constructorA/B results in NullPointerException #132

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
Whenever I want to use .constructorA or .constructorB within the class map 
definition I get a NullPointerException. Test case is attached to this issue.

ma.glasnost.orika.MappingException: exception while creating object factory for 
TestCase$Person2
    at ma.glasnost.orika.impl.generator.ObjectFactoryGenerator.build(ObjectFactoryGenerator.java:107)
    at ma.glasnost.orika.impl.DefaultMapperFactory.lookupObjectFactory(DefaultMapperFactory.java:825)
    at ma.glasnost.orika.impl.DefaultMapperFactory.lookupObjectFactory(DefaultMapperFactory.java:764)
    at ma.glasnost.orika.impl.DefaultMapperFactory.lookupObjectFactory(DefaultMapperFactory.java:751)
    at ma.glasnost.orika.impl.DefaultMapperFactory.buildObjectFactories(DefaultMapperFactory.java:1065)
    at ma.glasnost.orika.impl.DefaultMapperFactory.build(DefaultMapperFactory.java:1003)
    at ma.glasnost.orika.impl.DefaultMapperFactory.getMapperFacade(DefaultMapperFactory.java:1262)
    at ma.glasnost.orika.impl.DefaultMapperFactory.getMapperFacade(DefaultMapperFactory.java:1250)
    at ma.glasnost.orika.impl.DefaultMapperFactory.getMapperFacade(DefaultMapperFactory.java:1275)
    at TestCase.test(TestCase.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.NullPointerException
    at ma.glasnost.orika.constructor.SimpleConstructorResolverStrategy.resolve(SimpleConstructorResolverStrategy.java:64)
    at ma.glasnost.orika.impl.generator.ObjectFactoryGenerator.addSourceClassConstructor(ObjectFactoryGenerator.java:161)
    at ma.glasnost.orika.impl.generator.ObjectFactoryGenerator.addCreateMethod(ObjectFactoryGenerator.java:124)
    at ma.glasnost.orika.impl.generator.ObjectFactoryGenerator.build(ObjectFactoryGenerator.java:95)
    ... 35 more

What is the expected output? What do you see instead?
According to the user documentation the test case should work.

What version of the product are you using? 
1.4.3

Original issue reported on code.google.com by maxim.mo...@googlemail.com on 14 Oct 2013 at 6:58

Attachments:

GoogleCodeExporter commented 8 years ago
A fix has been applied to the latest master. 
With the fix in place, there's a slight adjustment required to make this work:

mapperFactory.classMap(Person1.class, Person2.class)
                .fieldAToB("firstname", "fn")
                .fieldBToA("forename","firstname")
                .register();

Original comment by matt.deb...@gmail.com on 28 Oct 2013 at 4:20

GoogleCodeExporter commented 8 years ago
I think I spotted a similar issue in Orkia 1.4.4.

My classes are:
public class A {
    public A(String firstname) {
    }
}

public class B {
    public A(String fn) {
    }
}

At first I did not have classmap and as expected no automatic mapping could be 
performed. Which resulted in the following stacktrace:
Caused by: java.lang.NullPointerException
    at ma.glasnost.orika.impl.generator.ObjectFactoryGenerator.addSourceClassConstructor(ObjectFactoryGenerator.java:168)
    at ma.glasnost.orika.impl.generator.ObjectFactoryGenerator.addCreateMethod(ObjectFactoryGenerator.java:123)
    at ma.glasnost.orika.impl.generator.ObjectFactoryGenerator.build(ObjectFactoryGenerator.java:94)

With the following mapping, the mapping works (I only needed one way):

mapperFactory.classMap(A.class, B.class)
                .field("firstname", "fn")
                .register();

Exception is caused by the following statement in ObjectFactoryGenerator (line 
168):

                if (properties.size() != constructorArguments.length) {
                    throw new MappingException("While attempting to generate ObjectFactory using constructor '" + constructor
                            + "', an automatic mapping of the source type ('" + sourceType
                            + "') to this constructor call could not be determined. Please "
                            + "register a custom ObjectFactory implementation which is able to create an instance of '" + destinationType
                            + "' from an instance of '" + sourceType + "'.");
                }

The constructorArguments is null and thus the .length causes the exception, 
instead of real exception to be thrown.

Original comment by villg...@gmail.com on 30 Mar 2014 at 7:01