/**
 * Copyright © 2010-2013 Nokia
 *
 * 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 org.jsonschema2pojo.util;

import android.os.Parcel;
import android.os.Parcelable.Creator;

import com.sun.codemodel.JClass;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JVar;

public class ParcelableHelper {

    public void addWriteToParcel(JDefinedClass jclass) {
        JMethod method = jclass.method(JMod.PUBLIC, void.class, "writeToParcel");
        JVar dest = method.param(Parcel.class, "dest");
        method.param(int.class, "flags");
        
        for (JFieldVar f : jclass.fields().values()) {
            method.body().invoke(dest, "writeValue").arg(f);
        }
    }
    
    public void addDescribeContents(JDefinedClass jclass) {
        JMethod method = jclass.method(JMod.PUBLIC, int.class, "describeContents");
        method.body()._return(JExpr.lit(0));
    }
    
    public void addCreator(JDefinedClass jclass) {
        JClass creatorType = jclass.owner().ref(Creator.class).narrow(jclass); 
        JDefinedClass creatorClass = jclass.owner().anonymousClass(creatorType);
        
        addCreateFromParcel(jclass, creatorClass);
        addNewArray(jclass, creatorClass);
        
        JFieldVar creatorField = jclass.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, creatorType, "CREATOR");
        creatorField.init(JExpr._new(creatorClass));
    }

    private void addNewArray(JDefinedClass jclass, JDefinedClass creatorClass) {
        JMethod newArray = creatorClass.method(JMod.PUBLIC, jclass.array(), "newArray");
        newArray.param(int.class, "size");
        newArray.body()._return(JExpr.direct("new " + jclass.name() + "[size]"));
    }

    private void addCreateFromParcel(JDefinedClass jclass, JDefinedClass creatorClass) {
        JMethod createFromParcel = creatorClass.method(JMod.PUBLIC, jclass, "createFromParcel");
        JVar in = createFromParcel.param(Parcel.class, "in");
        JVar instance = createFromParcel.body().decl(jclass, "instance", JExpr._new(jclass));
        for (JFieldVar f : jclass.fields().values()) {
            createFromParcel.body().assign(instance.ref(f), JExpr.cast(f.type(), in.invoke("readValue").arg(JExpr.direct(f.type().erasure().name() + ".class.getClassLoader()"))));
        }
        createFromParcel.body()._return(instance);
    }
    
}

@@ -1256,32 +1256,159 @@
 s().values()) %7B%0A
+            if (f.type().isArray()) %7B%0A                method.body().invoke(dest, %22writeList%22).arg(f);%0A            %7D else %7B%0A    
             meth
@@ -1444,32 +1444,46 @@
 Value%22).arg(f);%0A
+            %7D%0A
         %7D%0A    %7D%0A
@@ -2897,76 +2897,487 @@
     
-createFromParcel.body().assign(instance.ref(f), JExpr.cast(f.type(),
+if (f.type().isArray()) %7B%0A                createFromParcel.body()%0A                        .invoke(in, %22readList%22)%0A                        .arg(instance.ref(f))%0A                        .arg(JExpr.direct(f.type().erasure().name() + %22.class.getClassLoader()%22));%0A             %7D else %7B%0A                createFromParcel.body().assign(%0A                        instance.ref(f),%0A                        JExpr.cast(%0A                                f.type(),%0A                               
  in.
@@ -3468,19 +3468,76 @@
 der()%22))
-));
+%0A                        )%0A                );%0A            %7D%0A
 %0A       

FIX #354 `android.os.BadParcelableException: ClassNotFoundException when unmarshalling` Error Parcelable codes