Skip to content

Commit be0e0a5

Browse files
author
Vincent Potucek
committed
feat: Add ReplaceObsoletesStep
1 parent 2d32ccd commit be0e0a5

File tree

16 files changed

+467
-0
lines changed

16 files changed

+467
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2016-2025 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.generic;
17+
18+
import static java.util.regex.Pattern.MULTILINE;
19+
import static java.util.regex.Pattern.compile;
20+
21+
import java.io.File;
22+
23+
import com.diffplug.spotless.FormatterStep;
24+
25+
public final class ReplaceObsoletesStep implements FormatterStep {
26+
27+
private static final long serialVersionUID = -6643164760547140534L;
28+
29+
private ReplaceObsoletesStep() {}
30+
31+
public static FormatterStep forJava() {
32+
return new ReplaceObsoletesStep();
33+
}
34+
35+
@Override
36+
public String getName() {
37+
return "replaceObsoletes";
38+
}
39+
40+
@Override
41+
public String format(String rawUnix, File file) throws Exception {
42+
return removeRedundantInitializations("float|double", "0(?:\\.0)?(?:f|d)?",
43+
removeRedundantInitializations("int|long|short|byte", "0(?:L)?",
44+
removeRedundantInitializations("String|\\w+", "null",
45+
removeRedundantInitializations("boolean", "false",
46+
replaceLineSeparator(rawUnix)))));
47+
}
48+
49+
private static String replaceLineSeparator(String input) {
50+
return compile(
51+
"System\\.getProperty\\(\"line\\.separator\"(?:\\s*,\\s*\"\\\\n\")?\\)|" +
52+
"String\\s+\\w+\\s*=\\s*System\\.getProperty\\(\"line\\.separator\"(?:\\s*,\\s*\"\\\\n\")?\\)\\s*;",
53+
MULTILINE)
54+
.matcher(input)
55+
.replaceAll(match -> match.group().contains("String")
56+
? "String " + match.group().split("\\s+")[1].split("=")[0].trim() + " = System.lineSeparator();"
57+
: "System.lineSeparator()");
58+
}
59+
60+
private String removeRedundantInitializations(String typePattern, String defaultValuePattern, String input) {
61+
return compile(
62+
"([a-zA-Z]+\\s+(?:" + typePattern + ")\\s+\\w+)\\s*=\\s*" + defaultValuePattern + "\\s*;",
63+
MULTILINE)
64+
.matcher(input)
65+
.replaceAll("$1;");
66+
}
67+
68+
@Override
69+
public void close() throws Exception {}
70+
}

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
@@ -80,6 +80,10 @@ public void addRemoveWildcardImports(RemoveWildcardImports removeWildcardImports
8080
addStepFactory(removeWildcardImports);
8181
}
8282

