Skip to content

Commit 168e597

Browse files
committed
Add a sentinel step which triggers the "serialize to byte array hack"
1 parent e5a6f2b commit 168e597

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
package com.diffplug.spotless;
1717

1818
import java.io.IOException;
19+
import java.io.Serializable;
1920
import java.util.ArrayList;
2021
import java.util.Collection;
2122
import java.util.List;
2223
import java.util.Objects;
2324

25+
import com.diffplug.spotless.yaml.SerializeToByteArrayHack;
26+
2427
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2528

2629
/**
@@ -50,27 +53,43 @@
5053
* to make Spotless work with all of Gradle's cache systems at once.
5154
*/
5255
public class ConfigurationCacheHackList implements java.io.Serializable {
53-
private static final long serialVersionUID = 1L;
56+
private static final long serialVersionUID = 6914178791997323870L;
57+
5458
private boolean optimizeForEquality;
5559
private ArrayList<Object> backingList = new ArrayList<>();
5660

61+
private boolean shouldWeSerializeToByteArrayFirst() {
62+
return backingList.stream().anyMatch(step -> step instanceof SerializeToByteArrayHack);
63+
}
64+
5765
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
66+
boolean serializeToByteArrayFirst = shouldWeSerializeToByteArrayFirst();
67+
out.writeBoolean(serializeToByteArrayFirst);
5868
out.writeBoolean(optimizeForEquality);
5969
out.writeInt(backingList.size());
6070
for (Object obj : backingList) {
6171
// if write out the list on its own, we'll get java's non-deterministic object-graph serialization
6272
// by writing each object to raw bytes independently, we avoid this
63-
out.writeObject(obj);
73+
if (serializeToByteArrayFirst) {
74+
out.writeObject(LazyForwardingEquality.toBytes((Serializable) obj));
75+
} else {
76+
out.writeObject(obj);
77+
}
6478
}
6579
}
6680

6781
@SuppressFBWarnings("MC_OVERRIDABLE_METHOD_CALL_IN_READ_OBJECT")
6882
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
83+
boolean serializeToByteArrayFirst = in.readBoolean();
6984
optimizeForEquality = in.readBoolean();
7085
backingList = new ArrayList<>();
7186
int size = in.readInt();
7287
for (int i = 0; i < size; i++) {
73-
backingList.add(in.readObject());
88+
if (serializeToByteArrayFirst) {
89+
backingList.add(LazyForwardingEquality.fromBytes((byte[]) in.readObject()));
90+
} else {
91+
backingList.add(in.readObject());
92+
}
7493
}
7594
}
7695

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.yaml;
17+
18+
import java.io.File;
19+
20+
import javax.annotation.Nullable;
21+
22+
import com.diffplug.spotless.FormatterStep;
23+
24+
public class SerializeToByteArrayHack implements FormatterStep {
25+
private static final long serialVersionUID = 8071047581828362545L;
26+
27+
@Override
28+
public String getName() {
29+
return "hack to force serializing objects to byte array";
30+
}
31+
32+
@Nullable
33+
@Override
34+
public String format(String rawUnix, File file) throws Exception {
35+
return null;
36+
}
37+
38+
@Override
39+
public void close() throws Exception {
40+
41+
}
42+
}

testlib/src/test/java/com/diffplug/spotless/combined/CombinedJavaFormatStepTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 DiffPlug
2+
* Copyright 2023-2025 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.
@@ -31,6 +31,7 @@
3131
import com.diffplug.spotless.java.GoogleJavaFormatStep;
3232
import com.diffplug.spotless.java.ImportOrderStep;
3333
import com.diffplug.spotless.java.RemoveUnusedImportsStep;
34+
import com.diffplug.spotless.yaml.SerializeToByteArrayHack;
3435

3536
public class CombinedJavaFormatStepTest extends ResourceHarness {
3637

@@ -45,6 +46,7 @@ void checkIssue1679() {
4546
FenceStep toggleOffOnPair = FenceStep.named(FenceStep.defaultToggleName()).openClose("formatting:off", "formatting:on");
4647
try (StepHarness formatter = StepHarness.forSteps(
4748
toggleOffOnPair.preserveWithin(List.of(
49+
new SerializeToByteArrayHack(),
4850
gjf,
4951
indentWithSpaces,
5052
importOrder,

0 commit comments

Comments
 (0)