All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.prettypb.runtime.code.ProtoFieldInfo Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
package net.dongliu.prettypb.runtime.code;

import net.dongliu.prettypb.runtime.ExtensionRegistry;
import net.dongliu.prettypb.runtime.exception.IllegalBeanException;
import net.dongliu.prettypb.runtime.exception.IllegalDataException;
import net.dongliu.prettypb.runtime.exception.MissRequiredFieldException;
import net.dongliu.prettypb.runtime.include.ProtoField;
import net.dongliu.prettypb.runtime.utils.ProtoUtils;

import java.io.IOException;
import java.lang.reflect.Field;

/**
 * infos about proto value's field
 *
 * @author Dong Liu
 */
abstract public class ProtoFieldInfo {

    ProtoField protoField;

    protected Field field;

    protected Class clazz;

    ProtoFieldInfo(Field field, ProtoField protoField) {
        this.protoField = protoField;
        this.field = field;
        this.field.setAccessible(true);
        this.clazz = this.field.getType();
    }

    /**
     * get proto filed method, factory
     */
    public static  ProtoFieldInfo getProtoFieldInfo(Class clazz, Field field,
                                                       ProtoField protoField)
            throws IllegalBeanException {
        if (protoField.repeated()) {
            return ListProtoFieldInfo.getList(field, protoField);
        } else if (ProtoUtils.isPrimitive(protoField.type())) {
            Field helperField;
            try {
                helperField = clazz.getDeclaredField("_" + field.getName());
            } catch (NoSuchFieldException e) {
                throw new IllegalBeanException("No helper field for:" + field.getName());
            }
            return PrimitiveProtoFieldInfo.getPrimitive(field, helperField, protoField);
        } else {
            Field helperField;
            try {
                helperField = clazz.getDeclaredField("_" + field.getName());
            } catch (NoSuchFieldException e) {
                throw new IllegalBeanException("No helper field for:" + field.getName());
            }
            return ObjectProtoFieldInfo.getObject(field, helperField, protoField);
        }
    }

    public boolean required() {
        return protoField.required();
    }

    public boolean packed() {
        return protoField.packed();
    }

    public boolean repeated() {
        return protoField.repeated();
    }

    public int idx() {
        return protoField.idx();
    }

    public boolean hasDefault() {
        return protoField.hasDefault();
    }

    /**
     * if is a primitive type
     */
    abstract public boolean primitive();

    /**
     * if this filed hasField in bean
     */
    abstract public  boolean hasField(T bean) throws IllegalAccessException;

    public String getName() {
        return field.getName();
    }

    /**
     * serializer field
     *
     * @param bean
     * @param protoBufWriter
     * @param 
     * @throws IllegalAccessException
     * @throws IOException
     */
    public abstract  void serializeField(T bean, ProtoBufWriter protoBufWriter)
            throws IllegalAccessException, IOException, IllegalBeanException,
            MissRequiredFieldException, IllegalDataException;

    /**
     * deserializer field
     *
     * @param bean
     * @param protoBufReader
     * @param 
     */
    public abstract  void deSerializeField(T bean, ProtoBufReader protoBufReader,
                                              ExtensionRegistry extensionRegistry)
            throws IllegalAccessException, IOException, IllegalDataException;

    @Override
    public String toString() {
        return getName() + "idx(" + idx() + ")";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy