Skip to content

Commit 20ea6e1

Browse files
committed
Merge remote-tracking branch 'origin/main' into IntroduceCleanthatRefactorer
2 parents 7ea0f3a + 3226d2b commit 20ea6e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+509
-125
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
14+
## [2.34.1] - 2023-02-05
1315
### Changes
1416
* **POTENTIALLY BREAKING** Bump bytecode from Java 8 to 11 ([#1530](https://github.com/diffplug/spotless/pull/1530) part 2 of [#1337](https://github.com/diffplug/spotless/issues/1337))
17+
### Fixed
18+
* **POTENTIALLY BREAKING** `sortByKeys` for JSON formatting now takes into account objects inside arrays ([#1546](https://github.com/diffplug/spotless/pull/1546))
19+
* `freshmark` fixed on java 15+ ([#1304](https://github.com/diffplug/spotless/pull/1304) fixes [#803](https://github.com/diffplug/spotless/issues/803))
1520

1621
## [2.34.0] - 2023-01-26
1722
### Added

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ If it doesn't work, you can check the JitPack log at `https://jitpack.io/com/git
207207

208208
### Maven
209209

210-
Run `./gradlew publishToMavenLocal` to publish this to your local repository. The maven plugin is not published to JitPack due to [jitpack/jitpack.io#4112](https://github.com/jitpack/jitpack.io/issues/4112).
210+
Run `./gradlew publishToMavenLocal` to publish this to your local repository. You can also use the JitPack artifacts, using the same principles as Gradle above.
211211

212212
## License
213213

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ VER_DURIAN=1.2.0
2828
VER_JGIT=6.4.0.202211300538-r
2929
VER_JUNIT=5.9.2
3030
VER_ASSERTJ=3.24.2
31-
VER_MOCKITO=5.0.0
31+
VER_MOCKITO=5.1.1

gradle/java-publish.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ model {
170170
}
171171
}
172172

173-
if (!version.endsWith('-SNAPSHOT')) {
173+
if (System.env['JITPACK'] == 'true' || version.endsWith('-SNAPSHOT')) {
174+
signing {
175+
setRequired(false)
176+
}
177+
} else {
174178
signing {
175179
String gpg_key = decode64('ORG_GRADLE_PROJECT_gpg_key64')
176180
useInMemoryPgpKeys(gpg_key, System.env['ORG_GRADLE_PROJECT_gpg_passphrase'])

gradle/spotless-freshmark.gradle

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,4 @@
11

2-
import java.util.regex.Matcher
3-
import java.util.regex.Pattern
4-
5-
6-
def thisVm() {
7-
String jre = System.getProperty("java.version")
8-
if (jre.startsWith("1.8")) {
9-
return 8
10-
} else {
11-
Matcher matcher = Pattern.compile("(\\d+)").matcher(jre)
12-
if (!matcher.find()) {
13-
throw new IllegalArgumentException("Expected " + jre + " to start with an integer")
14-
}
15-
int version = Integer.parseInt(matcher.group(1))
16-
if (version <= 8) {
17-
throw new IllegalArgumentException("Expected " + jre + " to start with an integer greater than 8")
18-
}
19-
return version
20-
}
21-
}
22-
23-
if (thisVm() >= 15) {
24-
// freshmark doesn't run on JRE 15+
25-
return
26-
}
27-
282
apply plugin: 'com.diffplug.spotless'
293

304
import com.diffplug.gradle.spotless.FreshMarkExtension

jitpack.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
jdk:
2+
- openjdk11

lib-extra/src/main/java/com/diffplug/spotless/extra/GitAttributesLineEndings.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,6 +42,8 @@
4242
import org.eclipse.jgit.storage.file.FileBasedConfig;
4343
import org.eclipse.jgit.util.FS;
4444
import org.eclipse.jgit.util.SystemReader;
45+
import org.slf4j.Logger;
46+
import org.slf4j.LoggerFactory;
4547

4648
import com.googlecode.concurrenttrees.radix.ConcurrentRadixTree;
4749
import com.googlecode.concurrenttrees.radix.node.concrete.DefaultCharSequenceNodeFactory;
@@ -61,6 +63,8 @@
6163
* back to the platform native.
6264
*/
6365
public final class GitAttributesLineEndings {
66+
private static final Logger LOGGER = LoggerFactory.getLogger(GitAttributesLineEndings.class);
67+
6468
// prevent direct instantiation
6569
private GitAttributesLineEndings() {}
6670

@@ -261,7 +265,7 @@ private static String convertEolToLineEnding(String eol, File file) {
261265
case "crlf":
262266
return LineEnding.WINDOWS.str();
263267
default:
264-
System.err.println(".gitattributes file has unspecified eol value: " + eol + " for " + file + ", defaulting to platform native");
268+
LOGGER.warn(".gitattributes file has unspecified eol value: {} for {}, defaulting to platform native", eol, file);
265269
return LineEnding.PLATFORM_NATIVE.str();
266270
}
267271
}
@@ -341,8 +345,7 @@ private static List<AttributesRule> parseRules(@Nullable File file) {
341345
return parsed.getRules();
342346
} catch (IOException e) {
343347
// no need to crash the whole plugin
344-
System.err.println("Problem parsing " + file.getAbsolutePath());
345-
e.printStackTrace();
348+
LOGGER.warn("Problem parsing {}", file.getAbsolutePath(), e);
346349
}
347350
}
348351
return Collections.emptyList();

lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.gson.Gson;
2323
import com.google.gson.GsonBuilder;
24+
import com.google.gson.JsonArray;
2425
import com.google.gson.JsonElement;
2526
import com.google.gson.JsonObject;
2627
import com.google.gson.stream.JsonWriter;
@@ -57,8 +58,8 @@ public String apply(String inputString) {
5758
if (jsonElement == null) {
5859
throw new AssertionError(FAILED_TO_PARSE_ERROR_MESSAGE);
5960
}
60-
if (gsonConfig.isSortByKeys() && jsonElement.isJsonObject()) {
61-
jsonElement = sortByKeys(jsonElement.getAsJsonObject());
61+
if (gsonConfig.isSortByKeys()) {
62+
jsonElement = sortByKeys(jsonElement);
6263
}
6364
try (StringWriter stringWriter = new StringWriter()) {
6465
JsonWriter jsonWriter = new JsonWriter(stringWriter);
@@ -72,19 +73,36 @@ public String apply(String inputString) {
7273
return result;
7374
}
7475

76+
private JsonElement sortByKeys(JsonElement jsonElement) {
77+
if (jsonElement.isJsonArray()) {
78+
return sortByKeys(jsonElement.getAsJsonArray());
79+
} else if (jsonElement.isJsonObject()) {
80+
return sortByKeys(jsonElement.getAsJsonObject());
81+
} else {
82+
return jsonElement;
83+
}
84+
}
85+
7586
private JsonElement sortByKeys(JsonObject jsonObject) {
7687
JsonObject result = new JsonObject();
7788
jsonObject.keySet().stream().sorted()
7889
.forEach(key -> {
79-
JsonElement element = jsonObject.get(key);
80-
if (element.isJsonObject()) {
81-
element = sortByKeys(element.getAsJsonObject());
82-
}
83-
result.add(key, element);
90+
JsonElement sorted = sortByKeys(jsonObject.get(key));
91+
result.add(key, sorted);
8492
});
8593
return result;
8694
}
8795

96+
private JsonElement sortByKeys(JsonArray jsonArray) {
97+
var result = new JsonArray();
98+
for (JsonElement element : jsonArray) {
99+
JsonElement sorted = sortByKeys(element);
100+
result.add(sorted);
101+
}
102+
103+
return result;
104+
}
105+
88106
private String generateIndent(int indentSpaces) {
89107
return String.join("", Collections.nCopies(indentSpaces, " "));
90108
}

lib/src/main/java/com/diffplug/spotless/Formatter.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@
3939
public final class Formatter implements Serializable, AutoCloseable {
4040
private static final long serialVersionUID = 1L;
4141

42+
// The name is used for logging purpose. It does not convey any applicative purpose
43+
private String name;
4244
private LineEnding.Policy lineEndingsPolicy;
4345
private Charset encoding;
4446
private Path rootDir;
4547
private List<FormatterStep> steps;
4648
private FormatExceptionPolicy exceptionPolicy;
4749

48-
private Formatter(LineEnding.Policy lineEndingsPolicy, Charset encoding, Path rootDirectory, List<FormatterStep> steps, FormatExceptionPolicy exceptionPolicy) {
50+
private Formatter(String name, LineEnding.Policy lineEndingsPolicy, Charset encoding, Path rootDirectory, List<FormatterStep> steps, FormatExceptionPolicy exceptionPolicy) {
51+
this.name = name;
4952
this.lineEndingsPolicy = Objects.requireNonNull(lineEndingsPolicy, "lineEndingsPolicy");
5053
this.encoding = Objects.requireNonNull(encoding, "encoding");
5154
this.rootDir = Objects.requireNonNull(rootDirectory, "rootDir");
@@ -55,6 +58,7 @@ private Formatter(LineEnding.Policy lineEndingsPolicy, Charset encoding, Path ro
5558

5659
// override serialize output
5760
private void writeObject(ObjectOutputStream out) throws IOException {
61+
out.writeObject(name);
5862
out.writeObject(lineEndingsPolicy);
5963
out.writeObject(encoding.name());
6064
out.writeObject(rootDir.toString());
@@ -65,6 +69,7 @@ private void writeObject(ObjectOutputStream out) throws IOException {
6569
// override serialize input
6670
@SuppressWarnings("unchecked")
6771
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
72+
name = (String) in.readObject();
6873
lineEndingsPolicy = (LineEnding.Policy) in.readObject();
6974
encoding = Charset.forName((String) in.readObject());
7075
rootDir = Paths.get((String) in.readObject());
@@ -78,6 +83,10 @@ private void readObjectNoData() throws ObjectStreamException {
7883
throw new UnsupportedOperationException();
7984
}
8085

86+
public String getName() {
87+
return name;
88+
}
89+
8190
public LineEnding.Policy getLineEndingsPolicy() {
8291
return lineEndingsPolicy;
8392
}
@@ -103,6 +112,8 @@ public static Formatter.Builder builder() {
103112
}
104113

105114
public static class Builder {
115+
// optional parameters
116+
private String name = "unnamed";
106117
// required parameters
107118
private LineEnding.Policy lineEndingsPolicy;
108119
private Charset encoding;
@@ -112,6 +123,11 @@ public static class Builder {
112123

113124
private Builder() {}
114125

126+
public Builder name(String name) {
127+
this.name = name;
128+
return this;
129+
}
130+
115131
public Builder lineEndingsPolicy(LineEnding.Policy lineEndingsPolicy) {
116132
this.lineEndingsPolicy = lineEndingsPolicy;
117133
return this;
@@ -138,7 +154,7 @@ public Builder exceptionPolicy(FormatExceptionPolicy exceptionPolicy) {
138154
}
139155

140156
public Formatter build() {
141-
return new Formatter(lineEndingsPolicy, encoding, rootDir, steps,
157+
return new Formatter(name, lineEndingsPolicy, encoding, rootDir, steps,
142158
exceptionPolicy == null ? FormatExceptionPolicy.failOnlyOnError() : exceptionPolicy);
143159
}
144160
}
@@ -253,6 +269,7 @@ public String compute(String unix, File file) {
253269
public int hashCode() {
254270
final int prime = 31;
255271
int result = 1;
272+
result = prime * result + name.hashCode();
256273
result = prime * result + encoding.hashCode();
257274
result = prime * result + lineEndingsPolicy.hashCode();
258275
result = prime * result + rootDir.hashCode();
@@ -273,7 +290,8 @@ public boolean equals(Object obj) {
273290
return false;
274291
}
275292
Formatter other = (Formatter) obj;
276-
return encoding.equals(other.encoding) &&
293+
return name.equals(other.name) &&
294+
encoding.equals(other.encoding) &&
277295
lineEndingsPolicy.equals(other.lineEndingsPolicy) &&
278296
rootDir.equals(other.rootDir) &&
279297
steps.equals(other.steps) &&

lib/src/main/java/com/diffplug/spotless/SpotlessCache.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,13 +27,18 @@
2727

2828
import javax.annotation.Nullable;
2929

30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
3033
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
3134

3235
/**
3336
* Spotless' global cache. {@link SpotlessCache#clear()} should be called
3437
* when Spotless is no longer in use to release any resources it has grabbed.
3538
*/
3639
public final class SpotlessCache {
40+
private static final Logger LOGGER = LoggerFactory.getLogger(SpotlessCache.class);
41+
3742
/** Allows comparing keys based on their serialization. */
3843
static final class SerializedKey {
3944
final byte[] serialized;
@@ -68,7 +73,10 @@ synchronized ClassLoader classloader(JarState state) {
6873
synchronized ClassLoader classloader(Serializable key, JarState state) {
6974
SerializedKey serializedKey = new SerializedKey(key);
7075
return cache
71-
.computeIfAbsent(serializedKey, k -> new FeatureClassLoader(state.jarUrls(), this.getClass().getClassLoader()));
76+
.computeIfAbsent(serializedKey, k -> {
77+
LOGGER.debug("Allocating an additional FeatureClassLoader for key={} Cache.size was {}", key, cache.size());
78+
return new FeatureClassLoader(state.jarUrls(), this.getClass().getClassLoader());
79+
});
7280
}
7381

7482
static SpotlessCache instance() {

0 commit comments

Comments
 (0)