Search in sources :

Example 1 with DataSerializable

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

the class DataSerializableSerializer method readInternal.

private DataSerializable readInternal(ObjectDataInput in, Class aClass) throws IOException {
    setInputVersion(in, version);
    DataSerializable ds = null;
    if (null != aClass) {
        try {
            ds = (DataSerializable) aClass.newInstance();
        } catch (Exception e) {
            e = tryClarifyInstantiationException(aClass, e);
            throw new HazelcastSerializationException("Requested class " + aClass + " could not be instantiated.", e);
        }
    }
    final byte header = in.readByte();
    int id = 0;
    int factoryId = 0;
    String className = null;
    try {
        // BasicOperationService::extractOperationCallId
        if (isFlagSet(header, IDS_FLAG)) {
            factoryId = in.readInt();
            final DataSerializableFactory dsf = factories.get(factoryId);
            if (dsf == null) {
                throw new HazelcastSerializationException("No DataSerializerFactory registered for namespace: " + factoryId);
            }
            id = in.readInt();
            if (null == aClass) {
                ds = dsf.create(id);
                if (ds == null) {
                    throw new HazelcastSerializationException(dsf + " is not be able to create an instance for ID: " + id + " on factory ID: " + factoryId);
                }
            }
        } else {
            className = in.readString();
            if (null == aClass) {
                ds = ClassLoaderUtil.newInstance(in.getClassLoader(), className);
            }
        }
        if (isFlagSet(header, EE_FLAG)) {
            in.readByte();
            in.readByte();
        }
        ds.readData(in);
        return ds;
    } catch (Exception e) {
        e = tryClarifyNoSuchMethodException(in.getClassLoader(), className, e);
        throw rethrowReadException(id, factoryId, className, e);
    }
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) DataSerializableFactory(com.hazelcast.nio.serialization.DataSerializableFactory) IdentifiedDataSerializable(com.hazelcast.nio.serialization.IdentifiedDataSerializable) TypedDataSerializable(com.hazelcast.nio.serialization.TypedDataSerializable) DataSerializable(com.hazelcast.nio.serialization.DataSerializable) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) IOException(java.io.IOException)

Example 2 with DataSerializable

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

the class DataSerializableImplementsVersionedTest method testDataSerializableForVersionedInterface.

@Test
public void testDataSerializableForVersionedInterface() throws Exception {
    for (Class<? extends DataSerializable> dsClass : dsClasses) {
        System.out.println(dsClass.getSimpleName());
        DataSerializable dataSerializable = getInstance(dsClass);
        if (dataSerializable == null) {
            continue;
        }
        checkInstanceOfVersion(dsClass, dataSerializable);
    }
}
Also used : IdentifiedDataSerializable(com.hazelcast.nio.serialization.IdentifiedDataSerializable) DataSerializable(com.hazelcast.nio.serialization.DataSerializable) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with DataSerializable

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

the class DataSerializableConventionsTest method test_dataSerializableClasses_areIdentifiedDataSerializable.

/**
 * Verifies that any class which is {@link DataSerializable} and is not annotated with {@link BinaryInterface}
 * is also an {@link IdentifiedDataSerializable}.
 */
