-
Notifications
You must be signed in to change notification settings - Fork 549
Expand file tree
/
Copy pathConfigurationCacheHackList.java
More file actions
167 lines (151 loc) · 5.45 KB
/
Copy pathConfigurationCacheHackList.java
File metadata and controls
167 lines (151 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2024-2026 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;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import com.diffplug.spotless.yaml.SerializeToByteArrayHack;
/**
* Gradle requires three things:
* - Gradle defines cache equality based on your serialized representation
* - Combined with remote build cache, you cannot have any absolute paths in
* your serialized representation
* - Combined with configuration cache, you must be able to roundtrip yourself
* through serialization
*
* These requirements are at odds with each other, as described in these issues
* - Gradle issue to define custom equality
* https://github.com/gradle/gradle/issues/29816
* - Spotless plea for developer cache instead of configuration cache
* https://github.com/diffplug/spotless/issues/987
* - Spotless cache miss bug fixed by this class
* https://github.com/diffplug/spotless/issues/2168
*
* This class is a `List<FormatterStep>` which can optimize the
* serialized representation for either
* - roundtrip integrity
* - OR
* - equality
*
* Because it is not possible to provide both at the same time.
* It is a horrific hack, but it works, and it's the only way I can figure
* to make Spotless work with all of Gradle's cache systems at once.
*/
public final class ConfigurationCacheHackList implements Serializable {
@Serial
private static final long serialVersionUID = 6914178791997323870L;
private boolean optimizeForEquality;
private ArrayList<Object> backingList = new ArrayList<>();
private boolean shouldWeSerializeToByteArrayFirst() {
return backingList.stream().anyMatch(SerializeToByteArrayHack.class::isInstance);
}
private void writeObject(ObjectOutputStream out) throws IOException {
boolean serializeToByteArrayFirst = shouldWeSerializeToByteArrayFirst();
out.writeBoolean(serializeToByteArrayFirst);
out.writeBoolean(optimizeForEquality);
out.writeInt(backingList.size());
for (Object obj : backingList) {
// if write out the list on its own, we'll get java's non-deterministic object-graph serialization
// by writing each object to raw bytes independently, we avoid this
if (serializeToByteArrayFirst) {
out.writeObject(LazyForwardingEquality.toBytes((Serializable) obj));
} else {
out.writeObject(obj);
}
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
boolean serializeToByteArrayFirst = in.readBoolean();
optimizeForEquality = in.readBoolean();
backingList = new ArrayList<>();
int size = in.readInt();
for (int i = 0; i < size; i++) {
if (serializeToByteArrayFirst) {
backingList.add(LazyForwardingEquality.fromBytes((byte[]) in.readObject()));
} else {
backingList.add(in.readObject());
}
}
}
public static ConfigurationCacheHackList forEquality() {
return new ConfigurationCacheHackList(true);
}
public static ConfigurationCacheHackList forRoundtrip() {
return new ConfigurationCacheHackList(false);
}
private ConfigurationCacheHackList(boolean optimizeForEquality) {
this.optimizeForEquality = optimizeForEquality;
}
public void clear() {
backingList.clear();
}
public void addAll(Collection<? extends FormatterStep> c) {
for (FormatterStep step : c) {
if (step instanceof FormatterStepSerializationRoundtrip roundtrip) {
var clone = roundtrip.hackClone(optimizeForEquality);
backingList.add(clone);
} else {
backingList.add(step);
}
}
}
public List<FormatterStep> getSteps() {
var result = new ArrayList<FormatterStep>(backingList.size());
for (Object obj : backingList) {
if (obj instanceof FormatterStepSerializationRoundtrip.HackClone clone) {
result.add(clone.rehydrate());
} else {
result.add((FormatterStep) obj);
}
}
return result;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ConfigurationCacheHackList stepList = (ConfigurationCacheHackList) o;
return optimizeForEquality == stepList.optimizeForEquality
&& backingList.equals(stepList.backingList);
}
@Override
public int hashCode() {
return Objects.hash(optimizeForEquality, backingList);
}
/**
* Must not call {@link #hashCode()} — that fingerprints every step and may provision
* P2/Maven deps. Gradle includes this value in "cannot be serialized" messages; a
* side-effecting {@code toString} re-triggers provisioning and masks the real cause
* (see <a href="https://github.com/diffplug/spotless/issues/3004">#3004</a>).
*/
@Override
public String toString() {
return getClass().getName()
+ "@" + Integer.toHexString(System.identityHashCode(this))
+ "[optimizeForEquality=" + optimizeForEquality
+ ", size=" + backingList.size() + "]";
}
}