Skip to content

Move statements before super(..) in Java constructor #733 #736

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

rkampani
Copy link

@rkampani rkampani commented May 19, 2025

What's changed?

  • Relates to #5397
    Jep 513 calls for allowing statements before the call to super() in Java constructors, allowing for simple assignments and validations before doing expensive construction.

Java version >= 25
(Also double check Jep 513 lands in Java 25)

Before Applying Recipe

class A {
    public A(String bar) {
        super();
        if(bar.equals("test"))
            throw new RuntimeException();
    }
}

After Applying Recipe

class A {
    public A(String bar) {
        if(bar.equals("test"))
            throw new RuntimeException();
        super();
    }
}

What's your motivation?

Anything in particular you'd like reviewers to focus on?

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/resources/META-INF/rewrite/examples.yml
    • lines 997-1007
    • lines 1036-1042

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/resources/META-INF/rewrite/examples.yml
    • lines 997-1007
    • lines 1036-1042
    • lines 7108-7107

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/resources/META-INF/rewrite/examples.yml
    • lines 997-1007
    • lines 1036-1042
    • lines 7108-7107

@timtebeek
Copy link
Member

We're seeing some unrelated test failures; no need for alarm or debugging; we'll try to get those sorted soon. :)

@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite May 20, 2025
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/resources/META-INF/rewrite/examples.yml
    • lines 701-700
    • lines 997-1007
    • lines 1036-1042

}

// Move super() to the end
List<Statement> updated = new java.util.ArrayList<>(statements);
Copy link
Contributor

Choose a reason for hiding this comment

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

ATM you move the super() call to the very end of a constructor call. If i recall correctly there are limitations regarding the reference to instance fields. Could you please revisit JEP 513s section about Early-construction-contexts and verify that we are producing JLS compliant code?

@github-project-automation github-project-automation bot moved this from Ready to Review to In Progress in OpenRewrite May 20, 2025
@MBoegers
Copy link
Contributor

Thanks for your contribution, I love seeing new Language features been picked up :) I think 1 or 2 tests should be enough to make sure we are JLS compliant in the context of the Early construction contexts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
java 25+ recipe Recipe requested
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

3 participants