forked from openrewrite/rewrite-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added recipe to comment deprecated and removed properties witho…
…ut alternative Refs: openrewrite#634
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/main/java/org/openrewrite/java/spring/InlineCommentSpringProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.openrewrite.java.spring; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.openrewrite.*; | ||
|
||
import java.util.List; | ||
|
||
@EqualsAndHashCode(callSuper = false) | ||
@Value | ||
public class InlineCommentSpringProperties extends Recipe { | ||
|
||
@Override | ||
public @NotNull @NlsRewrite.DisplayName String getDisplayName() { | ||
return "Comment spring properties"; | ||
} | ||
|
||
@Override | ||
public @NotNull @NlsRewrite.Description String getDescription() { | ||
return "Add inline comments to specified spring properties."; | ||
} | ||
|
||
@Option(displayName = "Property keys list", | ||
description = "The list of names of the property keys to comment.", | ||
example = "management.metrics.binders.files.enabled") | ||
List<String> propertyKeys; | ||
|
||
@Option(displayName = "Inline comment", | ||
description = "Inline comment to be inserted", | ||
example = "this property is deprecated and no longer applicable starting from Spring Boot 3.0.x") | ||
String comment; | ||
|
||
@Override | ||
public @NotNull TreeVisitor<?, ExecutionContext> getVisitor() { | ||
String inlineComment = " # " + comment; | ||
return new TreeVisitor<Tree, ExecutionContext>() { | ||
@Override | ||
public @Nullable Tree visit(@Nullable Tree tree, @NotNull ExecutionContext ctx) { | ||
Tree processingTree = tree; | ||
for (String key : propertyKeys) { | ||
String regex = "(?<!" + inlineComment + ")$"; | ||
ChangeSpringPropertyValue changeSpringPropertyValue = new ChangeSpringPropertyValue(key, inlineComment, regex, true, null); | ||
processingTree = changeSpringPropertyValue.getVisitor().visit(processingTree, ctx); | ||
} | ||
return processingTree; | ||
} | ||
}; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/test/java/org/openrewrite/java/spring/InlineCommentSpringPropertiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.openrewrite.java.spring; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import java.util.List; | ||
|
||
import static org.openrewrite.properties.Assertions.properties; | ||
import static org.openrewrite.yaml.Assertions.yaml; | ||
|
||
public class InlineCommentSpringPropertiesTest implements RewriteTest { | ||
|
||
@Test | ||
void shouldInsertInlineCommentsIntoProperties() { | ||
rewriteRun( | ||
spec -> spec.recipe(new InlineCommentSpringProperties(List.of("test.propertyKey1", "test.propertyKey2"), "my comment")), | ||
yaml(""" | ||
test.propertyKey1: xxx | ||
test.propertyKey2: yyy""", | ||
""" | ||
test.propertyKey1: xxx # my comment | ||
test.propertyKey2: yyy # my comment""", | ||
spec -> spec.path("application.yaml")), | ||
properties(""" | ||
test.propertyKey1: xxx | ||
test.propertyKey2: yyy""", | ||
""" | ||
test.propertyKey1: xxx # my comment | ||
test.propertyKey2: yyy # my comment""", | ||
spec -> spec.path("application.properties"))); | ||
} | ||
} |