Skip to content

Commit b9ecb32

Browse files
author
Vincent Potucek
committed
Fix #5953: [java] PoC: Add rewrite support for errorprone.refasterrules
1 parent c1cd2cd commit b9ecb32

File tree

29 files changed

+256
-85
lines changed

29 files changed

+256
-85
lines changed

.github/workflows/build.yml

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
enableCrossOsArchive: true
3737
- name: Fast Build with Maven
3838
run: |
39-
./mvnw --show-version --errors --batch-mode \
39+
./mvnw --show-version --errors --batch-mode --no-transfer-progress \
4040
-Dmaven.repo.local=.m2/repository \
4141
verify -PfastSkip -DskipTests -Dcyclonedx.skip=false \
4242
deploy:deploy -DaltDeploymentRepository="dogfood::file://$(pwd)/target/staging"
@@ -97,7 +97,7 @@ jobs:
9797
name: compile-artifact
9898
- name: Full Build with Maven
9999
run: |
100-
./mvnw --show-version --errors --batch-mode \
100+
./mvnw --show-version --errors --batch-mode --no-transfer-progress \
101101
-Dmaven.repo.local=.m2/repository \
102102
verify -DskipTests
103103
- uses: actions/upload-artifact@v4
@@ -154,14 +154,42 @@ jobs:
154154
name: compile-artifact
155155
- name: Build with Maven and run unit tests
156156
run: |
157-
./mvnw --show-version --errors --batch-mode \
157+
./mvnw --show-version --errors --batch-mode --no-transfer-progress \
158158
-Dmaven.repo.local=.m2/repository \
159159
verify \
160160
-PfastSkip -Dcyclonedx.skip=false \
161161
-Djava8.home="${JAVA_HOME_8_X64}" \
162162
-Djava17.home="${JAVA_HOME_17_X64}" \
163163
-Djava21.home="${JAVA_HOME_21_X64}"
164164
165+
rewrite:
166+
needs: compile
167+
timeout-minutes: 90
168+
runs-on: ubuntu-latest
169+
defaults:
170+
run:
171+
shell: bash
172+
steps:
173+
- uses: actions/checkout@v4
174+
- uses: actions/setup-java@v4
175+
with:
176+
distribution: 'temurin'
177+
java-version: '11'
178+
- uses: actions/cache@v4
179+
with:
180+
key: maven-${{ hashFiles('**/pom.xml') }}
181+
restore-keys: maven-
182+
path: .m2/repository
183+
enableCrossOsArchive: true
184+
- uses: actions/download-artifact@v4
185+
with:
186+
name: compile-artifact
187+
- name: Rewrite
188+
run: |
189+
./mvnw --show-version --errors --batch-mode --no-transfer-progress \
190+
-Dmaven.repo.local=.m2/repository \
191+
clean rewrite:dryRun
192+
165193
dogfood:
166194
needs: compile
167195
timeout-minutes: 30
@@ -232,7 +260,7 @@ jobs:
232260
echo "::endgroup::"
233261
234262
echo "::group::Run ./mvnw verify"
235-
./mvnw --show-version --errors --batch-mode \
263+
./mvnw --show-version --errors --batch-mode --no-transfer-progress \
236264
--settings "${maven_settings_file}" \
237265
-Dmaven.repo.local=.m2/repository \
238266
verify \
@@ -272,7 +300,7 @@ jobs:
272300
name: compile-artifact
273301
- name: Generate rule docs
274302
run: |
275-
./mvnw --show-version --errors --batch-mode \
303+
./mvnw --show-version --errors --batch-mode --no-transfer-progress \
276304
-Dmaven.repo.local=.m2/repository \
277305
verify \
278306
-Pgenerate-rule-docs,fastSkip \

docs/pages/pmd/devdocs/contributing/contributing.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ There are various channels, on which you can ask questions:
8181