@Test
public void test_dataSerializableClasses_areIdentifiedDataSerializable() {
    Set<Class<? extends DataSerializable>> dataSerializableClasses = REFLECTIONS.getSubTypesOf(DataSerializable.class);
    Set<Class<? extends IdentifiedDataSerializable>> allIdDataSerializableClasses = REFLECTIONS.getSubTypesOf(IdentifiedDataSerializable.class);
    dataSerializableClasses.removeAll(allIdDataSerializableClasses);
    // also remove IdentifiedDataSerializable itself
    dataSerializableClasses.remove(IdentifiedDataSerializable.class);
    // do not check abstract classes & interfaces
    filterNonConcreteClasses(dataSerializableClasses);
    // locate all classes annotated with BinaryInterface and remove those as well
    Set<?> allAnnotatedClasses = REFLECTIONS.getTypesAnnotatedWith(BinaryInterface.class, true);
    dataSerializableClasses.removeAll(allAnnotatedClasses);
    // exclude @SerializableByConvention classes
    Set<?> serializableByConventions = REFLECTIONS.getTypesAnnotatedWith(SerializableByConvention.class, true);
    dataSerializableClasses.removeAll(serializableByConventions);
    if (dataSerializableClasses.size() > 0) {
        SortedSet<String> nonCompliantClassNames = new TreeSet<>();
        for (Object o : dataSerializableClasses) {
            if (!inheritsFromWhiteListedClass((Class) o)) {
                nonCompliantClassNames.add(o.toString());
            }
        }
        if (!nonCompliantClassNames.isEmpty()) {
            System.out.println("The following classes are DataSerializable while they should be IdentifiedDataSerializable:");
            // failure - output non-compliant classes to standard output and fail the test
            for (String s : nonCompliantClassNames) {
                System.out.println(s);
            }
            fail("There are " + dataSerializableClasses.size() + " classes which are DataSerializable, not @BinaryInterface-" + "annotated and are not IdentifiedDataSerializable.");
        }
    }
}
Also used : IdentifiedDataSerializable(com.hazelcast.nio.serialization.IdentifiedDataSerializable) TreeSet(java.util.TreeSet) EventObject(java.util.EventObject) IdentifiedDataSerializable(com.hazelcast.nio.serialization.IdentifiedDataSerializable) DataSerializable(com.hazelcast.nio.serialization.DataSerializable) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with DataSerializable

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

the class DataSerializableSerializationTest method testClarifiedExceptionsForUnsupportedClassTypes.

@Test
public void testClarifiedExceptionsForUnsupportedClassTypes() {
    class LocalClass implements DataSerializable {

        @Override
        public void writeData(ObjectDataOutput out) {
        }

        @Override
        public void readData(ObjectDataInput in) {
        }
    }
    DataSerializable anonymousInstance = new DataSerializable() {

        @Override
        public void writeData(ObjectDataOutput out) {
        }

        @Override
        public void readData(ObjectDataInput in) {
        }
    };
    DataSerializable[] throwingInstances = { new LocalClass(), anonymousInstance, new NonStaticMemberClass() };
    for (DataSerializable throwingInstance : throwingInstances) {
        try {
            ss.toObject(ss.toData(throwingInstance));
        } catch (HazelcastSerializationException e) {
            assertInstanceOf(NoSuchMethodException.class, e.getCause());
            assertContains(e.getCause().getMessage(), "can't conform to DataSerializable");
            assertInstanceOf(NoSuchMethodException.class, e.getCause().getCause());
            continue;
        }
        fail("deserialization of '" + throwingInstance.getClass() + "' is expected to fail");
    }
    for (DataSerializable throwingInstance : throwingInstances) {
        try {
            ss.toObject(ss.toData(throwingInstance), throwingInstance.getClass());
        } catch (HazelcastSerializationException e) {
            assertInstanceOf(InstantiationException.class, e.getCause());
            assertContains(e.getCause().getMessage(), "can't conform to DataSerializable");
            assertInstanceOf(InstantiationException.class, e.getCause().getCause());
            continue;
        }
        fail("deserialization of '" + throwingInstance.getClass() + "' is expected to fail");
    }
}
Also used : ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) IdentifiedDataSerializable(com.hazelcast.nio.serialization.IdentifiedDataSerializable) DataSerializable(com.hazelcast.nio.serialization.DataSerializable) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

DataSerializable (com.hazelcast.nio.serialization.DataSerializable)4 IdentifiedDataSerializable (com.hazelcast.nio.serialization.IdentifiedDataSerializable)4 QuickTest (com.hazelcast.test.annotation.QuickTest)3 Test (org.junit.Test)3 HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)2 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 ObjectDataInput (com.hazelcast.nio.ObjectDataInput)1 ObjectDataOutput (com.hazelcast.nio.ObjectDataOutput)1 DataSerializableFactory (com.hazelcast.nio.serialization.DataSerializableFactory)1 TypedDataSerializable (com.hazelcast.nio.serialization.TypedDataSerializable)1 IOException (java.io.IOException)1 EventObject (java.util.EventObject)1 TreeSet (java.util.TreeSet)1