Skip to content

Commit

Permalink
FormatExceptionPolicy implementations need to implement Serializable …
Browse files Browse the repository at this point in the history
…and equals/hashCode with the same constraints as SerializableFileFilter. Refactored the `toBytes()` method into the interface `NoLambda`, and moved `SerializableFileFilter.EqualityBasedOnSerialization` to `NoLambda.EqualityBasedOnSerialization`. Technically a breaking change, but will affect exactly zero real-world client code.
  • Loading branch information
nedtwigg committed Jan 16, 2017
1 parent 1b8483e commit e36ddab
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.nio.file.Path;

/** A policy for handling exceptions in the format. */
public interface FormatExceptionPolicy extends Serializable {
public interface FormatExceptionPolicy extends Serializable, NoLambda {
/** Called for every error in the formatter. */
void handleError(Throwable e, FormatterStep step, File file, Path rootDir);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

class FormatExceptionPolicyLegacy implements FormatExceptionPolicy {
class FormatExceptionPolicyLegacy extends NoLambda.EqualityBasedOnSerialization implements FormatExceptionPolicy {
private static final long serialVersionUID = 1L;

private static final Logger logger = Logger.getLogger(Formatter.class.getName());
Expand All @@ -34,9 +34,4 @@ public void handleError(Throwable e, FormatterStep step, File file, Path rootDir
logger.log(Level.WARNING, "Unable to apply step '" + step.getName() + "' to '" + rootDir.relativize(file.toPath()), e);
}
}

@Override
public byte[] toBytes() {
return LazyForwardingEquality.toBytes(this);
}
}
71 changes: 71 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/NoLambda.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.spotless;

import java.io.Serializable;
import java.util.Arrays;

/**
* Marker interface to prevent lambda implementations of
* single-method interfaces that require serializability.
*
* In order for Spotless to support up-to-date checks, all
* of its parameters must be {@link Serializable} so that
* entries can be written to file, and they must implement
* equals and hashCode correctly.
*
* This interface and its standard implementation,
* {@link EqualityBasedOnSerialization}, are a quick way
* to accomplish these goals.
*/
public interface NoLambda extends Serializable {
/**
* Returns a byte array representation of everything inside this `SerializableFileFilter`.
*
* The main purpose of this method is to ensure one can't instantiate this class with lambda
* expressions, which are notoriously difficult to serialize and deserialize properly. (See
* `SerializableFileFilterImpl.SkipFilesNamed` for an example of how to make a serializable
* subclass.)
*/
public byte[] toBytes();

/** An implementation of NoLambda in which equality is based on the serialized representation of itself. */
public static abstract class EqualityBasedOnSerialization implements NoLambda {
private static final long serialVersionUID = 1733798699224768949L;

@Override
public byte[] toBytes() {
return LazyForwardingEquality.toBytes(this);
}

@Override
public int hashCode() {
return Arrays.hashCode(toBytes());
}

@Override
public boolean equals(Object otherObj) {
if (otherObj == null) {
return false;
} else if (otherObj.getClass().equals(this.getClass())) {
EqualityBasedOnSerialization other = (EqualityBasedOnSerialization) otherObj;
return Arrays.equals(toBytes(), other.toBytes());
} else {
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,11 @@

import java.io.FileFilter;
import java.io.Serializable;
import java.util.Arrays;

/** A file filter with full support for serialization. */
public interface SerializableFileFilter extends FileFilter, Serializable {
public interface SerializableFileFilter extends FileFilter, Serializable, NoLambda {
/** Creates a FileFilter which will accept all files except files with the given name. */
public static SerializableFileFilter skipFilesNamed(String name) {
return new SerializableFileFilterImpl.SkipFilesNamed(name);
}

/**
* Returns a byte array representation of everything inside this `SerializableFileFilter`.
*
* The main purpose of this method is to ensure one can't instantiate this class with lambda
* expressions, which are notoriously difficult to serialize and deserialize properly. (See
* `SerializableFileFilterImpl.SkipFilesNamed` for an example of how to make a serializable
* subclass.)
*/
public byte[] toBytes();

/** An implementation of SerializableFileFilter in which equality is based on the serialized representation. */
public static abstract class EqualityBasedOnSerialization implements SerializableFileFilter {
private static final long serialVersionUID = 1733798699224768949L;

@Override
public byte[] toBytes() {
return LazyForwardingEquality.toBytes(this);
}

@Override
public int hashCode() {
return Arrays.hashCode(toBytes());
}

@Override
public boolean equals(Object otherObj) {
if (otherObj == null) {
return false;
} else if (otherObj.getClass().equals(this.getClass())) {
EqualityBasedOnSerialization other = (EqualityBasedOnSerialization) otherObj;
return Arrays.equals(toBytes(), other.toBytes());
} else {
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Objects;

class SerializableFileFilterImpl {
static class SkipFilesNamed extends SerializableFileFilter.EqualityBasedOnSerialization {
static class SkipFilesNamed extends NoLambda.EqualityBasedOnSerialization implements SerializableFileFilter {
private static final long serialVersionUID = 1L;

private final String nameToSkip;
Expand Down

0 comments on commit e36ddab

Please sign in to comment.