openrewrite / rewrite-templating

Automated templating using code snippets.
Apache License 2.0
16 stars 7 forks source link

When before template parameter is used twice in after template, do not call method invocations such as `iterator.next()` twice #27

Closed timtebeek closed 1 year ago

timtebeek commented 1 year ago

What problem are you trying to solve?

Given a recipe such as the following

private static class IsEmpty {
    @BeforeTemplate
    boolean before(String s) {
        return StringUtils.isEmpty(s);
    }

    @AfterTemplate
    boolean after(String s) {
        return (s == null || s.isEmpty());
    }
}

When the input parameter is a method invocation such as iterator.next() Then the template likely should not make any changes.

Have you considered any alternatives or workarounds?

Additional context

timtebeek commented 1 year ago

@knutwannheden had this to add in chat

We could probably without too much effort develop out own @Matches annotation where the corresponding Matcher receives a J as input. That would allow you to check if it is a MethodInvocation.