@SuppressWarnings("unchecked")
    private static void writeArrayTo(Output output, Object value, Schema<?> currentSchema,
            IdStrategy strategy, final Class<Object> clazz) throws IOException
    {
        final Class<?> componentType = clazz.getComponentType();
        
        final HasDelegate<Object> hdArray = strategy.tryWriteDelegateIdTo(output,
                ID_ARRAY_DELEGATE, (Class<Object>) componentType);

        if (hdArray != null)
        {
            if (output instanceof StatefulOutput)
            {
                // update using the derived schema.
                ((StatefulOutput) output).updateLast(hdArray.genericElementSchema,
                        currentSchema);
            }

            hdArray.genericElementSchema.writeTo(output, value);
            return;
        }
        
        final RuntimeFieldFactory<?> inlineArray = RuntimeFieldFactory.getInline(
                componentType);
        if (inlineArray != null)
        {
            // scalar
            final boolean primitive = componentType.isPrimitive();
            final ArraySchemas.Base arraySchema = ArraySchemas.getSchema(
                    inlineArray.id, primitive, strategy);

            output.writeUInt32(ID_ARRAY_SCALAR,
                    ArraySchemas.toArrayId(inlineArray.id, primitive),
                    false);

            if (output instanceof StatefulOutput)
            {
                // update using the derived schema.
                ((StatefulOutput) output).updateLast(arraySchema, currentSchema);
            }

            arraySchema.writeTo(output, value);
            return;
        }
        
        Class<?> c = componentType;
        if (c.isArray())
        {
            int dimensions = 2;
            c = c.getComponentType();
            while (c.isArray())
            {
                dimensions++;
                c = c.getComponentType();
            }
            
            writeComponentTo(output, value, currentSchema, strategy, c, dimensions);
            return;
        }
        
        if (componentType.isEnum() || 
                (null != (c = componentType.getSuperclass()) && c.isEnum()))
        {
            // enum
            final EnumIO<?> eio = strategy.getEnumIO(c);

            strategy.writeEnumIdTo(output, ID_ARRAY_ENUM, c);

            if (output instanceof StatefulOutput)
            {
                // update using the derived schema.
                ((StatefulOutput) output).updateLast(eio.genericElementSchema,
                        currentSchema);
            }

            eio.genericElementSchema.writeTo(output, value);
            return;
        }
        
        if (Object.class == componentType || Class.class == componentType)
        {
            writeComponentTo(output, value, currentSchema, strategy, componentType, 1);
            return;
        }
        
        HasSchema<Object> hs = strategy.tryWritePojoIdTo(output,
                ID_ARRAY_POJO, (Class<Object>) componentType, false);
        
        if (hs != null)
        {
            if (output instanceof StatefulOutput)
            {
                // update using the derived schema.
                ((StatefulOutput) output).updateLast(hs.genericElementSchema,
                        currentSchema);
            }

            hs.genericElementSchema.writeTo(output, value);
            return;
        }
        
        if (componentType.isInterface() || 
                Modifier.isAbstract(componentType.getModifiers()) ||
                !isComponentPojo(componentType))
        {
            writeComponentTo(output, value, currentSchema, strategy, componentType, 1);
            return;
        }
        
        // pojo
        hs = strategy.writePojoIdTo(output,
                ID_ARRAY_POJO, (Class<Object>) componentType);
        
        if (output instanceof StatefulOutput)
        {
            // update using the derived schema.
            ((StatefulOutput) output).updateLast(hs.genericElementSchema,
                    currentSchema);
        }
        
        hs.genericElementSchema.writeTo(output, value);
    }
A schema for dynamic types (fields where the type is {@link Object}).

@author David Yu
@created Feb 1, 2011
protostuff-runtime/src/main/java/io/protostuff/runtime/ObjectSchema.java