Support refactoring based on an annotation that provides the replacement template (e.g. `@InlineMe`)
What problem are you trying to solve?
Automated transformations via type attribution and annotations. Tim asked about automating refactoring during the KotlinParser development using deprecated annotations.
I thought it might be possible to create an OR annotation like DeprecatedX(...), where the annotation arguments provide information about how to handle a migration.
Describe the solution you'd like
- The annotations could be target-site specific, like METHOD, CLASS, etc.
- OR would own the annotation, and its existence could trigger a recipe.
- The type-attribution on a class, method, etc., may have arguments that provide contextual instructions on the migration.
class Example {
// I don't recall what is available in the annotation arguments, but I imagine something could be sorted out.
@org.openrewrite.DeprecatedMethod("methodMatcherSig", "someTransformationToMethodB")
void methodA(...) {...}
// Method calls of `methodA` would be migrated to `methodB`.
void methodB(...) {...}
}
Have you considered any alternatives or workarounds?
No, this was just a thought. This may already exist, but I haven't tracked all the changes across OR for awhile.
Are you interested in contributing this feature to OpenRewrite?
I'm pretty busy at the moment, but anything is possible!
Hi @traceyyoshima ; thanks for the suggestion! There's been a few such discussions in the past indeed, notably around
- https://github.com/openrewrite/rewrite-kotlin/issues/25
- https://errorprone.info/docs/inlineme
The second one is more geared towards Java, but seems to have only seen limited adoption so far.
I like the suggestion to introduce our own, as then we'd be better in control of just what we need to make these work, and potentially could run a processor to generate the recipes. Should also be easier now that we capture annotation argument values as of
- https://github.com/openrewrite/rewrite/pull/4746
- Add annotation element values to type attribution model #4746
Ah, that's very cool. I didn't know about that update, but it does simplify the concept.
Ownership has many benefits, mainly due to OR's velocity. A lightweight package may also help spread adoption of the framework!
Indeed makes things quite a bit easier. Could then be a single recipe that for instance already supports that @InlineMe, or a limited subset of those, to do the one to one replacements with JavaTemplate. Looks like that could be a fun experiment, but with a few conferences coming up it'll have to wait a bit before I'll allow myself to explore. Let me know if you'd want to collaborate with a quick draft!
hi @traceyyoshima ; this has now been implemented and proven to work:
- https://github.com/openrewrite/rewrite-migrate-java/pull/788#issuecomment-3200452153
Hope that helps you there!