package com.alibaba.fastjson.parser.deserializer;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.util.FieldInfo;

public abstract class FieldDeserializer {

    public final FieldInfo fieldInfo;

    protected final Class<?>  clazz;

    public FieldDeserializer(Class<?> clazz, FieldInfo fieldInfo){
        this.clazz = clazz;
        this.fieldInfo = fieldInfo;
    }
    
    public abstract void parseField(DefaultJSONParser parser, Object object, Type objectType,
                                    Map<String, Object> fieldValues);

    public int getFastMatchToken() {
        return 0;
    }

    public void setValue(Object object, boolean value) {
        setValue(object, Boolean.valueOf(value));
    }

    public void setValue(Object object, int value) {
        setValue(object, Integer.valueOf(value));
    }

    public void setValue(Object object, long value) {
        setValue(object, Long.valueOf(value));
    }

    public void setValue(Object object, String value) {
        setValue(object, (Object) value);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void setValue(Object object, Object value) {
        if (value == null) {
            Class<?> fieldClass = fieldInfo.fieldClass;
            if (fieldClass == byte.class //
                    || fieldClass == short.class //
                    || fieldClass == int.class //
                    || fieldClass == long.class //
                    || fieldClass == float.class //
                    || fieldClass == double.class //
                    || fieldClass == boolean.class //
                    || fieldClass == char.class //
                    ) {
                return;
            }
        }
        
        Method method = fieldInfo.method;
        if (method != null) {
            try {
                if (fieldInfo.getOnly) {
                    if (fieldInfo.fieldClass == AtomicInteger.class) {
                        AtomicInteger atomic = (AtomicInteger) method.invoke(object);
                        if (atomic != null) {
                            atomic.set(((AtomicInteger) value).get());
                        }
                    } else if (fieldInfo.fieldClass == AtomicLong.class) {
                        AtomicLong atomic = (AtomicLong) method.invoke(object);
                        if (atomic != null) {
                            atomic.set(((AtomicLong) value).get());
                        }
                    } else if (fieldInfo.fieldClass == AtomicBoolean.class) {
                        AtomicBoolean atomic = (AtomicBoolean) method.invoke(object);
                        if (atomic != null) {
                            atomic.set(((AtomicBoolean) value).get());
                        }
                    } else if (Map.class.isAssignableFrom(method.getReturnType())) {
                        Map map = (Map) method.invoke(object);
                        if (map != null) {
                            map.putAll((Map) value);
                        }
                    } else {
                        Collection collection = (Collection) method.invoke(object);
                        if (collection != null) {
                            collection.addAll((Collection) value);
                        }
                    }
                } else {
                    if (value == null && fieldInfo.fieldClass.isPrimitive()) {
                        return;
                    }
                    method.invoke(object, value);
                }
            } catch (Exception e) {
                throw new JSONException("set property error, " + fieldInfo.name, e);
            }
            return;
        }

        final Field field = fieldInfo.field;
        if (field != null) {
            try {
                field.set(object, value);
            } catch (Exception e) {
                throw new JSONException("set property error, " + fieldInfo.name, e);
            }
        }
    }
}

@@ -1535,27 +1535,27 @@
 alue == null
-) %7B
+ //
 %0A           
@@ -1559,487 +1559,49 @@
     
-Class%3C?%3E fieldClass = fieldInfo.fieldClass;%0A            if (fieldClass == byte.class //%0A                    %7C%7C fieldClass == short.class //%0A                    %7C%7C fieldClass == int.class //%0A                    %7C%7C fieldClass == long.class //%0A                    %7C%7C fieldClass == float.class //%0A                    %7C%7C fieldClass == double.class //%0A                    %7C%7C fieldClass == boolean.class //%0A                    %7C%7C fieldClass == char.class //%0A                    ) %7B%0A    
+&& fieldInfo.fieldClass.isPrimitive()) %7B%0A
     
@@ -1616,30 +1616,16 @@
 return;%0A
-            %7D%0A
         
@@ -3245,141 +3245,8 @@
 e %7B%0A
-                    if (value == null && fieldInfo.fieldClass.isPrimitive()) %7B%0A                        return;%0A                    %7D%0A
     

remove duplicate code