sts4 icon indicating copy to clipboard operation
sts4 copied to clipboard

refactoring recipe doesn't work with compile errors

Open martinlippert opened this issue 2 years ago • 2 comments

Steps to reproduce:

  • I have a simple project that has a dependency on a random library
  • Now the library changes the name of a method and I change the dependency in my ´pom.xml` to point to the new version of the library
  • As expected, compiler errors showing up for the method invocations, since my source code still uses the old method name
  • There is a recipe in YAML that looks like this:
---
type: specs.openrewrite.org/v1beta/recipe
name: com.yourorg.AdaptToNewVersionOfLibrary
displayName: Adapt to changed method of Movie Service
recipeList:
  - org.openrewrite.java.ChangeMethodName:
      methodPattern: com.example.testlib.library.MovieService getMovies(..)
      newMethodName: getAllMovies

But when I execute this recipe, it doesn't do anything.

When I switch back to use the old version of the library, the compile errors go away again. Then, I can execute the recipe and it switches my method invocations to the new method name. Then, I can change the dependency in the pom.xml file to point to the new version and everything is fine, but the flow is still problematic.

Why is the recipe not able to update the method invocation? I guess this is due to the OpenRewrite parsing not being able to resolve the method invocation (this the old method doesn't exist anymore) and therefore doesn't match that against the method pattern in the recipe.

martinlippert avatar Jun 19 '23 08:06 martinlippert

Rewrite recipes only work with all the code compiling and hence all types properly resolved. This also includes rewrite's recipe ChangeMethodName. However, this limitation does not mean that we cannot write a recipe that would work for the case above. The method's type would be "unknown" but the method target's type i think should be resolved as well as parameters. Therefore, the method name + types of the target and parameters should help to build the method signature for the method matcher. It wouldn't be easy if type of the target or types of parameters were not resolved though. This can be implemented as a separate recipe or perhaps included into the ChangeMethodName recipe...

BoykoAlex avatar Jun 22 '23 14:06 BoykoAlex

Understand, but it seems to be like the fundamental design of OpenRewrite isn't really made for cases like this. But I would love to see a way to make things like this work in more general. Probably something to discuss with the OpenRewrite team.

martinlippert avatar Jun 23 '23 09:06 martinlippert