com.dyuproject.protostuff.runtime.RuntimeReflectionFieldFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protostuff-runtime Show documentation
Show all versions of protostuff-runtime Show documentation
protobuf serialization for pre-existing objects
//========================================================================
//Copyright 2007-2011 David Yu [email protected]
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//========================================================================
package com.dyuproject.protostuff.runtime;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_BIGDECIMAL;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_BIGINTEGER;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_BOOL;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_BYTE;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_BYTES;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_BYTE_ARRAY;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_CHAR;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_DATE;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_DOUBLE;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_ENUM;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_FLOAT;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_INT32;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_INT64;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_OBJECT;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_POJO;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_SHORT;
import static com.dyuproject.protostuff.runtime.RuntimeFieldFactory.ID_STRING;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import com.dyuproject.protostuff.ByteString;
import com.dyuproject.protostuff.GraphInput;
import com.dyuproject.protostuff.Input;
import com.dyuproject.protostuff.Output;
import com.dyuproject.protostuff.Pipe;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.WireFormat.FieldType;
import com.dyuproject.protostuff.runtime.MappedSchema.Field;
/**
* Field factory via plain old reflection api.
*
* @author David Yu
* @created Jul 7, 2011
*/
public final class RuntimeReflectionFieldFactory
{
private RuntimeReflectionFieldFactory() {}
public static final RuntimeFieldFactory CHAR = new RuntimeFieldFactory(ID_CHAR)
{
public Field create(int number, String name, final java.lang.reflect.Field f)
{
final boolean primitive = f.getType().isPrimitive();
return new Field(FieldType.UINT32, number, name)
{
{
f.setAccessible(true);
}
protected void mergeFrom(Input input, T message) throws IOException
{
try
{
if(primitive)
f.setChar(message, (char)input.readUInt32());
else
f.set(message, Character.valueOf((char)input.readUInt32()));
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void writeTo(Output output, T message) throws IOException
{
try
{
if(primitive)
output.writeUInt32(number, f.getChar(message), false);
else
{
Character value = (Character)f.get(message);
if(value!=null)
output.writeUInt32(number, value.charValue(), false);
}
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void transfer(Pipe pipe, Input input, Output output,
boolean repeated) throws IOException
{
output.writeUInt32(number, input.readUInt32(), repeated);
}
};
}
protected void transfer(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
output.writeUInt32(number, input.readUInt32(), repeated);
}
protected Character readFrom(Input input) throws IOException
{
return Character.valueOf((char)input.readUInt32());
}
protected void writeTo(Output output, int number, Character value, boolean repeated)
throws IOException
{
output.writeUInt32(number, value.charValue(), repeated);
}
protected FieldType getFieldType()
{
return FieldType.UINT32;
}
protected Class> typeClass()
{
return Character.class;
}
};
public static final RuntimeFieldFactory SHORT = new RuntimeFieldFactory(ID_SHORT)
{
public Field create(int number, String name, final java.lang.reflect.Field f)
{
final boolean primitive = f.getType().isPrimitive();
return new Field(FieldType.UINT32, number, name)
{
{
f.setAccessible(true);
}
protected void mergeFrom(Input input, T message) throws IOException
{
try
{
if(primitive)
f.setShort(message, (short)input.readUInt32());
else
f.set(message, Short.valueOf((short)input.readUInt32()));
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void writeTo(Output output, T message) throws IOException
{
try
{
if(primitive)
output.writeUInt32(number, f.getShort(message), false);
else
{
Short value = (Short)f.get(message);
if(value!=null)
output.writeUInt32(number, value.shortValue(), false);
}
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void transfer(Pipe pipe, Input input, Output output,
boolean repeated) throws IOException
{
output.writeUInt32(number, input.readUInt32(), repeated);
}
};
}
protected void transfer(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
output.writeUInt32(number, input.readUInt32(), repeated);
}
protected Short readFrom(Input input) throws IOException
{
return Short.valueOf((short)input.readUInt32());
}
protected void writeTo(Output output, int number, Short value, boolean repeated)
throws IOException
{
output.writeUInt32(number, value.shortValue(), repeated);
}
protected FieldType getFieldType()
{
return FieldType.UINT32;
}
protected Class> typeClass()
{
return Short.class;
}
};
public static final RuntimeFieldFactory BYTE = new RuntimeFieldFactory(ID_BYTE)
{
public Field create(int number, String name, final java.lang.reflect.Field f)
{
final boolean primitive = f.getType().isPrimitive();
return new Field(FieldType.UINT32, number, name)
{
{
f.setAccessible(true);
}
protected void mergeFrom(Input input, T message) throws IOException
{
try
{
if(primitive)
f.setByte(message, (byte)input.readUInt32());
else
f.set(message, Byte.valueOf((byte)input.readUInt32()));
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void writeTo(Output output, T message) throws IOException
{
try
{
if(primitive)
output.writeUInt32(number, f.getByte(message), false);
else
{
Byte value = (Byte)f.get(message);
if(value!=null)
output.writeUInt32(number, value.byteValue(), false);
}
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void transfer(Pipe pipe, Input input, Output output,
boolean repeated) throws IOException
{
output.writeUInt32(number, input.readUInt32(), repeated);
}
};
}
protected void transfer(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
output.writeUInt32(number, input.readUInt32(), repeated);
}
protected Byte readFrom(Input input) throws IOException
{
return Byte.valueOf((byte)input.readUInt32());
}
protected void writeTo(Output output, int number, Byte value, boolean repeated)
throws IOException
{
output.writeUInt32(number, value.byteValue(), repeated);
}
protected FieldType getFieldType()
{
return FieldType.UINT32;
}
protected Class> typeClass()
{
return Byte.class;
}
};
public static final RuntimeFieldFactory INT32 = new RuntimeFieldFactory(ID_INT32)
{
public Field create(int number, java.lang.String name,
final java.lang.reflect.Field f)
{
final boolean primitive = f.getType().isPrimitive();
return new Field(FieldType.INT32, number, name)
{
{
f.setAccessible(true);
}
protected void mergeFrom(Input input, T message) throws IOException
{
try
{
if(primitive)
f.setInt(message, input.readInt32());
else
f.set(message, Integer.valueOf(input.readInt32()));
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void writeTo(Output output, T message) throws IOException
{
try
{
if(primitive)
output.writeInt32(number, f.getInt(message), false);
else
{
Integer value = (Integer)f.get(message);
if(value!=null)
output.writeInt32(number, value.intValue(), false);
}
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void transfer(Pipe pipe, Input input, Output output,
boolean repeated) throws IOException
{
output.writeInt32(number, input.readInt32(), repeated);
}
};
}
protected void transfer(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
output.writeInt32(number, input.readInt32(), repeated);
}
protected Integer readFrom(Input input) throws IOException
{
return Integer.valueOf(input.readInt32());
}
protected void writeTo(Output output, int number, Integer value, boolean repeated)
throws IOException
{
output.writeInt32(number, value.intValue(), repeated);
}
protected FieldType getFieldType()
{
return FieldType.INT32;
}
protected Class> typeClass()
{
return Integer.class;
}
};
public static final RuntimeFieldFactory INT64 = new RuntimeFieldFactory(ID_INT64)
{
public Field create(int number, java.lang.String name,
final java.lang.reflect.Field f)
{
final boolean primitive = f.getType().isPrimitive();
return new Field(FieldType.INT64, number, name)
{
{
f.setAccessible(true);
}
protected void mergeFrom(Input input, T message) throws IOException
{
try
{
if(primitive)
f.setLong(message, input.readInt64());
else
f.set(message, Long.valueOf(input.readInt64()));
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void writeTo(Output output, T message) throws IOException
{
try
{
if(primitive)
output.writeInt64(number, f.getLong(message), false);
else
{
Long value = (Long)f.get(message);
if(value!=null)
output.writeInt64(number, value.longValue(), false);
}
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void transfer(Pipe pipe, Input input, Output output,
boolean repeated) throws IOException
{
output.writeInt64(number, input.readInt64(), repeated);
}
};
}
protected void transfer(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
output.writeInt64(number, input.readInt64(), repeated);
}
protected Long readFrom(Input input) throws IOException
{
return Long.valueOf(input.readInt64());
}
protected void writeTo(Output output, int number, Long value, boolean repeated)
throws IOException
{
output.writeInt64(number, value.longValue(), repeated);
}
protected FieldType getFieldType()
{
return FieldType.INT64;
}
protected Class> typeClass()
{
return Long.class;
}
};
public static final RuntimeFieldFactory FLOAT = new RuntimeFieldFactory(ID_FLOAT)
{
public Field create(int number, java.lang.String name,
final java.lang.reflect.Field f)
{
final boolean primitive = f.getType().isPrimitive();
return new Field(FieldType.FLOAT, number, name)
{
{
f.setAccessible(true);
}
protected void mergeFrom(Input input, T message) throws IOException
{
try
{
if(primitive)
f.setFloat(message, input.readFloat());
else
f.set(message, new Float(input.readFloat()));
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void writeTo(Output output, T message) throws IOException
{
try
{
if(primitive)
output.writeFloat(number, f.getFloat(message), false);
else
{
Float value = (Float)f.get(message);
if(value!=null)
output.writeFloat(number, value.floatValue(), false);
}
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void transfer(Pipe pipe, Input input, Output output,
boolean repeated) throws IOException
{
output.writeFloat(number, input.readFloat(), repeated);
}
};
}
protected void transfer(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
output.writeFloat(number, input.readFloat(), repeated);
}
protected Float readFrom(Input input) throws IOException
{
return new Float(input.readFloat());
}
protected void writeTo(Output output, int number, Float value, boolean repeated)
throws IOException
{
output.writeFloat(number, value.floatValue(), repeated);
}
protected FieldType getFieldType()
{
return FieldType.FLOAT;
}
protected Class> typeClass()
{
return Float.class;
}
};
public static final RuntimeFieldFactory DOUBLE = new RuntimeFieldFactory(ID_DOUBLE)
{
public Field create(int number, java.lang.String name,
final java.lang.reflect.Field f)
{
final boolean primitive = f.getType().isPrimitive();
return new Field(FieldType.DOUBLE, number, name)
{
{
f.setAccessible(true);
}
protected void mergeFrom(Input input, T message) throws IOException
{
try
{
if(primitive)
f.setDouble(message, input.readDouble());
else
f.set(message, new Double(input.readDouble()));
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void writeTo(Output output, T message) throws IOException
{
try
{
if(primitive)
output.writeDouble(number, f.getDouble(message), false);
else
{
Double value = (Double)f.get(message);
if(value!=null)
output.writeDouble(number, value.doubleValue(), false);
}
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void transfer(Pipe pipe, Input input, Output output,
boolean repeated) throws IOException
{
output.writeDouble(number, input.readDouble(), repeated);
}
};
}
protected void transfer(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
output.writeDouble(number, input.readDouble(), repeated);
}
protected Double readFrom(Input input) throws IOException
{
return new Double(input.readDouble());
}
protected void writeTo(Output output, int number, Double value, boolean repeated)
throws IOException
{
output.writeDouble(number, value.doubleValue(), repeated);
}
protected FieldType getFieldType()
{
return FieldType.DOUBLE;
}
protected Class> typeClass()
{
return Double.class;
}
};
public static final RuntimeFieldFactory BOOL = new RuntimeFieldFactory(ID_BOOL)
{
public Field create(int number, java.lang.String name,
final java.lang.reflect.Field f)
{
final boolean primitive = f.getType().isPrimitive();
return new Field(FieldType.BOOL, number, name)
{
{
f.setAccessible(true);
}
protected void mergeFrom(Input input, T message) throws IOException
{
try
{
if(primitive)
f.setBoolean(message, input.readBool());
else
f.set(message, input.readBool() ? Boolean.TRUE : Boolean.FALSE);
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void writeTo(Output output, T message) throws IOException
{
try
{
if(primitive)
output.writeBool(number, f.getBoolean(message), false);
else
{
Boolean value = (Boolean)f.get(message);
if(value!=null)
output.writeBool(number, value.booleanValue(), false);
}
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void transfer(Pipe pipe, Input input, Output output,
boolean repeated) throws IOException
{
output.writeBool(number, input.readBool(), repeated);
}
};
}
protected void transfer(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
output.writeBool(number, input.readBool(), repeated);
}
protected Boolean readFrom(Input input) throws IOException
{
return input.readBool() ? Boolean.TRUE : Boolean.FALSE;
}
protected void writeTo(Output output, int number, Boolean value, boolean repeated)
throws IOException
{
output.writeBool(number, value.booleanValue(), repeated);
}
protected FieldType getFieldType()
{
return FieldType.BOOL;
}
protected Class> typeClass()
{
return Boolean.class;
}
};
public static final RuntimeFieldFactory STRING = new RuntimeFieldFactory(ID_STRING)
{
public Field create(int number, java.lang.String name,
final java.lang.reflect.Field f)
{
return new Field(FieldType.STRING, number, name)
{
{
f.setAccessible(true);
}
protected void mergeFrom(Input input, T message) throws IOException
{
try
{
f.set(message, input.readString());
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void writeTo(Output output, T message) throws IOException
{
try
{
String value = (String)f.get(message);
if(value!=null)
output.writeString(number, value, false);
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void transfer(Pipe pipe, Input input, Output output,
boolean repeated) throws IOException
{
input.transferByteRangeTo(output, true, number, repeated);
}
};
}
protected void transfer(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
input.transferByteRangeTo(output, true, number, repeated);
}
protected String readFrom(Input input) throws IOException
{
return input.readString();
}
protected void writeTo(Output output, int number, String value, boolean repeated)
throws IOException
{
output.writeString(number, value, repeated);
}
protected FieldType getFieldType()
{
return FieldType.STRING;
}
protected Class> typeClass()
{
return String.class;
}
};
public static final RuntimeFieldFactory BYTES = new RuntimeFieldFactory(ID_BYTES)
{
public Field create(int number, java.lang.String name,
final java.lang.reflect.Field f)
{
return new Field(FieldType.BYTES, number, name)
{
{
f.setAccessible(true);
}
protected void mergeFrom(Input input, T message) throws IOException
{
try
{
f.set(message, input.readBytes());
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void writeTo(Output output, T message) throws IOException
{
try
{
ByteString bs = (ByteString)f.get(message);
if(bs!=null)
output.writeBytes(number, bs, false);
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void transfer(Pipe pipe, Input input, Output output,
boolean repeated) throws IOException
{
input.transferByteRangeTo(output, false, number, repeated);
}
};
}
protected void transfer(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
input.transferByteRangeTo(output, false, number, repeated);
}
protected ByteString readFrom(Input input) throws IOException
{
return input.readBytes();
}
protected void writeTo(Output output, int number, ByteString value, boolean repeated)
throws IOException
{
output.writeBytes(number, value, repeated);
}
protected FieldType getFieldType()
{
return FieldType.BYTES;
}
protected Class> typeClass()
{
return ByteString.class;
}
};
public static final RuntimeFieldFactory BYTE_ARRAY = new RuntimeFieldFactory(ID_BYTE_ARRAY)
{
public Field create(int number, java.lang.String name,
final java.lang.reflect.Field f)
{
return new Field(FieldType.BYTES, number, name)
{
{
f.setAccessible(true);
}
protected void mergeFrom(Input input, T message) throws IOException
{
try
{
f.set(message, input.readByteArray());
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void writeTo(Output output, T message) throws IOException
{
try
{
byte[] array = (byte[])f.get(message);
if(array!=null)
output.writeByteArray(number, array, false);
}
catch(IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch(IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void transfer(Pipe pipe, Input input, Output output,
boolean repeated) throws IOException
{
input.transferByteRangeTo(output, false, number, repeated);
}
};
}
protected void transfer(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
input.transferByteRangeTo(output, false, number, repeated);
}
protected byte[] readFrom(Input input) throws IOException
{
return input.readByteArray();
}
protected void writeTo(Output output, int number, byte[] value, boolean repeated)
throws IOException
{
output.writeByteArray(number, value, repeated);
}
protected FieldType getFieldType()
{
return FieldType.BYTES;
}
protected Class> typeClass()
{
return byte[].class;
}
};
public static final RuntimeFieldFactory ENUM = new RuntimeFieldFactory(ID_ENUM)
{
public Field create(int number, java.lang.String name,
final java.lang.reflect.Field f)
{
final EnumIO extends Enum>> eio = EnumIO.get(f.getType());
return new Field(FieldType.ENUM, number, name)
{
{
f.setAccessible(true);
}
protected void mergeFrom(Input input, T message) throws IOException
{
try
{
f.set(message, eio.readFrom(input));
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
}
protected void writeTo(Output output, T message) throws IOException
{
final Enum> existing;
try
{
existing = (Enum>)f.get(message);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
if(existing != null)
EnumIO.writeTo(output, number, repeated, existing);
}
protected void transfer(Pipe pipe, Input input, Output output,
boolean repeated) throws IOException
{
EnumIO.transfer(pipe, input, output, number, repeated);
}
};
}
protected void transfer(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
throw new UnsupportedOperationException();
}
protected Integer readFrom(Input input) throws IOException
{
throw new UnsupportedOperationException();
}
protected void writeTo(Output output, int number, Integer value, boolean repeated) throws IOException
{
throw new UnsupportedOperationException();
}
protected FieldType getFieldType()
{
throw new UnsupportedOperationException();
}
protected Class> typeClass()
{
throw new UnsupportedOperationException();
}
};
// NON-INLINE VALUES
static final RuntimeFieldFactory