Skip to content

Add support for Java records (JEP 395, Java 16+)#126

Merged
phax merged 2 commits intophax:recordsfrom
joelittlejohn:java-records
Feb 3, 2026
Merged

Add support for Java records (JEP 395, Java 16+)#126
phax merged 2 commits intophax:recordsfrom
joelittlejohn:java-records

Conversation

@joelittlejohn
Copy link
Copy Markdown
Contributor

This commit adds:

  • EClassType.RECORD for record type declarations
  • JRecordComponent class to represent record components
  • _record() methods in IJClassContainer to create records
  • recordComponent() and recordComponentVararg() methods in JDefinedClass
  • compactConstructor() for validation-only constructors
  • isRecord() helper method
  • JDocComment.addParam(JRecordComponent) for javadoc support

Tests cover: basic records, empty records, object/array/ varargs components, generics, annotations, compact and canonical constructors, methods, static members, nested records, and javadoc.

This commit adds:

- EClassType.RECORD for record type declarations
- JRecordComponent class to represent record components
- _record() methods in IJClassContainer to create records
- recordComponent() and recordComponentVararg() methods in JDefinedClass
- compactConstructor() for validation-only constructors
- isRecord() helper method
- JDocComment.addParam(JRecordComponent) for javadoc support

Tests cover: basic records, empty records, object/array/
varargs components, generics, annotations, compact and canonical
constructors, methods, static members, nested records, and javadoc.
@phax phax self-assigned this Feb 1, 2026
@joelittlejohn
Copy link
Copy Markdown
Contributor Author

Claude wrote most of this code. I guided claude to stick with project conventions, and also guided the strategy for how records should fit into the current project.

You'll see that this implementation follows a similar pattern to other kinds of class (e.g. enum), where we don't define a new JRecord but instead this just a new class type, and in this kind of class has record 'components' (as per https://openjdk.org/jeps/395). Feedback and ideas on this very welcome.

I'm thinking I will migrate jsonschema2pojo to use this library so this PR will help me with supporting records there. Sealed types would be next.

- Change from ValueEnforcer to explicit IllegalStateException throws
- Fix missing semicolons in throw statements
- Fix error message in recordComponentVararg()
- Add tests for calling record methods on non-record classes
- Add test for duplicate compact constructor
@joelittlejohn
Copy link
Copy Markdown
Contributor Author

Added a new commit here with some additional tests for unhappy paths and exception cases. I'll rebase all this into one clean commit once we're happy to merge.

@phax
Copy link
Copy Markdown
Owner

phax commented Feb 3, 2026

Thanks - looks pretty good :)

@phax phax merged commit 680bff9 into phax:records Feb 3, 2026
1 check passed
This was referenced Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants