io.protostuff.Tag Java Examples

The following examples show how to use io.protostuff.Tag. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RuntimeDerivativeField.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public RuntimeDerivativeField(Class<Object> typeClass, FieldType type,
        int number, String name, boolean repeated, Tag tag,
        IdStrategy strategy)
{
    super(type, number, name, repeated, tag);
    this.typeClass = typeClass;

    schema = new DerivativeSchema(strategy)
    {
        @Override
        protected void doMergeFrom(Input input,
                Schema<Object> derivedSchema, Object owner)
                throws IOException
        {
            RuntimeDerivativeField.this.doMergeFrom(input, derivedSchema,
                    owner);
        }
    };
}
 
Example #2
Source File: EnumIO.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public EnumIO(Class<E> enumClass, IdStrategy strategy)
{
    this.enumClass = enumClass;
    this.strategy = strategy;
    genericElementSchema = new ArraySchemas.EnumArray(strategy, null, this);
    
    Field[] fields = enumClass.getFields();
    int n = fields.length;
    alias = new String[n];
    tag = new int[n];
    valueByAliasMap = new HashMap<String, E>(n * 2);
    valueByTagMap = new HashMap<Integer, E>(n * 2);
    for (E instance : enumClass.getEnumConstants())
    {
        int ordinal = instance.ordinal();
        try
        {
            Field field = enumClass.getField(instance.name());
            if (field.isAnnotationPresent(Tag.class))
            {
                Tag annotation = field.getAnnotation(Tag.class);
                tag[ordinal] = annotation.value();
                alias[ordinal] = annotation.alias();
                valueByTagMap.put(annotation.value(), instance);
                valueByAliasMap.put(annotation.alias(), instance);
            }
            else
            {
                tag[ordinal] = ordinal;
                alias[ordinal] = field.getName();
                valueByTagMap.put(ordinal, instance);
                valueByAliasMap.put(field.getName(), instance);
            }
        }
        catch (NoSuchFieldException e)
        {
            throw new IllegalStateException(e);
        }
    }
}
 
Example #3
Source File: Field.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public Field(WireFormat.FieldType type, int number, String name, boolean repeated,
        Tag tag)
{
    this.type = type;
    this.number = number;
    this.name = name;
    this.repeated = repeated;
    this.groupFilter = tag == null ? 0 : tag.groupFilter();
    // this.tag = tag;
}
 
Example #4
Source File: RuntimeMessageField.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public RuntimeMessageField(Class<P> typeClass, HasSchema<P> hasSchema,
        FieldType type, int number, String name, boolean repeated, Tag tag)
{
    super(type, number, name, repeated, tag);
    this.typeClass = typeClass;
    this.hasSchema = hasSchema;
}
 
Example #5
Source File: RuntimeCollectionField.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public RuntimeCollectionField(FieldType type, int number, String name,
        Tag tag, MessageFactory messageFactory, boolean allowNullElement)
{
    super(type, number, name, false, tag);
    schema = new CollectionSchema<V>(messageFactory, allowNullElement)
    {
        @Override
        protected void addValueFrom(Input input, Collection<V> collection)
                throws IOException
        {
            RuntimeCollectionField.this.addValueFrom(input, collection);
        }

        @Override
        protected void writeValueTo(Output output, int fieldNumber,
                V value, boolean repeated) throws IOException
        {
            RuntimeCollectionField.this.writeValueTo(output, fieldNumber,
                    value, repeated);
        }

        @Override
        protected void transferValue(Pipe pipe, Input input, Output output,
                int number, boolean repeated) throws IOException
        {
            RuntimeCollectionField.this.transferValue(pipe, input, output,
                    number, repeated);
        }
    };
}
 
Example #6
Source File: RuntimeObjectField.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public RuntimeObjectField(Class<?> typeClass, FieldType type, int number,
        String name, boolean repeated, Tag tag,
        PolymorphicSchema.Factory factory, IdStrategy strategy)
{
    super(type, number, name, repeated, tag);

    schema = factory.newSchema(typeClass, strategy, this);
}
 
