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

Expand getCoordinates of newClass to access arguments #4089

Open
timo-abele opened this issue Mar 9, 2024 · 1 comment
Open

Expand getCoordinates of newClass to access arguments #4089

timo-abele opened this issue Mar 9, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@timo-abele
Copy link

What problem are you trying to solve?

I would like to switch one constructor call for an other with JavaTemplate. E.g. if a class A offers two constructors A(int foo), A(int foo, double ba) I would like to be able to insert a new argument after, or replace the list of arguments altogether.

Describe the solution you'd like

API offers suitable methods through getCoordinates.

Have you considered any alternatives or workarounds?

Replacing the entire constructor call should work in theory.
withArguments() works as well, but JavaTemplates seem to be the way to go.

Additional context

Currently a constructor call can be replaced entirely, but it would be nice to be able to operate it without putting "new A" into the template, which requires me to find out the class name at runtime.
J.MethodInvocation already has getCoordinates().replaceArguments(), that could just be implemented in newClass as well.

Are you interested in contributing this feature to OpenRewrite?

probably won't find the time

@timo-abele timo-abele added the enhancement New feature or request label Mar 9, 2024
@timo-abele timo-abele changed the title Add getCoordinates to arguments of newClass Expand getCoordinates of newClass to access arguments Mar 9, 2024
@timtebeek
Copy link
Contributor

Thanks for the suggestion @timo-abele ; I imagine this to be very similar to how we handle J.MethodInvocation, which I think should make for a nice addition.

public static class MethodInvocation extends Statement {
MethodInvocation(J.MethodInvocation tree) {
super(tree);
}
public JavaCoordinates replaceArguments() {
return replace(Space.Location.METHOD_INVOCATION_ARGUMENTS);
}
/**
* Indicates replacement of the invocation's name and argument list, while preserving its select.
*/
public JavaCoordinates replaceMethod() {
return replace(Space.Location.METHOD_INVOCATION_NAME);
}
}

Which would then need to be returned instead of CoordinateBuilder.Statement in

public CoordinateBuilder.Statement getCoordinates() {
return new CoordinateBuilder.Statement(this);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

2 participants