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

Caused by: java.lang.NoSuchMethodException #1

Open
sergey-morenets opened this issue Oct 5, 2023 · 3 comments
Open

Caused by: java.lang.NoSuchMethodException #1

sergey-morenets opened this issue Oct 5, 2023 · 3 comments

Comments

@sergey-morenets
Copy link

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
Copy link

Ragilo commented Oct 25, 2023

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

@okaponta
Copy link

for detail, please see
modelmapper/modelmapper#546 (comment)

@Hwan0518
Copy link

Hwan0518 commented Nov 1, 2024

Do you support Pojo -> Record now?

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

No branches or pull requests

4 participants