net.dongliu.prettypb.runtime.code.PrimitiveProtoFieldInfo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prettypb-runtime Show documentation
Show all versions of prettypb-runtime Show documentation
Prettypb serialization runtime
package net.dongliu.prettypb.runtime.code;
import net.dongliu.prettypb.runtime.ExtensionRegistry;
import net.dongliu.prettypb.runtime.include.ProtoField;
import net.dongliu.prettypb.runtime.code.primitive.*;
import net.dongliu.prettypb.runtime.exception.IllegalBeanException;
import net.dongliu.prettypb.runtime.exception.IllegalDataException;
import net.dongliu.prettypb.runtime.exception.MissRequiredFieldException;
import java.io.IOException;
import java.lang.reflect.Field;
/**
* primitive field
*
* @author Dong Liu
*/
abstract public class PrimitiveProtoFieldInfo extends ProtoFieldInfo {
private Field helperField;
protected PrimitiveProtoFieldInfo(Field field, Field helperField, ProtoField protoField) {
super(field, protoField);
this.helperField = helperField;
this.helperField.setAccessible(true);
}
static ProtoFieldInfo getPrimitive(Field field, Field helperField, ProtoField protoField) {
switch (protoField.type()) {
case Int32:
return new Int32ProtoFieldInfo(field, helperField, protoField);
case UInt32:
return new UInt32ProtoFieldInfo(field, helperField, protoField);
case SInt32:
return new SInt32ProtoFieldInfo(field, helperField, protoField);
case Int64:
return new Int64ProtoFieldInfo(field, helperField, protoField);
case UInt64:
return new UInt64ProtoFieldInfo(field, helperField, protoField);
case SInt64:
return new SInt64ProtoFieldInfo(field, helperField, protoField);
case Float:
return new FloatProtoFieldInfo(field, helperField, protoField);
case Double:
return new DoubleProtoFieldInfo(field, helperField, protoField);
case Bool:
return new BoolProtoFieldInfo(field, helperField, protoField);
default:
throw new IllegalArgumentException();
}
}
@Override
public boolean primitive() {
return true;
}
@Override
public boolean hasField(T bean) throws IllegalAccessException {
// judge by the value is not null
return helperField.getBoolean(bean);
}
@Override
public void serializeField(T bean, ProtoBufWriter protoBufWriter)
throws IllegalBeanException, MissRequiredFieldException, IOException,
IllegalAccessException, IllegalDataException {
if (checkRequired(bean, helperField, required(), protoField.hasDefault())) {
serializeFieldInternal(bean, protoBufWriter);
}
}
/**
* for non-repeated types. check if this field has data, and if this field is required
*/
private boolean checkRequired(T bean, Field helperField, boolean required,
boolean hasDefault)
throws IllegalBeanException, MissRequiredFieldException {
try {
if (helperField.getBoolean(bean)) {
return true;
}
} catch (IllegalAccessException e) {
throw new IllegalBeanException("Helper field is not accessible", e);
}
if (required && !hasDefault) {
throw new MissRequiredFieldException("Field required not hasField");
}
return false;
}
protected abstract void serializeFieldInternal(T bean, ProtoBufWriter protoBufWriter)
throws IllegalAccessException, IOException, IllegalDataException;
@Override
public void deSerializeField(T bean, ProtoBufReader protoBufReader,
ExtensionRegistry extensionRegistry)
throws IllegalAccessException, IOException, IllegalDataException {
if (hasField(bean)) {
// has set a value
throw new IllegalDataException("Multi value for single field found");
}
deSerializeFieldInternal(bean, protoBufReader);
helperField.setBoolean(bean, true);
}
protected abstract void deSerializeFieldInternal(T bean, ProtoBufReader protoBufReader)
throws IOException, IllegalAccessException;
}