Skip to content

Commit

Permalink
feat: Added recipe to comment deprecated and removed properties witho…
Browse files Browse the repository at this point in the history
…ut alternative

Refs: openrewrite#634
  • Loading branch information
Andrei Shakirin authored and timtebeek committed Nov 26, 2024
1 parent 2d130c9 commit 2078976
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
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;
}
};
}
}
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")));
}
}

0 comments on commit 2078976

Please sign in to comment.