|
| 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.recipe; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Optional; |
| 26 | + |
| 27 | +import org.checkstyle.autofix.parser.CheckstyleViolation; |
| 28 | +import org.openrewrite.ExecutionContext; |
| 29 | +import org.openrewrite.Recipe; |
| 30 | +import org.openrewrite.Tree; |
| 31 | +import org.openrewrite.TreeVisitor; |
| 32 | +import org.openrewrite.java.JavaIsoVisitor; |
| 33 | +import org.openrewrite.java.tree.J; |
| 34 | +import org.openrewrite.java.tree.JavaSourceFile; |
| 35 | +import org.openrewrite.java.tree.Space; |
| 36 | + |
| 37 | +import com.puppycrawl.tools.checkstyle.api.CheckstyleException; |
| 38 | +import com.puppycrawl.tools.checkstyle.api.Configuration; |
| 39 | + |
| 40 | +public class Header extends Recipe { |
| 41 | + |
| 42 | + private static final String HEADER_PROP = "header"; |
| 43 | + private static final String HEADER_FILE_PROP = "headerFile"; |
| 44 | + |
| 45 | + private List<CheckstyleViolation> violations; |
| 46 | + private Configuration headerConfig; |
| 47 | + |
| 48 | + public Header(List<CheckstyleViolation> violations, Configuration headerConfig) { |
| 49 | + this.violations = violations; |
| 50 | + this.headerConfig = headerConfig; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String getDisplayName() { |
| 55 | + return "Header recipe"; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getDescription() { |
| 60 | + return "Adds headers to Java source files when missing."; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 65 | + final String licenseHeader = getLicenseHeader(headerConfig); |
| 66 | + return new HeaderVisitor(violations, licenseHeader); |
| 67 | + } |
| 68 | + |
| 69 | + private static String getLicenseHeader(Configuration child) { |
| 70 | + final String result; |
| 71 | + final String[] propertyNames = child.getPropertyNames(); |
| 72 | + final boolean hasHeaderProperty = Arrays.asList(propertyNames).contains(HEADER_PROP); |
| 73 | + try { |
| 74 | + if (hasHeaderProperty) { |
| 75 | + result = child.getProperty(HEADER_PROP); |
| 76 | + } |
| 77 | + else { |
| 78 | + final String headerFile = child.getProperty(HEADER_FILE_PROP); |
| 79 | + result = readHeaderFileContent(headerFile); |
| 80 | + } |
| 81 | + } |
| 82 | + catch (CheckstyleException exception) { |
| 83 | + throw new IllegalArgumentException("Failed to extract header from config", exception); |
| 84 | + } |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + private static String readHeaderFileContent(String headerFilePath) { |
| 89 | + final String content; |
| 90 | + try { |
| 91 | + content = Files.readString(Path.of(headerFilePath)); |
| 92 | + } |
| 93 | + catch (IOException exception) { |
| 94 | + throw new IllegalArgumentException("Failed to read: " + headerFilePath, exception); |
| 95 | + } |
| 96 | + return content; |
| 97 | + } |
| 98 | + |
| 99 | + private static class HeaderVisitor extends JavaIsoVisitor<ExecutionContext> { |
| 100 | + private final List<CheckstyleViolation> violations; |
| 101 | + private final String licenseHeader; |
| 102 | + |
| 103 | + HeaderVisitor(List<CheckstyleViolation> violations, String licenseHeader) { |
| 104 | + this.violations = violations; |
| 105 | + this.licenseHeader = licenseHeader; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public J visit(Tree tree, ExecutionContext ctx) { |
| 110 | + J result = super.visit(tree, ctx); |
| 111 | + |
| 112 | + if (tree instanceof JavaSourceFile) { |
| 113 | + JavaSourceFile sourceFile = (JavaSourceFile) tree; |
| 114 | + final Path absolutePath = sourceFile.getSourcePath().toAbsolutePath(); |
| 115 | + |
| 116 | + if (isAtViolation(absolutePath)) { |
| 117 | + sourceFile = sourceFile.withPrefix( |
| 118 | + Space.format(licenseHeader |
| 119 | + + System.lineSeparator())); |
| 120 | + } |
| 121 | + result = super.visit(sourceFile, ctx); |
| 122 | + } |
| 123 | + return result; |
| 124 | + } |
| 125 | + |
| 126 | + private boolean isAtViolation(Path currentFileName) { |
| 127 | + final Optional<CheckstyleViolation> match = violations.stream() |
| 128 | + .filter(violation -> { |
| 129 | + return violation.getColumn() == null |
| 130 | + && currentFileName.equals(Path.of(violation.getFileName()) |
| 131 | + .toAbsolutePath()); |
| 132 | + }).findFirst(); |
| 133 | + |
| 134 | + match.ifPresent(violations::remove); |
| 135 | + |
| 136 | + return match.isPresent(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments