How to refactor this class programmatically via Java code/Groovy plugin? Let's say, I need to:
- Rename
foo.method2tofoo.method3 - Rename
myMethodtoyourMethod - Change the imported package
org.you.core.util.AnotherClassFromExternalPackage
import com.me.core.util.AnotherClassFromExternalPackage;
import com.me.core.util.Foo;
public class MyClass implements AnotherClassFromExternalPackage {
private final Foo foo;
public MyClass() {
this.foo = new Foo();
}
@Override
public long myMethod() {
return foo.method2();
}
}
How to create a script that will parse the code, apply syntactical transformations and save it using given instructions?
The code above is just an example. The bigger problem is that I have a lot of projects that are using the same external library. And sometimes they release a new version with breaking changes that is breaking the current code after the dependency bump. And I must update the dependency to the latest version every 2 weeks for 10+ projects. I need to write a script that will fix these breaking changes automatically. Ideally, apply code transformations one by one.

Sounds like a job for OpenRewrite. Standard recipes, e.g., for renaming a package are available. You can also write your own recipes, which make use of a visitor pattern over a tree-based representation of your source code (which will preserve, e.g., indentation).
You can use Maven or Gradle to execute the rewrite.
For instance the change package configuration from the getting started page looks like this: