-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The new FormatterStep.create() and createLazy() utilized anonymous cl…
…asses which captured their parameters in non-transient fields. Thanks to FindBugs for spotting the error! New implementation ensures that the *only state* is the serializable key.
- Loading branch information
Showing
2 changed files
with
89 additions
and
37 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/main/java/com/diffplug/gradle/spotless/FormatExtensionStandardImpl.java
This file contains 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,86 @@ | ||
/* | ||
* Copyright 2016 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.gradle.spotless; | ||
|
||
import java.io.File; | ||
import java.io.Serializable; | ||
import java.util.Objects; | ||
import java.util.function.BiFunction; | ||
|
||
import com.diffplug.common.base.Throwing; | ||
import com.diffplug.gradle.spotless.FormatterStep.Strict; | ||
|
||
/** | ||
* Standard implementation of FormatExtension which cleanly enforces | ||
* separation of serializable configuration and a pure format function. | ||
*/ | ||
abstract class FormatExtensionStandardImpl<Key extends Serializable> extends Strict<Key> { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** Transient because only the state matters. */ | ||
final transient String name; | ||
/** Transient because only the state matters. */ | ||
final transient BiFunction<Key, String, String> formatter; | ||
|
||
public FormatExtensionStandardImpl(String name, BiFunction<Key, String, String> formatter) { | ||
this.name = Objects.requireNonNull(name); | ||
this.formatter = Objects.requireNonNull(formatter); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
protected String format(Key key, String rawUnix, File file) { | ||
return formatter.apply(key, rawUnix); | ||
} | ||
|
||
static class Eager<Key extends Serializable> extends FormatExtensionStandardImpl<Key> { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** Transient because the key is persisted by the superclass. */ | ||
final transient Key key; | ||
|
||
public Eager(String name, Key key, BiFunction<Key, String, String> formatter) { | ||
super(name, formatter); | ||
this.key = Objects.requireNonNull(key); | ||
} | ||
|
||
@Override | ||
protected Key calculateKey() throws Exception { | ||
return key; | ||
} | ||
} | ||
|
||
static class Lazy<Key extends Serializable> extends FormatExtensionStandardImpl<Key> { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** Transient because the key is persisted by the superclass. */ | ||
final transient Throwing.Specific.Supplier<Key, Exception> keySupplier; | ||
|
||
public Lazy(String name, Throwing.Specific.Supplier<Key, Exception> keySupplier, BiFunction<Key, String, String> formatter) { | ||
super(name, formatter); | ||
this.keySupplier = Objects.requireNonNull(keySupplier); | ||
} | ||
|
||
@Override | ||
protected Key calculateKey() throws Exception { | ||
return keySupplier.get(); | ||
} | ||
} | ||
} |
This file contains 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