/** * {@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(); } } }
/** * 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} */ @Override public InvocationSequenceData read(Kryo kryo, Input input, Class<InvocationSequenceData> type) { InvocationSequenceData invocation = super.read(kryo, input, type); connectChildren(invocation); return invocation; }
@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))); }
kryo.register(StorageBranch.class, new CustomCompatibleFieldSerializer<StorageBranch<?>>(kryo, StorageBranch.class, schemaManager), nextRegistrationId++); kryo.register(StorageBranchIndexer.class, new CustomCompatibleFieldSerializer<StorageBranchIndexer<?>>(kryo, StorageBranchIndexer.class, schemaManager), nextRegistrationId++); kryo.register(SimpleStorageDescriptor.class, new CustomCompatibleFieldSerializer<SimpleStorageDescriptor>(kryo, SimpleStorageDescriptor.class, schemaManager), nextRegistrationId++); CustomCompatibleFieldSerializer<ArrayBasedStorageLeaf<?>> leafSerializer = new CustomCompatibleFieldSerializer<>(kryo, ArrayBasedStorageLeaf.class, schemaManager); leafSerializer.setCopyTransient(false); kryo.register(ArrayBasedStorageLeaf.class, leafSerializer, nextRegistrationId++); kryo.register(LeafWithNoDescriptors.class, new CustomCompatibleFieldSerializer<LeafWithNoDescriptors<?>>(kryo, LeafWithNoDescriptors.class, schemaManager), nextRegistrationId++); kryo.register(StorageData.class, new CustomCompatibleFieldSerializer<StorageData>(kryo, StorageData.class, schemaManager), nextRegistrationId++); kryo.register(LocalStorageData.class, new CustomCompatibleFieldSerializer<LocalStorageData>(kryo, LocalStorageData.class, schemaManager), nextRegistrationId++); kryo.register(StorageState.class, new EnumSerializer(StorageState.class)); kryo.register(BooleanStorageLabel.class, new CustomCompatibleFieldSerializer<BooleanStorageLabel>(kryo, BooleanStorageLabel.class, schemaManager), nextRegistrationId++); kryo.register(DateStorageLabel.class, new CustomCompatibleFieldSerializer<DateStorageLabel>(kryo, DateStorageLabel.class, schemaManager), nextRegistrationId++); kryo.register(NumberStorageLabel.class, new CustomCompatibleFieldSerializer<NumberStorageLabel>(kryo, NumberStorageLabel.class, schemaManager), nextRegistrationId++); kryo.register(StringStorageLabel.class, new CustomCompatibleFieldSerializer<StringStorageLabel>(kryo, StringStorageLabel.class, schemaManager), nextRegistrationId++); kryo.register(AssigneeLabelType.class, new CustomCompatibleFieldSerializer<AssigneeLabelType>(kryo, AssigneeLabelType.class, schemaManager, true), nextRegistrationId++); kryo.register(CreationDateLabelType.class, new CustomCompatibleFieldSerializer<CreationDateLabelType>(kryo, CreationDateLabelType.class, schemaManager, true), nextRegistrationId++); kryo.register(CustomBooleanLabelType.class, new CustomCompatibleFieldSerializer<CustomBooleanLabelType>(kryo, CustomBooleanLabelType.class, schemaManager, true), nextRegistrationId++); kryo.register(CustomDateLabelType.class, new CustomCompatibleFieldSerializer<CustomDateLabelType>(kryo, CustomDateLabelType.class, schemaManager, true), nextRegistrationId++); kryo.register(CustomNumberLabelType.class, new CustomCompatibleFieldSerializer<CustomNumberLabelType>(kryo, CustomNumberLabelType.class, schemaManager, true), nextRegistrationId++); kryo.register(CustomStringLabelType.class, new CustomCompatibleFieldSerializer<CustomStringLabelType>(kryo, CustomStringLabelType.class, schemaManager, true), nextRegistrationId++); kryo.register(ExploredByLabelType.class, new CustomCompatibleFieldSerializer<ExploredByLabelType>(kryo, ExploredByLabelType.class, schemaManager, true), nextRegistrationId++); kryo.register(RatingLabelType.class, new CustomCompatibleFieldSerializer<RatingLabelType>(kryo, RatingLabelType.class, schemaManager, true), nextRegistrationId++); kryo.register(StatusLabelType.class, new CustomCompatibleFieldSerializer<StatusLabelType>(kryo, StatusLabelType.class, schemaManager, true), nextRegistrationId++); kryo.register(UseCaseLabelType.class, new CustomCompatibleFieldSerializer<UseCaseLabelType>(kryo, UseCaseLabelType.class, schemaManager, true), nextRegistrationId++); kryo.register(MethodIdentIndexer.class, new FieldSerializer<MethodIdentIndexer<?>>(kryo, MethodIdentIndexer.class), nextRegistrationId++);
/** * {@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); } } }
@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))); }
@Test public void allFields() { long longValue = random.nextLong(); double doubleValue = random.nextDouble(); float floatValue = random.nextFloat(); TestClassLongDoubleFloat object = new TestClassLongDoubleFloat(); object.longField = longValue; object.doubleField = doubleValue; object.floatField = floatValue; Mockito.when(classSchema.getFieldMarker("longField")).thenReturn(Integer.valueOf(1)); Mockito.when(classSchema.getFieldMarker("doubleField")).thenReturn(Integer.valueOf(2)); Mockito.when(classSchema.getFieldMarker("floatField")).thenReturn(Integer.valueOf(3)); Kryo kryo = new Kryo(); kryo.register(TestClassLongDoubleFloat.class, new CustomCompatibleFieldSerializer<TestClassLongDoubleFloat>(kryo, TestClassLongDoubleFloat.class, classSchemaManager)); Output output = new Output(1024); kryo.writeClassAndObject(output, object); Input input = new Input(output.getBuffer()); Object deserialized = kryo.readClassAndObject(input); Assert.assertTrue(deserialized instanceof TestClassLongDoubleFloat); TestClassLongDoubleFloat deserializedTestClass = (TestClassLongDoubleFloat) deserialized; assertThat(deserializedTestClass.longField, is(equalTo(longValue))); assertThat(deserializedTestClass.doubleField, is(equalTo(doubleValue))); assertThat(deserializedTestClass.floatField, is(equalTo(floatValue))); }
/** * {@inheritDoc} */ @Override public void write(Kryo kryo, Output output, T object) { CachedField[] fields = getFields(); ObjectMap context = kryo.getGraphContext(); if (!context.containsKey(this)) { context.put(this, null); if (TRACE) { trace("kryo", "Write " + fields.length + " field names."); } output.writeInt(fields.length, true); for (int i = 0, n = fields.length; i < n; i++) { // Changed by ISE output.writeInt(fieldMarkers[i], true); } } OutputChunked outputChunked = new OutputChunked(output, 1024); for (com.esotericsoftware.kryo.serializers.FieldSerializer.CachedField field : fields) { field.write(outputChunked, object); outputChunked.endChunks(); } }
@Test public void removeField() { long longValue = random.nextLong(); double doubleValue = random.nextDouble(); float floatValue = random.nextFloat(); TestClassLongDoubleFloat object = new TestClassLongDoubleFloat(); object.longField = longValue; object.doubleField = doubleValue; object.floatField = floatValue; Mockito.when(classSchema.getFieldMarker("longField")).thenReturn(Integer.valueOf(1)); Mockito.when(classSchema.getFieldMarker("doubleField")).thenReturn(Integer.valueOf(2)); Mockito.when(classSchema.getFieldMarker("floatField")).thenReturn(Integer.valueOf(3)); Kryo kryo = new Kryo(); kryo.register(TestClassLongDoubleFloat.class, new CustomCompatibleFieldSerializer<TestClassLongDoubleFloat>(kryo, TestClassLongDoubleFloat.class, classSchemaManager)); Output output = new Output(1024); kryo.writeClassAndObject(output, object); kryo = new Kryo(); kryo.register(TestClassLongDouble.class, new CustomCompatibleFieldSerializer<TestClassLongDouble>(kryo, TestClassLongDouble.class, classSchemaManager)); Input input = new Input(output.getBuffer()); Object deserialized = kryo.readClassAndObject(input); assertThat(deserialized, is(instanceOf(TestClassLongDouble.class))); TestClassLongDouble deserializedTestClass = (TestClassLongDouble) deserialized; assertThat(deserializedTestClass.longField, is(equalTo(longValue))); assertThat(deserializedTestClass.doubleField, is(equalTo(doubleValue))); }
CachedField[] allFields = getFields(); outer: for (int i = 0, n = markers.length; i < n; i++) { int fieldMarker = markers[i];
@Test public void zeroField() { long longValue = random.nextLong(); double doubleValue = random.nextDouble(); float floatValue = random.nextFloat(); TestClassLongDoubleFloat object = new TestClassLongDoubleFloat(); object.longField = longValue; object.doubleField = doubleValue; object.floatField = floatValue; Mockito.when(classSchema.getFieldMarker("longField")).thenReturn(null); Mockito.when(classSchema.getFieldMarker("doubleField")).thenReturn(null); Mockito.when(classSchema.getFieldMarker("floatField")).thenReturn(null); Kryo kryo = new Kryo(); kryo.register(TestClassLongDoubleFloat.class, new CustomCompatibleFieldSerializer<TestClassLongDoubleFloat>(kryo, TestClassLongDoubleFloat.class, classSchemaManager)); Output output = new Output(1024); kryo.writeClassAndObject(output, object); Input input = new Input(output.getBuffer()); Object deserialized = kryo.readClassAndObject(input); Assert.assertTrue(deserialized instanceof TestClassLongDoubleFloat); TestClassLongDoubleFloat deserializedTestClass = (TestClassLongDoubleFloat) deserialized; assertThat(deserializedTestClass.longField, is(equalTo(0L))); assertThat(deserializedTestClass.doubleField, is(equalTo(0D))); assertThat(deserializedTestClass.floatField, is(equalTo(0F))); }
kryo.register(PlatformIdent.class, new CustomCompatibleFieldSerializer<PlatformIdent>(kryo, PlatformIdent.class, schemaManager)); kryo.register(MethodIdent.class, new CustomCompatibleFieldSerializer<MethodIdent>(kryo, MethodIdent.class, schemaManager)); kryo.register(SensorTypeIdent.class, new CustomCompatibleFieldSerializer<SensorTypeIdent>(kryo, SensorTypeIdent.class, schemaManager)); kryo.register(MethodSensorTypeIdent.class, new CustomCompatibleFieldSerializer<MethodSensorTypeIdent>(kryo, MethodSensorTypeIdent.class, schemaManager)); kryo.register(PlatformSensorTypeIdent.class, new CustomCompatibleFieldSerializer<PlatformSensorTypeIdent>(kryo, PlatformSensorTypeIdent.class, schemaManager, true)); kryo.register(ExceptionSensorData.class, new InvocationAwareDataSerializer<ExceptionSensorData>(kryo, ExceptionSensorData.class, schemaManager)); kryo.register(ExceptionEvent.class, new EnumSerializer(ExceptionEvent.class)); kryo.register(ParameterContentData.class, new CustomCompatibleFieldSerializer<ParameterContentData>(kryo, ParameterContentData.class, schemaManager)); kryo.register(MemoryInformationData.class, new CustomCompatibleFieldSerializer<MemoryInformationData>(kryo, MemoryInformationData.class, schemaManager)); kryo.register(CpuInformationData.class, new CustomCompatibleFieldSerializer<CpuInformationData>(kryo, CpuInformationData.class, schemaManager)); kryo.register(SystemInformationData.class, new CustomCompatibleFieldSerializer<SystemInformationData>(kryo, SystemInformationData.class, schemaManager)); kryo.register(VmArgumentData.class, new CustomCompatibleFieldSerializer<VmArgumentData>(kryo, VmArgumentData.class, schemaManager)); kryo.register(ThreadInformationData.class, new CustomCompatibleFieldSerializer<ThreadInformationData>(kryo, ThreadInformationData.class, schemaManager)); kryo.register(RuntimeInformationData.class, new CustomCompatibleFieldSerializer<RuntimeInformationData>(kryo, RuntimeInformationData.class, schemaManager)); kryo.register(CompilationInformationData.class, new CustomCompatibleFieldSerializer<CompilationInformationData>(kryo, CompilationInformationData.class, schemaManager)); kryo.register(ClassLoadingInformationData.class, new CustomCompatibleFieldSerializer<ClassLoadingInformationData>(kryo, ClassLoadingInformationData.class, schemaManager)); kryo.register(ParameterContentType.class, new EnumSerializer(ParameterContentType.class)); kryo.register(MethodIdentToSensorType.class, new CustomCompatibleFieldSerializer<MethodIdentToSensorType>(kryo, MethodIdentToSensorType.class, schemaManager)); kryo.register(TimeFrame.class, new CustomCompatibleFieldSerializer<TimeFrame>(kryo, TimeFrame.class, schemaManager), nextRegistrationId++); kryo.register(JmxSensorTypeIdent.class, new CustomCompatibleFieldSerializer<JmxSensorTypeIdent>(kryo, JmxSensorTypeIdent.class, schemaManager, true), nextRegistrationId++); kryo.register(JmxDefinitionDataIdent.class, new CustomCompatibleFieldSerializer<JmxDefinitionDataIdent>(kryo, JmxDefinitionDataIdent.class, schemaManager), nextRegistrationId++); kryo.register(JmxSensorValueData.class, new CustomCompatibleFieldSerializer<JmxSensorValueData>(kryo, JmxSensorValueData.class, schemaManager), nextRegistrationId++); kryo.register(HttpInfo.class, new CustomCompatibleFieldSerializer<HttpInfo>(kryo, HttpInfo.class, schemaManager), nextRegistrationId++);