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

net.dongliu.prettypb.runtime.ExtensionRegistry Maven / Gradle / Ivy

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

import net.dongliu.prettypb.runtime.include.ExtensionField;
import net.dongliu.prettypb.runtime.include.ProtoBean;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * if want to deSerialize extension field, should use ExtensionRegistry to register first
 * this is not thread-safe
 *
 * @author Dong Liu
 */
public class ExtensionRegistry {
    private Map map;

    public ExtensionRegistry() {
        map = new HashMap<>();
    }

    /**
     * register all extension field declared in this class
     */
    public void addAll(Class clazz) {
        Field[] declaredFields = clazz.getDeclaredFields();
        List extensionFields = new ArrayList<>();
        for (Field field : declaredFields) {
            if (Modifier.isStatic(field.getModifiers())
                    && field.getType().equals(ExtensionField.class)) {
                extensionFields.add(field);
            }
        }

        if (extensionFields.isEmpty()) {
            throw new IllegalArgumentException(clazz.getName() + " has no extension fields");
        }

        for (Field field : extensionFields) {
            field.setAccessible(true);
            ExtensionField ef;
            try {
                ef = (ExtensionField) field.get(null);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            add(ef);
        }
    }

    /**
     * register this field
     *
     * @param field
     */
    public  void add(ExtensionField field) {
        if (field.getNumber() < 1) {
            //should not happen
            throw new IllegalArgumentException("not valid tag: " + field);
        }
        String extendee = field.getExtendee();
        SingleMessageExtension se = map.get(extendee);
        if (se == null) {
            se = new SingleMessageExtension();
            map.put(extendee, se);
        }
        se.add(field);
    }

    public  boolean registered(ExtensionField field) {
        SingleMessageExtension se = map.get(field.getExtendee());
        if (se == null) {
            return false;
        }
        return se.registered(field);
    }

    public  ExtensionField getRegistered(ProtoBean protoBean, int number) {
        String fullName;
        if (protoBean.protoPackage().isEmpty()) {
            fullName = protoBean.name();
        } else {
            fullName = protoBean.protoPackage() + "." + protoBean.name();
        }
        SingleMessageExtension se = map.get(fullName);
        if (se == null) {
            return null;
        }
        return se.getRegistered(number);
    }
}

class SingleMessageExtension {
    private Map map = new HashMap<>();

    public  void add(ExtensionField field) {
        map.put(field.getNumber(), field);
    }

    public  boolean registered(ExtensionField field) {
        return this.map.containsKey(field.getNumber());
    }

    public List getAllRegistries() {
        return new ArrayList<>(this.map.values());
    }

    public  ExtensionField getRegistered(int number) {
        return map.get(number);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy