Search in sources :

Example 1 with ClassDefinitionBuilder

use of com.hazelcast.nio.serialization.ClassDefinitionBuilder in project hazelcast by hazelcast.

the class ClassDefinitionWriter method writePortableArray.

@Override
public void writePortableArray(String fieldName, Portable[] portables) throws IOException {
    if (portables == null || portables.length == 0) {
        throw new HazelcastSerializationException("Cannot write null portable array without explicitly " + "registering class definition!");
    }
    Portable p = portables[0];
    int classId = p.getClassId();
    for (int i = 1; i < portables.length; i++) {
        if (portables[i].getClassId() != classId) {
            throw new IllegalArgumentException("Detected different class-ids in portable array!");
        }
    }
    int version = SerializationUtil.getPortableVersion(p, context.getVersion());
    ClassDefinition nestedClassDef = createNestedClassDef(p, new ClassDefinitionBuilder(p.getFactoryId(), classId, version));
    builder.addPortableArrayField(fieldName, nestedClassDef);
}
Also used : Portable(com.hazelcast.nio.serialization.Portable) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder)

Example 2 with ClassDefinitionBuilder

use of com.hazelcast.nio.serialization.ClassDefinitionBuilder in project hazelcast by hazelcast.

the class ClassAndFieldDefinitionTest method testClassDef_equal_hashCode.

@Test
public void testClassDef_equal_hashCode() throws Exception {
    ClassDefinitionImpl cdEmpty1 = (ClassDefinitionImpl) new ClassDefinitionBuilder(1, 2, 3).build();
    ClassDefinitionImpl cdEmpty2 = (ClassDefinitionImpl) new ClassDefinitionBuilder(1, 2, 3).build();
    ClassDefinitionImpl cd1 = (ClassDefinitionImpl) new ClassDefinitionBuilder(1, 2, 5).build();
    ClassDefinitionImpl cd2 = (ClassDefinitionImpl) new ClassDefinitionBuilder(2, 2, 3).build();
    ClassDefinitionImpl cd3 = (ClassDefinitionImpl) new ClassDefinitionBuilder(1, 9, 3).build();
    ClassDefinitionImpl cdWithField = (ClassDefinitionImpl) new ClassDefinitionBuilder(1, 2, 3).addIntField("f1").build();
    assertEquals(cdEmpty1, cdEmpty2);
    assertNotEquals(cd1, cdEmpty1);
    assertNotEquals(cd2, cdEmpty1);
    assertNotEquals(cd3, cdEmpty1);
    assertNotEquals(cdWithField, classDefinition);
    assertNotEquals(cdEmpty1, classDefinition);
    assertNotEquals(classDefinition, null);
    assertNotEquals(classDefinition, "Another Class");
    assertNotEquals(0, cd1.hashCode());
}
Also used : ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with ClassDefinitionBuilder

use of com.hazelcast.nio.serialization.ClassDefinitionBuilder in project hazelcast by hazelcast.

the class ClassAndFieldDefinitionTest method setUp.

@Before
public void setUp() throws Exception {
    ClassDefinitionBuilder builder = new ClassDefinitionBuilder(1, 2, 3);
    for (String fieldName : fieldNames) {
        builder.addByteField(fieldName);
    }
    classDefinition = (ClassDefinitionImpl) builder.build();
}
Also used : ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Before(org.junit.Before)

Example 4 with ClassDefinitionBuilder

use of com.hazelcast.nio.serialization.ClassDefinitionBuilder in project hazelcast by hazelcast.

the class AbstractNearCacheSerializationCountTest method prepareSerializationConfig.

/**
     * Adds the serialization configuration for the used {@link Portable} domain object to the given {@link SerializationConfig}.
     *
     * @param serializationConfig the given {@link SerializationConfig} for the {@link DataStructureAdapter}
     */
protected static void prepareSerializationConfig(SerializationConfig serializationConfig) {
    ClassDefinition classDefinition = new ClassDefinitionBuilder(SerializationCountingData.FACTORY_ID, SerializationCountingData.CLASS_ID).build();
    serializationConfig.addClassDefinition(classDefinition);
    serializationConfig.addPortableFactory(SerializationCountingData.FACTORY_ID, new PortableFactory() {

        @Override
        public Portable create(int classId) {
            return new SerializationCountingData();
        }
    });
}
Also used : Portable(com.hazelcast.nio.serialization.Portable) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder)

Example 5 with ClassDefinitionBuilder

use of com.hazelcast.nio.serialization.ClassDefinitionBuilder in project hazelcast by hazelcast.

the class BinaryCompatibilityTest method createSerializationService.

private SerializationService createSerializationService() {
    SerializationConfig config = new SerializationConfig();
    {
        SerializerConfig serializerConfig = new SerializerConfig();
        serializerConfig.setImplementation(new CustomByteArraySerializer()).setTypeClass(CustomByteArraySerializable.class);
        config.addSerializerConfig(serializerConfig);
    }
    {
        SerializerConfig serializerConfig = new SerializerConfig();
        serializerConfig.setImplementation(new CustomStreamSerializer()).setTypeClass(CustomStreamSerializable.class);
        config.addSerializerConfig(serializerConfig);
    }
    config.setAllowUnsafe(allowUnsafe);
    config.setByteOrder(byteOrder);
    ClassDefinition classDefinition = new ClassDefinitionBuilder(ReferenceObjects.PORTABLE_FACTORY_ID, ReferenceObjects.INNER_PORTABLE_CLASS_ID).addIntField("i").addFloatField("f").build();
    return new DefaultSerializationServiceBuilder().setConfig(config).setVersion(version).addPortableFactory(ReferenceObjects.PORTABLE_FACTORY_ID, new APortableFactory()).addDataSerializableFactory(ReferenceObjects.IDENTIFIED_DATA_SERIALIZABLE_FACTORY_ID, new ADataSerializableFactory()).addClassDefinition(classDefinition).build();
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializerConfig(com.hazelcast.config.SerializerConfig) SerializationConfig(com.hazelcast.config.SerializationConfig) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder)

Aggregations

ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)8 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)6 SerializationConfig (com.hazelcast.config.SerializationConfig)2 SerializerConfig (com.hazelcast.config.SerializerConfig)2 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)2 HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)2 Portable (com.hazelcast.nio.serialization.Portable)2 FieldType (com.hazelcast.nio.serialization.FieldType)1 PortableFactory (com.hazelcast.nio.serialization.PortableFactory)1 ParallelTest (com.hazelcast.test.annotation.ParallelTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 Before (org.junit.Before)1 Test (org.junit.Test)1