-
Notifications
You must be signed in to change notification settings - Fork 481
Start making each FormatterStep
roundtrip serializable.
#1945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
eb0df83
StepHarness (and WithFile) now have a `supportsRoundTrip` method so w…
nedtwigg e3843cd
`StepHarnessBase` is now embarked on a methodical journey to make eve…
nedtwigg cb346a4
Introduce a new `FormatterStep` base class for the serialized roundtr…
nedtwigg decbd1b
Convert `IndentStep` to be roundtrip serializable.
nedtwigg 317b2ac
Remove the long-deprecated and completely unused `Set<String> mavenCo…
nedtwigg e4d6c49
Introduce `FileSignature.RoundTrippable` and `JarState.Promised` so t…
nedtwigg 6f7edd3
Make `DiktatStep` round-trippable.
nedtwigg 9fa1da3
Fix spotbugs warnings.
nedtwigg cce20d4
Rename `FileSignature.RoundTrippable` to `FileSignature.Promised` to …
nedtwigg 56bb67e
More improvements to `StepHarnessWithFile`.
nedtwigg c56968a
Add a roundtrip-serializable method to `FormatterStep`.
nedtwigg 46635c6
Fixup the `FormatterStep` api.
nedtwigg f5f6836
Use the new API.
nedtwigg 44b3126
Fixup spotbugs warnings.
nedtwigg 55481fa
`FileSignature.Promised` and `JarState.Promised` should both have `ge…
nedtwigg ab1452d
Streamline the `FileSignature` api.
nedtwigg 4e54fc2
Update the `CONTRIBUTING.md` for our new roundtrip serializable world.
nedtwigg 5a05beb
Merge branch 'main' into serializable-refactor
nedtwigg 005113e
Remove unneeded `public` declarations.
nedtwigg ce34f89
Fix various typos found by @jbduncan
nedtwigg d102709
merge main (DiktatStep conflict)
nedtwigg 3bd4243
Copyright updates.
nedtwigg 19aff77
Merge branch 'main' into serializable-refactor
nedtwigg 4fdb178
Remove unused method.
nedtwigg 14991e4
Update changelog.
nedtwigg 4b15b39
Fix an oversight from the merge conflict earlier.
nedtwigg 93eb099
Merge branch 'main' into serializable-refactor
nedtwigg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
lib/src/main/java/com/diffplug/spotless/FormatterStepEqualityOnStateSerialization.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2016-2024 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.File; | ||
import java.io.Serializable; | ||
import java.util.Arrays; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
||
/** | ||
* Standard implementation of FormatterStep which cleanly enforces | ||
* separation of a lazily computed "state" object whose serialized form | ||
* is used as the basis for equality and hashCode, which is separate | ||
* from the serialized form of the step itself, which can include absolute paths | ||
* and such without interfering with buildcache keys. | ||
*/ | ||
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED") | ||
abstract class FormatterStepEqualityOnStateSerialization<State extends Serializable> implements FormatterStep, Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
protected abstract State stateSupplier() throws Exception; | ||
|
||
protected abstract FormatterFunc stateToFormatter(State state) throws Exception; | ||
|
||
private transient FormatterFunc formatter; | ||
private transient State stateInternal; | ||
private transient byte[] serializedStateInternal; | ||
|
||
@Override | ||
public String format(String rawUnix, File file) throws Exception { | ||
if (formatter == null) { | ||
formatter = stateToFormatter(state()); | ||
} | ||
return formatter.apply(rawUnix, file); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null) { | ||
return false; | ||
} else if (getClass() != o.getClass()) { | ||
return false; | ||
} else { | ||
return Arrays.equals(serializedState(), ((FormatterStepEqualityOnStateSerialization<?>) o).serializedState()); | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Arrays.hashCode(serializedState()); | ||
} | ||
|
||
void cleanupFormatterFunc() { | ||
if (formatter instanceof FormatterFunc.Closeable) { | ||
((FormatterFunc.Closeable) formatter).close(); | ||
formatter = null; | ||
} | ||
} | ||
|
||
private State state() throws Exception { | ||
if (stateInternal == null) { | ||
stateInternal = stateSupplier(); | ||
} | ||
return stateInternal; | ||
} | ||
|
||
private byte[] serializedState() { | ||
if (serializedStateInternal == null) { | ||
serializedStateInternal = ThrowingEx.get(() -> LazyForwardingEquality.toBytes(state())); | ||
} | ||
return serializedStateInternal; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.