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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import dev.equo.solstice.Solstice;
import dev.equo.solstice.p2.CacheLocations;

import static java.lang.System.lineSeparator;

/** Spotless-Formatter step which calls out to the Groovy-Eclipse formatter. */
public class GrEclipseFormatterStepImpl {
static {
Expand Down Expand Up @@ -143,7 +145,7 @@ public String toString() {
}
for (Throwable error : errors) {
error.printStackTrace();
string.append(System.lineSeparator());
string.append(lineSeparator());
string.append(error.getMessage());
}

Expand Down
5 changes: 4 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/Jvm.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import static java.lang.System.lineSeparator;
import static java.util.stream.Collectors.joining;

/** Java virtual machine helper */
public final class Jvm {
private static final int VERSION;
Expand Down Expand Up @@ -255,7 +258,7 @@ private String buildUpgradeFormatterMessage(V fmtVersion) {
public String toString() {
return String.format("%s alternatives:%n", fmtName) +
jvm2fmtMaxVersion.entrySet().stream().map(
e -> String.format("- Version %s requires JVM %d+", e.getValue(), e.getKey())).collect(Collectors.joining(System.lineSeparator()));
e -> String.format("- Version %s requires JVM %d+", e.getValue(), e.getKey())).collect(joining(lineSeparator()));
}

@SuppressFBWarnings("SE_COMPARATOR_SHOULD_BE_SERIALIZABLE")
Expand Down
4 changes: 3 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/LineEnding.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Objects;
import java.util.function.Supplier;

import static java.lang.System.lineSeparator;

/**
* Represents the line endings which should be written by the tool.
*/
Expand Down Expand Up @@ -151,7 +153,7 @@ static String getEndingFor(Reader reader) throws IOException {
private static final Policy UNIX_POLICY = new ConstantLineEndingPolicy(UNIX.str());
private static final Policy MAC_CLASSIC_POLICY = new ConstantLineEndingPolicy(MAC_CLASSIC.str());
private static final Policy PRESERVE_POLICY = new PreserveLineEndingPolicy();
private static final String _platformNative = System.getProperty("line.separator");
private static final String _platformNative = lineSeparator();
private static final Policy _platformNativePolicy = new ConstantLineEndingPolicy(_platformNative);
private static final boolean nativeIsWin = _platformNative.equals(WINDOWS.str());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;

import static java.lang.System.lineSeparator;

/**
* Removes all semicolons from the end of lines.
*
Expand Down Expand Up @@ -51,7 +53,7 @@ FormatterFunc toFormatter() {
String line;
while ((line = reader.readLine()) != null) {
result.append(removeSemicolon(line));
result.append(System.lineSeparator());
result.append(lineSeparator());
}
return result.toString();
}
Expand Down
34 changes: 18 additions & 16 deletions lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2024 DiffPlug
* Copyright 2021-2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@

import java.io.Serializable;

import static java.lang.System.lineSeparator;

// Class and members must be public, otherwise we get failed to access class com.diffplug.spotless.pom.SortPomInternalState from class com.diffplug.spotless.pom.SortPomFormatterFunc (com.diffplug.spotless.pom.SortPomInternalState is in unnamed module of loader org.codehaus.plexus.classworlds.realm.ClassRealm @682bd3c4; com.diffplug.spotless.pom.SortPomFormatterFunc is in unnamed module of loader com.diffplug.spotless.pom.DelegatingClassLoader @573284a5)
public class SortPomCfg implements Serializable {
private static final long serialVersionUID = 1L;
Expand All @@ -25,41 +27,41 @@ public class SortPomCfg implements Serializable {

public String encoding = "UTF-8";

public String lineSeparator = System.getProperty("line.separator");
public String lineSeparator = lineSeparator();

public boolean expandEmptyElements = true;
public boolean expandEmptyElements;

public boolean spaceBeforeCloseEmptyElement = false;
public boolean spaceBeforeCloseEmptyElement;

public boolean keepBlankLines = true;

public boolean endWithNewline = true;

public int nrOfIndentSpace = 2;

public boolean indentBlankLines = false;
public boolean indentBlankLines;

public boolean indentSchemaLocation = false;
public boolean indentSchemaLocation;

public String indentAttribute = null;
public String indentAttribute;

public String predefinedSortOrder = "recommended_2008_06";

public boolean quiet = false;
public boolean quiet;

public String sortOrderFile = null;
public String sortOrderFile;

public String sortDependencies = null;
public String sortDependencies;

public String sortDependencyManagement = null;
public String sortDependencyManagement;

public String sortDependencyExclusions = null;
public String sortDependencyExclusions;

public String sortPlugins = null;
public String sortPlugins;

public boolean sortProperties = false;
public boolean sortProperties;

public boolean sortModules = false;
public boolean sortModules;

public boolean sortExecutions = false;
public boolean sortExecutions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import com.diffplug.spotless.annotations.Internal;

import static java.lang.System.lineSeparator;

/**
* **Warning:** Use this class at your own risk. It is an implementation detail and is not
* guaranteed to exist in future versions.
Expand Down Expand Up @@ -67,7 +69,7 @@
}

if (isSqlEndsWithNewLine) {
after.append(getDefaultLineSeparator());
after.append(lineSeparator());
}

return after.toString();
Expand Down Expand Up @@ -388,19 +390,12 @@
return sqlDialect.getKeywordType(name) == DBPKeywordType.FUNCTION;
}

private static String getDefaultLineSeparator() {
return System.getProperty("line.separator", "\n");
Copy link
Copy Markdown
Author

@Pankraz76 Pankraz76 Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feature envy, reimplementing java.

}

private int insertReturnAndIndent(final List<FormatterToken> argList, final int argIndex, final int argIndent) {
if (functionBracket.contains(Boolean.TRUE))
return 0;
try {
final String defaultLineSeparator = getDefaultLineSeparator();
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRY.

StringBuilder s = new StringBuilder(defaultLineSeparator);
for (int index = 0; index < argIndent; index++) {
s.append(formatterCfg.getIndentString());
}
StringBuilder s = new StringBuilder(lineSeparator());
s.append(String.valueOf(formatterCfg.getIndentString()).repeat(Math.max(0, argIndent)));
if (argIndex > 0) {
final FormatterToken token = argList.get(argIndex);
final FormatterToken prevToken = argList.get(argIndex - 1);
Expand All @@ -409,10 +404,7 @@
prevToken.getType() != TokenType.END) {
s.setCharAt(0, ' ');
s.setLength(1);

final String comment = token.getString();
final String withoutTrailingWhitespace = comment.replaceFirst("\\s*$", "");
token.setString(withoutTrailingWhitespace);
Copy link
Copy Markdown
Author

@Pankraz76 Pankraz76 Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

token.setString(token.getString().replaceFirst("\\s*$", ""));

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 with many repetitions of ' '.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be, logic is same as before. Just the field burden imposing coupling was inlined to avoid boilerplate. So only removed the documentation overhead in favor of 1 LOC concise possible statement.

}
}

Expand Down
178 changes: 178 additions & 0 deletions lib/src/test/java/com/diffplug/spotless/pom/SortPomCfgTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* 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.pom;

import static java.lang.System.lineSeparator;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class SortPomCfgTest {

@Test
void testDefaultValues() {
SortPomCfg cfg = new SortPomCfg();

// Test default values using AssertJ
assertThat(cfg.version).isEqualTo("4.0.0");
assertThat(cfg.encoding).isEqualTo("UTF-8");
assertThat(cfg.lineSeparator).isEqualTo(lineSeparator());
assertThat(cfg.expandEmptyElements).isFalse();
assertThat(cfg.spaceBeforeCloseEmptyElement).isFalse();
assertThat(cfg.keepBlankLines).isTrue();
assertThat(cfg.endWithNewline).isTrue();
assertThat(cfg.nrOfIndentSpace).isEqualTo(2);
assertThat(cfg.indentBlankLines).isFalse();
assertThat(cfg.indentSchemaLocation).isFalse();
assertThat(cfg.indentAttribute).isNull();
assertThat(cfg.predefinedSortOrder).isEqualTo("recommended_2008_06");
assertThat(cfg.quiet).isFalse();
assertThat(cfg.sortOrderFile).isNull();
assertThat(cfg.sortDependencies).isNull();
assertThat(cfg.sortDependencyManagement).isNull();
assertThat(cfg.sortDependencyExclusions).isNull();
assertThat(cfg.sortPlugins).isNull();
assertThat(cfg.sortProperties).isFalse();
assertThat(cfg.sortModules).isFalse();
assertThat(cfg.sortExecutions).isFalse();
}

@Test
void testFieldSetters() {
SortPomCfg cfg = new SortPomCfg();

// Set all fields
cfg.version = "4.1.0";
cfg.encoding = "ISO-8859-1";
cfg.lineSeparator = "\n";
cfg.expandEmptyElements = true;
cfg.spaceBeforeCloseEmptyElement = true;
cfg.keepBlankLines = false;
cfg.endWithNewline = false;
cfg.nrOfIndentSpace = 4;
cfg.indentBlankLines = true;
cfg.indentSchemaLocation = true;
cfg.indentAttribute = "attribute";
cfg.predefinedSortOrder = "custom";
cfg.quiet = true;
cfg.sortOrderFile = "sortOrder.xml";
cfg.sortDependencies = "groupId,artifactId";
cfg.sortDependencyManagement = "scope,groupId";
cfg.sortDependencyExclusions = "artifactId";
cfg.sortPlugins = "groupId";
cfg.sortProperties = true;
cfg.sortModules = true;
cfg.sortExecutions = true;

// Verify all set values with AssertJ
assertThat(cfg.version).isEqualTo("4.1.0");
assertThat(cfg.encoding).isEqualTo("ISO-8859-1");
assertThat(cfg.lineSeparator).isEqualTo("\n");
assertThat(cfg.expandEmptyElements).isTrue();
assertThat(cfg.spaceBeforeCloseEmptyElement).isTrue();
assertThat(cfg.keepBlankLines).isFalse();
assertThat(cfg.endWithNewline).isFalse();
assertThat(cfg.nrOfIndentSpace).isEqualTo(4);
assertThat(cfg.indentBlankLines).isTrue();
assertThat(cfg.indentSchemaLocation).isTrue();
assertThat(cfg.indentAttribute).isEqualTo("attribute");
assertThat(cfg.predefinedSortOrder).isEqualTo("custom");
assertThat(cfg.quiet).isTrue();
assertThat(cfg.sortOrderFile).isEqualTo("sortOrder.xml");
assertThat(cfg.sortDependencies).isEqualTo("groupId,artifactId");
assertThat(cfg.sortDependencyManagement).isEqualTo("scope,groupId");
assertThat(cfg.sortDependencyExclusions).isEqualTo("artifactId");
assertThat(cfg.sortPlugins).isEqualTo("groupId");
assertThat(cfg.sortProperties).isTrue();
assertThat(cfg.sortModules).isTrue();
assertThat(cfg.sortExecutions).isTrue();
}

@Test
void testNullHandling() {
SortPomCfg cfg = new SortPomCfg();

// Set nullable fields to null
cfg.version = null;
cfg.encoding = null;
cfg.lineSeparator = null;
cfg.indentAttribute = null;
cfg.predefinedSortOrder = null;
cfg.sortOrderFile = null;
cfg.sortDependencies = null;
cfg.sortDependencyManagement = null;
cfg.sortDependencyExclusions = null;
cfg.sortPlugins = null;

// Verify null values with AssertJ
assertThat(cfg.version).isNull();
assertThat(cfg.encoding).isNull();
assertThat(cfg.lineSeparator).isNull();
assertThat(cfg.indentAttribute).isNull();
assertThat(cfg.predefinedSortOrder).isNull();
assertThat(cfg.sortOrderFile).isNull();
assertThat(cfg.sortDependencies).isNull();
assertThat(cfg.sortDependencyManagement).isNull();
assertThat(cfg.sortDependencyExclusions).isNull();
assertThat(cfg.sortPlugins).isNull();
}

@Test
void testBooleanFieldsEdgeCases() {
SortPomCfg cfg = new SortPomCfg();

// Toggle all boolean fields
cfg.expandEmptyElements = !cfg.expandEmptyElements;
cfg.spaceBeforeCloseEmptyElement = !cfg.spaceBeforeCloseEmptyElement;
cfg.keepBlankLines = !cfg.keepBlankLines;
cfg.endWithNewline = !cfg.endWithNewline;
cfg.indentBlankLines = !cfg.indentBlankLines;
cfg.indentSchemaLocation = !cfg.indentSchemaLocation;
cfg.quiet = !cfg.quiet;
cfg.sortProperties = !cfg.sortProperties;
cfg.sortModules = !cfg.sortModules;
cfg.sortExecutions = !cfg.sortExecutions;

// Verify all boolean fields are toggled
assertThat(cfg.expandEmptyElements).isTrue();
assertThat(cfg.spaceBeforeCloseEmptyElement).isTrue();
assertThat(cfg.keepBlankLines).isFalse();
assertThat(cfg.endWithNewline).isFalse();
assertThat(cfg.indentBlankLines).isTrue();
assertThat(cfg.indentSchemaLocation).isTrue();
assertThat(cfg.quiet).isTrue();
assertThat(cfg.sortProperties).isTrue();
assertThat(cfg.sortModules).isTrue();
assertThat(cfg.sortExecutions).isTrue();
}

@Test
void testNumericFieldEdgeCases() {
SortPomCfg cfg = new SortPomCfg();

// Test minimum value
cfg.nrOfIndentSpace = 0;
assertThat(cfg.nrOfIndentSpace).isZero();

// Test negative value
cfg.nrOfIndentSpace = -1;
assertThat(cfg.nrOfIndentSpace).isNegative();

// Test large value
cfg.nrOfIndentSpace = Integer.MAX_VALUE;
assertThat(cfg.nrOfIndentSpace).isEqualTo(Integer.MAX_VALUE);
}
}
Loading
Loading