-
I have created a rewrite.yml with the following recipe list: type: specs.openrewrite.org/v1beta/recipe
name: com.example.ReplaceSingletonAnnotation
displayName: Replace annotation example
recipeList:
- org.openrewrite.java.ReplaceAnnotation:
annotationPatternToReplace: '@javax.ejb.Singleton'
annotationTemplateToInsert: '@org.springframework.stereotype.Component' With this a simple module project: A parent pom: <project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>services</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project> and a module pom: <project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project> And within the module an example Java class: package com.example;
import javax.ejb.Singleton;
@Singleton
public class SingletonExample {
} After running the command package com.example;
@org.springframework.stereotype.Component
public class SingletonExample {
} Why is a full qualified name added and not an import to Component? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hoi @rregout , No idea why the import isn't shortened in your particular case. The recipe does contain instructions to shorten those imports where possible. If you look at our unit tests you'll see that we do shorten the imports for the cases seen there. You could always run the recipe we have to shorten fully qualified type references explicitly after the annotation has been replaced to check whether that sees any improvement: https://docs.openrewrite.org/recipes/java/shortenfullyqualifiedtypereferences |
Beta Was this translation helpful? Give feedback.
Hoi @rregout , No idea why the import isn't shortened in your particular case. The recipe does contain instructions to shorten those imports where possible.
rewrite/rewrite-java/src/main/java/org/openrewrite/java/ReplaceAnnotation.java
Lines 93 to 97 in 03aa622
If you look at our unit tests you'll see that we do shorten the imports for the cases seen there.
rewrite/rewrite-java…