rewrite
rewrite copied to clipboard
`AddOrUpdateAnnotationAttribute` throws an error when adding a `class` as `attributeValue`
What version of OpenRewrite are you using?
I am using
- OpenRewrite v2.6.3
- Maven v3.9.5
- rewrite-maven-plugin 5.21.0
- org.openrewrite:rewrite-java:8.13.4
How are you running OpenRewrite?
I am building my own recipe in a rewrite.yml that looks like this:
---
type: specs.openrewrite.org/v1beta/recipe
name: company.JpaMigration
displayName: JpaMigration
description: ...
recipeList:
- org.openrewrite.java.AddOrUpdateAnnotationAttribute:
annotationType: 'org.springframework.boot.autoconfigure.SpringBootApplication'
attributeName: 'exclude'
attributeValue: 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class'
addOnly: null
In another project i call this maven command:
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=company:migration:1.0.0-SNAPSHOT -Drewrite.activeRecipes=company.JpaMigration
What is the full stack trace of any errors you encountered?
[INFO] Running recipe(s)...
[ERROR] The recipe produced an error. Please report this to the recipe author.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 34.298 s
[INFO] Finished at: 2024-02-06T14:55:49+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:5.21.0:run (default-cli) on project spring-petclinic: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:5.21.0:run failed: Error while visiting src\test\java\org\springframework\samples\petclinic\system\CrashControllerIntegrationTests.java: java.lang.ClassCastException: class org.openrewrite.java.tree.J$NewArray cannot be cast to class org.openrewrite.java.tree.J$Literal (org.openrewrite.java.tree.J$NewArray and org.openrewrite.java.tree.J$Literal are in unnamed module of loader org.codehaus.plexus.classworlds.realm.ClassRealm @467924d8)
[ERROR] org.openrewrite.java.AddOrUpdateAnnotationAttribute$1.lambda$visitAnnotation$0(AddOrUpdateAnnotationAttribute.java:107)
[ERROR] org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
[ERROR] org.openrewrite.java.AddOrUpdateAnnotationAttribute$1.visitAnnotation(AddOrUpdateAnnotationAttribute.java:100)
[ERROR] org.openrewrite.java.AddOrUpdateAnnotationAttribute$1.visitAnnotation(AddOrUpdateAnnotationAttribute.java:71)
[ERROR] org.openrewrite.java.tree.J$Annotation.acceptJava(J.java:235)
[ERROR] org.openrewrite.java.tree.J.accept(J.java:59)
[ERROR] org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
[ERROR] org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:361)
[ERROR] org.openrewrite.java.JavaVisitor.lambda$visitClassDeclaration$5(JavaVisitor.java:455)
[ERROR] org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
[ERROR] org.openrewrite.java.JavaVisitor.visitClassDeclaration(JavaVisitor.java:455)
[ERROR] org.openrewrite.java.JavaIsoVisitor.visitClassDeclaration(JavaIsoVisitor.java:108)
[ERROR] org.openrewrite.java.JavaIsoVisitor.visitClassDeclaration(JavaIsoVisitor.java:30)
[ERROR] org.openrewrite.java.tree.J$ClassDeclaration.acceptJava(J.java:1290)
[ERROR] org.openrewrite.java.tree.J.accept(J.java:59)
[ERROR] org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
[ERROR] ...
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
Process finished with exit code 1
Are you interested in contributing a fix to OpenRewrite?
It looks like there is a wrong cast to J.Literal
in AddOrUpdateAnnotationAttribute:107
. This seems to happen, because i input a class instead of a text value. I don't know what it should get parsed as...
I also tried to insert this as value:
---
type: specs.openrewrite.org/v1beta/recipe
name: company.JpaMigration
displayName: JpaMigration
description: ...
recipeList:
- org.openrewrite.java.AddOrUpdateAnnotationAttribute:
annotationType: 'org.springframework.boot.autoconfigure.SpringBootApplication'
attributeName: 'exclude'
attributeValue: '{org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration.class,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class}'
addOnly: null
but this also didn't work.
Hi @JohannesRabauer ! Looks like you're running up against a limitation where AddOrUpdateAnnotationAttribute
can only update literal values for now.
https://github.com/openrewrite/rewrite/blob/516c70b89ebbb43803d2dd6e0bc2cf31307a80d3/rewrite-java/src/main/java/org/openrewrite/java/AddOrUpdateAnnotationAttribute.java#L97-L107
Your case of adding an attribute that's a class had not yet been reported; It could likely be added by updating the recipe itself. Starting with a draft PR with a failing test case of what you'd like to see is a great way to get this started, if you're up for that.
Thanks @timtebeek. I tried to do a minimal test with PR.