Skip to content

Commit

Permalink
Check for class availability before registering java 8 classes
Browse files Browse the repository at this point in the history
As reported in EsotericSoftware#410, on android java version detection via `java.version`
system property does not work - it's just not set.
Therefore we now check directly if the class is available as guard
for registering an optional default serializer.

Refs EsotericSoftware#410, EsotericSoftware#358, EsotericSoftware#360
  • Loading branch information
magro committed Mar 24, 2016
1 parent cef15a3 commit 8fd3f89
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package com.esotericsoftware.kryo.serializers;

import static com.esotericsoftware.kryo.util.JavaVersion.isJava;
import static com.esotericsoftware.kryo.util.Util.isClassAvailable;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
Expand All @@ -38,12 +38,14 @@
public final class OptionalSerializers {

public static void addDefaultSerializers(Kryo kryo) {
if (isJava(8)) {
if(isClassAvailable("java.util.Optional"))
kryo.addDefaultSerializer(Optional.class, new OptionalSerializer());
if(isClassAvailable("java.util.OptionalInt"))
kryo.addDefaultSerializer(OptionalInt.class, new OptionalIntSerializer());
if(isClassAvailable("java.util.OptionalLong"))
kryo.addDefaultSerializer(OptionalLong.class, new OptionalLongSerializer());
if(isClassAvailable("java.util.OptionalDouble"))
kryo.addDefaultSerializer(OptionalDouble.class, new OptionalDoubleSerializer());
}
}

private static class OptionalSerializer extends Serializer<Optional> {
Expand Down
18 changes: 15 additions & 3 deletions src/com/esotericsoftware/kryo/serializers/TimeSerializers.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package com.esotericsoftware.kryo.serializers;

import static com.esotericsoftware.kryo.util.JavaVersion.isJava;
import static com.esotericsoftware.kryo.util.Util.isClassAvailable;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
Expand All @@ -40,22 +40,34 @@
public final class TimeSerializers {

public static void addDefaultSerializers(Kryo kryo) {
if(isJava(8)) {
if(isClassAvailable("java.time.Duration"))
kryo.addDefaultSerializer(Duration.class, new DurationSerializer());
if(isClassAvailable("java.time.Instant"))
kryo.addDefaultSerializer(Instant.class, new InstantSerializer());
if(isClassAvailable("java.time.LocalDate"))
kryo.addDefaultSerializer(LocalDate.class, new LocalDateSerializer());
if(isClassAvailable("java.time.LocalTime"))
kryo.addDefaultSerializer(LocalTime.class, new LocalTimeSerializer());
if(isClassAvailable("java.time.LocalDateTime"))
kryo.addDefaultSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
if(isClassAvailable("java.time.ZoneOffset"))
kryo.addDefaultSerializer(ZoneOffset.class, new ZoneOffsetSerializer());
if(isClassAvailable("java.time.ZoneId"))
kryo.addDefaultSerializer(ZoneId.class, new ZoneIdSerializer());
if(isClassAvailable("java.time.OffsetTime"))
kryo.addDefaultSerializer(OffsetTime.class, new OffsetTimeSerializer());
if(isClassAvailable("java.time.OffsetDateTime"))
kryo.addDefaultSerializer(OffsetDateTime.class, new OffsetDateTimeSerializer());
if(isClassAvailable("java.time.ZonedDateTime"))
kryo.addDefaultSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer());
if(isClassAvailable("java.time.Year"))
kryo.addDefaultSerializer(Year.class, new YearSerializer());
if(isClassAvailable("java.time.YearMonth"))
kryo.addDefaultSerializer(YearMonth.class, new YearMonthSerializer());
if(isClassAvailable("java.time.MonthDay"))
kryo.addDefaultSerializer(MonthDay.class, new MonthDaySerializer());
if(isClassAvailable("java.time.Period"))
kryo.addDefaultSerializer(Period.class, new PeriodSerializer());
}
}

private static class DurationSerializer extends Serializer<Duration> {
Expand Down
36 changes: 0 additions & 36 deletions src/com/esotericsoftware/kryo/util/JavaVersion.java

This file was deleted.

16 changes: 13 additions & 3 deletions src/com/esotericsoftware/kryo/util/Util.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.util;

import static com.esotericsoftware.minlog.Log.*;
Expand All @@ -26,6 +26,16 @@
public class Util {
static public boolean isAndroid = "Dalvik".equals(System.getProperty("java.vm.name"));

public static boolean isClassAvailable(String className) {
try {
Class.forName(className);
return true;
} catch (Exception e) {
debug("kryo", "Class not available: " + className);
return false;
}
}

/** Returns the primitive wrapper class for a primitive class.
* @param type Must be a primitive class. */
static public Class getWrapperClass (Class type) {
Expand Down Expand Up @@ -166,4 +176,4 @@ static public long swapLong(long value) {
( ( ( value >> 48 ) & 0xff ) << 8 )|
( ( ( value >> 56 ) & 0xff ) << 0 );
}
}
}

0 comments on commit 8fd3f89

Please sign in to comment.