openrewrite / rewrite

Automated mass refactoring of source code.

Home Page:https://docs.openrewrite.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gradle Plugin/Openrewrite should skip files anntoated with javax.annotation.Generated

blipper opened this issue · comments

What problem are you trying to solve?

Avoiding updating generated java files. e.g. Those generated by XJC

Describe the solution you'd like

Skip classes/files annotated with https://docs.oracle.com/en/java/javase/17/docs/api/java.compiler/javax/annotation/processing/Generated.html

https://docs.oracle.com/javase/8/docs/technotes/tools/unix/xjc.html

Have you considered any alternatives or workarounds?

Directory skipping but this is build dependent and prone to additional issues.

Additional context

Are you interested in [contributing this feature to OpenRewrite]

Yes

Hi! Thanks for the suggestion and offer to help. As a bit of context let me link you to how for instance the Maven plugin handles generated sources, as that might factor in here as well: https://github.com/openrewrite/rewrite-maven-plugin/blob/09fe6c413311aba7ae160bd4e6e8b38f1d41bad6/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java#L428-L433

You'll notice that generated sources get a org.openrewrite.marker.Generated marker attached to them, which then later excludes them from showing changes:

if (source.getMarkers().findFirst(Generated.class).isPresent()) {
// skip edits made to generated source files so that they don't show up in a diff
// that later fails to apply on a freshly cloned repository
return source;
}

if (original.getMarkers().findFirst(Generated.class).isPresent()) {
continue;
}

It appears we don't yet take javax.annotation.Generated into account, but I could see a point being made to also add our internal generated marker to classes annotated as generated. Would you want to help explore that option?

That seems pretty straightforward. I assume you would want to use reflection to test or are you okay taking a dependency on the generated class directly?

Great to hear; elsewhere we use FindAnnotations.find to find annotations on LST elements, with those dependencies only necessary on the projects themselves, not for the recipe development.

if (FindAnnotations.find(method, "@java.lang.Override").isEmpty()) {