Matt Micheletti created GROOVY-12417:
----------------------------------------
Summary: Support Flexible Constructor Bodies (JEP-513) Delivered
in Java 25+
Key: GROOVY-12417
URL: https://issues.apache.org/jira/browse/GROOVY-12417
Project: Groovy
Issue Type: Improvement
Components: Compiler
Reporter: Matt Micheletti
# Summary
Relatively recently, Java added support for allowing code before the obligatory
`this(...)`/`super(...)` statements in constructors. Essentially, the JEP
loosens the decades long restriction of _always_ forcing one of those
aforementioned statements to be the mandatory very first statement. There may
now be early constructor code that appears _before_ `this(...)`/`super(...)`
albeit with some practical JLS limitations. This feature was fully accepted,
delivered, and shipped as finalized in JDK 25.
Groovy should continue aligning with supporting as much of Java syntax and
semantics as possible. Subsequently, Groovy's internal handling of constructors
should be enhanced to allow supporting JEP-513 rather than maintaining the
previously held restriction.
# Expected Behavior
The following code should successfully compile and work as expected, intended,
and/or desired as if it was written in Java.
```groovy
class Child extends Parent
{
public Child(int age)
{
if (age <= 0)
{
throw new IllegalArgumentException(...);
}
super(age);
//...
}
}
```
# Actual Behavior
Currently, the Groovy compiler hard bakes in compliance checks to verify
against the long standing restriction of enforcing constructors' special
semantics around `this(...)`/`super(...)`.
```groovy
class Child extends Parent
{
public Child(int age)
{
if (age <= 0) // Compiler error: "super (age) should be the first
statement in the constructor[Child] @ line X, column Y."
{
throw new IllegalArgumentException(...);
}
super(age);
//...
}
}
```
###### Source/Attribution Credits:
- [JEP-513 (Flexible Constructor Bodies)|https://openjdk.org/jeps/513]
--
This message was sent by Atlassian Jira
(v8.20.10#820010)