/** * {@inheritDoc} */ @Override public InvocationSequenceData read(Kryo kryo, Input input, Class<InvocationSequenceData> type) { InvocationSequenceData invocation = super.read(kryo, input, type); connectChildren(invocation); return invocation; }
/** * Creates a deep copy of the given object. * * @param object * object to clone * @param <T> * the class of the given object * @return the new object */ private synchronized <T> T copy(T object) { return serializationManager.copy(object); }
/** * {@inheritDoc} */ @Override public void afterPropertiesSet() throws Exception { initKryo(); }
/** * Performs the serialization of the given object to bytes and then performs de-serialization * from those bytes and returns the de-serialized object back. * * @param original * Original object. * @return De-serialized objects from bytes gotten from the serialization of original. * @throws SerializationException * If serialization fails. */ @SuppressWarnings("unchecked") private <T> T serializeBackAndForth(Object original) throws SerializationException { ByteBufferOutputStream byteBufferOutputStream = new ByteBufferOutputStream(byteBuffer); Output output = new Output(byteBufferOutputStream); serializer.serialize(original, output); byteBuffer.flip(); ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(byteBuffer); Input input = new Input(byteBufferInputStream); return (T) serializer.deserialize(input); }
/** * Tests if the data deserialzied from empty buffer is <code>null</code>. * * @throws SerializationException * Serialization Exception */ @Test public void emptyBufferSerialization() throws SerializationException { // I need to create a new buffer, because clear on the buffer will not actually erase the // data in the buffer, but only move the pointers ByteBuffer newByteBuffer = ByteBuffer.allocateDirect(1024); ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(newByteBuffer); Input input = new Input(byteBufferInputStream); Object data = serializer.deserialize(input); assertThat(data, is(nullValue())); }
@Test public void addField() { long longValue = random.nextLong(); double doubleValue = random.nextDouble(); TestClassLongDouble object = new TestClassLongDouble(); object.longField = longValue; object.doubleField = doubleValue; Mockito.when(classSchema.getFieldMarker("longField")).thenReturn(Integer.valueOf(1)); Mockito.when(classSchema.getFieldMarker("doubleField")).thenReturn(Integer.valueOf(2)); Kryo kryo = new Kryo(); kryo.register(TestClassLongDouble.class, new CustomCompatibleFieldSerializer<TestClassLongDouble>(kryo, TestClassLongDouble.class, classSchemaManager)); Output output = new Output(1024); kryo.writeClassAndObject(output, object); Mockito.when(classSchema.getFieldMarker("floatField")).thenReturn(Integer.valueOf(3)); kryo = new Kryo(); kryo.register(TestClassLongDoubleFloat.class, new CustomCompatibleFieldSerializer<TestClassLongDoubleFloat>(kryo, TestClassLongDoubleFloat.class, classSchemaManager)); Input input = new Input(output.getBuffer()); Object deserialized = kryo.readClassAndObject(input); assertThat(deserialized, is(instanceOf(TestClassLongDoubleFloat.class))); TestClassLongDoubleFloat deserializedTestClass = (TestClassLongDoubleFloat) deserialized; assertThat(deserializedTestClass.longField, is(equalTo(longValue))); assertThat(deserializedTestClass.doubleField, is(equalTo(doubleValue))); assertThat(deserializedTestClass.floatField, is(equalTo(0F))); }
/** * {@inheritDoc} */ @Override public void serialize(Object object, Output output) throws SerializationException { serialize(object, output, Collections.emptyMap()); }
/** * Tests that the Hibernate {@link PersistentList} can be serialized, but in way that * deserialized class will be java list and but not {@link PersistentList}. * * @throws SerializationException * SerializationException */ @Test public void hibernatePersistentList() throws SerializationException { PersistentList object = new PersistentList(); Object deserialized = serializeBackAndForth(object); assertThat(deserialized, is(not(instanceOf(PersistentList.class)))); assertThat(deserialized, is(instanceOf(List.class))); }
/** * Sets the parent to all nested sequences of the invocation to the correct one. * * @param parent * Parent to start from. */ private void connectChildren(InvocationSequenceData parent) { if (null != parent.getNestedSequences()) { for (InvocationSequenceData child : parent.getNestedSequences()) { child.setParentSequence(parent); connectChildren(child); } } }
/** * Default constructor. * * @param kryo * Kryo instance * @param type * Class to be serialized * @param schemaManager * {@link ClassSchemaManager} holding information about values. * @param useSuperclassSchema * If the superclass schema should be used if the one for the class is not available. */ public CustomCompatibleFieldSerializer(Kryo kryo, Class<?> type, ClassSchemaManager schemaManager, boolean useSuperclassSchema) { super(kryo, type); schema = schemaManager.getSchema(type.getName()); if (useSuperclassSchema && (null == schema)) { Class<?> superclass = type.getSuperclass(); while (null != superclass) { schema = schemaManager.getSchema(superclass.getName()); if (null != schema) { break; } superclass = superclass.getSuperclass(); } } if (schema == null) { throw new IllegalArgumentException("Schema for the class '" + type.getName() + "' does not exists in provided schema manager."); } initializeCachedFields(); }
/** * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void write(Kryo kryo, Output output, T object) { ObjectMap<Object, Object> garphContext = kryo.getGraphContext(); if (Boolean.FALSE.equals(garphContext.get(KryoSerializationPreferences.WRITE_INVOCATION_AFFILIATION_DATA))) { Map<Long, MutableInt> temp = object.getInvocationsParentsIdMap(); object.setInvocationsParentsIdMap(Collections.<Long, MutableInt> emptyMap()); super.write(kryo, output, object); object.setInvocationsParentsIdMap(temp); } else { super.write(kryo, output, object); } } }
/** * {@inheritDoc} */ @Override protected final void initializeCachedFields() { if (null != schema) { CachedField<?>[] fields = getFields(); // Remove unwanted fields for (com.esotericsoftware.kryo.serializers.FieldSerializer.CachedField<?> field2 : fields) { Field field = field2.getField(); if (null == schema.getFieldMarker(field.getName())) { super.removeField(field.getName()); } } // Cache markers fields = getFields(); fieldMarkers = new int[fields.length]; for (int i = 0, n = fields.length; i < n; i++) { fieldMarkers[i] = schema.getFieldMarker(fields[i].getField().getName()).intValue(); } } }
/** * Initialize {@link Kryo} properties. */ public void initKryo() { // if hibernateUtil is provided, we create special kind of class resolver ClassResolver classResolver; if (null != hibernateUtil) { classResolver = new HibernateAwareClassResolver(hibernateUtil); } else { classResolver = new DefaultClassResolver(); } // we disable references for DefaultData objects because they are not needed // invocations will be handled manually ReferenceResolver referenceResolver = new MapReferenceResolver() { @SuppressWarnings("rawtypes") @Override public boolean useReferences(Class paramClass) { if (DefaultData.class.isAssignableFrom(paramClass)) { return false; } else { return super.useReferences(paramClass); } } }; kryo = new Kryo(classResolver, referenceResolver); kryo.setRegistrationRequired(false); registerClasses(kryo); }
/** * Performs the serialization of the given object to bytes and then performs de-serialization * from those bytes and returns the de-serialized object back. * * @param original * Original object. * @return De-serialized objects from bytes gotten from the serialization of original. * @throws SerializationException * If serialization fails. */ @SuppressWarnings("unchecked") private <T> T serializeBackAndForth(Object original) throws SerializationException { ByteBufferOutputStream byteBufferOutputStream = new ByteBufferOutputStream(byteBuffer); Output output = new Output(byteBufferOutputStream); serializer.serialize(original, output); byteBuffer.flip(); ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(byteBuffer); Input input = new Input(byteBufferInputStream); return (T) serializer.deserialize(input); }
/** * Tests if the data de-serialzed from buffer with random data is <code>null</code>. * * @throws SerializationException * Serialization Exception */ @Test public void radomBufferDataSerialization() throws SerializationException { for (int i = 0; i < 64; i++) { byteBuffer.putInt(i); } byteBuffer.flip(); ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(byteBuffer); Input input = new Input(byteBufferInputStream); Object data = serializer.deserialize(input); assertThat(data, is(nullValue())); }
@Test public void addAndRemoveFields() { long longValue = random.nextLong(); double doubleValue = random.nextDouble(); TestClassLongDouble object = new TestClassLongDouble(); object.longField = longValue; object.doubleField = doubleValue; Mockito.when(classSchema.getFieldMarker("longField")).thenReturn(Integer.valueOf(1)); Mockito.when(classSchema.getFieldMarker("doubleField")).thenReturn(Integer.valueOf(2)); Kryo kryo = new Kryo(); kryo.register(TestClassLongDouble.class, new CustomCompatibleFieldSerializer<TestClassLongDouble>(kryo, TestClassLongDouble.class, classSchemaManager)); Output output = new Output(1024); kryo.writeClassAndObject(output, object); Mockito.when(classSchema.getFieldMarker("floatField")).thenReturn(3); kryo = new Kryo(); kryo.register(TestClassDoubleFloat.class, new CustomCompatibleFieldSerializer<TestClassDoubleFloat>(kryo, TestClassDoubleFloat.class, classSchemaManager)); Input input = new Input(output.getBuffer()); Object deserialized = kryo.readClassAndObject(input); assertThat(deserialized, is(instanceOf(TestClassDoubleFloat.class))); TestClassDoubleFloat deserializedTestClass = (TestClassDoubleFloat) deserialized; assertThat(deserializedTestClass.doubleField, is(equalTo(doubleValue))); assertThat(deserializedTestClass.floatField, is(equalTo(0F))); }
/** * Creates the cloned {@link HttpTimerData} by using the kryo and {@link #serializationManager}. * Sets id of the clone to zero. * * @param original * Data to be cloned. * @return Cloned {@link HttpTimerData} with id zero. * @throws SerializationException * If serialization fails. */ private synchronized HttpTimerData getClone(HttpTimerData original) throws SerializationException { HttpTimerData httpTimerData = serializationManager.copy(original); httpTimerData.setId(0L); return httpTimerData; }
/** * Tests that the Hibernate {@link PersistentMap} can be serialized, but in way that * deserialized class will be java map and but not {@link PersistentMap}. * * @throws SerializationException * SerializationException */ @Test public void hibernatePersistentMap() throws SerializationException { PersistentMap object = new PersistentMap(); Object deserialized = serializeBackAndForth(object); assertThat(deserialized, is(not(instanceOf(PersistentMap.class)))); assertThat(deserialized, is(instanceOf(Map.class))); }
@Test(dataProvider = "classProvider") public void copy(Class<?> testingClass) throws IllegalArgumentException, IllegalAccessException, InstantiationException { Object object = getInstanceWithPrimitiveFieldsSet(testingClass); Object copy = serializer.copy(object); assertThat(copy, is(equalTo(object))); assertThat(copy == object, is(false)); }
/** * Tests that the Hibernate {@link PersistentSet} can be serialized, but in way that * deserialized class will be java set and but not {@link PersistentSet}. * * @throws SerializationException * SerializationException */ @Test public void hibernatePersistentSet() throws SerializationException { PersistentSet object = new PersistentSet(); Object deserialized = serializeBackAndForth(object); assertThat(deserialized, is(not(instanceOf(PersistentSet.class)))); assertThat(deserialized, is(instanceOf(Set.class))); }