modelmapper / modelmapper-module-record

ModelMapper Module for Record
Apache License 2.0
5 stars 0 forks source link

Caused by: java.lang.NoSuchMethodException #1

Open sergey-morenets opened 1 year ago

sergey-morenets commented 1 year ago

Hi

We tried to use together Java Records (Java 21) and ModelMapper (3.1.1) and added this dependency to our project. However the conversion still fails.

Here's source code:

public class Starter {

    public static void main(String[] args) {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.registerModule(new RecordModule());
        MyClass obj = new MyClass();
        MyRecord record = modelMapper.map(obj, MyRecord.class);

    }

    public static record MyRecord(int i) {}

    public static class MyClass {
        public int i;
    }

}

Here's exception:

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

1) Failed to instantiate instance of destination com.example.demo.Starter$MyRecord. Ensure that com.example.demo.Starter$MyRecord has a non-private no-argument constructor.

1 error
    at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:386)
    at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:80)
    at org.modelmapper.ModelMapper.mapInternal(ModelMapper.java:589)
    at org.modelmapper.ModelMapper.map(ModelMapper.java:422)
    at com.example.demo.Starter.main(Starter.java:14)
Caused by: java.lang.NoSuchMethodException: com.example.demo.Starter$MyRecord.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3761)
    at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2930)
    at org.modelmapper.internal.MappingEngineImpl.instantiate(MappingEngineImpl.java:336)
    at org.modelmapper.internal.MappingEngineImpl.createDestination(MappingEngineImpl.java:351)
    at org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:140)
    at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:114)
    at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:71)
    ... 3 more
Ragilo commented 1 year ago

Only Record -> Pojo is working at the moment. Pojo -> Record doesn't work yet.

okaponta commented 9 months ago

for detail, please see https://github.com/modelmapper/modelmapper/issues/546#issuecomment-1068925341

Hwan0518 commented 1 week ago

Do you support Pojo -> Record now?