Skip to content

Commit 601aa27

Browse files
committed
Tolerate breaking change in Checkstyle 10.22
In Checkstyle 10.22 the scope property on JavadocVariableCheck was renamed to accessModifiers. This commit tolerates this change by templating spring-checkstyle.xml and replacing the property name in the XML based on the name of the method that's available on JavadocVariableCheck. Closes gh-433
1 parent bdd396f commit 601aa27

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/SpringConfigurationLoader.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 the original author or authors.
2+
* Copyright 2017-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,11 @@
1616

1717
package io.spring.javaformat.checkstyle;
1818

19+
import java.io.IOException;
1920
import java.io.InputStream;
21+
import java.io.StringReader;
22+
import java.io.UncheckedIOException;
23+
import java.nio.charset.StandardCharsets;
2024
import java.util.Arrays;
2125
import java.util.Collection;
2226
import java.util.stream.Collectors;
@@ -30,6 +34,8 @@
3034
import com.puppycrawl.tools.checkstyle.api.Context;
3135
import com.puppycrawl.tools.checkstyle.api.Contextualizable;
3236
import com.puppycrawl.tools.checkstyle.api.FileSetCheck;
37+
import com.puppycrawl.tools.checkstyle.api.Scope;
38+
import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocVariableCheck;
3339
import org.xml.sax.InputSource;
3440

3541
/**
@@ -50,17 +56,45 @@ class SpringConfigurationLoader {
5056
}
5157

5258
public Collection<FileSetCheck> load(PropertyResolver propertyResolver) {
53-
Configuration config = loadConfiguration(getClass().getResourceAsStream("spring-checkstyle.xml"),
54-
propertyResolver);
59+
Configuration config = loadConfiguration(loadConfigurationSource(), propertyResolver);
5560
return Arrays.stream(config.getChildren())
5661
.filter(this.moduleFactory::nonFiltered)
5762
.map(this::load)
5863
.collect(Collectors.toList());
5964
}
6065

61-
private Configuration loadConfiguration(InputStream inputStream, PropertyResolver propertyResolver) {
66+
private String loadConfigurationSource() {
67+
try (InputStream stream = getClass().getResourceAsStream("spring-checkstyle.xml")) {
68+
StringBuilder builder = new StringBuilder();
69+
byte[] buffer = new byte[4096];
70+
int read;
71+
while ((read = stream.read(buffer)) > 0) {
72+
builder.append(new String(buffer, 0, read, StandardCharsets.UTF_8));
73+
}
74+
return preprocessConfigurationSource(builder.toString());
75+
}
76+
catch (IOException ex) {
77+
throw new UncheckedIOException(ex);
78+
}
79+
}
80+
81+
private String preprocessConfigurationSource(String source) {
82+
return source.replace("{{javadocVariableCheckScopeProperty}}", javadocVariableCheckScopeProperty());
83+
}
84+
85+
private String javadocVariableCheckScopeProperty() {
86+
try {
87+
JavadocVariableCheck.class.getMethod("setScope", Scope.class);
88+
return "scope";
89+
}
90+
catch (NoSuchMethodException ex) {
91+
return "accessModifiers";
92+
}
93+
}
94+
95+
private Configuration loadConfiguration(String source, PropertyResolver propertyResolver) {
6296
try {
63-
InputSource inputSource = new InputSource(inputStream);
97+
InputSource inputSource = new InputSource(new StringReader(source));
6498
return ConfigurationLoader.loadConfiguration(inputSource, propertyResolver, IgnoredModulesOptions.EXECUTE);
6599
}
66100
catch (CheckstyleException ex) {

spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/spring-checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
</module>
9191
<module name="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck" />
9292
<module name="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocVariableCheck">
93-
<property name="scope" value="public"/>
93+
<property name="{{javadocVariableCheckScopeProperty}}" value="public"/>
9494
</module>
9595
<module name="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck">
9696
<property name="checkEmptyJavadoc" value="true"/>

0 commit comments

Comments
 (0)