/** {@inheritDoc} */ @Override public int typeId(String typeName) { if (binaryCtx == null) return 0; return binaryCtx.typeId(typeName); }
/** * @param clsName Class name. * @param ctx Binary context. */ public BinaryObjectBuilderImpl(BinaryContext ctx, String clsName) { this(ctx, ctx.typeId(clsName), ctx.userTypeName(clsName)); }
/** {@inheritDoc} */ @Override public int typeId(String typeName) { return marsh.context().typeId(typeName); }
/** {@inheritDoc} */ @Override public BinaryType type(String typeName) { if (typeName == null || typeName.isEmpty()) throw new IllegalArgumentException("typeName"); int typeId = marsh.context().typeId(typeName); return type(typeId); }
/** {@inheritDoc} */ @Override public BinaryObject buildEnum(String typeName, int ord) { if (typeName == null || typeName.isEmpty()) throw new IllegalArgumentException("typeName"); BinaryContext ctx = marsh.context(); int typeId = ctx.typeId(typeName); return new BinaryEnumObjectImpl(ctx, typeId, null, ord); }
/** {@inheritDoc} */ @Override public int typeId() { int off = start + GridBinaryMarshaller.TYPE_ID_POS; int typeId = BinaryPrimitives.readInt(arr, off); if (typeId == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) { off = start + GridBinaryMarshaller.DFLT_HDR_LEN; assert arr[off] == GridBinaryMarshaller.STRING : arr[off]; int len = BinaryPrimitives.readInt(arr, ++off); String clsName = new String(arr, off + 4, len, UTF_8); typeId = ctx.typeId(clsName); } return typeId; }
/** * @return Target type. */ private BinaryType target() { if (target == null) { synchronized (this) { if (target == null) { if (typeId == GridBinaryMarshaller.UNREGISTERED_TYPE_ID && clsName != null) typeId = ctx.typeId(clsName); target = ctx.metadata(typeId); if (target == null) throw new BinaryObjectException("Failed to get binary type details [typeId=" + typeId + ']'); } } } return target; }
/** {@inheritDoc} */ @Override public BinaryType registerEnum(String typeName, Map<String, Integer> vals) { if (typeName == null || typeName.isEmpty()) throw new IllegalArgumentException("typeName"); BinaryContext ctx = marsh.context(); int typeId = ctx.typeId(typeName); ctx.updateMetadata(typeId, new BinaryMetadata(typeId, typeName, null, null, null, true, vals), false); return ctx.metadata(typeId); } }
/** {@inheritDoc} */ @Override public int typeId() { int typeId = BinaryPrimitives.readInt(ptr, start + GridBinaryMarshaller.TYPE_ID_POS); if (typeId == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) { int off = start + GridBinaryMarshaller.DFLT_HDR_LEN; String clsName = BinaryUtils.doReadClassName(new BinaryOffheapInputStream(ptr + off, size)); typeId = ctx.typeId(clsName); } return typeId; }
/** {@inheritDoc} */ @Override public BinaryObject buildEnum(String typeName, String name) { if (typeName == null || typeName.isEmpty()) throw new IllegalArgumentException("typeName"); if (name == null || name.isEmpty()) throw new IllegalArgumentException("name"); BinaryContext ctx = marsh.context(); int typeId = ctx.typeId(typeName); BinaryMetadata metadata = ctx.metadata0(typeId); if (metadata == null) throw new BinaryObjectException( String.format("Failed to get metadata for type [typeId=%s, typeName='%s']", typeId, typeName) ); Integer ordinal = metadata.getEnumOrdinalByName(name); if (ordinal == null) throw new BinaryObjectException(String.format( "Failed to resolve enum ordinal by name [typeId=%s, typeName='%s', name='%s']", typeId, typeName, name )); return new BinaryEnumObjectImpl(ctx, typeId, null, ordinal); }
/** {@inheritDoc} */ @Override public BinaryObject buildEnum(String typeName, int ord) throws BinaryObjectException { A.notNullOrEmpty(typeName, "enum type name"); int typeId = binaryCtx.typeId(typeName); typeName = binaryCtx.userTypeName(typeName); updateMetadata(typeId, typeName, null, null, true, null); return new BinaryEnumObjectImpl(binaryCtx, typeId, null, ord); }
/** {@inheritDoc} */ @Override public BinaryType registerEnum(String typeName, Map<String, Integer> vals) throws BinaryObjectException { A.notNullOrEmpty(typeName, "enum type name"); int typeId = binaryCtx.typeId(typeName); typeName = binaryCtx.userTypeName(typeName); BinaryUtils.validateEnumValues(typeName, vals); updateMetadata(typeId, typeName, null, null, true, vals); return binaryCtx.metadata(typeId); }
/** * @throws Exception If failed. */ @Test public void testPredefinedTypeIds() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); BinaryContext bCtx = binaryContext(marsh); Field field = bCtx.getClass().getDeclaredField("predefinedTypeNames"); field.setAccessible(true); Map<String, Integer> map = (Map<String, Integer>)field.get(bCtx); assertTrue(!map.isEmpty()); for (Map.Entry<String, Integer> entry : map.entrySet()) { int id = entry.getValue(); if (id == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) continue; BinaryClassDescriptor desc = bCtx.descriptorForTypeId(false, entry.getValue(), null, false); assertEquals(desc.typeId(), bCtx.typeId(desc.describedClass().getName())); } }
/** * @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()))); }
/** {@inheritDoc} */ @Override public BinaryObject buildEnum(String typeName, String name) throws BinaryObjectException { A.notNullOrEmpty(typeName, "enum type name"); A.notNullOrEmpty(name, "enum name"); int typeId = binaryCtx.typeId(typeName); BinaryMetadata metadata = metadata0(typeId); if (metadata == null) throw new BinaryObjectException("Failed to get metadata for type [typeId=" + typeId + ", typeName='" + typeName + "']"); Integer ordinal = metadata.getEnumOrdinalByName(name); typeName = binaryCtx.userTypeName(typeName); if (ordinal == null) throw new BinaryObjectException("Failed to resolve enum ordinal by name [typeId=" + typeId + ", typeName='" + typeName + "', name='" + name + "']"); return new BinaryEnumObjectImpl(binaryCtx, typeId, null, ordinal); }
/** * Perform post-write hash code update if necessary. * * @param clsName Class name. Always null if class is registered. */ public void postWriteHashCode(@Nullable String clsName) { int typeId = clsName == null ? this.typeId : ctx.typeId(clsName); BinaryIdentityResolver identity = ctx.identity(typeId); if (out.hasArray()) { // Heap. byte[] data = out.array(); BinaryObjectImpl obj = new BinaryObjectImpl(ctx, data, start); BinaryPrimitives.writeInt(data, start + GridBinaryMarshaller.HASH_CODE_POS, identity.hashCode(obj)); } else { // Offheap. long ptr = out.rawOffheapPointer(); assert ptr != 0; BinaryObjectOffheapImpl obj = new BinaryObjectOffheapImpl(ctx, ptr, start, out.capacity()); BinaryPrimitives.writeInt(ptr, start + GridBinaryMarshaller.HASH_CODE_POS, identity.hashCode(obj)); } }
assertEquals("NotConfiguredClass".hashCode(), ctx.typeId("NotConfiguredClass")); assertEquals(Key.class.getName().hashCode(), ctx.typeId(Key.class.getName())); assertEquals("org.gridgain.NonExistentClass3".hashCode(), ctx.typeId("org.gridgain.NonExistentClass3")); assertEquals("NonExistentClass4".hashCode(), ctx.typeId("NonExistentClass4")); assertEquals(300, ctx.typeId(Value.class.getName())); assertEquals(400, ctx.typeId("org.gridgain.NonExistentClass1")); assertEquals(500, ctx.typeId("NonExistentClass2")); assertEquals("nonexistentclass0".hashCode(), ctx.typeId("NonExistentClass0"));
assertEquals("notconfiguredclass".hashCode(), ctx.typeId("NotConfiguredClass")); assertEquals("key".hashCode(), ctx.typeId(Key.class.getName())); assertEquals("nonexistentclass3".hashCode(), ctx.typeId("org.gridgain.NonExistentClass3")); assertEquals("nonexistentclass4".hashCode(), ctx.typeId("NonExistentClass4")); assertEquals(300, ctx.typeId(Value.class.getName())); assertEquals(400, ctx.typeId("org.gridgain.NonExistentClass1")); assertEquals(500, ctx.typeId("NonExistentClass2")); assertEquals("nonexistentclass0".hashCode(), ctx.typeId("NonExistentClass0"));
assertEquals("notconfiguredclass".hashCode(), ctx.typeId("NotConfiguredClass")); assertEquals("notconfiguredclass".hashCode(), ctx.typeId("org.blabla.NotConfiguredClass")); assertEquals("key".hashCode(), ctx.typeId(Key.class.getName())); assertEquals("nonexistentclass3".hashCode(), ctx.typeId("org.gridgain.NonExistentClass3")); assertEquals("nonexistentclass4".hashCode(), ctx.typeId("NonExistentClass4")); assertEquals(300, ctx.typeId(Value.class.getName())); assertEquals(400, ctx.typeId("org.gridgain.NonExistentClass1")); assertEquals(500, ctx.typeId("NonExistentClass2")); assertEquals(DateClass1.class.getName().hashCode(), ctx.typeId(DateClass1.class.getName())); assertEquals("nonexistentclass0".hashCode(), ctx.typeId("NonExistentClass0"));
assertEquals(999, ctx.typeId("NotConfiguredClass")); assertEquals(999, ctx.typeId("org.blabla.NotConfiguredClass")); assertEquals("notconfiguredspecialclass".hashCode(), ctx.typeId("org.blabla.NotConfiguredSpecialClass")); assertEquals(991, ctx.typeId(Key.class.getName())); assertEquals(992, ctx.typeId("org.gridgain.NonExistentClass3")); assertEquals(993, ctx.typeId("NonExistentClass4")); assertEquals(300, ctx.typeId(Value.class.getName())); assertEquals(400, ctx.typeId("org.gridgain.NonExistentClass1")); assertEquals(500, ctx.typeId("NonExistentClass2")); assertEquals("nonexistentclass0".hashCode(), ctx.typeId("NonExistentClass0")); assertEquals(DateClass1.class.getName().hashCode(), ctx.typeId(DateClass1.class.getName())); assertEquals("mytestclass".hashCode(), ctx.typeId(MyTestClass.class.getName()));