Example #7
Source File: RuntimeMapField.java    From protostuff with Apache License 2.0 4 votes vote down vote up
public RuntimeMapField(FieldType type, int number, String name, Tag tag,
        MessageFactory messageFactory)
{
    super(type, number, name, false, tag);
    schema = new MapSchema<K, V>(messageFactory)
    {
        @Override
        protected K readKeyFrom(Input input, MapWrapper<K, V> wrapper)
                throws IOException
        {
            return kFrom(input, wrapper);
        }

        @Override
        protected void putValueFrom(Input input, MapWrapper<K, V> wrapper,
                K key) throws IOException
        {
            vPutFrom(input, wrapper, key);
        }

        @Override
        protected void writeKeyTo(Output output, int fieldNumber, K key,
                boolean repeated) throws IOException
        {
            kTo(output, fieldNumber, key, repeated);
        }

        @Override
        protected void writeValueTo(Output output, int fieldNumber, V val,
                boolean repeated) throws IOException
        {
            vTo(output, fieldNumber, val, repeated);
        }

        @Override
        protected void transferKey(Pipe pipe, Input input, Output output,
                int number, boolean repeated) throws IOException
        {
            kTransfer(pipe, input, output, number, repeated);
        }

        @Override
        protected void transferValue(Pipe pipe, Input input, Output output,
                int number, boolean repeated) throws IOException
        {
            vTransfer(pipe, input, output, number, repeated);
        }
    };
}
 
Example #8
Source File: Field.java    From protostuff with Apache License 2.0 4 votes vote down vote up
public Field(WireFormat.FieldType type, int number, String name, Tag tag)
{
    this(type, number, name, false, tag);
}
 
Example #9
Source File: RuntimeSchema.java    From protostuff with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a schema from the given class with the exclusion of certain fields.
 */
public static <T> RuntimeSchema<T> createFrom(Class<T> typeClass,
        Set<String> exclusions, IdStrategy strategy)
{
    if (typeClass.isInterface()
            || Modifier.isAbstract(typeClass.getModifiers()))
    {
        throw new RuntimeException(
                "The root object can neither be an abstract "
                        + "class nor interface: \"" + typeClass.getName());
    }

    final Map<String, java.lang.reflect.Field> fieldMap = findInstanceFields(typeClass);
    final ArrayList<Field<T>> fields = new ArrayList<Field<T>>(
            fieldMap.size());
    int i = 0;
    boolean annotated = false;
    for (java.lang.reflect.Field f : fieldMap.values())
    {
        if (!exclusions.contains(f.getName()))
        {
            if (f.getAnnotation(Deprecated.class) != null)
            {
                // this field should be ignored by ProtoStuff.
                // preserve its field number for backward-forward compat
                i++;
                continue;
            }

            final Tag tag = f.getAnnotation(Tag.class);
            final int fieldMapping;
            final String name;
            if (tag == null)
            {
                // Fields gets assigned mapping tags according to their
                // definition order
                if (annotated)
                {
                    String className = typeClass.getCanonicalName();
                    String fieldName = f.getName();
                    String message = String.format("%s#%s is not annotated with @Tag", className, fieldName);
                    throw new RuntimeException(message);
                }
                fieldMapping = ++i;

                name = f.getName();
            }
            else
            {
                // Fields gets assigned mapping tags according to their
                // annotation
                if (!annotated && !fields.isEmpty())
                {
                    throw new RuntimeException(
                            "When using annotation-based mapping, "
                                    + "all fields must be annotated with @"
                                    + Tag.class.getSimpleName());
                }
                annotated = true;
                fieldMapping = tag.value();

                if (fieldMapping < MIN_TAG_VALUE || fieldMapping > MAX_TAG_VALUE)
                {
                    throw new IllegalArgumentException(ERROR_TAG_VALUE + ": " + fieldMapping + " on " + typeClass);
                }

                name = tag.alias().isEmpty() ? f.getName() : tag.alias();
            }

            final Field<T> field = RuntimeFieldFactory.getFieldFactory(
                    f.getType(), strategy).create(fieldMapping, name, f,
                    strategy);
            fields.add(field);
        }
    }

    return new RuntimeSchema<T>(typeClass, fields,
            RuntimeEnv.newInstantiator(typeClass));
}