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

RemoveMethodInvocationsVisitor can remove static method #4754

Merged

Conversation

dralagen
Copy link
Contributor

@dralagen dralagen commented Dec 6, 2024

What's changed?

Add test to verify if RemoveMethodInvocationsVisitor can delete static method

What's your motivation?

After moving recipe org.openrewrite.java.migrate.RemoveMethodInvocation defined on project rewrite-migrate-java to org.openrewrite.java.RemoveMethodInvocations in `rewrite-java' some custom recipe now failed

type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.easymock.EasymockRemoveReplay
displayName: Remove EasyMock `replay()`"
description: Remove EasyMock `replay()` method calls.
recipeList:
  - org.openrewrite.java.migrate.RemoveMethodInvocation:
      methodPattern: org.easymock..EasyMock replay(..)

this exemple work but the recipe has been removed :

this exemple break TU : https://github.com/dralagen/rewrite-testing-frameworks/blob/removeMethodInvocations/src/main/resources/META-INF/rewrite/easymock.yml

Anyone you would like to review specifically?

@timtebeek I created the PR as draft with test the remove Method static invocation detect on the Migration from easymock to mockito

Have you considered any alternatives or workarounds?

I see the same issue on PR :

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

@timtebeek
Copy link
Contributor

Thanks for calling that out @dralagen ! The old recipe in rewrite-migrate-java used this visitor that still exists in rewrite-static-analysis.
https://github.com/openrewrite/rewrite-static-analysis/blob/b412e1d6b0b6fd8bba17e6f29dbb6af63749e638/src/main/java/org/openrewrite/staticanalysis/RemoveMethodCallVisitor.java#L36-L93

We'll need to work out the difference between that and the new RemoveMethodInvocationsVisitor that lives here.

public class RemoveMethodInvocationsVisitor extends JavaVisitor<ExecutionContext> {
private final Map<MethodMatcher, Predicate<List<Expression>>> matchers;
public RemoveMethodInvocationsVisitor(Map<MethodMatcher, Predicate<List<Expression>>> matchers) {
this.matchers = matchers;
}
public RemoveMethodInvocationsVisitor(List<String> methodSignatures) {
this(methodSignatures.stream().collect(Collectors.toMap(
MethodMatcher::new,
signature -> args -> true
)));
}

Any help appreciated, but not expected or required; just let me know if you're open to explore this further and know that we already appreciate you logging this as a runnable issue.

@timtebeek timtebeek self-requested a review December 6, 2024 15:13
@timtebeek timtebeek added the bug Something isn't working label Dec 6, 2024
@dralagen dralagen changed the title RemoveMethodInvocationsVisitor can remove static method RemoveMethodInvocationsVisitor can remove static method Dec 6, 2024
@timtebeek timtebeek requested a review from jevanlingen December 9, 2024 10:21
@jevanlingen jevanlingen force-pushed the RemoveMethodInvocations-staticMethod branch from dd2441a to 3aae7a2 Compare December 10, 2024 07:11
@jevanlingen jevanlingen self-assigned this Dec 10, 2024
@knutwannheden
Copy link
Contributor

Looking at the tests I believe the static method call is not removed by RemoveMethodInvocationsVisitor as the call is part of a local variable assignment. That is of course something that could be changed (e.g. completely remove the initializer or replace it with null), but as it currently stands this visitor will just do nothing in this situation:

private boolean isStatement() {
return getCursor().dropParentUntil(p -> p instanceof J.Block ||
p instanceof J.Assignment ||
p instanceof J.VariableDeclarations.NamedVariable ||
p instanceof J.Return ||
p instanceof JContainer ||
p == Cursor.ROOT_VALUE
).getValue() instanceof J.Block;
}

@jevanlingen
Copy link
Contributor

Looking at the tests I believe the static method call is not removed by RemoveMethodInvocationsVisitor as the call is part of a local variable assignment. That is of course something that could be changed (e.g. completely remove the initializer or replace it with null), but as it currently stands this visitor will just do nothing in this situation:

private boolean isStatement() {
return getCursor().dropParentUntil(p -> p instanceof J.Block ||
p instanceof J.Assignment ||
p instanceof J.VariableDeclarations.NamedVariable ||
p instanceof J.Return ||
p instanceof JContainer ||
p == Cursor.ROOT_VALUE
).getValue() instanceof J.Block;
}

That's right, I just pushed a first implementation.

@jevanlingen jevanlingen added the recipe Requested Recipe label Dec 10, 2024
@jevanlingen jevanlingen marked this pull request as ready for review December 10, 2024 10:57
Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Thanks for taking this on @jevanlingen ! Did you happen to check if this also resolves the outstanding items on

And does this mean the visitor in rewrite-static-analysis can now be replaced & removed?

@jevanlingen
Copy link
Contributor

jevanlingen commented Dec 10, 2024

Thanks for taking this on @jevanlingen ! Did you happen to check if this also resolves the outstanding items on

And does this mean the visitor in rewrite-static-analysis can now be replaced & removed?

Yes and yes, I just tested it locally, all test of rewrite-testing-frameworks succeed:

image

@timtebeek timtebeek merged commit 54a2057 into openrewrite:main Dec 10, 2024
2 checks passed
@dralagen dralagen deleted the RemoveMethodInvocations-staticMethod branch December 10, 2024 15:24
@dralagen
Copy link
Contributor Author

Thank you very much for your responsiveness and the time taken to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working recipe Requested Recipe
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants