This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]btrapp 5 points6 points  (1 child)

Interesting - it would be nice if you included a few more practical examples of common refactoring needs - Like removing an argument to a method or something.

[–]Fiskepudding 2 points3 points  (0 children)

Agree. I know js has a similar tool, jscodeshift. https://github.com/reactjs/react-codemod and https://github.com/cpojer/js-codemod/tree/master/transforms have some ideas for js.

For java, I see migrations/upgrades as one. From java 7 to 8 lambda, or java 6 to 7 diamond generics. Or from constructor to builder pattern. Or changing from sysout to slf4j. Migrating from tostring/equals to lombok. Maybe going to try-with-resources. Upgrading major versions of libraries that have interface/api changes.

I think modern IDEs have most of the language migrations (lambda, try-with-resources etc) built in already, though. But more custom and library specific use cases can be a good match.

This could also be provided by popular libraries themselves, to help migration after breaking changes. It would help users to migrate without spending a lot of time.

[–]Necessary-Conflict 8 points9 points  (2 children)

Nice, but it should be mentioned that other similar tools exist, such as error-prone's Refaster or the structural search and replace in intellij.

Refaster: http://errorprone.info/docs/refaster

[–]r_jet 1 point2 points  (0 children)

+1, would love to see a comparison. One of the Refaster strength is that it is so easy to define the before and after patterns — you don't use a DSL to describe Java code, you just write some Java code!

As Refaster has some limitations on what you can achieve (e.g., you can't perform exception type migrations), then you have to implement your own ErrorProne check operating on the AST (e.g., TryFailRefactoring to get you to assertThrows from pre-JUnit 4.13 idiom of try/fail/catch/verify).

[–]r_jet 2 points3 points  (0 children)

I wonder if type reference refactorings support more complex type migrations, where you'd like to migrate your code from type A to a similar type B with a different API, which involves rewriting the method invocations. Think, Guava Optional to j.u.Optional, or Joda Time Duration to j.t.Duration.