-
Notifications
You must be signed in to change notification settings - Fork 22
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
Mapping collection with specific implementation #140
Comments
Hm, I will have a look. Are you sure about the type ReMap tries to set using the set-method mentioned in the stacktrace? You said
Could it be that ReMap creates something else than a LinkedHashSet? Otherwise the error makes no sense... confused :D |
Yeah, I'm sorry, it creates a HashSet, will update the orignal description. This issue is that it creates a HashSet and calls a method that requires a LinkedHashSet as argument. It's in |
I think perhaps a solution could be to do something in the line of (little cleanup needed, perhaps create a getCollectionSupplier() method) in
|
Yeah I thought about something like this, too. Since the JavaDoc for
Collection class says something like "every collection should provide a
default constructor" we're not that hacky as I thought initially :D
And you're handling the error case adequately.
Would you like to do a PR? 🤓
Jens Teglhus Møller <notifications@github.com> schrieb am Mi., 27. Jan.
2021, 16:59:
… I think perhaps a solution could be to do something in the line of (little
cleanup needed, perhaps create a getCollectionSupplier() medthod) in
ReflectionUtil:
static Collector getCollector(Class<? extends Collection> collectionType) {
final Supplier<Collection> collectionSupplier = () -> {
try {
return collectionType.newInstance();
} catch (Exception e) {
throw new RuntimeException("No default constructor for " + collectionType, e);
}
};
if (Set.class.isAssignableFrom(collectionType)) {
return Collectors.toCollection(collectionSupplier);
} else if (List.class.isAssignableFrom(collectionType)) {
return Collectors.toCollection(collectionSupplier);
} else {
throw MappingException.unsupportedCollection(collectionType);
}
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#140 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADNFVQQLSPKFIZ37MVYDY43S4AZ6NANCNFSM4WVEXZOQ>
.
|
I can have a go at it. I'm a little unsure about error handling (just rethrowing instantiation exceptions as a RuntimeException) and if you are ok with using newInstance(), it comes up as deprecated since java 9. |
Cool! Does this help? |
Hi
I had the need for a set with order retained, so I created beans that used the specific LinkedHashSet implementation instead of the Set interface.
This resulted in a runtime mapping error, since remap internally creates a HashSet and calls the setter, this leads to an IllegalArgumentException: argument type mismatch (it took me a while in the debugger to actually figure it out).
Example that fails (I use lombok for brevity):
with
I worked around it by adding the following to the mapper:
.useMapper(TypeMapping.from(LinkedHashSet.class).to(LinkedHashSet.class).applying(x -> x))
But this means that I don't get a copy of the collection, which I can live with in my use case.
But, question is if there should be some extra checks to ensure that the used collection types are assignable or perhaps smart code that will use the destination type and instantiate the "correct" type instead of a HashSet.
Best regards Jens
The text was updated successfully, but these errors were encountered: