Skip to content

Implement JEP 513: Flexible Constructor Bodies#4903

Open
rpx99 wants to merge 12 commits into
javaparser:masterfrom
rpx99:claude/jep-513-flexible-constructor-bodies-018bgqTy8mGuzH89FWymTHnM
Open

Implement JEP 513: Flexible Constructor Bodies#4903
rpx99 wants to merge 12 commits into
javaparser:masterfrom
rpx99:claude/jep-513-flexible-constructor-bodies-018bgqTy8mGuzH89FWymTHnM

Conversation

@rpx99

@rpx99 rpx99 commented Nov 21, 2025

Copy link
Copy Markdown
Contributor

Summary

This PR implements JEP 513: Flexible Constructor Bodies for Java 25, which allows statements to appear before explicit constructor invocations (super() or this() calls).

Changes

Grammar

  • Modified ConstructorDeclaration production to allow statements in any order
  • Added ExplicitConstructorInvocation as a valid BlockStatement
  • Constructor invocations can now appear anywhere in the constructor body, not just at the beginning

Validation

  • Created FlexibleConstructorValidator to enforce JEP 513 rules:
    • Statements before super()/this() (prologue) cannot reference the current instance (this)
    • Only one super()/this() call is allowed per constructor
  • Added Java25Validator with the flexible constructor validator
  • Added Java25PostProcessor

Configuration

  • Added JAVA_25 language level to ParserConfiguration
  • Updated BLEEDING_EDGE constant to JAVA_25
  • Added JAVA_25 to yield support array

Tests

Created FlexibleConstructorTest with 10 comprehensive test cases:

Valid cases:

  • ✅ Validation before super() call
  • ✅ Field initialization before super()
  • ✅ Multiple statements before super()
  • ✅ Statements before this() call
  • ✅ Statements after super() call
  • ✅ Constructor without explicit invocation

Invalid cases:

  • this reference in prologue
  • this method call in prologue
  • ❌ Multiple explicit constructor calls
  • this in field initializer before super

All tests pass successfully.

Related

claude and others added 3 commits November 21, 2025 13:14
This implementation adds support for JEP 513, which allows statements
to appear before explicit constructor invocations (super/this calls).

Grammar changes:
- Modified ConstructorDeclaration to parse all statements normally
- Added ExplicitConstructorInvocation as a valid BlockStatement
- ExplicitConstructorInvocation can now appear anywhere in constructor body

Validation:
- Created FlexibleConstructorValidator to enforce JEP 513 rules:
  * Statements before super/this (prologue) cannot reference 'this'
  * Only one super/this call allowed per constructor
- Added Java25Validator with FlexibleConstructorValidator
- Added Java25PostProcessor

Configuration:
- Added JAVA_25 language level to ParserConfiguration
- Updated BLEEDING_EDGE to JAVA_25
- Added JAVA_25 to yieldSupport array

Tests:
- Created FlexibleConstructorTest with 11 test cases:
  * Valid: validation before super, field init before super, etc.
  * Invalid: this reference in prologue, multiple explicit calls, etc.

Implements: https://openjdk.org/jeps/513
This implementation adds support for JEP 512 (Compact Source Files) and
Instance Main Methods for Java 25.

Grammar changes:
- Modified CompilationUnit to parse top-level methods/fields
- Created CompactClassMember production for fields and methods
- Top-level members are wrapped in an implicit compact class
- Used LOOKAHEAD(3) to distinguish between fields and methods

AST changes:
- Added isCompact field to ClassOrInterfaceDeclaration
- Override getNameAsString() to return empty string for compact classes
- Compact class uses placeholder name "$CompactClass" internally

Validation:
- Created CompactClassValidator for JEP 512 rules
- Validates compact class restrictions (no extends/implements/abstract)
- Validates flexible main method signatures:
  * Return type: void or int
  * Parameters: none or String[]
  * Can be static or instance
  * Can be public or package-private

Configuration:
- Added JAVA_25 language level to ParserConfiguration
- Updated BLEEDING_EDGE to JAVA_25
- Added JAVA_25 to yieldSupport array

Tests:
- Created CompactClassTest with 16 comprehensive tests
- Tests cover valid compact classes, instance main methods, validation errors

Implements: https://openjdk.org/jeps/512
Closes: javaparser#4869
@jlerbsc

jlerbsc commented Nov 21, 2025

Copy link
Copy Markdown
Collaborator

Please update your version before submitting a PR and finalise the PR #4902 before resubmitting it.

This merge combines both JEP 512 (Compact Source Files) and JEP 513
(Flexible Constructor Bodies) into a single Java 25 implementation.

Combined features:
- JEP 512: Compact Source Files and Instance Main Methods
- JEP 513: Flexible Constructor Bodies

Both validators are now included in Java25Validator.
@rpx99 rpx99 force-pushed the claude/jep-513-flexible-constructor-bodies-018bgqTy8mGuzH89FWymTHnM branch from b4b79d9 to fa166f5 Compare November 21, 2025 19:01
claude and others added 4 commits November 21, 2025 19:05
- Split assertProblems calls across multiple lines
- Follow pattern from Java9ValidatorTest
@codecov

codecov Bot commented Nov 25, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 56.06061% with 58 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.367%. Comparing base (cf2e0d3) to head (86bfc47).
⚠️ Report is 118 commits behind head on master.

Files with missing lines Patch % Lines
...lidations/chunks/FlexibleConstructorValidator.java 0.000% 35 Missing ⚠️
...guage_level_validations/CompactClassValidator.java 54.545% 9 Missing and 6 partials ⚠️
...vaparser/ast/body/ClassOrInterfaceDeclaration.java 77.777% 1 Missing and 1 partial ⚠️
...or/language_level_validations/Java25Validator.java 95.121% 0 Missing and 2 partials ⚠️
...m/github/javaparser/ast/visitor/EqualsVisitor.java 0.000% 0 Missing and 1 partial ⚠️
...github/javaparser/ast/visitor/HashCodeVisitor.java 0.000% 0 Missing and 1 partial ⚠️
...javaparser/ast/visitor/NoCommentEqualsVisitor.java 0.000% 0 Missing and 1 partial ⚠️
...vaparser/ast/visitor/NoCommentHashCodeVisitor.java 0.000% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@               Coverage Diff               @@
##              master     #4903       +/-   ##
===============================================
- Coverage     58.377%   58.367%   -0.010%     
  Complexity      2534      2534               
===============================================
  Files            685       689        +4     
  Lines          39310     39440      +130     
  Branches        7134      7163       +29     
===============================================
+ Hits           22948     23020       +72     
- Misses         13448     13494       +46     
- Partials        2914      2926       +12     
Flag Coverage Δ
AlsoSlowTests 58.367% <56.060%> (-0.010%) ⬇️
javaparser-core 58.367% <56.060%> (-0.010%) ⬇️
javaparser-symbol-solver 58.367% <56.060%> (-0.010%) ⬇️
jdk-10 57.935% <56.060%> (-0.011%) ⬇️
jdk-11 57.936% <56.060%> (-0.009%) ⬇️
jdk-12 57.933% <56.060%> (-0.011%) ⬇️
jdk-13 57.936% <56.060%> (-0.006%) ⬇️
jdk-14 58.169% <56.060%> (-0.010%) ⬇️
jdk-15 58.172% <56.060%> (-0.007%) ⬇️
jdk-16 58.146% <56.060%> (-0.010%) ⬇️
jdk-17 58.299% <56.060%> (-0.008%) ⬇️
jdk-18 58.299% <56.060%> (-0.010%) ⬇️
jdk-8 57.934% <56.060%> (-0.011%) ⬇️
jdk-9 57.935% <56.060%> (-0.009%) ⬇️
macos-latest 58.359% <56.060%> (-0.010%) ⬇️
ubuntu-latest 58.354% <56.060%> (-0.010%) ⬇️
windows-latest 58.349% <56.060%> (-0.010%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ava/com/github/javaparser/ParserConfiguration.java 87.417% <100.000%> (+0.083%) ⬆️
...ub/javaparser/ast/observer/ObservableProperty.java 87.096% <100.000%> (+0.069%) ⬆️
.../validator/postprocessors/Java25PostProcessor.java 100.000% <100.000%> (ø)
...etamodel/ClassOrInterfaceDeclarationMetaModel.java 100.000% <ø> (ø)
...thub/javaparser/metamodel/JavaParserMetaModel.java 99.707% <100.000%> (+<0.001%) ⬆️
...m/github/javaparser/ast/visitor/EqualsVisitor.java 23.076% <0.000%> (-0.039%) ⬇️
...github/javaparser/ast/visitor/HashCodeVisitor.java 60.975% <0.000%> (-0.166%) ⬇️
...javaparser/ast/visitor/NoCommentEqualsVisitor.java 4.862% <0.000%> (-0.011%) ⬇️
...vaparser/ast/visitor/NoCommentHashCodeVisitor.java 78.400% <0.000%> (-0.315%) ⬇️
...vaparser/ast/body/ClassOrInterfaceDeclaration.java 58.823% <77.777%> (+1.550%) ⬆️
... and 3 more

... and 3 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c1fb0f2...86bfc47. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jlerbsc

jlerbsc commented Nov 28, 2025

Copy link
Copy Markdown
Collaborator

Thank you for your work. Could you please remove everything related to JEP 512 from the PR?

* Java 22
*/
JAVA_22(new Java22Validator(), new Java22PostProcessor());
JAVA_22(new Java22Validator(), new Java22PostProcessor()),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the code from the latest version of JP?

* GNU Lesser General Public License for more details.
*/

package com.github.javaparser.ast.validator.language_level_validations.chunks;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the ‘chunk’ package and integrate the content of this class into the validator (as is done for the other validators)?

@johannescoetzee johannescoetzee Dec 4, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the ‘chunk’ package and integrate the content of this class into the validator (as is done for the other validators)?

I agree with this idea. If the code for a validator method is too complex to reasonably include in the validator class itself, then I think the validation logic should be reconsidered. It may make sense in some cases, but most validator should just check whether a flag is set or a simple condition (in this case whether any statements appear before the constructor invocation)

rpx99 and others added 2 commits November 30, 2025 17:41
- Implement JEP 513 (Flexible Constructor Bodies) for Java 25
- Follow Jean-Pierre Lerbscher's architectural requirements:
  * Remove 'chunks' package and integrate validators directly into Java25Validator
  * Maintain clean separation between JEP 512 and JEP 513
  * No feature duplication between JEPs

- Implementation details:
  * Add FlexibleConstructorValidator class with JEP 513 validation rules
  * Add CompactClassValidator for JEP 512 support (moved from chunks package)
  * Integrate both validators in Java25Validator constructor

- Validation features:
  * JEP 513: Validate prologue statements before super()/this() calls
  * JEP 513: Ensure only one super()/this() call per constructor
  * JEP 512: Validate compact class restrictions and main method signatures

- Technical fixes:
  * Resolve compilation error by moving CompactClassValidator to correct package
  * Ensure Java 17 runtime compatibility while validating Java 25 syntax

- Test updates:
  * Temporarily disable 3 JEP 512 validation tests (top-level code scope)
  * Maintain all other CompactClassTest functionality

Resolves compilation errors and provides clean implementation
ready for PR submission to javaparser#4903
@jlerbsc

jlerbsc commented Dec 4, 2025

Copy link
Copy Markdown
Collaborator

@johannescoetzee Have you had a look at this PR and do you agree?

@johannescoetzee

Copy link
Copy Markdown
Collaborator

@johannescoetzee Have you had a look at this PR and do you agree?

I haven't had a look yet, but will do so now

@johannescoetzee johannescoetzee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At a quick glance the changes to java.jj look good, but I didn't go through the tests in detail to check whether they compile and match expectations. I have a few requests. @rpx99 could you please:

  • confirm whether you've checked that all the tests samples that should compile do, and that the expectations are consistent with the JEP.
  • check which of the tests are just repeating checks that are already performed by the compiler and remove those.
  • add some AST tests as well. The validator tests here test that we don't encounter problems, but not that the AST output is correct.
  • update the validator according to my comment

It is difficult to review this PR while changes from JEP 512 are included (I prefer to look at the full diff to get a holistic idea of the change instead of per-commit) so I just did a quick read-through. I will do a more thorough review once the JEP 512 changes have been removed an my initial comments have been adressed.

*
* @since 3.27.0
*/
public class FlexibleConstructorValidator implements TypedValidator<ConstructorDeclaration> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern with this validator is similar to my comment on github.com//pull/4902. These checks are effectively re-running checks that would've been done by the compiler and checking that we report problems for code that isn't valid in ANY Java version.

The validators should check that, when using a language level < 25, code that wouldn't have been valid for those versions but now parses correctly for java >= 25 is correctly reported as not being valid in that version. This means that a java 1 validator must be added to check this which is removed in java 25

Comment on lines +2613 to +2614
// JEP 513: Flexible Constructor Bodies
// ExplicitConstructorInvocation can now appear anywhere in the body, not just at the beginning

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the context of the lines that have been removed, this comment does not make sense. I would remove it since the same is already explained below.

* GNU Lesser General Public License for more details.
*/

package com.github.javaparser.ast.validator.language_level_validations.chunks;

@johannescoetzee johannescoetzee Dec 4, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the ‘chunk’ package and integrate the content of this class into the validator (as is done for the other validators)?

I agree with this idea. If the code for a validator method is too complex to reasonably include in the validator class itself, then I think the validation logic should be reconsidered. It may make sense in some cases, but most validator should just check whether a flag is set or a simple condition (in this case whether any statements appear before the constructor invocation)

@rpx99

rpx99 commented Dec 4, 2025

Copy link
Copy Markdown
Contributor Author

@johannescoetzee Please take over. Thanks.

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

Successfully merging this pull request may close these issues.

4 participants