|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.spring; |
| 17 | + |
| 18 | +import lombok.EqualsAndHashCode; |
| 19 | +import lombok.Value; |
| 20 | +import org.jspecify.annotations.NonNull; |
| 21 | +import org.jspecify.annotations.Nullable; |
| 22 | +import org.openrewrite.*; |
| 23 | +import org.openrewrite.properties.tree.Properties; |
| 24 | +import org.openrewrite.yaml.tree.Yaml; |
| 25 | + |
| 26 | +@EqualsAndHashCode(callSuper = false) |
| 27 | +@Value |
| 28 | +public class CommentOutSpringPropertyKey extends Recipe { |
| 29 | + |
| 30 | + @Override |
| 31 | + public String getDisplayName() { |
| 32 | + return "Comment out Spring properties"; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public String getDescription() { |
| 37 | + return "Add comment to specified Spring properties, and comment out the property."; |
| 38 | + } |
| 39 | + |
| 40 | + @Option(displayName = "Property key", |
| 41 | + description = "The name of the property key to comment out.", |
| 42 | + example = "management.metrics.binders.files.enabled") |
| 43 | + String propertyKey; |
| 44 | + |
| 45 | + @Option(displayName = "Comment", |
| 46 | + description = "Comment to replace the property key.", |
| 47 | + example = "This property is deprecated and no longer applicable starting from Spring Boot 3.0.x") |
| 48 | + String comment; |
| 49 | + |
| 50 | + @Override |
| 51 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 52 | + String inlineComment = " # " + comment; |
| 53 | + String regex = "(?<!" + inlineComment + ")$"; |
| 54 | + Recipe changeProperties = new org.openrewrite.properties.ChangePropertyValue(propertyKey, inlineComment, regex, true, null); |
| 55 | + Recipe changeYaml = new org.openrewrite.yaml.CommentOutProperty(propertyKey, comment) ; |
| 56 | + return new TreeVisitor<Tree, ExecutionContext>() { |
| 57 | + @Override |
| 58 | + public @Nullable Tree preVisit(@NonNull Tree tree, ExecutionContext ctx) { |
| 59 | + stopAfterPreVisit(); |
| 60 | + if (tree instanceof Properties.File) { |
| 61 | + return changeProperties.getVisitor().visit(tree, ctx); |
| 62 | + } else if (tree instanceof Yaml.Documents) { |
| 63 | + return changeYaml.getVisitor().visit(tree, ctx); |
| 64 | + } |
| 65 | + return tree; |
| 66 | + } |
| 67 | + }; |
| 68 | + } |
| 69 | +} |
0 commit comments