Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //========================================================================
  //Copyright 2007-2011 David Yu dyuproject@gmail.com
  //------------------------------------------------------------------------
  //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 io.protostuff.runtime;
 
 
Static utility for creating runtime java.util.Collection fields.

Author(s):
David Yu
Created:
Jan 26, 2011
 
 {
 
     private RuntimeCollectionFieldFactory()
     {
     }

    
For lazy initialization called by RuntimeFieldFactory.
 
     {
         return ;
     }
 
     /*
      * private static final DerivativeSchema POLYMORPHIC_COLLECTION_VALUE_SCHEMA = new DerivativeSchema() {
      * 
      * @SuppressWarnings("unchecked") protected void doMergeFrom(Input input, Schema<Object> derivedSchema, Object
      * owner) throws IOException { final Object value = derivedSchema.newMessage();
      * 
      * // the owner will always be a Collection ((Collection<Object>)owner).add(value);
      * 
      * if(input instanceof GraphInput) { // update the actual reference. ((GraphInput)input).updateLast(value, owner); }
      * 
      * derivedSchema.mergeFrom(input, value); } };
      */
 
     /*
      * private static final ObjectSchema OBJECT_COLLECTION_VALUE_SCHEMA = new ObjectSchema() {
      * 
      * @SuppressWarnings("unchecked") protected void setValue(Object value, Object owner) { // the owner will always be
      * a Collection ((Collection<Object>)owner).add(value); } };
      */
 
     private static <T> Field<T> createCollectionInlineV(int number,
             String namefinal java.lang.reflect.Field f,
             MessageFactory messageFactoryfinal Delegate<Objectinline)
     {
         return new RuntimeCollectionField<T, Object>(inline.getFieldType(),
                 numbernamef.getAnnotation(Tag.class), messageFactory)
         {
             {
                 f.setAccessible(true);
             }
 
             @Override
             @SuppressWarnings("unchecked")
             protected void mergeFrom(Input input, T messagethrows IOException
             {
                 try
                 {
                     f.set(messageinput.mergeObject(
                             (Collection<Object>) f.get(message), ));
                 }
                 catch (IllegalArgumentException | IllegalAccessException e)
                 {
                     throw new RuntimeException(e);
                 }
             }
            @Override
            @SuppressWarnings("unchecked")
            protected void writeTo(Output output, T messagethrows IOException
            {
                final Collection<Objectexisting;
                try
                {
                    existing = (Collection<Object>) f.get(message);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
                if (existing != null)
                    output.writeObject(existingfalse);
            }
            @Override
            protected void transfer(Pipe pipeInput inputOutput output,
                    boolean repeatedthrows IOException
            {
                output.writeObject(pipe.repeated);
            }
            @Override
            protected void addValueFrom(Input input,
                    Collection<Objectcollectionthrows IOException
            {
                collection.add(inline.readFrom(input));
            }
            @Override
            protected void writeValueTo(Output outputint fieldNumber,
                    Object valueboolean repeatedthrows IOException
            {
                inline.writeTo(outputfieldNumbervaluerepeated);
            }
            @Override
            protected void transferValue(Pipe pipeInput inputOutput output,
                    int numberboolean repeatedthrows IOException
            {
                inline.transfer(pipeinputoutputnumberrepeated);
            }
        };
    }
    private static <T> Field<T> createCollectionEnumV(int numberString name,
            final java.lang.reflect.Field fMessageFactory messageFactory,
            Class<ObjectgenericTypeIdStrategy strategy)
    {
        final EnumIO<?> eio = strategy.getEnumIO(genericType);
        return new RuntimeCollectionField<T, Enum<?>>(.number,
                namef.getAnnotation(Tag.class), messageFactory)
        {
            {
                f.setAccessible(true);
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void mergeFrom(Input input, T messagethrows IOException
            {
                try
                {
                    f.set(messageinput.mergeObject(
                            (Collection<Enum<?>>) f.get(message), ));
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void writeTo(Output output, T messagethrows IOException
            {
                final Collection<Enum<?>> existing;
                try
                {
                    existing = (Collection<Enum<?>>) f.get(message);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
                if (existing != null)
                    output.writeObject(existingfalse);
            }
            @Override
            protected void transfer(Pipe pipeInput inputOutput output,
                    boolean repeatedthrows IOException
            {
                output.writeObject(pipe.repeated);
            }
            @Override
            protected void addValueFrom(Input input,
                    Collection<Enum<?>> collectionthrows IOException
            {
                collection.add(eio.readFrom(input));
            }
            @Override
            protected void writeValueTo(Output outputint fieldNumber,
                    Enum<?> valueboolean repeatedthrows IOException
            {
                eio.writeTo(outputfieldNumberrepeatedvalue);
            }
            @Override
            protected void transferValue(Pipe pipeInput inputOutput output,
                    int numberboolean repeatedthrows IOException
            {
                EnumIO.transfer(pipeinputoutputnumberrepeated);
            }
        };
    }
    private static <T> Field<T> createCollectionPojoV(int numberString name,
            final java.lang.reflect.Field fMessageFactory messageFactory,
            Class<ObjectgenericTypeIdStrategy strategy)
    {
        final HasSchema<ObjectschemaV = strategy.getSchemaWrapper(
                genericTypetrue);
        return new RuntimeCollectionField<T, Object>(.number,
                namef.getAnnotation(Tag.class), messageFactory)
        {
            {
                f.setAccessible(true);
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void mergeFrom(Input input, T messagethrows IOException
            {
                try
                {
                    f.set(messageinput.mergeObject(
                            (Collection<Object>) f.get(message), ));
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void writeTo(Output output, T messagethrows IOException
            {
                final Collection<Objectexisting;
                try
                {
                    existing = (Collection<Object>) f.get(message);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
                if (existing != null)
                    output.writeObject(existingfalse);
            }
            @Override
            protected void transfer(Pipe pipeInput inputOutput output,
                    boolean repeatedthrows IOException
            {
                output.writeObject(pipe.repeated);
            }
            @Override
            protected void addValueFrom(Input input,
                    Collection<Objectcollectionthrows IOException
            {
                collection.add(input.mergeObject(nullschemaV.getSchema()));
            }
            @Override
            protected void writeValueTo(Output outputint fieldNumber,
                    Object valueboolean repeatedthrows IOException
            {
                output.writeObject(fieldNumbervalueschemaV.getSchema(),
                        repeated);
            }
            @Override
            protected void transferValue(Pipe pipeInput inputOutput output,
                    int numberboolean repeatedthrows IOException
            {
                output.writeObject(numberpipeschemaV.getPipeSchema(),
                        repeated);
            }
        };
    }
    private static <T> Field<T> createCollectionPolymorphicV(int number,
            String namefinal java.lang.reflect.Field f,
            MessageFactory messageFactoryClass<ObjectgenericType,
            final IdStrategy strategy)
    {
        return new RuntimeCollectionField<T, Object>(.number,
                namef.getAnnotation(Tag.class), messageFactory)
        {
            {
                f.setAccessible(true);
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void mergeFrom(Input input, T messagethrows IOException
            {
                try
                {
                    f.set(messageinput.mergeObject(
                            (Collection<Object>) f.get(message), ));
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void writeTo(Output output, T messagethrows IOException
            {
                final Collection<Objectexisting;
                try
                {
                    existing = (Collection<Object>) f.get(message);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
                if (existing != null)
                    output.writeObject(existingfalse);
            }
            @Override
            protected void transfer(Pipe pipeInput inputOutput output,
                    boolean repeatedthrows IOException
            {
                output.writeObject(pipe.repeated);
            }
            @Override
            protected void addValueFrom(Input input,
                    Collection<Objectcollectionthrows IOException
            {
                final Object value = input.mergeObject(collection,
                        strategy.POLYMORPHIC_POJO_ELEMENT_SCHEMA);
                if (input instanceof GraphInput
                        && ((GraphInputinput).isCurrentMessageReference())
                {
                    collection.add(value);
                }
            }
            @Override
            protected void writeValueTo(Output outputint fieldNumber,
                    Object valueboolean repeatedthrows IOException
            {
                output.writeObject(fieldNumbervalue,
                        strategy.POLYMORPHIC_POJO_ELEMENT_SCHEMArepeated);
            }
            @Override
            protected void transferValue(Pipe pipeInput inputOutput output,
                    int numberboolean repeatedthrows IOException
            {
                output.writeObject(numberpipe,
                        strategy.POLYMORPHIC_POJO_ELEMENT_SCHEMA.pipeSchema,
                        repeated);
            }
        };
    }
    private static <T> Field<T> createCollectionObjectV(int number,
            String namefinal java.lang.reflect.Field f,
            MessageFactory messageFactoryfinal Schema<ObjectvalueSchema,
            final Pipe.Schema<ObjectvaluePipeSchemafinal IdStrategy strategy)
    {
        return new RuntimeCollectionField<T, Object>(.number,
                namef.getAnnotation(Tag.class), messageFactory)
        {
            {
                f.setAccessible(true);
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void mergeFrom(Input input, T messagethrows IOException
            {
                try
                {
                    f.set(messageinput.mergeObject(
                            (Collection<Object>) f.get(message), ));
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void writeTo(Output output, T messagethrows IOException
            {
                final Collection<Objectexisting;
                try
                {
                    existing = (Collection<Object>) f.get(message);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
                if (existing != null)
                    output.writeObject(existingfalse);
            }
            @Override
            protected void transfer(Pipe pipeInput inputOutput output,
                    boolean repeatedthrows IOException
            {
                output.writeObject(pipe.repeated);
            }
            @Override
            protected void addValueFrom(Input input,
                    Collection<Objectcollectionthrows IOException
            {
                final Object value = input.mergeObject(collectionvalueSchema);
                if (input instanceof GraphInput
                        && ((GraphInputinput).isCurrentMessageReference())
                {
                    collection.add(value);
                }
            }
            @Override
            protected void writeValueTo(Output outputint fieldNumber,
                    Object valueboolean repeatedthrows IOException
            {
                output.writeObject(fieldNumbervaluevalueSchemarepeated);
            }
            @Override
            protected void transferValue(Pipe pipeInput inputOutput output,
                    int numberboolean repeatedthrows IOException
            {
                output.writeObject(numberpipevaluePipeSchemarepeated);
            }
        };
    }
    private static final RuntimeFieldFactory<Collection<?>> COLLECTION = new RuntimeFieldFactory<Collection<?>>(
            .)
    {
        @Override
        @SuppressWarnings("unchecked")
        public <T> Field<T> create(int numberString name,
                final java.lang.reflect.Field fIdStrategy strategy)
        {
            final Class<?> clazz = f.getType();
            if (Modifier.isAbstract(clazz.getModifiers()))
            {
                if (!clazz.isInterface())
                {
                    // abstract class
                    return .create(numbernamefstrategy);
                }
                final Morph morph = f.getAnnotation(Morph.class);
                if (morph == null)
                {
                    if (.)
                        return .create(numbernamefstrategy);
                }
                else if (morph.value())
                    return .create(numbernamefstrategy);
            }
            if (EnumSet.class.isAssignableFrom(f.getType()))
            {
                final Class<ObjectenumType = (Class<Object>) getGenericType(
                        f, 0);
                if (enumType == null)
                {
                    // still handle the serialization of EnumSets even without
                    // generics
                    return ..create(numbernamef,
                            strategy);
                }
                // TODO optimize
                return createCollectionEnumV(numbernamefstrategy
                        .getEnumIO(enumType).getEnumSetFactory(), enumType,
                        strategy);
            }
            final MessageFactory messageFactory = strategy
                    .getCollectionFactory(f.getType());
            final Class<ObjectgenericType = (Class<Object>) getGenericType(f,
                    0);
            if (genericType == null)
            {
                // the value is not a simple parameterized type.
                return createCollectionObjectV(numbernamefmessageFactory,
                        strategy.OBJECT_ELEMENT_SCHEMA,
                        strategy.OBJECT_ELEMENT_SCHEMA.pipeSchemastrategy);
            }
            final Delegate<Objectinline = getDelegateOrInline(genericType,
                    strategy);
            if (inline != null)
                return createCollectionInlineV(numbernamefmessageFactory,
                        inline);
            if (Message.class.isAssignableFrom(genericType))
                return createCollectionPojoV(numbernamefmessageFactory,
                        genericTypestrategy);
            if (genericType.isEnum())
                return createCollectionEnumV(numbernamefmessageFactory,
                        genericTypestrategy);
            final PolymorphicSchema ps = PolymorphicSchemaFactories
                    .getSchemaFromCollectionOrMapGenericType(genericType,
                            strategy);
            if (ps != null)
            {
                return createCollectionObjectV(numbernamefmessageFactory,
                        psps.getPipeSchema(), strategy);
            }
            if (pojo(genericTypef.getAnnotation(Morph.class), strategy))
                return createCollectionPojoV(numbernamefmessageFactory,
                        genericTypestrategy);
            if (genericType.isInterface())
            {
                return createCollectionObjectV(numbernamefmessageFactory,
                        strategy.OBJECT_ELEMENT_SCHEMA,
                        strategy.OBJECT_ELEMENT_SCHEMA.pipeSchemastrategy);
            }
            return createCollectionPolymorphicV(numbernamef,
                    messageFactorygenericTypestrategy);
        }
        @Override
        public void transfer(Pipe pipeInput inputOutput outputint number,
                boolean repeatedthrows IOException
        {
            throw new UnsupportedOperationException();
        }
        @Override
        public Collection<?> readFrom(Input inputthrows IOException
        {
            throw new UnsupportedOperationException();
        }
        @Override
        public void writeTo(Output outputint numberCollection<?> value,
                boolean repeatedthrows IOException
        {
            throw new UnsupportedOperationException();
        }
        @Override
        public FieldType getFieldType()
        {
            throw new UnsupportedOperationException();
        }
        @Override
        public Class<?> typeClass()
        {
            throw new UnsupportedOperationException();
        }
    };
New to GrepCode? Check out our FAQ X