Skip to content

Commit e9efb13

Browse files
committed
Add documentation, tests, and hit a wall as mutators are not detected
1 parent 23c97a4 commit e9efb13

File tree

12 files changed

+181
-12
lines changed

12 files changed

+181
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ lib('java.PalantirJavaFormatStep') +'{{yes}} | {{yes}}
8484
lib('java.RemoveUnusedImportsStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
8585
extra('java.EclipseJdtFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
8686
lib('java.FormatAnnotationsStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
87+
lib('java.CleanthatJavaStep') +'{{no}} | {{yes}} | {{no}} | {{no}} |',
8788
lib('json.gson.GsonStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
8889
lib('json.JacksonJsonStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
8990
lib('json.JsonSimpleStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
@@ -131,6 +132,7 @@ lib('yaml.JacksonYamlStep') +'{{yes}} | {{yes}}
131132
| [`java.RemoveUnusedImportsStep`](lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
132133
| [`java.EclipseJdtFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
133134
| [`java.FormatAnnotationsStep`](lib/src/main/java/com/diffplug/spotless/java/FormatAnnotationsStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
135+
| [`java.CleanthatJavaStep`](lib/src/main/java/com/diffplug/spotless/java/CleanthatJavaStep.java) | :white_large_square: | :+1: | :white_large_square: | :white_large_square: |
134136
| [`json.gson.GsonStep`](lib/src/main/java/com/diffplug/spotless/json/gson/GsonStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
135137
| [`json.JacksonJsonStep`](lib/src/main/java/com/diffplug/spotless/json/JacksonJsonStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
136138
| [`json.JsonSimpleStep`](lib/src/main/java/com/diffplug/spotless/json/JsonSimpleStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121

22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
2225
import com.diffplug.spotless.FormatterFunc;
2326

2427
import eu.solven.cleanthat.config.pojo.CleanthatEngineProperties;
@@ -28,6 +31,8 @@
2831
import eu.solven.cleanthat.formatter.LineEnding;
2932

3033
public class JavaCleanthatRefactorerFunc implements FormatterFunc {
34+
private static final Logger LOGGER = LoggerFactory.getLogger(JavaCleanthatRefactorerFunc.class);
35+
3136
private String jdkVersion;
3237
private List<String> included;
3338
private List<String> excluded;
@@ -53,6 +58,9 @@ public String apply(String input) throws Exception {
5358

5459
JavaRefactorer refactorer = new JavaRefactorer(engineProperties, refactorerProperties);
5560

61+
LOGGER.debug("Processing sourceJdk={} included={} excluded={}", jdkVersion, included, excluded);
62+
LOGGER.debug("Available mutators: {}", JavaRefactorer.getAllIncluded());
63+
5664
// Spotless calls steps always with LF eol.
5765
return refactorer.doFormat(input, LineEnding.LF);
5866
}

lib/src/main/java/com/diffplug/spotless/java/CleanthatStepFactory.java renamed to lib/src/main/java/com/diffplug/spotless/java/CleanthatJavaStep.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@
3434
* @author Benoit Lacelle
3535
*/
3636
// https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md#how-to-add-a-new-formatterstep
37-
public final class CleanthatStepFactory {
37+
public final class CleanthatJavaStep {
3838

3939
private static final String NAME = "cleanthat";
40-
private static final String MAVEN_COORDINATE = "io.github.solven-eu.cleanthat:java:";
40+
private static final String MAVEN_COORDINATE = "io.github.solven-eu.cleanthat:java";
4141

4242
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(11, "2.0");
4343

4444
// prevent direct instantiation
45-
private CleanthatStepFactory() {}
45+
private CleanthatJavaStep() {}
4646

4747
/** Creates a step which apply default CleanThat mutators. */
4848
public static FormatterStep create(Provisioner provisioner) {
@@ -83,7 +83,7 @@ public static FormatterStep create(String groupArtifact,
8383
Provisioner provisioner) {
8484
Objects.requireNonNull(groupArtifact, "groupArtifact");
8585
if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) {
86-
throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'");
86+
throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'. it was: " + groupArtifact);
8787
}
8888
Objects.requireNonNull(version, "version");
8989
Objects.requireNonNull(provisioner, "provisioner");

plugin-maven/README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ user@machine repo % mvn spotless:check
3939
- [Requirements](#requirements)
4040
- [Binding to maven phase](#binding-to-maven-phase)
4141
- **Languages**
42-
- [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [prettier](#prettier), [palantir-java-format](#palantir-java-format), [formatAnnotations](#formatAnnotations))
42+
- [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [prettier](#prettier), [palantir-java-format](#palantir-java-format), [formatAnnotations](#formatAnnotations), [cleanthat](#cleanthat))
4343
- [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy))
4444
- [Kotlin](#kotlin) ([ktfmt](#ktfmt), [ktlint](#ktlint), [diktat](#diktat), [prettier](#prettier))
4545
- [Scala](#scala) ([scalafmt](#scalafmt))
@@ -182,6 +182,8 @@ any other maven phase (i.e. compile) then it can be configured as below;
182182
<include>src/test/java/**/*.java</include>
183183
</includes>
184184

185+
<cleanthat /> <!-- has its own section below -->
186+
185187
<googleJavaFormat /> <!-- has its own section below -->
186188
<eclipse /> <!-- has its own section below -->
187189
<prettier /> <!-- has its own section below -->
@@ -274,6 +276,25 @@ list of well-known type annotations. You can make a pull request to add new one
274276
In the future there will be mechanisms to add/remove annotations from the list.
275277
These mechanisms already exist for the Gradle plugin.
276278

279+
### Cleanthat
280+
281+
[homepage](https://github.com/solven-eu/cleanthat). CleanThat enables automatic refactoring of Java code
282+
283+
```xml
284+
<cleanthat>
285+
<version>2.0</version> <!-- optional version of Cleanthat -->
286+
<sourceJdk>${maven.compiler.source}</sourceJdk> <!-- optional. Default to ${maven.compiler.source} else '1.7' -->
287+
<mutators>
288+
<mutator>*</mutator> <!-- optional. Default to '*' to include all mutators -->
289+
</mutators>
290+
<mutators> <!-- List of mutators: https://github.com/solven-eu/cleanthat/tree/master/java/src/main/java/eu/solven/cleanthat/engine/java/refactorer/mutators -->
291+
<mutator>LiteralsFirstInComparisons</mutator> <!-- You may alternatively list the requested mutators -->
292+
</mutators>
293+
<excludedMutators>
294+
<excludedMutator>OptionalNotEmpty</excludedMutator> <!-- You can discard specific rules -->
295+
</excludedMutators>
296+
</cleanthat>
297+
```
277298

278299
## Groovy
279300

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.apache.maven.plugins.annotations.Parameter;
2121

2222
import com.diffplug.spotless.FormatterStep;
23-
import com.diffplug.spotless.java.CleanthatStepFactory;
23+
import com.diffplug.spotless.java.CleanthatJavaStep;
2424
import com.diffplug.spotless.maven.FormatterStepConfig;
2525
import com.diffplug.spotless.maven.FormatterStepFactory;
2626

@@ -33,19 +33,19 @@ public class CleanthatJava implements FormatterStepFactory {
3333

3434
// https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#source
3535
@Parameter(property = "maven.compiler.source")
36-
private String sourceJdk = CleanthatStepFactory.defaultJdkVersion();
36+
private String sourceJdk = CleanthatJavaStep.defaultJdkVersion();
3737

3838
@Parameter
39-
private List<String> mutators = CleanthatStepFactory.defaultMutators();
39+
private List<String> mutators = CleanthatJavaStep.defaultMutators();
4040

4141
@Parameter
42-
private List<String> excludedMutators = CleanthatStepFactory.defaultExcludedMutators();
42+
private List<String> excludedMutators = CleanthatJavaStep.defaultExcludedMutators();
4343

4444
@Override
4545
public FormatterStep newFormatterStep(FormatterStepConfig config) {
46-
String groupArtifact = this.groupArtifact != null ? this.groupArtifact : CleanthatStepFactory.defaultGroupArtifact();
47-
String version = this.version != null ? this.version : CleanthatStepFactory.defaultVersion();
46+
String groupArtifact = this.groupArtifact != null ? this.groupArtifact : CleanthatJavaStep.defaultGroupArtifact();
47+
String version = this.version != null ? this.version : CleanthatJavaStep.defaultVersion();
4848

49-
return CleanthatStepFactory.create(groupArtifact, version, sourceJdk, mutators, excludedMutators, config.getProvisioner());
49+
return CleanthatJavaStep.create(groupArtifact, version, sourceJdk, mutators, excludedMutators, config.getProvisioner());
5050
}
5151
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public void addFormatAnnotations(FormatAnnotations formatAnnotations) {
7979
addStepFactory(formatAnnotations);
8080
}
8181

82+
public void addCleanthat(CleanthatJava cleanthat) {
83+
addStepFactory(cleanthat);
84+
}
85+
8286
private static String fileMask(Path path) {
8387
String dir = path.toString();
8488
if (!dir.endsWith(File.separator)) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2022-2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.java;
17+
18+
import org.assertj.core.api.Assertions;
19+
import org.junit.jupiter.api.Test;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
24+
25+
class CleanthatJavaRefactorerTest extends MavenIntegrationHarness {
26+
private static final Logger LOGGER = LoggerFactory.getLogger(CleanthatJavaRefactorerTest.class);
27+
28+
@Test
29+
void testLiteralsFirstInComparisons() throws Exception {
30+
writePomWithJavaSteps(
31+
"<cleanthat>",
32+
"</cleanthat>");
33+
34+
runTest("LiteralsFirstInComparisons.dirty.java", "LiteralsFirstInComparisons.clean.java");
35+
}
36+
37+
@Test
38+
void testMultipleMutators() throws Exception {
39+
writePomWithJavaSteps(
40+
"<cleanthat>",
41+
"</cleanthat>");
42+
43+
runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.java");
44+
}
45+
46+
@Test
47+
void testExcludeOptionalNotEmpty() throws Exception {
48+
writePomWithJavaSteps(
49+
"<cleanthat>",
50+
" <excludedMutators>",
51+
" <excludedMutator>OptionalNotEmpty</excludedMutator>",
52+
" </excludedMutators>",
53+
"</cleanthat>");
54+
55+
runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyLiteralsFirst.java");
56+
}
57+
58+
@Test
59+
void testIncludeOnlyLiteralsFirstInComparisons() throws Exception {
60+
writePomWithJavaSteps(
61+
"<cleanthat>",
62+
" <mutators>",
63+
" <mutator>LiteralsFirstInComparisons</mutator>",
64+
" </mutators>",
65+
"</cleanthat>");
66+
67+
runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyLiteralsFirst.java");
68+
}
69+
70+
private void runTest(String dirtyPath, String cleanPath) throws Exception {
71+
String path = "src/main/java/test.java";
72+
setFile(path).toResource("java/cleanthat/" + dirtyPath);
73+
Assertions.assertThat(mavenRunner().withArguments("spotless:apply -X").runNoError().stdOutUtf8()).isEmpty();
74+
assertFile(path).sameAsResource("java/cleanthat/" + cleanPath);
75+
}
76+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me;
2+
3+
public class LiteralsFirstInComparisonsCases {
4+
5+
public boolean isHardcoded(String input) {
6+
return "hardcoded".equals(input);
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me;
2+
3+
public class LiteralsFirstInComparisonsCases {
4+
5+
public boolean isHardcoded(String input) {
6+
return input.equals("hardcoded");
7+
}
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me;
2+
3+
import java.util.Optional;
4+
5+
public class LiteralsFirstInComparisonsCases {
6+
7+
public boolean isHardcoded(String input) {
8+
return input.equals("hardcoded");
9+
}
10+
11+
public boolean isPresent(Optional<?> optional) {
12+
return optional.isPresent();
13+
}
14+
}

0 commit comments

Comments
 (0)