/** @param elementType May be null if the type is unknown. */ public void readField( Object object, String fieldName, String jsonName, Class elementType, Object jsonData) { OrderedMap jsonMap = (OrderedMap) jsonData; Class type = object.getClass(); ObjectMap<String, FieldMetadata> fields = typeToFields.get(type); if (fields == null) fields = cacheFields(type); FieldMetadata metadata = fields.get(fieldName); if (metadata == null) throw new SerializationException( "Field not found: " + fieldName + " (" + type.getName() + ")"); Field field = metadata.field; Object jsonValue = jsonMap.get(jsonName); if (jsonValue == null) return; if (elementType == null) elementType = metadata.elementType; try { field.set(object, readValue(field.getType(), elementType, jsonValue)); } catch (IllegalAccessException ex) { throw new SerializationException( "Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field.getName() + " (" + type.getName() + ")"); throw ex; } catch (RuntimeException runtimeEx) { SerializationException ex = new SerializationException(runtimeEx); ex.addTrace(field.getName() + " (" + type.getName() + ")"); throw ex; } }
public void readFields(Object object, Object jsonData) { OrderedMap<String, Object> jsonMap = (OrderedMap) jsonData; Class type = object.getClass(); ObjectMap<String, FieldMetadata> fields = typeToFields.get(type); if (fields == null) fields = cacheFields(type); for (Entry<String, Object> entry : jsonMap.entries()) { FieldMetadata metadata = fields.get(entry.key); if (metadata == null) { if (ignoreUnknownFields) { if (debug) System.out.println( "Ignoring unknown field: " + entry.key + " (" + type.getName() + ")"); continue; } else throw new SerializationException( "Field not found: " + entry.key + " (" + type.getName() + ")"); } Field field = metadata.field; if (entry.value == null) continue; try { field.set(object, readValue(field.getType(), metadata.elementType, entry.value)); } catch (IllegalAccessException ex) { throw new SerializationException( "Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field.getName() + " (" + type.getName() + ")"); throw ex; } catch (RuntimeException runtimeEx) { SerializationException ex = new SerializationException(runtimeEx); ex.addTrace(field.getName() + " (" + type.getName() + ")"); throw ex; } } }
@Override public void innerRun() { try (CDC ignored = _lock.getCdc().restore()) { CellMessageAnswerable callback = _lock.getCallback(); CellMessage answer; Object obj; try { answer = _message.decode(); obj = answer.getMessageObject(); } catch (SerializationException e) { LOGGER.warn(e.getMessage()); obj = e; answer = null; } EventLogger.sendEnd(_lock.getMessage()); if (obj instanceof Exception) { callback.exceptionArrived(_lock.getMessage(), (Exception) obj); } else { callback.answerArrived(_lock.getMessage(), answer); } LOGGER.trace("addToEventQueue : callback done for : {}", _message); } }
/** @param elementType May be null if the type is unknown. */ public void writeField(Object object, String fieldName, String jsonName, Class elementType) { Class type = object.getClass(); ObjectMap<String, FieldMetadata> fields = typeToFields.get(type); if (fields == null) fields = cacheFields(type); FieldMetadata metadata = fields.get(fieldName); if (metadata == null) throw new SerializationException( "Field not found: " + fieldName + " (" + type.getName() + ")"); Field field = metadata.field; if (elementType == null) elementType = metadata.elementType; try { if (debug) System.out.println("Writing field: " + field.getName() + " (" + type.getName() + ")"); writer.name(jsonName); writeValue(field.get(object), field.getType(), elementType); } catch (IllegalAccessException ex) { throw new SerializationException( "Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field + " (" + type.getName() + ")"); throw ex; } catch (Exception runtimeEx) { SerializationException ex = new SerializationException(runtimeEx); ex.addTrace(field + " (" + type.getName() + ")"); throw ex; } }
public void readFields(Object object, JsonValue jsonMap) { Type type = ReflectionCache.getType(object.getClass()); ObjectMap<String, FieldMetadata> fields = typeToFields.get(type); if (fields == null) fields = cacheFields(type); for (JsonValue child = jsonMap.child(); child != null; child = child.next()) { FieldMetadata metadata = fields.get(child.name()); if (metadata == null) { if (ignoreUnknownFields) { if (debug) System.out.println( "Ignoring unknown field: " + child.name() + " (" + type.getName() + ")"); continue; } else throw new SerializationException( "Field not found: " + child.name() + " (" + type.getName() + ")"); } Field field = metadata.field; // if (entry.value == null) continue; // I don't remember what this did. :( try { field.set(object, readValue(field.getType().getClassOfType(), metadata.elementType, child)); } catch (IllegalAccessException ex) { throw new SerializationException( "Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field.getName() + " (" + type.getName() + ")"); throw ex; } catch (RuntimeException runtimeEx) { SerializationException ex = new SerializationException(runtimeEx); ex.addTrace(field.getName() + " (" + type.getName() + ")"); throw ex; } } }
@Override public void innerRun() { EventLogger.queueEnd(_event); if (_event instanceof LastMessageEvent) { LOGGER.trace("messageThread : LastMessageEvent arrived"); _cell.messageArrived((MessageEvent) _event); } else if (_event instanceof RoutedMessageEvent) { LOGGER.trace("messageThread : RoutedMessageEvent arrived"); _cell.messageArrived((RoutedMessageEvent) _event); } else if (_event instanceof MessageEvent) { MessageEvent msgEvent = (MessageEvent) _event; LOGGER.trace("messageThread : MessageEvent arrived"); CellMessage msg; try { msg = msgEvent.getMessage().decode(); } catch (SerializationException e) { CellMessage envelope = msgEvent.getMessage(); LOGGER.error( String.format( "Discarding a malformed message from %s with UOID %s and session [%s]: %s", envelope.getSourcePath(), envelope.getUOID(), envelope.getSession(), e.getMessage()), e); return; } CDC.setMessageContext(msg); try { LOGGER.trace("messageThread : delivering message: {}", msg); _cell.messageArrived(new MessageEvent(msg)); LOGGER.trace("messageThread : delivering message done: {}", msg); } catch (RuntimeException e) { if (!msg.isReply()) { try { msg.revertDirection(); msg.setMessageObject(e); sendMessage(msg); } catch (NoRouteToCellException f) { LOGGER.error("PANIC : Problem returning answer: {}", f); } } throw e; } finally { CDC.clearMessageContext(); } } }
public void testDeserializeStreamClassNotFound() throws Exception { ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(new ClassNotFoundSerialization()); oos.flush(); oos.close(); ByteArrayInputStream inTest = new ByteArrayInputStream(streamReal.toByteArray()); try { Object test = SerializationUtils.deserialize(inTest); } catch (SerializationException se) { assertEquals("java.lang.ClassNotFoundException: " + CLASS_NOT_FOUND_MESSAGE, se.getMessage()); } }
public void testSerializeIOException() throws Exception { // forces an IOException when the ObjectOutputStream is created, to test not closing the stream // in the finally block OutputStream streamTest = new OutputStream() { public void write(int arg0) throws IOException { throw new IOException(SERIALIZE_IO_EXCEPTION_MESSAGE); } }; try { SerializationUtils.serialize(iMap, streamTest); } catch (SerializationException e) { assertEquals("java.io.IOException: " + SERIALIZE_IO_EXCEPTION_MESSAGE, e.getMessage()); } }
public void testException() { SerializationException serEx; Exception ex = new Exception(); serEx = new SerializationException(); assertSame(null, serEx.getMessage()); assertSame(null, serEx.getCause()); serEx = new SerializationException("Message"); assertSame("Message", serEx.getMessage()); assertSame(null, serEx.getCause()); serEx = new SerializationException(ex); assertEquals("java.lang.Exception", serEx.getMessage()); assertSame(ex, serEx.getCause()); serEx = new SerializationException("Message", ex); assertSame("Message", serEx.getMessage()); assertSame(ex, serEx.getCause()); }
private Object[] getDefaultValues(Class type) { if (!usePrototypes) return null; if (classToDefaultValues.containsKey(type)) return classToDefaultValues.get(type); Object object; try { object = newInstance(type); } catch (Exception ex) { classToDefaultValues.put(type, null); return null; } ObjectMap<String, FieldMetadata> fields = typeToFields.get(type); if (fields == null) fields = cacheFields(type); Object[] values = new Object[fields.size]; classToDefaultValues.put(type, values); int i = 0; for (FieldMetadata metadata : fields.values()) { Field field = metadata.field; try { values[i++] = field.get(object); } catch (IllegalAccessException ex) { throw new SerializationException( "Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field + " (" + type.getName() + ")"); throw ex; } catch (RuntimeException runtimeEx) { SerializationException ex = new SerializationException(runtimeEx); ex.addTrace(field + " (" + type.getName() + ")"); throw ex; } } return values; }
public void writeFields(Object object) { Class type = object.getClass(); Object[] defaultValues = getDefaultValues(type); ObjectMap<String, FieldMetadata> fields = typeToFields.get(type); if (fields == null) fields = cacheFields(type); int i = 0; for (FieldMetadata metadata : fields.values()) { Field field = metadata.field; try { Object value = field.get(object); if (defaultValues != null) { Object defaultValue = defaultValues[i++]; if (value == null && defaultValue == null) continue; if (value != null && defaultValue != null && value.equals(defaultValue)) continue; } if (debug) System.out.println("Writing field: " + field.getName() + " (" + type.getName() + ")"); writer.name(field.getName()); writeValue(value, field.getType(), metadata.elementType); } catch (IllegalAccessException ex) { throw new SerializationException( "Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field + " (" + type.getName() + ")"); throw ex; } catch (Exception runtimeEx) { SerializationException ex = new SerializationException(runtimeEx); ex.addTrace(field + " (" + type.getName() + ")"); throw ex; } } }