83+
public void addReplaceObsoletes(ReplaceObsoletes replaceObsoletes) {
84+
addStepFactory(replaceObsoletes);
85+
}
86+
8387
public void addFormatAnnotations(FormatAnnotations formatAnnotations) {
8488
addStepFactory(formatAnnotations);
8589
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2025 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 com.diffplug.spotless.FormatterStep;
19+
import com.diffplug.spotless.generic.ReplaceObsoletesStep;
20+
import com.diffplug.spotless.maven.FormatterStepConfig;
21+
import com.diffplug.spotless.maven.FormatterStepFactory;
22+
23+
public class ReplaceObsoletes implements FormatterStepFactory {
24+
@Override
25+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
26+
return ReplaceObsoletesStep.forJava();
27+
}
28+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2016-2025 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.junit.jupiter.api.Disabled;
19+
import org.junit.jupiter.api.Test;
20+
21+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
22+
23+
class ReplaceObsoletesStepTest extends MavenIntegrationHarness {
24+
@Test
25+
void testSortPomCfg() throws Exception {
26+
writePomWithJavaSteps("<replaceObsoletes/>");
27+
28+
String path = "src/main/java/test.java";
29+
setFile(path).toResource("java/replaceobsoletes/SortPomCfgPre.test");
30+
mavenRunner().withArguments("spotless:apply").runNoError();
31+
assertFile(path).sameAsResource("java/replaceobsoletes/SortPomCfgPost.test");
32+
}
33+
34+
@Test
35+
@Disabled("feature envy: (edge case/hard to implement) having a dedicated method")
36+
void testSQLTokenizedFormatterPost() throws Exception {
37+
writePomWithJavaSteps("<replaceObsoletes/>");
38+
39+
String path = "src/main/java/test.java";
40+
setFile(path).toResource("java/replaceobsoletes/SQLTokenizedFormatterPre.test");
41+
mavenRunner().withArguments("spotless:apply").runNoError();
42+
assertFile(path).sameAsResource("java/replaceobsoletes/SQLTokenizedFormatterPost.test");
43+
}
44+
45+
@Test
46+
void testSystemLineSeparator() throws Exception {
47+
writePomWithJavaSteps("<replaceObsoletes/>");
48+
49+
String path = "src/main/java/test.java";
50+
setFile(path).toResource("java/replaceobsoletes/SystemLineSeparatorPre.test");
51+
mavenRunner().withArguments("spotless:apply").runNoError();
52+
assertFile(path).sameAsResource("java/replaceobsoletes/SystemLineSeparatorPost.test");
53+
}
54+
55+
@Test
56+
void testBooleanInitializers() throws Exception {
57+
writePomWithJavaSteps("<replaceObsoletes/>");
58+
59+
String path = "src/main/java/test.java";
60+
setFile(path).toResource("java/replaceobsoletes/BooleanInitializersPre.test");
61+
mavenRunner().withArguments("spotless:apply").runNoError();
62+
assertFile(path).sameAsResource("java/replaceobsoletes/BooleanInitializersPost.test");
63+
}
64+
65+
@Test
66+
void testNullInitializers() throws Exception {
67+
writePomWithJavaSteps("<replaceObsoletes/>");
68+
69+
String path = "src/main/java/test.java";
70+
setFile(path).toResource("java/replaceobsoletes/NullInitializersPre.test");
71+
mavenRunner().withArguments("spotless:apply").runNoError();
72+
assertFile(path).sameAsResource("java/replaceobsoletes/NullInitializersPost.test");
73+
}
74+
75+
@Test
76+
void testIntInitializers() throws Exception {
77+
writePomWithJavaSteps("<replaceObsoletes/>");
78+
79+
String path = "src/main/java/test.java";
80+
setFile(path).toResource("java/replaceobsoletes/IntInitializersPre.test");
81+
mavenRunner().withArguments("spotless:apply").runNoError();
82+
assertFile(path).sameAsResource("java/replaceobsoletes/IntInitializersPost.test");
83+
}
84+
85+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Test {
2+
public boolean flag1;
3+
public boolean flag2 = true;
4+
public boolean flag3;
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Test {
2+
public boolean flag1 = false;
3+
public boolean flag2 = true;
4+
public boolean flag3 = false;
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Test {
2+
public int int1;
3+
public int int2 = 1;
4+
public long long1;
5+
public long long2 = 1L;
6+
public float float1;
7+
public float float2 = 1.0f;
8+
public double double1;
9+
public double double2 = 1.0;
10+
public short short1;
11+
public short short2 = 1;
12+
public byte byte1;
13+
public byte byte2 = 1;
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Test {
2+
public int int1 = 0;
3+
public int int2 = 1;
4+
public long long1 = 0L;
5+
public long long2 = 1L;
6+
public float float1 = 0.0f;
7+
public float float2 = 1.0f;
8+
public double double1 = 0.0;
9+
public double double2 = 1.0;
10+
public short short1 = 0;
11+
public short short2 = 1;
12+
public byte byte1 = 0;
13+
public byte byte2 = 1;
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Test {
2+
public String str1;
3+
public String str2 = "value";
4+
public Object obj1;
5+
public Object obj2 = new Object();
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Test {
2+
public String str1 = null;
3+
public String str2 = "value";
4+
public Object obj1 = null;
5+
public Object obj2 = new Object();
6+
}

0 commit comments

Comments
 (0)