Skip to content

Commit

Permalink
Removed auto registration of Java8 closures.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed Jan 6, 2015
1 parent bfc02be commit 1c5562d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
35 changes: 9 additions & 26 deletions src/com/esotericsoftware/kryo/Kryo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package com.esotericsoftware.kryo;

import static com.esotericsoftware.kryo.util.Util.*;
Expand Down Expand Up @@ -103,7 +103,6 @@
import com.esotericsoftware.kryo.util.MapReferenceResolver;
import com.esotericsoftware.kryo.util.ObjectMap;
import com.esotericsoftware.kryo.util.Util;
import com.esotericsoftware.minlog.Log;
import com.esotericsoftware.reflectasm.ConstructorAccess;

/** Maps classes to serializers so object graphs can be serialized automatically.
Expand Down Expand Up @@ -223,20 +222,6 @@ public Kryo (ClassResolver classResolver, ReferenceResolver referenceResolver, S
register(long.class, new LongSerializer());
register(double.class, new DoubleSerializer());
register(void.class, new VoidSerializer());

// Lambdas support
// Enable only if JVM supports it
try {
String version = System.getProperty("java.version");
char minor = version.charAt(2);
if (minor >= '8') {
register(Class.forName("java.lang.invoke.SerializedLambda"));
register(Closure.class, (Serializer)Class.forName("com.esotericsoftware.kryo.serializers.ClosureSerializer")
.newInstance());
}
} catch (Exception e) {
Log.trace("Serialization of Java8 lambdas is not available on this system.");
}
}

// --- Default serializers ---
Expand Down Expand Up @@ -361,7 +346,7 @@ public Serializer getDefaultSerializer (Class type) {
if (type == null) throw new IllegalArgumentException("type cannot be null.");

final Serializer serializerForAnnotation = getDefaultSerializerForAnnotatedType(type);
if (serializerForAnnotation!=null) return serializerForAnnotation;
if (serializerForAnnotation != null) return serializerForAnnotation;

for (int i = 0, n = defaultSerializers.size(); i < n; i++) {
DefaultSerializerEntry entry = defaultSerializers.get(i);
Expand All @@ -374,8 +359,7 @@ public Serializer getDefaultSerializer (Class type) {
return newDefaultSerializer(type);
}

protected Serializer getDefaultSerializerForAnnotatedType(Class type)
{
protected Serializer getDefaultSerializerForAnnotatedType (Class type) {
if (type.isAnnotationPresent(DefaultSerializer.class)) {
DefaultSerializer defaultSerializerAnnotation = (DefaultSerializer)type.getAnnotation(DefaultSerializer.class);
return ReflectionSerializerFactory.makeSerializer(this, defaultSerializerAnnotation.value(), type);
Expand Down Expand Up @@ -1097,7 +1081,7 @@ public <T> T newInstance (Class<T> type) {
Registration registration = getRegistration(type);
ObjectInstantiator instantiator = registration.getInstantiator();
if (instantiator == null) {
instantiator = newInstantiator(type);
instantiator = newInstantiator(type);
registration.setInstantiator(instantiator);
}
return (T)instantiator.newInstance();
Expand Down Expand Up @@ -1153,11 +1137,10 @@ public boolean isFinal (Class type) {
if (type.isArray()) return Modifier.isFinal(Util.getElementClass(type).getModifiers());
return Modifier.isFinal(type.getModifiers());
}

/** Returns true if the specified type is a closure.
* <p>
* This can be overridden to support alternative implementations of clousres. Current version supports
* Oracle's Java8 only */
* This can be overridden to support alternative implementations of clousres. Current version supports Oracle's Java8 only */
public boolean isClousre (Class type) {
if (type == null) throw new IllegalArgumentException("type cannot be null.");
return type.getName().indexOf('/') >= 0;
Expand Down Expand Up @@ -1289,7 +1272,7 @@ public Object newInstance () {
return fallbackStrategy.newInstantiatorOf(type);
}
}

private static class Closure {
}
}
}
14 changes: 8 additions & 6 deletions src/com/esotericsoftware/kryo/serializers/ClosureSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package com.esotericsoftware.kryo.serializers;

import java.io.IOException;
import java.lang.reflect.Method;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

/** Serializer for Java8 closures.
/** Serializer for Java8 closures. To serialize closures, use:
* <p>
* <code>
* kryo.register(java.lang.invoke.SerializedLambda);<br>
* kryo.register(Closure.class, new ClosureSerializer());</code>
* @author Roman Levenstein <[email protected]> */
public class ClosureSerializer extends Serializer {

Expand Down Expand Up @@ -86,4 +88,4 @@ public Object copy (Kryo kryo, Object original) {
throw new RuntimeException("Could not serialize lambda", e);
}
}
}
}

0 comments on commit 1c5562d

Please sign in to comment.