Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modelmapper throws java.lang.IllegalArgumentException: object is not an instance of declaring class #198

Closed
wrssmi opened this issue Jan 26, 2017 · 4 comments
Labels

Comments

@wrssmi
Copy link

wrssmi commented Jan 26, 2017

Following Testcode:

public class MapTest{

    public static void main(String[] args) {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.addMappings(new PropertyMap<A, B>() {
            @Override
            protected void configure() {
                map().setDiff(ServiceUtil.getDaysBetween(source.getFrom(), source.getTo()));
            }
        });
        A a = new A(LocalDate.now(), LocalDate.now());
        B b = modelMapper.map(a, B.class);
    }

    public static int getDaysBetween(LocalDate von, LocalDate bis) {
        if (von != null && bis != null) {
            return Period.between(von, bis).getDays();
        }
        return 0;
    }
}

with following Objects:

public class A
{
    LocalDate from;

    LocalDate to;

    public A(LocalDate from, LocalDate to) {
        this.from = from;
        this.to = to;
    }

    public LocalDate getFrom() {
        return from;
    }

    public void setFrom(LocalDate from) {
        this.from = from;
    }

    public LocalDate getTo() {
        return to;
    }

    public void setTo(LocalDate to) {
        this.to = to;
    }
}
public class B
{

    int diff;

    public B()
    {

    }

    public void setDiff(int diff) {
        this.diff = diff;
    }

    public int getDiff() {
        return diff;
    }
}

throws following Exception:
Exception in thread "main" org.modelmapper.MappingException: ModelMapper mapping errors:

  1. Error mapping A to B

1 error
at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:374)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:69)
at org.modelmapper.ModelMapper.mapInternal(ModelMapper.java:497)
at org.modelmapper.ModelMapper.map(ModelMapper.java:340)
at MapTest.main(MapTest.java:31)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.modelmapper.MappingException: ModelMapper mapping errors:

  1. Failed to get value from A.getTo()

1 error
at org.modelmapper.internal.Errors.toMappingException(Errors.java:258)
at org.modelmapper.internal.PropertyInfoImpl$MethodAccessor.getValue(PropertyInfoImpl.java:100)
at org.modelmapper.internal.MappingEngineImpl.resolveSourceValue(MappingEngineImpl.java:189)
at org.modelmapper.internal.MappingEngineImpl.propertyMap(MappingEngineImpl.java:156)
at org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:131)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:92)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:60)
... 8 more
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
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.modelmapper.internal.PropertyInfoImpl$MethodAccessor.getValue(PropertyInfoImpl.java:95)
... 13 more

@wrssmi wrssmi changed the title calc Modelmapper throws java.lang.IllegalArgumentException: object is not an instance of declaring class Jan 26, 2017
@wrssmi wrssmi closed this as completed Jan 26, 2017
@wrssmi wrssmi reopened this Jan 26, 2017
@chhsiao90
Copy link
Member

@wrssmi - It seems that PropertyMap only support simple getter/setter method.
The implementation about how a PropertyMap work is based on java reflection and it cannot handle for calculation or converting.

The name PropertyMap implied that it was only used for mapping,
So I think you should use converters for converting!

@wrssmi
Copy link
Author

wrssmi commented Jan 26, 2017

Thank you for that hint. following is working.

public static void main(String[] args) {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.addMappings(new PropertyMap<A, B>() {
            @Override
            protected void configure() {
                using(new Converter<A, Integer>() {
                    public Integer convert(MappingContext<A, Integer> context) {
                        return context.getSource() == null ? null : Period.between(context.getSource().getFrom(),
                              context.getSource().getTo()).getDays();
                    }
                }).map(source).setDiff(0);
            }
        });
        A a = new A(LocalDate.now(), LocalDate.now().plusDays(10));
        B b = modelMapper.map(a, B.class);
        System.out.println(b.diff);
    }

@chhsiao90
Copy link
Member

Wow, the code that used PropertyMap with converter looks great!
Hadn't used that before.

@mbm56767
Copy link

mbm56767 commented Feb 1, 2018

you can simply put the conversion logic outside of property map and simply use the computed value inside mappings. This way things will be easy to maintain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants