/*
 * Copyright (C) 2016 Qiujuer <qiujuer@live.cn>
 * WebSite http://www.qiujuer.net
 * Created 1/1/2016
 * Changed 1/1/2016
 * Version 1.0.0
 * Author Qiujuer
 *
 * 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 net.qiujuer.common.okhttp.impl;

import com.google.gson.Gson;
import com.google.gson.internal.$Gson$Types;

import net.qiujuer.common.okhttp.core.HttpCore;
import net.qiujuer.common.okhttp.core.Resolver;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * This Resolver by Gson
 */
public class GsonResolver implements Resolver {
    private Gson mGson;

    public GsonResolver() {
        mGson = new Gson();
    }

    public GsonResolver(Gson gson) {
        mGson = gson;
    }

    public Gson getGson() {
        return mGson;
    }

    public void setGson(Gson gson) {
        this.mGson = gson;
    }

    @Override
    public Object analysis(String rsp, Type type) {
        if (type == String.class)
            return rsp;
        return mGson.fromJson(rsp, type);
    }

    @Override
    public Object analysis(String rsp, Class<?> subclass) {
        if (subclass == String.class)
            return rsp;
        try {
            return analysis(rsp, getSuperclassTypeParameter(subclass));
        } catch (RuntimeException e) {
            if (HttpCore.DEBUG)
                e.printStackTrace();
            return mGson.fromJson(rsp, subclass);
        }
    }

    Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
    }
}

@@ -835,16 +835,68 @@
 Types;%0A%0A
+import net.qiujuer.common.okhttp.core.HttpCallback;%0A
 import n
@@ -987,52 +987,57 @@
 er;%0A
-%0A
 import 
-java.lang.reflect.ParameterizedType;
+net.qiujuer.genius.kit.reflect.Reflector;%0A
 %0Aimp
@@ -1729,32 +1729,33 @@
     return rsp;%0A
+%0A
         try %7B%0A  
@@ -1768,64 +1768,153 @@
     
-return analysis(rsp, getSuperclassTypeParameter(subclass
+Type%5B%5D types = Reflector.getActualTypeArguments(HttpCallback.class, subclass);%0A            return analysis(rsp, $Gson$Types.canonicalize(types%5B0%5D
 ));%0A
@@ -2090,408 +2090,7 @@
    %7D
-%0A%0A    Type getSuperclassTypeParameter(Class%3C?%3E subclass) %7B%0A        Type superclass = subclass.getGenericSuperclass();%0A        if (superclass instanceof Class) %7B%0A            throw new RuntimeException(%22Missing type parameter.%22);%0A        %7D%0A        ParameterizedType parameterized = (ParameterizedType) superclass;%0A        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()%5B0%5D);%0A    %7D
 %0A%7D%0A

Change GsonResolver get type logic