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

Add ConfigurationConverter for Java Records to allow Records as complex configuration elements #165

Open
HannesWell opened this issue Sep 2, 2023 · 4 comments

Comments

@HannesWell
Copy link

Record classes available since Java-16 seem to be convenient to model complex configuration elements of a Maven Mojo, if that Plugin requires at least Java-16.

Therefore it would be great to add a ConfigurationConverter that supports records similar to the ObjectWithFieldsConverter or EnumConverter.

It might be already sufficient to have a ObjectWithFieldsConverter that uses 'Constructor'-injection and calls the suitable constructor.
But finding the suitable constructor might be non-trivial. It probably should consider java.lang.reflect.Parameter.getName() (but that is not necessarily present) a reasonable strategy is needed if not all record components are specified and the record has an alternative constructor.

@kwin
Copy link
Contributor

kwin commented Sep 4, 2023

@HannesWell Any idea if that can be supported (e.g. via reflection) while still emitting class files compatible with Java 8? Calling a constructor implicitly for type conversion has been added in eclipse-sisu/sisu.plexus#43 but that isn't sufficient for record support (as that is just supporting a single argument constructor of type String). Would you be able to come up with a PR?

@kwin
Copy link
Contributor

kwin commented Sep 4, 2023

ASM has some support for records: https://asm.ow2.io/javadoc/org/objectweb/asm/RecordComponentVisitor.html. Maybe that can be used for this case or at least some inspiration taken from it.

@HannesWell
Copy link
Author

@HannesWell Any idea if that can be supported (e.g. via reflection) while still emitting class files compatible with Java 8? Calling a constructor implicitly for type conversion has been added in eclipse-sisu/sisu.plexus#43 but that isn't sufficient for record support (as that is just supporting a single argument constructor of type String).

Thanks for the hint.

We could do something like in https://stackoverflow.com/a/67126110 to find the caconical constructor and call it with the element names matched to component-names (passing null for missing ones).
This could probably be done reflectivly or with a Multi-Release jar (though reflectivly ist probably simpler in overall).

Would you be able to come up with a PR?

Sure I can try to provide one (probably in the next days).

@kwin
Copy link
Contributor

kwin commented Sep 5, 2023

The method https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Class.html#getRecordComponents() is only available in Java 16 or above so even that one probably needs to be called via reflection.

@kwin kwin transferred this issue from eclipse-sisu/sisu.plexus Sep 13, 2024
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

2 participants