|
| 1 | +/////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite. |
| 3 | +// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +/////////////////////////////////////////////////////////////////////////////////////////////// |
| 17 | + |
| 18 | +package org.checkstyle.autofix; |
| 19 | + |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +import org.openrewrite.ExecutionContext; |
| 23 | +import org.openrewrite.Recipe; |
| 24 | +import org.openrewrite.Tree; |
| 25 | +import org.openrewrite.TreeVisitor; |
| 26 | +import org.openrewrite.java.JavaIsoVisitor; |
| 27 | +import org.openrewrite.java.tree.J; |
| 28 | +import org.openrewrite.java.tree.JavaSourceFile; |
| 29 | +import org.openrewrite.java.tree.Space; |
| 30 | + |
| 31 | +public class InputConfigRemover extends Recipe { |
| 32 | + |
| 33 | + @Override |
| 34 | + public String getDisplayName() { |
| 35 | + return "InputConfigRemover recipe"; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public String getDescription() { |
| 40 | + return "Remove inline config."; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 45 | + return new CommentVisitor(); |
| 46 | + } |
| 47 | + |
| 48 | + private static final class CommentVisitor extends JavaIsoVisitor<ExecutionContext> { |
| 49 | + |
| 50 | + @Override |
| 51 | + public J visit(Tree tree, ExecutionContext ctx) { |
| 52 | + J result = super.visit(tree, ctx); |
| 53 | + |
| 54 | + if (tree instanceof JavaSourceFile) { |
| 55 | + JavaSourceFile sourceFile = (JavaSourceFile) tree; |
| 56 | + |
| 57 | + final String currentHeader = extractCurrentHeader(sourceFile); |
| 58 | + |
| 59 | + if (currentHeader.startsWith("/*xml")) { |
| 60 | + sourceFile = sourceFile.withPrefix(Space.format("")); |
| 61 | + } |
| 62 | + |
| 63 | + result = super.visit(sourceFile, ctx); |
| 64 | + } |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + private String extractCurrentHeader(JavaSourceFile sourceFile) { |
| 69 | + return sourceFile.getComments().stream() |
| 70 | + .map(comment -> comment.printComment(getCursor())) |
| 71 | + .collect(Collectors.joining(System.lineSeparator())); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments