Skip to content

Commit

Permalink
Add a sentinel step which triggers the "serialize to byte array hack"
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Jan 6, 2025
1 parent e5a6f2b commit 168e597
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
package com.diffplug.spotless;

import java.io.IOException;
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;

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

/**
Expand Down Expand Up @@ -50,27 +53,43 @@
* to make Spotless work with all of Gradle's cache systems at once.
*/
public class ConfigurationCacheHackList implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 6914178791997323870L;

private boolean optimizeForEquality;
private ArrayList<Object> backingList = new ArrayList<>();

private boolean shouldWeSerializeToByteArrayFirst() {
return backingList.stream().anyMatch(step -> step instanceof SerializeToByteArrayHack);
}

private void writeObject(java.io.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
out.writeObject(obj);
if (serializeToByteArrayFirst) {
out.writeObject(LazyForwardingEquality.toBytes((Serializable) obj));
} else {
out.writeObject(obj);
}
}
}

@SuppressFBWarnings("MC_OVERRIDABLE_METHOD_CALL_IN_READ_OBJECT")
private void readObject(java.io.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++) {
backingList.add(in.readObject());
if (serializeToByteArrayFirst) {
backingList.add(LazyForwardingEquality.fromBytes((byte[]) in.readObject()));
} else {
backingList.add(in.readObject());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.yaml;

import java.io.File;

import javax.annotation.Nullable;

import com.diffplug.spotless.FormatterStep;

public class SerializeToByteArrayHack implements FormatterStep {
private static final long serialVersionUID = 8071047581828362545L;

@Override
public String getName() {
return "hack to force serializing objects to byte array";
}

@Nullable
@Override
public String format(String rawUnix, File file) throws Exception {
return null;
}

@Override
public void close() throws Exception {

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 DiffPlug
* Copyright 2023-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 Down Expand Up @@ -31,6 +31,7 @@
import com.diffplug.spotless.java.GoogleJavaFormatStep;
import com.diffplug.spotless.java.ImportOrderStep;
import com.diffplug.spotless.java.RemoveUnusedImportsStep;
import com.diffplug.spotless.yaml.SerializeToByteArrayHack;

public class CombinedJavaFormatStepTest extends ResourceHarness {

Expand All @@ -45,6 +46,7 @@ void checkIssue1679() {
FenceStep toggleOffOnPair = FenceStep.named(FenceStep.defaultToggleName()).openClose("formatting:off", "formatting:on");
try (StepHarness formatter = StepHarness.forSteps(
toggleOffOnPair.preserveWithin(List.of(
new SerializeToByteArrayHack(),
gjf,
indentWithSpaces,
importOrder,
Expand Down

0 comments on commit 168e597

Please sign in to comment.