/*
 * Copyright 2010 Martin Grotzke
 *
 * 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 &quot;AS IS&quot; 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 com.twitter.chill.java;

import com.twitter.chill.IKryoRegistrar;
import com.twitter.chill.SingleRegistrar;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

/**
 * A kryo {@link Serializer} for lists created via {@link Arrays#asList(Object...)}.
 * <p>
 * Note: This serializer does not support cyclic references, so if one of the objects
 * gets set the list as attribute this might cause an error during deserialization.
 * </p>
 *
 * @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
 */
public class ArraysAsListSerializer extends Serializer<List<?>> {

    static public IKryoRegistrar registrar() {
      return new SingleRegistrar(Arrays.asList("").getClass(), new ArraysAsListSerializer());
    }

    private Field _arrayField;

    public ArraysAsListSerializer() {
        try {
            _arrayField = Class.forName( "java.util.Arrays$ArrayList" ).getDeclaredField( "a" );
            _arrayField.setAccessible( true );
        } catch ( final Exception e ) {
            throw new RuntimeException( e );
        }
    }

    @Override
    public List<?> read(final Kryo kryo, final Input input, final Class<List<?>> type) {
        final int length = input.readInt(true);
        final Class<?> componentType = kryo.readClass( input ).getType();
        try {
            final Object[] items = (Object[]) Array.newInstance( componentType, length );
            for( int i = 0; i < length; i++ ) {
                items[i] = kryo.readClassAndObject( input );
            }
            return Arrays.asList( items );
        } catch ( final Exception e ) {
            throw new RuntimeException( e );
        }
    }

    @Override
    public void write(final Kryo kryo, final Output output, final List<?> obj) {
        try {
            final Object[] array = (Object[]) _arrayField.get( obj );
            output.writeInt(array.length, true);
            final Class<?> componentType = array.getClass().getComponentType();
            kryo.writeClass( output, componentType );
            for( final Object item : array ) {
                kryo.writeClassAndObject( output, item );
            }
        } catch ( final RuntimeException e ) {
             // Don't eat and wrap RuntimeExceptions because the ObjectBuffer.write...
             // handles SerializationException specifically (resizing the buffer)...
             throw e;
        } catch ( final Exception e ) {
             throw new RuntimeException( e );
        }
    }
}

@@ -2175,18 +2175,16 @@
 ject
-%5B%5D
  items =
  (Ob
@@ -2179,27 +2179,16 @@
  items =
- (Object%5B%5D)
  Array.n
@@ -2198,17 +2198,16 @@
 nstance(
- 
 componen
@@ -2219,17 +2219,16 @@
 , length
- 
 );%0A     
@@ -2237,18 +2237,18 @@
      for
-(
  
+(
 int i = 
@@ -2265,17 +2265,16 @@
 gth; i++
- 
 ) %7B%0A    
@@ -2289,18 +2289,27 @@
     
-items%5Bi%5D =
+Array.set(items, i,
  kry
@@ -2329,23 +2329,22 @@
 dObject(
- 
 input
- 
+)
 );%0A     
@@ -2385,23 +2385,21 @@
 .asList(
- 
 items
- 
 );%0A     

Fix issue #255