|
| 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.ArrayList; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.Iterator; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import org.checkstyle.autofix.parser.CheckstyleViolation; |
| 29 | +import org.openrewrite.ExecutionContext; |
| 30 | +import org.openrewrite.Recipe; |
| 31 | +import org.openrewrite.Tree; |
| 32 | +import org.openrewrite.TreeVisitor; |
| 33 | +import org.openrewrite.java.JavaIsoVisitor; |
| 34 | +import org.openrewrite.java.tree.Comment; |
| 35 | +import org.openrewrite.java.tree.J; |
| 36 | +import org.openrewrite.java.tree.JavaSourceFile; |
| 37 | +import org.openrewrite.java.tree.Space; |
| 38 | + |
| 39 | +import com.puppycrawl.tools.checkstyle.api.CheckstyleException; |
| 40 | +import com.puppycrawl.tools.checkstyle.api.Configuration; |
| 41 | + |
| 42 | +public class Header extends Recipe { |
| 43 | + |
| 44 | + private static final String HEADER_LITERAL = "header"; |
| 45 | + private static final String NEWLINE = "\n"; |
| 46 | + |
| 47 | + private static int violationLine; |
| 48 | + |
| 49 | + private List<CheckstyleViolation> violations; |
| 50 | + private Configuration headerConfig; |
| 51 | + |
| 52 | + public Header() { |
| 53 | + this.violations = new ArrayList<>(); |
| 54 | + this.headerConfig = null; |
| 55 | + } |
| 56 | + |
| 57 | + public Header(List<CheckstyleViolation> violations, Configuration headerConfig) { |
| 58 | + this.violations = violations; |
| 59 | + this.headerConfig = headerConfig; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public String getDisplayName() { |
| 64 | + return "Header recipe"; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public String getDescription() { |
| 69 | + return "Adds headers to Java source files when missing."; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 74 | + return new HeaderVisitor(violations, headerConfig); |
| 75 | + } |
| 76 | + |
| 77 | + private static String getLicenseHeader(Configuration child) { |
| 78 | + final String result; |
| 79 | + final String[] propertyNames = child.getPropertyNames(); |
| 80 | + final boolean hasHeaderProperty = Arrays.asList(propertyNames).contains(HEADER_LITERAL); |
| 81 | + try { |
| 82 | + if (hasHeaderProperty) { |
| 83 | + result = child.getProperty(HEADER_LITERAL); |
| 84 | + } |
| 85 | + else { |
| 86 | + final String headerFile = child.getProperty("headerFile"); |
| 87 | + result = readHeaderFileContent(headerFile); |
| 88 | + } |
| 89 | + } |
| 90 | + catch (CheckstyleException exception) { |
| 91 | + throw new IllegalArgumentException("Failed to extract header from config", exception); |
| 92 | + } |
| 93 | + return result; |
| 94 | + } |
| 95 | + |
| 96 | + private static String readHeaderFileContent(String headerFilePath) { |
| 97 | + final String content; |
| 98 | + try { |
| 99 | + content = Files.readString(Path.of(headerFilePath)); |
| 100 | + } |
| 101 | + catch (IOException exception) { |
| 102 | + throw new IllegalArgumentException("Failed to read: " + headerFilePath, exception); |
| 103 | + } |
| 104 | + return content; |
| 105 | + } |
| 106 | + |
| 107 | + private static class HeaderVisitor extends JavaIsoVisitor<ExecutionContext> { |
| 108 | + private final List<CheckstyleViolation> violations; |
| 109 | + private final Configuration headerConfig; |
| 110 | + |
| 111 | + HeaderVisitor(List<CheckstyleViolation> violations, Configuration headerConfig) { |
| 112 | + this.violations = violations; |
| 113 | + this.headerConfig = headerConfig; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public J visit(Tree tree, ExecutionContext ctx) { |
| 118 | + J result = super.visit(tree, ctx); |
| 119 | + |
| 120 | + if (tree instanceof JavaSourceFile) { |
| 121 | + JavaSourceFile sourceFile = (JavaSourceFile) java.util.Objects.requireNonNull(tree); |
| 122 | + final Path absolutePath = sourceFile.getSourcePath().toAbsolutePath(); |
| 123 | + |
| 124 | + if (sourceFile.getComments().isEmpty() && isAtViolation(absolutePath)) { |
| 125 | + sourceFile = sourceFile.withPrefix( |
| 126 | + Space.format(getLicenseHeader(headerConfig) |
| 127 | + + System.lineSeparator().repeat(2))); |
| 128 | + } |
| 129 | + else if (!sourceFile.getComments().isEmpty() && isAtViolation(absolutePath)) { |
| 130 | + final StringBuilder sourceHeaderBuilder = new StringBuilder(); |
| 131 | + for (Comment comment : sourceFile.getComments()) { |
| 132 | + sourceHeaderBuilder.append(comment |
| 133 | + .printComment(getCursor())).append(NEWLINE); |
| 134 | + } |
| 135 | + final String sourceHeader = sourceHeaderBuilder.toString(); |
| 136 | + final String actualHeader = getLicenseHeader(headerConfig); |
| 137 | + |
| 138 | + final String[] sourceLines = sourceHeader.split(NEWLINE); |
| 139 | + final String[] actualLines = actualHeader.split(NEWLINE); |
| 140 | + |
| 141 | + sourceLines[violationLine - 1] = actualLines[violationLine - 1]; |
| 142 | + final String newHeader = String.join(NEWLINE, sourceLines); |
| 143 | + |
| 144 | + sourceFile = sourceFile.withComments(new ArrayList<>()); |
| 145 | + sourceFile = sourceFile.withPrefix( |
| 146 | + Space.format(newHeader + System.lineSeparator().repeat(2))); |
| 147 | + } |
| 148 | + result = super.visit(sourceFile, ctx); |
| 149 | + } |
| 150 | + return result; |
| 151 | + } |
| 152 | + |
| 153 | + private boolean isAtViolation(Path currentFileName) { |
| 154 | + boolean found = false; |
| 155 | + final Iterator<CheckstyleViolation> iterator = violations.iterator(); |
| 156 | + |
| 157 | + while (iterator.hasNext()) { |
| 158 | + final CheckstyleViolation violation = iterator.next(); |
| 159 | + final Path absolutePath = Path.of(violation.getFileName()).toAbsolutePath(); |
| 160 | + |
| 161 | + if (violation.getColumn() == null && absolutePath.equals(currentFileName)) { |
| 162 | + violationLine = violation.getLine(); |
| 163 | + iterator.remove(); |
| 164 | + found = true; |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | + return found; |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments