/*
 * Copyright 2016 XuJiaji
 *
 * 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 io.xujiaji.hnbc.utils;

import java.lang.reflect.Constructor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;

import io.xujiaji.hnbc.contracts.Contract;

/**
 * Created by qibin on 2015/11/15.
 */
public class GenericHelper {

    public static <T> Class<T> getViewClass(Class<?> klass) {
        Type type = klass.getGenericSuperclass();
        if(type == null || !(type instanceof ParameterizedType)) return null;
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Type[] types = parameterizedType.getActualTypeArguments();
        if(types == null || types.length == 0) return null;
        return (Class<T>) types[0];
    }

    public static  <T> T initPresenter(Object obj) {
        try {
            Class<?> currentClass = obj.getClass();
            LogUtil.e3(currentClass.toString());
            LogUtil.e3(getViewClass(currentClass).toString());
            LogUtil.e3(getViewInterface(currentClass).toString());
            Constructor construct = getViewClass(currentClass).getConstructor(getViewInterface(currentClass));
            return  (T) construct.newInstance(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new RuntimeException("instance of presenter fail\n" +
                " Remind presenter need to extends BasePresenter");
    }


    public static Class<?> getViewInterface(Class currentClass) {
        Class<?>[] classes = currentClass.getInterfaces();
        LogUtil.e3(Arrays.toString(classes));
        for (Class<?> c : classes) {
            if (c != Contract.BaseView.class) {
                if (getViewInterface(c) == Contract.BaseView.class) {
                    return c;
                }
            }
            return c;
        }
        throw new RuntimeException("no implement Contract.BaseView");
    }
}

@@ -1194,24 +1194,174 @@
 rguments();%0A
+        for (Type t : types) %7B%0A            if (isPresenter(t)) %7B%0A                return (Class%3CT%3E) t;%0A            %7D%0A        %7D%0A%0A        return null;%0A//
         if(t
@@ -1400,32 +1400,34 @@
 0) return null;%0A
+//
         return (
@@ -1449,24 +1449,314 @@
 %5B0%5D;%0A    %7D%0A%0A
+    private static boolean isPresenter(Type t) %7B%0A        Class%3C?%3E aClass = (Class%3C?%3E) t;%0A        Class%3C?%3E%5B%5D classes = aClass.getInterfaces();%0A        for (Class%3C?%3E c : classes) %7B%0A            return c == Contract.BasePresenter.class %7C%7C isPresenter(c);%0A        %7D%0A        return false;%0A    %7D%0A%0A
     public s

修改GenericHelper，添加对Presenter的判断。