diff --git a/src/main/java/org/openrewrite/java/spring/InlineCommentSpringProperties.java b/src/main/java/org/openrewrite/java/spring/InlineCommentSpringProperties.java new file mode 100644 index 00000000..b19c9c6d --- /dev/null +++ b/src/main/java/org/openrewrite/java/spring/InlineCommentSpringProperties.java @@ -0,0 +1,65 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.spring; + +import lombok.EqualsAndHashCode; +import lombok.Value; +import org.jetbrains.annotations.Nullable; +import org.openrewrite.*; + +import java.util.List; + +@EqualsAndHashCode(callSuper = false) +@Value +public class InlineCommentSpringProperties extends Recipe { + + @Override + public String getDisplayName() { + return "Comment Spring properties"; + } + + @Override + public 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 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 TreeVisitor getVisitor() { + String inlineComment = " # " + comment; + return new TreeVisitor() { + @Override + public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { + Tree processingTree = tree; + for (String key : propertyKeys) { + String regex = "(? + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.spring; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.test.RewriteTest; + +import java.util.List; + +import static org.openrewrite.properties.Assertions.properties; +import static org.openrewrite.yaml.Assertions.yaml; + +class InlineCommentSpringPropertiesTest implements RewriteTest { + + @DocumentExample + @Test + void shouldInsertInlineCommentsIntoProperties() { + rewriteRun( + spec -> spec.recipe(new InlineCommentSpringProperties(List.of("test.propertyKey1", "test.propertyKey2"), "my comment")), + //language=yaml + yaml( + """ + test.propertyKey1: xxx + test.propertyKey2: yyy + test.propertyKey3: zzz + """, + """ + test.propertyKey1: xxx # my comment + test.propertyKey2: yyy # my comment + test.propertyKey3: zzz + """, + spec -> spec.path("application.yaml") + ), + //language=properties + properties( + """ + test.propertyKey1=xxx + test.propertyKey2=yyy + test.propertyKey3=zzz + """, + """ + test.propertyKey1=xxx # my comment + test.propertyKey2=yyy # my comment + test.propertyKey3=zzz + """, + spec -> spec.path("application.properties") + ) + ); + } +}