-
Notifications
You must be signed in to change notification settings - Fork 486
feat: Add ReplaceObsoletesStep
#2530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright 2016-2025 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.generic; | ||
|
||
import static java.util.regex.Pattern.MULTILINE; | ||
import static java.util.regex.Pattern.compile; | ||
|
||
import java.io.File; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
|
||
public final class ReplaceObsoletesStep implements FormatterStep { | ||
|
||
private static final long serialVersionUID = -6643164760547140534L; | ||
|
||
private ReplaceObsoletesStep() {} | ||
|
||
public static FormatterStep forJava() { | ||
return new ReplaceObsoletesStep(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "replaceObsoletes"; | ||
} | ||
|
||
@Override | ||
public String format(String rawUnix, File file) throws Exception { | ||
return removeRedundantAbstractInInterfaces( | ||
removeRedundantPublicStaticInEnumsAndInterfaces( | ||
removeRedundantInitializations("float|double", "0(?:\\.0)?(?:f|d)?", | ||
removeRedundantInitializations("int|long|short|byte", "0(?:L)?", | ||
removeRedundantInitializations("String|\\w+", "null", | ||
removeRedundantInitializations("boolean", "false", | ||
replaceLineSeparator(rawUnix))))))); | ||
} | ||
|
||
private static String replaceLineSeparator(String input) { | ||
return compile( | ||
"System\\.getProperty\\(\"line\\.separator\"(?:\\s*,\\s*\"\\\\n\")?\\)|" + | ||
"String\\s+\\w+\\s*=\\s*System\\.getProperty\\(\"line\\.separator\"(?:\\s*,\\s*\"\\\\n\")?\\)\\s*;", | ||
MULTILINE) | ||
.matcher(input) | ||
.replaceAll(match -> match.group().contains("String") | ||
? "String " + match.group().split("\\s+")[1].split("=")[0].trim() + " = System.lineSeparator();" | ||
: "System.lineSeparator()"); | ||
} | ||
|
||
private String removeRedundantInitializations(String typePattern, String defaultValuePattern, String input) { | ||
return compile( | ||
"([a-zA-Z]+\\s+(?:" + typePattern + ")\\s+\\w+)\\s*=\\s*" + defaultValuePattern + "\\s*;", | ||
MULTILINE) | ||
.matcher(input) | ||
.replaceAll("$1;"); | ||
} | ||
|
||
private String removeRedundantPublicStaticInEnumsAndInterfaces(String input) { | ||
// Handle enum constants | ||
String processed = compile( | ||
"(enum\\s+\\w+\\s*\\{[^}]*?)(public\\s+static\\s+)(\\w+\\s*,\\s*|\\w+\\s*;)", | ||
MULTILINE) | ||
.matcher(input) | ||
.replaceAll("$1$3"); | ||
|
||
// Handle enum methods | ||
processed = compile( | ||
"(enum\\s+\\w+\\s*\\{[^}]*?)(public\\s+static\\s+)(\\w+\\s+\\w+\\s*\\([^)]*\\)\\s*(?:throws\\s+[\\w.]+(?:\\s*,\\s*[\\w.]+)*\\s*)?\\{)", | ||
MULTILINE) | ||
.matcher(processed) | ||
Check failureCode scanning / CodeQL Polynomial regular expression used on uncontrolled data High
This
regular expression Error loading related location Loading user-provided value Error loading related location Loading This regular expression Error loading related location Loading user-provided value Error loading related location Loading |
||
.replaceAll("$1$3"); | ||
|
||
// Handle interface constants - keep 'final' but remove 'public static' | ||
processed = compile( | ||
"(interface\\s+\\w+\\s*\\{[^}]*?)(public\\s+static\\s+)(final\\s+)(\\w+\\s+\\w+\\s*=)", | ||
MULTILINE) | ||
.matcher(processed) | ||
Check failureCode scanning / CodeQL Polynomial regular expression used on uncontrolled data High
This
regular expression Error loading related location Loading user-provided value Error loading related location Loading |
||
.replaceAll("$1$3$4"); | ||
|
||
// Handle interface methods | ||
processed = compile( | ||
"(interface\\s+\\w+\\s*\\{[^}]*?)(public\\s+static\\s+)(\\w+\\s+\\w+\\s*\\([^)]*\\)\\s*(?:throws\\s+[\\w.]+(?:\\s*,\\s*[\\w.]+)*\\s*)?;)", | ||
MULTILINE) | ||
.matcher(processed) | ||
Check failureCode scanning / CodeQL Polynomial regular expression used on uncontrolled data High
This
regular expression Error loading related location Loading user-provided value Error loading related location Loading This regular expression Error loading related location Loading user-provided value Error loading related location Loading |
||
.replaceAll("$1$3"); | ||
|
||
// Handle interface inner classes | ||
processed = compile( | ||
"(interface\\s+\\w+\\s*\\{[^}]*?)(public\\s+static\\s+)(class\\s+\\w+\\s*\\{)", | ||
MULTILINE) | ||
.matcher(processed) | ||
Check failureCode scanning / CodeQL Polynomial regular expression used on uncontrolled data High
This
regular expression Error loading related location Loading user-provided value Error loading related location Loading |
||
.replaceAll("$1$3"); | ||
|
||
return processed; | ||
} | ||
|
||
private String removeRedundantAbstractInInterfaces(String input) { | ||
// Handle abstract methods in interfaces | ||
return compile( | ||
"(interface\\s+\\w+\\s*\\{[^}]*?)(?:public\\s+)?abstract\\s+(\\w+\\s+\\w+\\s*\\([^)]*\\)\\s*(?:throws\\s+[\\w.]+(?:\\s*,\\s*[\\w.]+)*\\s*)?;)", | ||
MULTILINE) | ||
.matcher(input) | ||
Check failureCode scanning / CodeQL Polynomial regular expression used on uncontrolled data High
This
regular expression Error loading related location Loading user-provided value Error loading related location Loading This regular expression Error loading related location Loading user-provided value Error loading related location Loading |
||
.replaceAll("$1$2"); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2025 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.maven.java; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.generic.ReplaceObsoletesStep; | ||
import com.diffplug.spotless.maven.FormatterStepConfig; | ||
import com.diffplug.spotless.maven.FormatterStepFactory; | ||
|
||
public class ReplaceObsoletes implements FormatterStepFactory { | ||
@Override | ||
public FormatterStep newFormatterStep(FormatterStepConfig config) { | ||
return ReplaceObsoletesStep.forJava(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2016-2025 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.maven.java; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.diffplug.spotless.maven.MavenIntegrationHarness; | ||
|
||
class ReplaceObsoletesStepTest extends MavenIntegrationHarness { | ||
@Test | ||
void testSortPomCfg() throws Exception { | ||
writePomWithJavaSteps("<replaceObsoletes/>"); | ||
|
||
String path = "src/main/java/test.java"; | ||
setFile(path).toResource("java/replaceobsoletes/SortPomCfgPre.test"); | ||
mavenRunner().withArguments("spotless:apply").runNoError(); | ||
assertFile(path).sameAsResource("java/replaceobsoletes/SortPomCfgPost.test"); | ||
} | ||
|
||
@Test | ||
void testSystemLineSeparator() throws Exception { | ||
writePomWithJavaSteps("<replaceObsoletes/>"); | ||
|
||
String path = "src/main/java/test.java"; | ||
setFile(path).toResource("java/replaceobsoletes/SystemLineSeparatorPre.test"); | ||
mavenRunner().withArguments("spotless:apply").runNoError(); | ||
assertFile(path).sameAsResource("java/replaceobsoletes/SystemLineSeparatorPost.test"); | ||
} | ||
|
||
@Test | ||
void testBooleanInitializers() throws Exception { | ||
writePomWithJavaSteps("<replaceObsoletes/>"); | ||
|
||
String path = "src/main/java/test.java"; | ||
setFile(path).toResource("java/replaceobsoletes/BooleanInitializersPre.test"); | ||
mavenRunner().withArguments("spotless:apply").runNoError(); | ||
assertFile(path).sameAsResource("java/replaceobsoletes/BooleanInitializersPost.test"); | ||
} | ||
|
||
@Test | ||
void testNullInitializers() throws Exception { | ||
writePomWithJavaSteps("<replaceObsoletes/>"); | ||
|
||
String path = "src/main/java/test.java"; | ||
setFile(path).toResource("java/replaceobsoletes/NullInitializersPre.test"); | ||
mavenRunner().withArguments("spotless:apply").runNoError(); | ||
assertFile(path).sameAsResource("java/replaceobsoletes/NullInitializersPost.test"); | ||
} | ||
|
||
@Test | ||
void testIntInitializers() throws Exception { | ||
writePomWithJavaSteps("<replaceObsoletes/>"); | ||
|
||
String path = "src/main/java/test.java"; | ||
setFile(path).toResource("java/replaceobsoletes/IntInitializersPre.test"); | ||
mavenRunner().withArguments("spotless:apply").runNoError(); | ||
assertFile(path).sameAsResource("java/replaceobsoletes/IntInitializersPost.test"); | ||
} | ||
|
||
@Test | ||
void testEnumPublicStatic() throws Exception { | ||
writePomWithJavaSteps("<replaceObsoletes/>"); | ||
|
||
String path = "src/main/java/test.java"; | ||
setFile(path).toResource("java/replaceobsoletes/EnumPublicStaticPre.test"); | ||
mavenRunner().withArguments("spotless:apply").runNoError(); | ||
assertFile(path).sameAsResource("java/replaceobsoletes/EnumPublicStaticPost.test"); | ||
} | ||
|
||
@Test | ||
void testInterfacePublicStatic() throws Exception { | ||
writePomWithJavaSteps("<replaceObsoletes/>"); | ||
|
||
String path = "src/main/java/test.java"; | ||
setFile(path).toResource("java/replaceobsoletes/InterfacePublicStaticPre.test"); | ||
mavenRunner().withArguments("spotless:apply").runNoError(); | ||
assertFile(path).sameAsResource("java/replaceobsoletes/InterfacePublicStaticPost.test"); | ||
} | ||
|
||
@Test | ||
@Disabled("feature envy: (edge case/hard to implement) having a dedicated method") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding this. Since it's currently disabled, maybe a follow-up issue should be created ? |
||
void testSQLTokenizedFormatterPost() throws Exception { | ||
writePomWithJavaSteps("<replaceObsoletes/>"); | ||
|
||
String path = "src/main/java/test.java"; | ||
setFile(path).toResource("java/replaceobsoletes/SQLTokenizedFormatterPre.test"); | ||
mavenRunner().withArguments("spotless:apply").runNoError(); | ||
assertFile(path).sameAsResource("java/replaceobsoletes/SQLTokenizedFormatterPost.test"); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class Test { | ||
public boolean flag1; | ||
public boolean flag2 = true; | ||
public boolean flag3; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class Test { | ||
public boolean flag1 = false; | ||
public boolean flag2 = true; | ||
public boolean flag3 = false; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public enum TestEnum { | ||
VALUE1, | ||
VALUE2; | ||
|
||
void method() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public enum TestEnum { | ||
public static VALUE1, | ||
public static VALUE2; | ||
|
||
public static void method() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class Test { | ||
public int int1; | ||
public int int2 = 1; | ||
public long long1; | ||
public long long2 = 1L; | ||
public float float1; | ||
public float float2 = 1.0f; | ||
public double double1; | ||
public double double2 = 1.0; | ||
public short short1; | ||
public short short2 = 1; | ||
public byte byte1; | ||
public byte byte2 = 1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class Test { | ||
public int int1 = 0; | ||
public int int2 = 1; | ||
public long long1 = 0L; | ||
public long long2 = 1L; | ||
public float float1 = 0.0f; | ||
public float float2 = 1.0f; | ||
public double double1 = 0.0; | ||
public double double2 = 1.0; | ||
public short short1 = 0; | ||
public short short2 = 1; | ||
public byte byte1 = 0; | ||
public byte byte2 = 1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
interface TestInterface { | ||
final int CONSTANT = 1; | ||
void method(); | ||
class InnerClass {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
interface TestInterface { | ||
public static final int CONSTANT = 1; | ||
public abstract void method(); | ||
public static class InnerClass {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
public class Test { | ||
public String str1; | ||
public String str2 = "value"; | ||
public Object obj1; | ||
public Foo Foo1; | ||
public Foo Foo2 = "null"; | ||
public Object obj2 = new Object(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
public class Test { | ||
public String str1 = null; | ||
public String str2 = "value"; | ||
public Object obj1 = null; | ||
public Foo Foo1 = null; | ||
public Foo Foo2 = "null"; | ||
public Object obj2 = new Object(); | ||
} |
Check failure
Code scanning / CodeQL
Polynomial regular expression used on uncontrolled data High