Skip to content

Commit 8018947

Browse files
committed
added header recipe
1 parent a3c2db6 commit 8018947

File tree

9 files changed

+232
-2
lines changed

9 files changed

+232
-2
lines changed

src/main/java/org/checkstyle/autofix/parser/CheckstyleViolation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public CheckstyleViolation(Integer line, Integer column,
4141
this.fileName = fileName;
4242
}
4343

44-
public int getLine() {
44+
public Integer getLine() {
4545
return line;
4646
}
4747

48-
public int getColumn() {
48+
public Integer getColumn() {
4949
return column;
5050
}
5151

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.nio.file.Path;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.Objects;
24+
25+
import org.checkstyle.autofix.parser.CheckstyleViolation;
26+
import org.openrewrite.ExecutionContext;
27+
import org.openrewrite.Recipe;
28+
import org.openrewrite.Tree;
29+
import org.openrewrite.TreeVisitor;
30+
import org.openrewrite.java.JavaIsoVisitor;
31+
import org.openrewrite.java.tree.J;
32+
import org.openrewrite.java.tree.JavaSourceFile;
33+
import org.openrewrite.java.tree.Space;
34+
35+
public class Header extends Recipe {
36+
37+
private List<CheckstyleViolation> violations;
38+
private String licenseText;
39+
40+
public Header() {
41+
this.violations = new ArrayList<>();
42+
this.licenseText = "";
43+
}
44+
45+
public Header(List<CheckstyleViolation> violations, String licenseText) {
46+
this.violations = violations;
47+
this.licenseText = licenseText;
48+
}
49+
50+
@Override
51+
public String getDisplayName() {
52+
return "Add header";
53+
}
54+
55+
@Override
56+
public String getDescription() {
57+
return "Adds headers to Java source files when missing."
58+
+ " Does not override existing license headers.";
59+
}
60+
61+
@Override
62+
public TreeVisitor<?, ExecutionContext> getVisitor() {
63+
return new JavaIsoVisitor<>() {
64+
@Override
65+
public J visit(Tree tree, ExecutionContext ctx) {
66+
final J result;
67+
if (tree instanceof JavaSourceFile) {
68+
JavaSourceFile sourceFile =
69+
(JavaSourceFile) java.util.Objects.requireNonNull(tree);
70+
if (sourceFile.getComments().isEmpty()
71+
&& isAtViolationLocation(sourceFile.getSourcePath().toString())) {
72+
sourceFile = sourceFile
73+
.withPrefix(Space.format(licenseText + "\n"));
74+
}
75+
result = super.visit(sourceFile, ctx);
76+
}
77+
else {
78+
result = super.visit(tree, ctx);
79+
}
80+
return result;
81+
}
82+
};
83+
}
84+
85+
private boolean isAtViolationLocation(String currentFileName) {
86+
87+
return violations.stream().anyMatch(violation -> {
88+
return violation.getLine() == 1
89+
&& Objects.isNull(violation.getColumn())
90+
&& Path.of(violation.getFileName()).equals(Path.of(currentFileName));
91+
});
92+
}
93+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.List;
24+
25+
import org.checkstyle.autofix.parser.CheckstyleReportParser;
26+
import org.checkstyle.autofix.parser.CheckstyleViolation;
27+
import org.junit.jupiter.api.Test;
28+
import org.openrewrite.Recipe;
29+
30+
public class HeaderTest extends AbstractRecipeTest {
31+
32+
@Override
33+
protected Recipe getRecipe() {
34+
final String reportPath = "src/test/resources/org/checkstyle/autofix/recipe/header"
35+
+ "/report.xml";
36+
final String licensePath = "src/test/resources/org/checkstyle/autofix/recipe/header"
37+
+ "/header.txt";
38+
final List<CheckstyleViolation> violations =
39+
CheckstyleReportParser.parse(Path.of(reportPath));
40+
final String licenseText;
41+
try {
42+
licenseText = Files.readString(Path.of(licensePath));
43+
}
44+
catch (IOException exception) {
45+
throw new IllegalArgumentException("Failed to read: " + licensePath, exception);
46+
}
47+
return new Header(violations, licenseText);
48+
}
49+
50+
@Test
51+
void headerTest() throws IOException {
52+
testRecipe("header", "HeaderBlankLines");
53+
}
54+
55+
@Test
56+
void headerCommentTest() throws IOException {
57+
testRecipe("header", "HeaderComments");
58+
}
59+
60+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
///////////////////////////////////////////////////////////////////////////////////////////////
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.checkstyle.autofix.recipe.header.headerblanklines;
2+
3+
public class InputHeaderBlankLines {
4+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
package org.checkstyle.autofix.recipe.header.headerblanklines;
18+
19+
public class OutputHeaderBlankLines {
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.checkstyle.autofix.recipe.header.headercomments;
2+
3+
public class InputHeaderComments {
4+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
package org.checkstyle.autofix.recipe.header.headercomments;
18+
19+
public class OutputHeaderComments {
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<checkstyle version="10.12.3">
3+
<file name="org/checkstyle/autofix/recipe/header/headerblanklines/InputHeaderBlankLines.java">
4+
<error line="1" severity="error"
5+
message="Line does not match expected header line"
6+
source="header"/>
7+
</file>
8+
<file name="org/checkstyle/autofix/recipe/header/headercomments/InputHeaderComments.java">
9+
<error line="1" severity="error"
10+
message="Line does not match expected header line"
11+
source="header"/>
12+
</file>
13+
</checkstyle>

0 commit comments

Comments
 (0)