Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddOrUpdateAnnotationAttribute throws an error when adding a class as attributeValue #3978

Open
JohannesRabauer opened this issue Feb 6, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@JohannesRabauer
Copy link

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.

@JohannesRabauer JohannesRabauer added the bug Something isn't working label Feb 6, 2024
@timtebeek timtebeek added the question Further information is requested label Feb 6, 2024
@timtebeek
Copy link
Contributor

Hi @JohannesRabauer ! Looks like you're running up against a limitation where AddOrUpdateAnnotationAttribute can only update literal values for now.

// First assume the value exists amongst the arguments and attempt to update it
AtomicBoolean foundAttributeWithDesiredValue = new AtomicBoolean(false);
final J.Annotation finalA = a;
List<Expression> newArgs = ListUtils.map(currentArgs, it -> {
if (it instanceof J.Assignment) {
J.Assignment as = (J.Assignment) it;
J.Identifier var = (J.Identifier) as.getVariable();
if (attributeName == null || !attributeName.equals(var.getSimpleName())) {
return it;
}
J.Literal value = (J.Literal) as.getAssignment();

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.

@timtebeek timtebeek removed the question Further information is requested label Feb 6, 2024
@timtebeek timtebeek changed the title Recipe: AddOrUpdateAnnotationAttribute: Adding a class as attributeValue throws error AddOrUpdateAnnotationAttribute throws an error when adding a class as attributeValue Feb 6, 2024
@JohannesRabauer
Copy link
Author

Thanks @timtebeek. I tried to do a minimal test with PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Backlog
Development

No branches or pull requests

2 participants