/** * Gets the schema that was either registered or lazily initialized at runtime. * <p> * Method overload for backwards compatibility. */ public static <T> Schema<T> getSchema(Class<T> typeClass) { return getSchema(typeClass, ID_STRATEGY); }
/** * Gets the schema that was either registered or lazily initialized at runtime. * <p> * Method overload for backwards compatibility. */ public static <T> Schema<T> getSchema(Class<T> typeClass) { return getSchema(typeClass, ID_STRATEGY); }
@SuppressWarnings("unchecked") @Override public void writeObject(Object obj) throws IOException { byte[] bytes; byte[] classNameBytes; try { if (WrapperUtils.needWrapper(obj)) { Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class); Wrapper wrapper = new Wrapper(obj); bytes = GraphIOUtil.toByteArray(wrapper, schema, buffer); classNameBytes = Wrapper.class.getName().getBytes(); } else { Schema schema = RuntimeSchema.getSchema(obj.getClass()); bytes = GraphIOUtil.toByteArray(obj, schema, buffer); classNameBytes = obj.getClass().getName().getBytes(); } } finally { buffer.clear(); } dos.writeInt(classNameBytes.length); dos.writeInt(bytes.length); dos.write(classNameBytes); dos.write(bytes); }
@SuppressWarnings("unchecked") @Override public void writeObject(Object obj) throws IOException { byte[] bytes; byte[] classNameBytes; try { if (WrapperUtils.needWrapper(obj)) { Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class); Wrapper wrapper = new Wrapper(obj); bytes = GraphIOUtil.toByteArray(wrapper, schema, buffer); classNameBytes = Wrapper.class.getName().getBytes(); } else { Schema schema = RuntimeSchema.getSchema(obj.getClass()); bytes = GraphIOUtil.toByteArray(obj, schema, buffer); classNameBytes = obj.getClass().getName().getBytes(); } } finally { buffer.clear(); } dos.writeInt(classNameBytes.length); dos.writeInt(bytes.length); dos.write(classNameBytes); dos.write(bytes); }
@SuppressWarnings("ResultOfMethodCallIgnored") @Override public Object readObject() throws IOException, ClassNotFoundException { int classNameLength = dis.readInt(); int bytesLength = dis.readInt(); if (classNameLength < 0 || bytesLength < 0) { throw new IOException(); } byte[] classNameBytes = new byte[classNameLength]; dis.readFully(classNameBytes, 0, classNameLength); byte[] bytes = new byte[bytesLength]; dis.readFully(bytes, 0, bytesLength); String className = new String(classNameBytes); Class clazz = Class.forName(className); Object result; if (WrapperUtils.needWrapper(clazz)) { Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class); Wrapper wrapper = schema.newMessage(); GraphIOUtil.mergeFrom(bytes, wrapper, schema); result = wrapper.getData(); } else { Schema schema = RuntimeSchema.getSchema(clazz); result = schema.newMessage(); GraphIOUtil.mergeFrom(bytes, result, schema); } return result; }
@SuppressWarnings("ResultOfMethodCallIgnored") @Override public Object readObject() throws IOException, ClassNotFoundException { int classNameLength = dis.readInt(); int bytesLength = dis.readInt(); if (classNameLength < 0 || bytesLength < 0) { throw new IOException(); } byte[] classNameBytes = new byte[classNameLength]; dis.readFully(classNameBytes, 0, classNameLength); byte[] bytes = new byte[bytesLength]; dis.readFully(bytes, 0, bytesLength); String className = new String(classNameBytes); Class clazz = Class.forName(className); Object result; if (WrapperUtils.needWrapper(clazz)) { Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class); Wrapper wrapper = schema.newMessage(); GraphIOUtil.mergeFrom(bytes, wrapper, schema); result = wrapper.getData(); } else { Schema schema = RuntimeSchema.getSchema(clazz); result = schema.newMessage(); GraphIOUtil.mergeFrom(bytes, result, schema); } return result; }
@Override public void writeExternal(ObjectOutput out) throws IOException { ProtostuffIOUtil.writeDelimitedTo(out, this, RuntimeSchema.getSchema(Bar.class)); }
@Override public void readExternal(ObjectInput in) throws IOException { ProtostuffIOUtil.mergeDelimitedFrom(in, this, RuntimeSchema.getSchema(Baz.class)); }
@Test public void testPojoWithMappedAbstractTypes() { RuntimeSchema<PojoWithMappedAbstractTypes> schema = (RuntimeSchema<PojoWithMappedAbstractTypes>) RuntimeSchema .getSchema(PojoWithMappedAbstractTypes.class, RuntimeEnv.ID_STRATEGY); assertTrue(schema.getFields().size() == 4); assertTrue(schema.getFields().get(0) instanceof RuntimeMessageField); assertTrue(schema.getFields().get(1) instanceof RuntimeMessageField); assertTrue(schema.getFields().get(2) instanceof RuntimeObjectField); assertTrue(schema.getFields().get(3) instanceof RuntimeDerivativeField); }
private static <T> T deserializeGraph(byte[] bytes, Class<T> clz) { Schema<T> schema = RuntimeSchema.getSchema(clz); T obj = schema.newMessage(); GraphIOUtil.mergeFrom(bytes, obj, schema); return obj; }
private void writeObject(ObjectOutputStream out) throws IOException { byte[] data = ProtostuffIOUtil.toByteArray(this, RuntimeSchema.getSchema(PojoWithArrayAndSet.class), LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE)); out.writeInt(data.length); out.write(data); out.close(); }
@Test public void testEntityWithFieldAlias() { RuntimeSchema<EntityWithFieldAlias> schema = (RuntimeSchema<EntityWithFieldAlias>) RuntimeSchema .getSchema(EntityWithFieldAlias.class, RuntimeEnv.ID_STRATEGY); assertTrue(schema.getFieldCount() == 2); // The field with the smallest field number will be written first. // In this case, field200 (despite field400 being declared 1st) verify(schema, 200, "f200", 0); verify(schema, 400, "field400", 1); }
/** * Class and field names should be included in the message when one of fields is not * annotated by @Tag (and at least one other field is) */ @Test public void testMissingTagException() throws Exception { thrown.expect(RuntimeException.class); thrown.expectMessage("io.protostuff.runtime.RuntimeSchemaTagTest.OneFieldIsNotAnnotated#b is not annotated with @Tag"); RuntimeSchema.getSchema(OneFieldIsNotAnnotated.class); }
public void testBar() throws Exception { Schema<Bar> schema = RuntimeSchema.getSchema(Bar.class); for (Bar barCompare : new Bar[] { bar, negativeBar }) { Bar dbar = new Bar(); byte[] data = JsonIOUtil.toByteArray(barCompare, schema, false); JsonIOUtil.mergeFrom(data, dbar, schema, false); SerializableObjects.assertEquals(barCompare, dbar); } }
public void testPolymorphic() throws Exception { Schema<Zoo> schema = RuntimeSchema.getSchema(Zoo.class); Pipe.Schema<Zoo> pipeSchema = ((RuntimeSchema<Zoo>) schema).getPipeSchema(); Zoo zoo = PolymorphicSerializationTest.filledZoo(); // numeric only protobufRoundTrip(zoo, schema, pipeSchema, true); protostuffRoundTrip(zoo, schema, pipeSchema, true); }
public void testBaz() throws Exception { Schema<Baz> schema = RuntimeSchema.getSchema(Baz.class); for (Baz bazCompare : new Baz[] { baz, negativeBaz }) { Baz dbaz = new Baz(); byte[] data = JsonIOUtil.toByteArray(bazCompare, schema, true); JsonIOUtil.mergeFrom(data, dbaz, schema, true); SerializableObjects.assertEquals(bazCompare, dbaz); } }
public void testFoo() throws Exception { Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class); Foo fooCompare = foo; Foo dfoo = new Foo(); byte[] data = JsonIOUtil.toByteArray(fooCompare, schema, true); JsonIOUtil.mergeFrom(data, dfoo, schema, true); SerializableObjects.assertEquals(fooCompare, dfoo); }
public void testPolymorphic() throws Exception { Schema<Zoo> schema = RuntimeSchema.getSchema(Zoo.class); Zoo zooCompare = PolymorphicSerializationTest.filledZoo(); Zoo dzoo = new Zoo(); byte[] data = JsonIOUtil.toByteArray(zooCompare, schema, true); JsonIOUtil.mergeFrom(data, dzoo, schema, true); SerializableObjects.assertEquals(zooCompare, dzoo); }
public void testFoo() throws Exception { Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class); Foo fooCompare = foo; Foo dfoo = new Foo(); byte[] data = JsonXIOUtil.toByteArray(fooCompare, schema, true, buf()); JsonIOUtil.mergeFrom(data, dfoo, schema, true); SerializableObjects.assertEquals(fooCompare, dfoo); }
public void testPolymorphic() throws Exception { Schema<Zoo> schema = RuntimeSchema.getSchema(Zoo.class); Zoo zooCompare = PolymorphicSerializationTest.filledZoo(); Zoo dzoo = new Zoo(); byte[] data = JsonXIOUtil.toByteArray(zooCompare, schema, true, buf()); JsonIOUtil.mergeFrom(data, dzoo, schema, true); SerializableObjects.assertEquals(zooCompare, dzoo); }