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

feat: Added recipe to comment deprecated and removed properties witho… #635

Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.openrewrite.java.spring;
ashakirin marked this conversation as resolved.
Show resolved Hide resolved

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jetbrains.annotations.NotNull;
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
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";
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
}

@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;
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, @NotNull ExecutionContext ctx) {
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
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;
ashakirin marked this conversation as resolved.
Show resolved Hide resolved

import org.junit.jupiter.api.Test;
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
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 {
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
ashakirin marked this conversation as resolved.
Show resolved Hide resolved

@Test
ashakirin marked this conversation as resolved.
Show resolved Hide resolved
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")));
}
}
Loading