/** * @param val Array. */ void doWriteEnumArray(@Nullable Object[] val) { assert val == null || val.getClass().getComponentType().isEnum(); if (val == null) out.writeByte(GridBinaryMarshaller.NULL); else { BinaryClassDescriptor desc = ctx.descriptorForClass( val.getClass().getComponentType(), false, failIfUnregistered); out.unsafeEnsure(1 + 4); out.unsafeWriteByte(GridBinaryMarshaller.ENUM_ARR); if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(val.getClass().getComponentType().getName()); } out.writeInt(val.length); // TODO: Denis: Redundant data for each element of the array. for (Object o : val) doWriteEnum((Enum<?>)o); } }
/** * @param val Value. */ void doWriteEnum(@Nullable Enum<?> val) { if (val == null) out.writeByte(GridBinaryMarshaller.NULL); else { BinaryClassDescriptor desc = ctx.descriptorForClass(val.getDeclaringClass(), false, failIfUnregistered); out.unsafeEnsure(1 + 4); out.unsafeWriteByte(GridBinaryMarshaller.ENUM); if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(val.getDeclaringClass().getName()); } out.writeInt(val.ordinal()); } }
/** * @param val Array of objects. * @throws org.apache.ignite.binary.BinaryObjectException In case of error. */ void doWriteObjectArray(@Nullable Object[] val) throws BinaryObjectException { if (val == null) out.writeByte(GridBinaryMarshaller.NULL); else { if (tryWriteAsHandle(val)) return; BinaryClassDescriptor desc = ctx.descriptorForClass( val.getClass().getComponentType(), false, failIfUnregistered); out.unsafeEnsure(1 + 4); out.unsafeWriteByte(GridBinaryMarshaller.OBJ_ARR); if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(val.getClass().getComponentType().getName()); } out.writeInt(val.length); for (Object obj : val) doWriteObject(obj); } }
/** * @param val Class. */ void doWriteClass(@Nullable Class val) { if (val == null) out.writeByte(GridBinaryMarshaller.NULL); else { BinaryClassDescriptor desc = ctx.descriptorForClass(val, false, failIfUnregistered); out.unsafeEnsure(1 + 4); out.unsafeWriteByte(GridBinaryMarshaller.CLASS); if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(val.getName()); } } }
/** * @param proxy Proxy. */ public void doWriteProxy(Proxy proxy, Class<?>[] intfs) { if (proxy == null) out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); out.unsafeWriteByte(GridBinaryMarshaller.PROXY); out.unsafeWriteInt(intfs.length); for (Class<?> intf : intfs) { BinaryClassDescriptor desc = ctx.descriptorForClass(intf, false, failIfUnregistered); if (desc.registered()) out.writeInt(desc.typeId()); else { out.writeInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(intf.getName()); } } InvocationHandler ih = Proxy.getInvocationHandler(proxy); assert ih != null; doWriteObject(ih); } }
/** * @throws Exception If failed. */ @Test public void testCustomTypeRegistration() throws Exception { BinaryTypeConfiguration customType = new BinaryTypeConfiguration(Value.class.getName()); BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(customType)); BinaryContext ctx = binaryContext(marsh); int typeId = ctx.typeId(Value.class.getName()); BinaryClassDescriptor descriptor = ctx.descriptorForTypeId(true, typeId, null, false); assertEquals(Value.class, descriptor.describedClass()); assertEquals(true, descriptor.registered()); assertEquals(true, descriptor.userType()); // Custom explicit types must be registered in 'predefinedTypes' in order not to break the interoperability. Field field = ctx.getClass().getDeclaredField("predefinedTypes"); field.setAccessible(true); Map<Integer, BinaryClassDescriptor> map = (Map<Integer, BinaryClassDescriptor>)field.get(ctx); assertTrue(!map.isEmpty()); assertNotNull(map.get(typeId)); // Custom explicit types must NOT be registered in 'predefinedTypeNames'. field = ctx.getClass().getDeclaredField("predefinedTypeNames"); field.setAccessible(true); Map<String, Integer> map2 = (Map<String, Integer>)field.get(ctx); assertTrue(!map2.isEmpty()); assertNull(map2.get(ctx.userTypeName(Value.class.getName()))); }
else if (!desc.registered()) { if (!desc.userType()) { BinaryClassDescriptor desc0 = new BinaryClassDescriptor(
/** * @param val Array. */ void doWriteEnumArray(@Nullable Object[] val) { assert val == null || val.getClass().getComponentType().isEnum(); if (val == null) out.writeByte(GridBinaryMarshaller.NULL); else { BinaryClassDescriptor desc = ctx.descriptorForClass( val.getClass().getComponentType(), false, failIfUnregistered); out.unsafeEnsure(1 + 4); out.unsafeWriteByte(GridBinaryMarshaller.ENUM_ARR); if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(val.getClass().getComponentType().getName()); } out.writeInt(val.length); // TODO: Denis: Redundant data for each element of the array. for (Object o : val) doWriteEnum((Enum<?>)o); } }
/** * @param val Value. */ void doWriteEnum(@Nullable Enum<?> val) { if (val == null) out.writeByte(GridBinaryMarshaller.NULL); else { BinaryClassDescriptor desc = ctx.descriptorForClass(val.getDeclaringClass(), false, failIfUnregistered); out.unsafeEnsure(1 + 4); out.unsafeWriteByte(GridBinaryMarshaller.ENUM); if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(val.getDeclaringClass().getName()); } out.writeInt(val.ordinal()); } }
/** * @param val Array of objects. * @throws org.apache.ignite.binary.BinaryObjectException In case of error. */ void doWriteObjectArray(@Nullable Object[] val) throws BinaryObjectException { if (val == null) out.writeByte(GridBinaryMarshaller.NULL); else { if (tryWriteAsHandle(val)) return; BinaryClassDescriptor desc = ctx.descriptorForClass( val.getClass().getComponentType(), false, failIfUnregistered); out.unsafeEnsure(1 + 4); out.unsafeWriteByte(GridBinaryMarshaller.OBJ_ARR); if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(val.getClass().getComponentType().getName()); } out.writeInt(val.length); for (Object obj : val) doWriteObject(obj); } }
/** * @param val Class. */ void doWriteClass(@Nullable Class val) { if (val == null) out.writeByte(GridBinaryMarshaller.NULL); else { BinaryClassDescriptor desc = ctx.descriptorForClass(val, false, failIfUnregistered); out.unsafeEnsure(1 + 4); out.unsafeWriteByte(GridBinaryMarshaller.CLASS); if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(val.getName()); } } }
/** * @param proxy Proxy. */ public void doWriteProxy(Proxy proxy, Class<?>[] intfs) { if (proxy == null) out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); out.unsafeWriteByte(GridBinaryMarshaller.PROXY); out.unsafeWriteInt(intfs.length); for (Class<?> intf : intfs) { BinaryClassDescriptor desc = ctx.descriptorForClass(intf, false, failIfUnregistered); if (desc.registered()) out.writeInt(desc.typeId()); else { out.writeInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(intf.getName()); } } InvocationHandler ih = Proxy.getInvocationHandler(proxy); assert ih != null; doWriteObject(ih); } }
else if (!desc.registered()) { if (!desc.userType()) { BinaryClassDescriptor desc0 = new BinaryClassDescriptor(