8282
* Ask your question our [PMD Guru at Gurubase](https://gurubase.io/g/pmd).
8383

84+
## 🚀 Code Transformation & Automated Rewriting
85+
*(Advanced Refactoring, Modernization, and Bulk Code Changes)*
86+
87+
#### 🔧 Automated rewriting of source code to
88+
- **Refactor** safely (e.g., rename methods, migrate APIs)
89+
- **Modernize** (e.g., Java 8 → Java 17 features)
90+
- **Patterns** (e.g., replace `Vector` with `ArrayList`)
91+
- **Enforce conventions** (e.g., JUnit's naming rules)
92+
93+
The build system incorporates [OpenRewrite](https://docs.openrewrite.org/) capabilities for automated code transformations.
94+
95+
- Check for compliance with `mvn rewrite:dryRun`.
96+
- Apply suggestions with `mvn rewrite:run`.
97+
8498
## Code Style
8599

86100
PMD uses [Checkstyle](https://checkstyle.org/) to enforce a common code style.
@@ -89,6 +103,8 @@ See [pmd-checkstyle-config.xml](https://github.com/pmd/build-tools/blob/main/src
89103
[the eclipse configuration files](https://github.com/pmd/build-tools/tree/main/eclipse) that can
90104
be imported into a fresh workspace.
91105

106+
Rewrite will automatically apply all rules, by using the CodeCleanup recipe like in this [showcase](https://docs.openrewrite.org/running-recipes/popular-recipe-guides/automatically-fix-checkstyle-violations).
107+
92108
## Add yourself as contributor
93109

94110
We use [All Contributors](https://allcontributors.org/en) - all our contributors are listed on the page [Credits](pmd_projectdocs_credits.html).

pmd-ant/src/main/java/net/sourceforge/pmd/ant/Formatter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ private void start(String baseDir) {
8787
Charset charset;
8888
{
8989
String s = (String) properties.get("encoding");
90-
if (null == s) {
90+
if (s == null) {
9191

9292
if (toConsole) {
9393
s = getConsoleEncoding();
94-
if (null == s) {
94+
if (s == null) {
9595
s = System.getProperty("file.encoding");
9696
}
9797
}
9898

99-
if (null == s) {
99+
if (s == null) {
100100
charset = StandardCharsets.UTF_8;
101101
} else {
102102
charset = Charset.forName(s);

pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTUserInterface.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ protected <P, R> R acceptApexVisitor(ApexVisitor<? super P, ? extends R> visitor
2525
* The type name does NOT include type arguments.
2626
*/
2727
public String getSuperInterfaceName() {
28-
return node.getExtendsTypes().stream().map(TypeRef::asTypeErasedString).findFirst().orElse("");
28+
return node.getExtendsTypes().stream().findFirst().map(TypeRef::asTypeErasedString).orElse("");
2929
}
3030
}

pmd-core/src/main/java/net/sourceforge/pmd/internal/Slf4jSimpleConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void reconfigureDefaultLogLevel(Level level) {
7373
Map<String, Logger> loggerMap = (Map<String, Logger>) loggerMapField.get(loggerFactory);
7474
for (Logger logger : loggerMap.values()) {
7575
if (logger.getName().startsWith(PMD_ROOT_LOGGER)
76-
&& simpleLoggerClass.isAssignableFrom(logger.getClass())) {
76+
&& simpleLoggerClass.isInstance(logger)) {
7777
String newConfiguredLevel = (String) levelStringMethod.invoke(logger);
7878
int newLogLevel = newDefaultLogLevel;
7979
if (newConfiguredLevel != null) {
@@ -132,7 +132,7 @@ public static boolean isSimpleLogger() {
132132
try {
133133
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
134134
Class<?> loggerFactoryClass = loggerFactory.getClass().getClassLoader().loadClass(SIMPLE_LOGGER_FACTORY_CLASS);
135-
return loggerFactoryClass.isAssignableFrom(loggerFactory.getClass());
135+
return loggerFactoryClass.isInstance(loggerFactory);
136136
} catch (ClassNotFoundException e) {
137137
// not slf4j simple logger
138138
return false;

pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/ParseException.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import java.util.LinkedHashSet;
88
import java.util.Set;
9-
import java.util.stream.Collectors;
109

1110
import org.checkerframework.checker.nullness.qual.NonNull;
1211
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -94,7 +93,7 @@ private static String makeMessage(@NonNull JavaccToken currentToken,
9493
expectedBranches.add(expected.toString());
9594
}
9695

97-
String expected = expectedBranches.stream().collect(Collectors.joining(System.lineSeparator() + " "));
96+
String expected = String.join(System.lineSeparator() + " ", expectedBranches);
9897

9998
StringBuilder retval = new StringBuilder("Encountered ");
10099
if (maxSize > 1) {

pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/MetricsUtil.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import java.util.DoubleSummaryStatistics;
88
import java.util.Objects;
9-
import java.util.stream.Collectors;
109
import java.util.stream.StreamSupport;
1110

1211
import net.sourceforge.pmd.lang.ast.Node;
@@ -65,8 +64,7 @@ public static <O extends Node> DoubleSummaryStatistics computeStatistics(Metric<
6564
Objects.requireNonNull(ops, NULL_NODE_MESSAGE);
6665

6766
return StreamSupport.stream(ops.spliterator(), false)
68-
.filter(key::supports)
69-
.collect(Collectors.summarizingDouble(op -> computeMetric(key, op, options).doubleValue()));
67+
.filter(key::supports).mapToDouble(op -> computeMetric(key, op, options).doubleValue()).summaryStatistics();
7068
}
7169

7270
/**

pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/AbstractRule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public boolean equals(Object o) {
286286

287287
AbstractRule that = (AbstractRule) o;
288288
return Objects.equals(getName(), that.getName())
289-
&& Objects.equals(getPriority(), that.getPriority())
289+
&& getPriority() == that.getPriority()
290290
&& super.equals(o);
291291
}
292292

pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/impl/AttributeAxisIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private boolean isConsideredReturnType(Method method) {
132132
try {
133133
// ignore type variables, such as List<N>… we could check all bounds, but probably it's overkill
134134
Type actualTypeArgument = ((ParameterizedType) t).getActualTypeArguments()[0];
135-
if (!TypeVariable.class.isAssignableFrom(actualTypeArgument.getClass())) {
135+
if (!TypeVariable.class.isInstance(actualTypeArgument)) {
136136
Class<?> elementKlass = Class.forName(actualTypeArgument.getTypeName());
137137
return CONSIDERED_RETURN_TYPES.contains(elementKlass) || elementKlass.isEnum();
138138
}

pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/internal/AstElementNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public String getStringValue() {
226226
// potentially text nodes
227227
return node.descendants(TextNode.class).toStream()
228228
.map(TextNode::getText)
229-
.collect(Collectors.joining(""));
229+
.collect(Collectors.joining());
230230
}
231231

232232
@Override

0 commit comments

Comments
 (0)