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);
}
}
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);
}
}
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.");
}
}
}
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");
}
}
Aggregations