Skip to content

Commit 57effb8

Browse files
authored
Introduce javaparserVersion option to Cleanthat (#2903)
2 parents 83b5011 + a162f5e commit 57effb8

10 files changed

Lines changed: 85 additions & 13 deletions

File tree

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
14+
- Add `javaparserVersion` option to the Cleanthat step, allowing callers to override the JavaParser version pulled in transitively by Cleanthat. ([#2903](https://github.com/diffplug/spotless/pull/2903))
15+
### Changes
16+
- Bump default `cleanthat` version `2.24` -> `2.25`. ([#2903](https://github.com/diffplug/spotless/pull/2903))
1317

1418
## [4.5.0] - 2026-03-18
1519
### Added

lib/src/cleanthat/java/com/diffplug/spotless/glue/java/JavaCleanthatRefactorerFunc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private String doApply(String input, File file) throws IOException {
8787

8888
JavaRefactorer refactorer = new JavaRefactorer(engineProperties, refactorerProperties);
8989

90-
LOGGER.debug("Processing sourceJdk={} included={} excluded={}", jdkVersion, included, excluded, includeDraft);
90+
LOGGER.debug("Processing sourceJdk={} included={} excluded={} includeDraft={}", jdkVersion, included, excluded, includeDraft);
9191
LOGGER.debug("Available mutators: {}", JavaRefactorer.getAllIncluded());
9292

9393
PathAndContent pathAndContent = new PathAndContent(file.toPath(), input);

lib/src/main/java/com/diffplug/spotless/java/CleanthatJavaStep.java

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2025 DiffPlug
2+
* Copyright 2023-2026 DiffPlug
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.
@@ -21,6 +21,7 @@
2121
import java.lang.reflect.Constructor;
2222
import java.lang.reflect.Method;
2323
import java.nio.file.Path;
24+
import java.util.ArrayList;
2425
import java.util.List;
2526
import java.util.Objects;
2627

@@ -31,6 +32,8 @@
3132
import com.diffplug.spotless.Jvm;
3233
import com.diffplug.spotless.Provisioner;
3334

35+
import edu.umd.cs.findbugs.annotations.Nullable;
36+
3437
/**
3538
* Enables CleanThat as a SpotLess step.
3639
*
@@ -42,26 +45,30 @@ public final class CleanthatJavaStep implements Serializable {
4245
private static final long serialVersionUID = 1L;
4346
private static final String NAME = "cleanthat";
4447
private static final String MAVEN_COORDINATE = "io.github.solven-eu.cleanthat:java";
48+
private static final String JAVAPARSER_MAVEN_COORDINATE = "com.github.javaparser:javaparser-symbol-solver-core";
4549
/**
4650
* CleanThat changelog is available at <a href="https://github.com/solven-eu/cleanthat/blob/master/CHANGES.MD">here</a>.
4751
*/
48-
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(17, "2.24");
52+
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(17, "2.25");
4953

5054
private final JarState.Promised jarState;
5155
private final String version;
56+
private final String javaparserVersion;
5257
private final String sourceJdkVersion;
5358
private final List<String> included;
5459
private final List<String> excluded;
5560
private final boolean includeDraft;
5661

5762
private CleanthatJavaStep(JarState.Promised jarState,
5863
String version,
64+
String javaparserVersion,
5965
String sourceJdkVersion,
6066
List<String> included,
6167
List<String> excluded,
6268
boolean includeDraft) {
6369
this.jarState = jarState;
6470
this.version = version;
71+
this.javaparserVersion = javaparserVersion;
6572

6673
this.sourceJdkVersion = sourceJdkVersion;
6774
this.included = included;
@@ -76,7 +83,14 @@ public static FormatterStep create(Provisioner provisioner) {
7683

7784
/** Creates a step that applies default CleanThat mutators. */
7885
public static FormatterStep create(String version, Provisioner provisioner) {
79-
return createWithStepName(NAME, MAVEN_COORDINATE, version, defaultSourceJdk(), defaultMutators(), defaultExcludedMutators(), defaultIncludeDraft(), provisioner);
86+
return createWithStepName(NAME, MAVEN_COORDINATE, version, defaultJavaparserVersion(), defaultSourceJdk(), defaultMutators(), defaultExcludedMutators(), defaultIncludeDraft(), provisioner);
87+
}
88+
89+
/**
90+
* Default JavaParser version: {@code null}, meaning whichever transitive version is brought in by Cleanthat.
91+
*/
92+
@Nullable public static String defaultJavaparserVersion() {
93+
return null;
8094
}
8195

8296
public static String defaultSourceJdk() {
@@ -106,6 +120,7 @@ public static boolean defaultIncludeDraft() {
106120
static FormatterStep createWithStepName(String stepName,
107121
String groupArtifact,
108122
String version,
123+
String javaparserVersion,
109124
String sourceJdkVersion,
110125
List<String> included,
111126
List<String> excluded,
@@ -117,8 +132,14 @@ static FormatterStep createWithStepName(String stepName,
117132
}
118133
Objects.requireNonNull(version, "version");
119134
Objects.requireNonNull(provisioner, "provisioner");
135+
List<String> coordinates = new ArrayList<>();
136+
coordinates.add(groupArtifact + ":" + version);
137+
if (javaparserVersion != null) {
138+
// Added alongside Cleanthat so dependency resolution can upgrade the transitive JavaParser.
139+
coordinates.add(JAVAPARSER_MAVEN_COORDINATE + ":" + javaparserVersion);
140+
}
120141
return FormatterStep.create(stepName,
121-
new CleanthatJavaStep(JarState.promise(() -> JarState.from(groupArtifact + ":" + version, provisioner)), version, sourceJdkVersion, included, excluded, includeDraft),
142+
new CleanthatJavaStep(JarState.promise(() -> JarState.from(coordinates, provisioner)), version, javaparserVersion, sourceJdkVersion, included, excluded, includeDraft),
122143
CleanthatJavaStep::equalityState,
123144
State::createFormat);
124145
}
@@ -131,7 +152,19 @@ public static FormatterStep create(String groupArtifact,
131152
List<String> excluded,
132153
boolean includeDraft,
133154
Provisioner provisioner) {
134-
return createWithStepName(NAME, groupArtifact, version, sourceJdkVersion, included, excluded, includeDraft, provisioner);
155+
return createWithStepName(NAME, groupArtifact, version, defaultJavaparserVersion(), sourceJdkVersion, included, excluded, includeDraft, provisioner);
156+
}
157+
158+
/** Creates a step that applies selected CleanThat mutators, with a custom JavaParser version. */
159+
public static FormatterStep create(String groupArtifact,
160+
String version,
161+
String javaparserVersion,
162+
String sourceJdkVersion,
163+
List<String> included,
164+
List<String> excluded,
165+
boolean includeDraft,
166+
Provisioner provisioner) {
167+
return createWithStepName(NAME, groupArtifact, version, javaparserVersion, sourceJdkVersion, included, excluded, includeDraft, provisioner);
135168
}
136169

137170
/** Get default formatter version */
@@ -144,7 +177,7 @@ public static String defaultGroupArtifact() {
144177
}
145178

146179
private State equalityState() {
147-
return new State(jarState.get(), version, sourceJdkVersion, included, excluded, includeDraft);
180+
return new State(jarState.get(), version, javaparserVersion, sourceJdkVersion, included, excluded, includeDraft);
148181
}
149182

150183
private static final class State implements Serializable {
@@ -153,13 +186,15 @@ private static final class State implements Serializable {
153186

154187
private final JarState jarState;
155188
private final String version;
189+
private final String javaparserVersion;
156190
private final String sourceJdkVersion;
157191
private final List<String> included;
158192
private final List<String> excluded;
159193
private final boolean includeDraft;
160194

161195
State(JarState jarState,
162196
String version,
197+
String javaparserVersion,
163198
String sourceJdkVersion,
164199
List<String> included,
165200
List<String> excluded,
@@ -168,6 +203,7 @@ private static final class State implements Serializable {
168203
ModuleHelper.doOpenInternalPackagesIfRequired();
169204
this.jarState = jarState;
170205
this.version = version;
206+
this.javaparserVersion = javaparserVersion;
171207
this.sourceJdkVersion = sourceJdkVersion;
172208
this.included = included;
173209
this.excluded = excluded;

lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2025 DiffPlug
2+
* Copyright 2016-2026 DiffPlug
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.
@@ -53,7 +53,7 @@ public static FormatterStep create(String unusedImportRemover, Provisioner provi
5353
case GJF:
5454
return GoogleJavaFormatStep.createRemoveUnusedImportsOnly(provisioner);
5555
case CLEANTHAT:
56-
return CleanthatJavaStep.createWithStepName(NAME, CleanthatJavaStep.defaultGroupArtifact(), CleanthatJavaStep.defaultVersion(), "99.9", List.of(CLEANTHAT_MUTATOR), List.of(), false, provisioner);
56+
return CleanthatJavaStep.createWithStepName(NAME, CleanthatJavaStep.defaultGroupArtifact(), CleanthatJavaStep.defaultVersion(), CleanthatJavaStep.defaultJavaparserVersion(), "99.9", List.of(CLEANTHAT_MUTATOR), List.of(), false, provisioner);
5757
default:
5858
throw new IllegalArgumentException("Invalid unusedImportRemover: " + unusedImportRemover);
5959
}

plugin-gradle/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
- Add `withIndentStyle` and `withIndentSize` configuration to `tableTestFormatter` for setting the fallback indent when no `.editorconfig` is found. ([#2893](https://github.com/diffplug/spotless/pull/2893))
8+
- Add `javaparserVersion(...)` to `cleanthat`, allowing users to override the JavaParser version pulled in transitively by Cleanthat. ([#2903](https://github.com/diffplug/spotless/pull/2903))
89
### Fixed
910
- Fix `tableTestFormatter` editorconfig cache not honoring `.editorconfig` changes across Gradle daemon runs due to a shared static `EditorConfigProvider`. ([#2893](https://github.com/diffplug/spotless/pull/2893))
11+
### Changes
12+
- Bump default `cleanthat` version `2.24` -> `2.25`. ([#2903](https://github.com/diffplug/spotless/pull/2903))
1013

1114
## [8.4.0] - 2026-03-18
1215
### Added

plugin-gradle/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,8 @@ spotless {
422422
// optional: you can specify a specific version and/or config file
423423
cleanthat()
424424
.groupArtifact('io.github.solven-eu.cleanthat:java') // Optional. Default is 'io.github.solven-eu.cleanthat:java'
425-
.version('2.8') // You may force a custom version of Cleanthat
425+
.version('2.25') // You may force a custom version of Cleanthat
426+
.javaparserVersion('3.26.4') // Advanced: override the JavaParser version transitively pulled by Cleanthat
426427
.sourceCompatibility('1.7') // default is '1.7'
427428
.addMutator('SafeAndConsensual') // Default includes the SafeAndConsensual composite mutator
428429
.addMutator('your.custom.MagicMutator') // List of mutators: https://github.com/solven-eu/cleanthat/blob/master/MUTATORS.generated.MD

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ public class CleanthatJavaConfig {
433433

434434
private String version = CleanthatJavaStep.defaultVersion();
435435

436+
private String javaparserVersion = CleanthatJavaStep.defaultJavaparserVersion();
437+
436438
private String sourceJdk = CleanthatJavaStep.defaultSourceJdk();
437439

438440
private List<String> mutators = new ArrayList<>(CleanthatJavaStep.defaultMutators());
@@ -459,6 +461,18 @@ public CleanthatJavaConfig version(String version) {
459461
return this;
460462
}
461463

464+
/**
465+
* Advanced: override the version of JavaParser pulled in transitively by Cleanthat. The coordinate
466+
* {@code com.github.javaparser:javaparser-symbol-solver-core:<version>} is appended to the resolved classpath;
467+
* standard dependency resolution rules then apply (newest wins).
468+
*/
469+
public CleanthatJavaConfig javaparserVersion(String javaparserVersion) {
470+
Objects.requireNonNull(javaparserVersion);
471+
this.javaparserVersion = javaparserVersion;
472+
replaceStep(createStep());
473+
return this;
474+
}
475+
462476
public CleanthatJavaConfig sourceCompatibility(String jdkVersion) {
463477
Objects.requireNonNull(jdkVersion);
464478
this.sourceJdk = jdkVersion;
@@ -504,6 +518,7 @@ private FormatterStep createStep() {
504518
return CleanthatJavaStep.create(
505519
groupArtifact,
506520
version,
521+
javaparserVersion,
507522
sourceJdk, mutators, excludedMutators, includeDraft, provisioner());
508523
}
509524
}

plugin-maven/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
- Add `<javaparserVersion>` option to `<cleanthat>`, allowing users to override the JavaParser version pulled in transitively by Cleanthat. ([#2903](https://github.com/diffplug/spotless/pull/2903))
8+
### Changes
9+
- Bump default `cleanthat` version `2.24` -> `2.25`. ([#2903](https://github.com/diffplug/spotless/pull/2903))
610

711
## [3.4.0] - 2026-03-18
812
### Added

plugin-maven/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ These mechanisms already exist for the Gradle plugin.
386386

387387
```xml
388388
<cleanthat>
389-
<version>2.8</version> <!-- optional version of Cleanthat -->
389+
<version>2.25</version> <!-- optional version of Cleanthat -->
390+
<javaparserVersion>3.26.4</javaparserVersion> <!-- optional. Advanced: override the JavaParser version pulled in transitively by Cleanthat -->
390391
<sourceJdk>${maven.compiler.source}</sourceJdk> <!-- optional. Default to ${maven.compiler.source} else '1.7' -->
391392
<mutators>
392393
<mutator>SafeAndConsensual</mutator> <!-- optional. Default to 'SafeAndConsensual' to include all mutators -->

plugin-maven/src/main/java/com/diffplug/spotless/maven/java/CleanthatJava.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2026 DiffPlug
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.
@@ -31,6 +31,14 @@ public class CleanthatJava implements FormatterStepFactory {
3131
@Parameter
3232
private String version;
3333

34+
/**
35+
* Optional: override the version of JavaParser pulled in as a transitive dependency of Cleanthat. Advanced use-case:
36+
* declaring a version here appends {@code com.github.javaparser:javaparser-symbol-solver-core:<javaparserVersion>}
37+
* to the resolved classpath, so standard Maven resolution rules (newest wins) apply. At your own risk.
38+
*/
39+
@Parameter
40+
private String javaparserVersion;
41+
3442
// https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#source
3543
@Parameter(property = "maven.compiler.source")
3644
private String sourceJdk = CleanthatJavaStep.defaultSourceJdk();
@@ -49,6 +57,6 @@ public FormatterStep newFormatterStep(FormatterStepConfig config) {
4957
String groupArtifact = this.groupArtifact != null ? this.groupArtifact : CleanthatJavaStep.defaultGroupArtifact();
5058
String version = this.version != null ? this.version : CleanthatJavaStep.defaultVersion();
5159

52-
return CleanthatJavaStep.create(groupArtifact, version, sourceJdk, mutators, excludedMutators, includeDraft, config.getProvisioner());
60+
return CleanthatJavaStep.create(groupArtifact, version, javaparserVersion, sourceJdk, mutators, excludedMutators, includeDraft, config.getProvisioner());
5361
}
5462
}

0 commit comments

Comments
 (0)