Skip to content
Permalink
Browse files

fixed bug JPPF-597

  • Loading branch information...
lolocohen committed Jul 27, 2019
1 parent 980e773 commit 38de3ab720b924a8380bf547c03ad3af36298567
@@ -27,7 +27,7 @@
*/
public class ConcurrentHashMapHandler extends AbstractSerializationHandler {
@Override
public void writeDeclaredFields(final Serializer serializer, final ClassDescriptor cd, final Object obj) throws Exception {
public void writeObject(final Object obj, final Serializer serializer, final ClassDescriptor cd) throws Exception {
final Map<?, ?> map = (Map<?, ?>) obj;
ClassDescriptor tmpDesc = null;
try {
@@ -45,14 +45,12 @@ public void writeDeclaredFields(final Serializer serializer, final ClassDescript
}

@Override
public void readDeclaredFields(final Deserializer deserializer, final ClassDescriptor cd, final Object obj) throws Exception {
@SuppressWarnings("unchecked")
final Map<? super Object, ? super Object> map = (Map<? super Object, ? super Object>) obj;
public Object readDObject(final Deserializer deserializer, final ClassDescriptor cd) throws Exception {
final ConcurrentHashMap<? super Object, ? super Object> map = new ConcurrentHashMap<>();
ClassDescriptor tmpDesc = null;
try {
tmpDesc = deserializer.currentClassDescriptor;
deserializer.currentClassDescriptor = cd;
if (map instanceof ConcurrentHashMap) copyFields(new ConcurrentHashMap<>(), obj, cd);
final int size = deserializer.readInt();
for (int i=0; i<size; i++) {
final Object key = deserializer.readObject();
@@ -62,5 +60,6 @@ public void readDeclaredFields(final Deserializer deserializer, final ClassDescr
} finally {
deserializer.currentClassDescriptor = tmpDesc;
}
return map;
}
}
@@ -133,12 +133,13 @@ else if (cd.enumType) {
final Object val = (name == null) ? null : Enum.valueOf((Class<? extends Enum>) cd.clazz, name);
caches.handleToObjectMap.put(handle, val);
} else {
final Object obj = newInstance(cd);
currentObject = obj;
currentClassDescriptor = cd;
final SerializationHandler handler = SerializationReflectionHelper.getSerializationHandler(cd.clazz);
final Object obj = (handler != null) ? handler.readDObject(this, cd) : newInstance(cd);
currentObject = obj;
if (traceEnabled) try { log.trace("reading handle={}, object={}", handle, StringUtils.toIdentityString(obj)); } catch(@SuppressWarnings("unused") final Exception e) {}
caches.handleToObjectMap.put(handle, obj);
readFields(cd, obj);
if (handler == null) readFields(cd, obj);
}
}

@@ -168,9 +169,9 @@ void readFields(final ClassDescriptor cd, final Object obj) throws Exception {
tmpDesc = tmpDesc.superClass;
}
for (ClassDescriptor desc: stack) {
final SerializationHandler handler = SerializationReflectionHelper.getSerializationHandler(desc.clazz);
/*final SerializationHandler handler = SerializationReflectionHelper.getSerializationHandler(desc.clazz);
if (handler != null) handler.readDeclaredFields(this, desc, obj);
else if (desc.hasReadWriteObject) {
else*/ if (desc.hasReadWriteObject) {
final Method m = desc.readObjectMethod;
if (traceEnabled) try { log.trace("invoking readObject() for object = {}, class = {}", StringUtils.toIdentityString(obj), desc); } catch(@SuppressWarnings("unused") final Exception e) {}
try {
@@ -0,0 +1,184 @@
/*
* JPPF.
* Copyright (C) 2005-2019 JPPF Team.
* http://www.jppf.org
*
* 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 org.jppf.serialization;

import java.time.*;
import java.util.Vector;

import org.slf4j.*;

/**
* A specfic serialization handler for {@link Vector}.
* @author Laurent Cohen
*/
public class JavaTimeSerializationHandler extends AbstractSerializationHandler {
/**
* Logger for this class.
*/
private static final Logger log = LoggerFactory.getLogger(Serializer.class);
/**
* Determines whether the debug level is enabled in the log configuration, without the cost of a method call.
*/
private static final boolean traceEnabled = log.isTraceEnabled();

@Override
public void writeObject(final Object obj, final Serializer serializer, final ClassDescriptor cd) throws Exception {
if (traceEnabled) log.trace("writing declared fields for cd={}", cd);
if (obj instanceof LocalDateTime) writeLocalDateTime(serializer, (LocalDateTime) obj);
else if (obj instanceof LocalDate) writeLocalDate(serializer, (LocalDate) obj);
else if (obj instanceof LocalTime) writeLocalTime(serializer, (LocalTime) obj);
else if (obj instanceof ZonedDateTime) writeZonedDateTime(serializer, (ZonedDateTime) obj);
else if (obj instanceof ZoneId) writeZoneId(serializer, (ZoneId) obj);
else serializer.writeDeclaredFields(obj, cd);
}

@Override
public Object readDObject(final Deserializer deserializer, final ClassDescriptor cd) throws Exception {
if (traceEnabled) log.trace("reading declared fields for cd={}", cd);
if (cd.clazz == LocalDateTime.class) return readLocalDateTime(deserializer);
else if (cd.clazz == LocalDate.class) return readLocalDate(deserializer);
else if (cd.clazz == LocalTime.class) return readLocalTime(deserializer);
else if (cd.clazz == ZonedDateTime.class) return readZonedDateTime(deserializer);
else if (ZoneId.class.isAssignableFrom(cd.clazz)) return readZoneId(deserializer);
final Object obj = newInstance(cd);
deserializer.readDeclaredFields(cd, obj);
return obj;
}

/**
*
* @param serializer the serializer to use.
* @param obj the object to write.
* @throws Exception if any error occurs.
*/
private static void writeLocalDateTime(final Serializer serializer, final LocalDateTime obj) throws Exception {
writeLocalTime(serializer, obj.toLocalTime());
writeLocalDate(serializer, obj.toLocalDate());
}

/**
*
* @param deserializer the deserializer to use.
* @return a deserialized object.
* @throws Exception if any error occurs.
*/
private static LocalDateTime readLocalDateTime(final Deserializer deserializer) throws Exception {
final LocalTime time = readLocalTime(deserializer);
final LocalDate date = readLocalDate(deserializer);
return LocalDateTime.of(date, time);
}

/**
*
* @param serializer the serializer to use.
* @param obj the object to write.
* @throws Exception if any error occurs.
*/
private static void writeZonedDateTime(final Serializer serializer, final ZonedDateTime obj) throws Exception {
writeLocalTime(serializer, obj.toLocalTime());
writeLocalDate(serializer, obj.toLocalDate());
writeZoneId(serializer, obj.getZone());
}

/**
*
* @param deserializer the deserializer to use.
* @return a deserialized object.
* @throws Exception if any error occurs.
*/
private static ZonedDateTime readZonedDateTime(final Deserializer deserializer) throws Exception {
final LocalTime time = readLocalTime(deserializer);
final LocalDate date = readLocalDate(deserializer);
final ZoneId zoneId = readZoneId(deserializer);
return ZonedDateTime.of(date, time, zoneId);
}

/**
*
* @param serializer the serializer to use.
* @param obj the object for which to write the fields.
* @throws Exception if any error occurs.
*/
private static void writeZoneId(final Serializer serializer, final ZoneId obj) throws Exception {
serializer.writeString(obj.getId());
}

/**
*
* @param deserializer the deserializer to use.
* @return a deserialized object.
* @throws Exception if any error occurs.
*/
private static ZoneId readZoneId(final Deserializer deserializer) throws Exception {
final String id = deserializer.readString();
return ZoneId.of(id);
}

/**
*
* @param serializer the serializer to use.
* @param obj the object for which to write the fields.
* @throws Exception if any error occurs.
*/
private static void writeLocalDate(final Serializer serializer, final LocalDate obj) throws Exception {
serializer.writeInt(obj.getYear());
serializer.out.writeByte((byte) obj.getMonthValue());
serializer.out.writeByte((byte) obj.getDayOfMonth());
}

/**
*
* @param deserializer the deserializer to use.
* @return a LocalDate
* @throws Exception if any error occurs.
*/
private static LocalDate readLocalDate(final Deserializer deserializer) throws Exception {
final int year = deserializer.readInt();
final int month = deserializer.in.readByte();
final int day = deserializer.in.readByte();
return LocalDate.of(year, month, day);
}

/**
*
* @param serializer the serializer to use.
* @param time the object for which to write the fields.
* @throws Exception if any error occurs.
*/
private static void writeLocalTime(final Serializer serializer, final LocalTime time) throws Exception {
serializer.out.writeByte(time.getHour());
serializer.out.writeByte(time.getMinute());
serializer.out.writeByte(time.getSecond());
serializer.writeInt(time.getNano());
}

/**
*
* @param deserializer the deserializer to use.
* @throws Exception if any error occurs.
* @return a LocalTime.
*/
private static LocalTime readLocalTime(final Deserializer deserializer) throws Exception {
final int hour = deserializer.in.readByte();
final int minute = deserializer.in.readByte();
final int second = deserializer.in.readByte();
final int nano = deserializer.readInt();
return LocalTime.of(hour, minute, second, nano);
}
}
@@ -24,20 +24,30 @@
*/
public interface SerializationHandler {
/**
* Write the declared fields of the specified class.
* @param cd the class descriptor.
* @param serializer the serializer to use.
* Serialize an object of the type processed by this serialization handler.
* @param obj the object for which to write the fields.
* @param serializer the serializer to use.
* @param cd the class descriptor.
* @throws Exception if any error occurs.
*/
void writeDeclaredFields(final Serializer serializer, final ClassDescriptor cd, final Object obj) throws Exception;
void writeObject(final Object obj, final Serializer serializer, final ClassDescriptor cd) throws Exception;

/**
* Read the declared fields of the specified class.
* @param cd the class descriptor.
* Deserialize an object of the type processed by this serialization handler.
* @param deserializer the deserializer to use.
* @param obj the object for which to read the fields.
* @param cd the class descriptor.
* @return the deserialized object.
* @throws Exception if any error occurs.
*/
Object readDObject(final Deserializer deserializer, final ClassDescriptor cd) throws Exception;

/**
* Create a new instance of the class described by the specified descriptor.
* @param cd the class descriptor to use.
* @return a new instance of the class.
* @throws Exception if any error occurs.
*/
void readDeclaredFields(final Deserializer deserializer, final ClassDescriptor cd, final Object obj) throws Exception;
default Object newInstance(final ClassDescriptor cd) throws Exception {
return SerializationReflectionHelper.create(cd.clazz);
}
}
@@ -98,6 +98,10 @@
*
*/
private static final Set<Class<?>> TRANSIENT_EXCEPTION_CLASSES = initTransientExceptionClasses();
/**
* Handler for java.time.* classes.
*/
static final SerializationHandler javaTimeHandler = new JavaTimeSerializationHandler();
/**
* Map of classes to their assigned {@link SerializationHandler}, if any.
*/
@@ -492,6 +496,10 @@ public static String createString(final char[] chars) throws Exception {
* @return a {@link SerializationHandler} instance, or null if none is defined for the class.
*/
static SerializationHandler getSerializationHandler(final Class<?> clazz) {
return handlerMap.get(clazz);
SerializationHandler handler = handlerMap.get(clazz);
if (handler == null) {
if (clazz.getName().startsWith("java.time.")) handler = javaTimeHandler;
}
return handler;
}
}
@@ -121,7 +121,6 @@ void writeObject(final Object obj) throws Exception {
writeString((String) obj);
} else writeObject(obj, handle);
} else {
//writeHeaderAndHandle(isString ? STRING_HEADER : OBJECT_HEADER, handle);
writeHeaderAndHandle(OBJECT_HEADER, handle);
}
}
@@ -143,7 +142,11 @@ private void writeObject(final Object obj, final int handle) throws Exception {
//if (traceEnabled) try { log.trace("writing object " + obj + ", handle=" + handle + ", class=" + obj.getClass() + ", cd=" + cd); } catch(Exception e) {}
if (cd.array) writeArray(obj, cd);
else if (cd.enumType) writeString(((Enum<?>) obj).name());
else writeFields(obj, cd);
else {
final SerializationHandler serializationHandler = SerializationReflectionHelper.getSerializationHandler(cd.clazz);
if (serializationHandler != null) serializationHandler.writeObject(obj, this, cd);
else writeFields(obj, cd);
}
}

/**
@@ -175,9 +178,9 @@ void writeFields(final Object obj, final ClassDescriptor cd) throws Exception {
tmpDesc = tmpDesc.superClass;
}
for (final ClassDescriptor desc: stack) {
final SerializationHandler handler = SerializationReflectionHelper.getSerializationHandler(desc.clazz);
/*final SerializationHandler handler = SerializationReflectionHelper.getSerializationHandler(desc.clazz);
if (handler != null) handler.writeDeclaredFields(this, desc, obj);
else if (desc.hasReadWriteObject) {
else*/ if (desc.hasReadWriteObject) {
final Method m = desc.writeObjectMethod;
//if (traceEnabled) try { log.trace("invoking writeObject() for class=" + desc + " on object " + obj.hashCode()); } catch(Exception e) { log.trace(e.getMessage(), e); }
try {
@@ -37,7 +37,7 @@
private static final boolean traceEnabled = log.isTraceEnabled();

@Override
public void writeDeclaredFields(final Serializer serializer, final ClassDescriptor cd, final Object obj) throws Exception {
public void writeObject(final Object obj, final Serializer serializer, final ClassDescriptor cd) throws Exception {
if (traceEnabled) log.trace("writing declared fields for cd={}", cd);
final Vector<?> vector = (Vector<?>) obj;
ClassDescriptor tmpDesc = null;
@@ -55,10 +55,10 @@ public void writeDeclaredFields(final Serializer serializer, final ClassDescript
}

@Override
public void readDeclaredFields(final Deserializer deserializer, final ClassDescriptor cd, final Object obj) throws Exception {
public Object readDObject(final Deserializer deserializer, final ClassDescriptor cd) throws Exception {
if (traceEnabled) log.trace("reading declared fields for cd={}", cd);
@SuppressWarnings("unchecked")
final Vector<? super Object> vector = (Vector<? super Object>) obj;
final Vector<? super Object> vector = new Vector<>();
ClassDescriptor tmpDesc = null;
try {
tmpDesc = deserializer.currentClassDescriptor;
@@ -72,5 +72,6 @@ public void readDeclaredFields(final Deserializer deserializer, final ClassDescr
} finally {
deserializer.currentClassDescriptor = tmpDesc;
}
return null;
}
}

0 comments on commit 38de3ab

Please sign in to comment.
You can’t perform that action at this time.