Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* Add support for removing wildcard imports via `removeWildcardImports` step. ([#2517](https://github.com/diffplug/spotless/pull/2517))
* Add support for removing generic obsoletes imports via `replaceObsoletes` step. ([#2530](https://github.com/diffplug/spotless/pull/2530))

## [3.1.2] - 2025-05-27
### Fixed
Expand Down
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)

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on a
user-provided value
may run slow on strings starting with 'enum 0{' and with many repetitions of 'enum 0{'.
.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 failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on a
user-provided value
may run slow on strings starting with 'enum 0{' and with many repetitions of 'enum 0{'.
This
regular expression
that depends on a
user-provided value
may run slow on strings starting with 'enum 0{public static 0 0(' and with many repetitions of 'public static 0 0('.
.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 failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on a
user-provided value
may run slow on strings starting with 'interface 0{' and with many repetitions of 'interface 0{'.
.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 failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on a
user-provided value
may run slow on strings starting with 'interface 0{' and with many repetitions of 'interface 0{'.
This
regular expression
that depends on a
user-provided value
may run slow on strings starting with 'interface 0{public static 0 0(' and with many repetitions of 'public static 0 0('.
.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 failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on a
user-provided value
may run slow on strings starting with 'interface 0{' and with many repetitions of 'interface 0{'.
.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 failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on a
user-provided value
may run slow on strings starting with 'interface 0{' and with many repetitions of 'interface 0{'.
This
regular expression
that depends on a
user-provided value
may run slow on strings starting with 'interface 0{abstract 0 0(' and with many repetitions of 'abstract 0 0('.
.replaceAll("$1$2");
}

@Override
public void close() throws Exception {}
}
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* Add support for removing wildcard imports via `removeWildcardImports` step. ([#2517](https://github.com/diffplug/spotless/pull/2517))
* Add support for removing generic obsoletes imports via `replaceObsoletes` step. ([#2530](https://github.com/diffplug/spotless/pull/2530))

## [7.0.4] - 2025-05-27
### Fixed
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* Add support for removing wildcard imports via `removeWildcardImports` step. ([#2517](https://github.com/diffplug/spotless/pull/2517))
* Add support for removing generic obsoletes imports via `replaceObsoletes` step. ([#2530](https://github.com/diffplug/spotless/pull/2530))

## [2.44.5] - 2025-05-27
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public void addRemoveWildcardImports(RemoveWildcardImports removeWildcardImports
addStepFactory(removeWildcardImports);
}

public void addReplaceObsoletes(ReplaceObsoletes replaceObsoletes) {
addStepFactory(replaceObsoletes);
}

public void addFormatAnnotations(FormatAnnotations formatAnnotations) {
addStepFactory(formatAnnotations);
}
Expand Down
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")
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}
Loading
Loading