// Path: easymvp-weaver/src/main/java/easymvp/weaver/JavassistUtils.java
// static String[] ctClassToString(CtClass[] params) {
//     String[] strings = new String[params.length];
//     for (int i = 0; i < params.length; i++) {
//         strings[i] = params[i].getName();
//     }
//     return strings;
// }
// 
// Path: easymvp-weaver/src/main/java/easymvp/weaver/JavassistUtils.java
// static boolean sameSignature(List<String> parameters, CtMethod method)
//         throws NotFoundException {
//     CtClass[] methodParameters = method.getParameterTypes();
//     if (methodParameters.length == 0 && parameters.size() == 0) return true;
//     if (methodParameters.length != 0 && parameters.size() == 0) return false;
//     if (methodParameters.length == 0 && parameters.size() != 0) return false;
//     for (CtClass clazz : method.getParameterTypes()) {
//         if (!parameters.contains(clazz.getName())) return false;
//     }
//     return true;
// }
// 
// Path: easymvp-weaver/src/main/java/easymvp/weaver/JavassistUtils.java
// static CtClass[] stringToCtClass(ClassPool pool, String[] params)
//         throws NotFoundException {
//     CtClass[] ctClasses = new CtClass[params.length];
//     for (int i = 0; i < params.length; i++) {
//         ctClasses[i] = pool.get(params[i]);
//     }
//     return ctClasses;
// }


import weaver.processor.WeaverProcessor;
import static easymvp.weaver.JavassistUtils.ctClassToString;
import static easymvp.weaver.JavassistUtils.sameSignature;
import static easymvp.weaver.JavassistUtils.stringToCtClass;
import easymvp.annotation.ActivityView;
import easymvp.annotation.CustomView;
import easymvp.annotation.FragmentView;
import easymvp.annotation.conductor.ConductorController;
import java.util.Arrays;
import java.util.Set;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
import weaver.common.WeaveEnvironment;
import weaver.instrumentation.injection.ClassInjector;
/*
 * Copyright (C) 2016 6thSolution.
 *
 * 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 easymvp.weaver;



/**
 * Bytecode weaver processor.
 * <p>
 * After generating <code>ViewDelegate</code> classes, this processor will run and do these things:
 * <ol>
 * <li>Finds all classes that annotated with {@link ActivityView} ,{@link FragmentView} and {@link
 * CustomView}.</li>
 * <li>For each view, Adds a field correspond to his generated <code>ViewDelegate</code>
 * class.</li>
 * <li>Invokes all methods from injected field in the right place of view lifecycle.</li>
 * </ol>
 *
 * @author Saeed Masoumi (saeed@6thsolution.com)
 */
public class ViewDelegateBinder extends WeaverProcessor {

    private static final String FIELD_VIEW_DELEGATE = "$$viewDelegate";
    private static final String FIELD_PRESENTER_PROVIDER = "$$presenterProvider";

    private static final String BUNDLE_CLASS = "android.os.Bundle";
    private static final String LAYOUT_INFLATER_CLASS = "android.view.LayoutInflater";
    private static final String VIEW_GROUP_CLASS = "android.view.ViewGroup";
    private static final String VIEW_CLASS = "android.view.View";

    private static final String POINT_CUT = "org.aspectj.lang.JoinPoint";

    private static final String STATEMENT_CALL_INITIALIZE =
            "$s." + FIELD_VIEW_DELEGATE + ".initialize($s);";
    private static final String STATEMENT_CALL_INITIALIZE_WITH_FACTORY =
            "$s." + FIELD_VIEW_DELEGATE + ".initialize($s, $s." + FIELD_PRESENTER_PROVIDER + ");";
    private static final String STATEMENT_CALL_ATTACH =
            "$s." + FIELD_VIEW_DELEGATE + ".attachView($s);";
    private static final String STATEMENT_CALL_DETACH =
            "$s." + FIELD_VIEW_DELEGATE + ".detachView();";
    private static final String STATEMENT_CALL_DESTROY =
            "$s." + FIELD_VIEW_DELEGATE + ".destroy($s);";

    private static final String ASPECTJ_GEN_METHOD = "_aroundBody";
// Path: easymvp-weaver/src/main/java/easymvp/weaver/JavassistUtils.java
// static String[] ctClassToString(CtClass[] params) {
//     String[] strings = new String[params.length];
//     for (int i = 0; i < params.length; i++) {
//         strings[i] = params[i].getName();
//     }
//     return strings;
// }
// 
// Path: easymvp-weaver/src/main/java/easymvp/weaver/JavassistUtils.java
// static boolean sameSignature(List<String> parameters, CtMethod method)
//         throws NotFoundException {
//     CtClass[] methodParameters = method.getParameterTypes();
//     if (methodParameters.length == 0 && parameters.size() == 0) return true;
//     if (methodParameters.length != 0 && parameters.size() == 0) return false;
//     if (methodParameters.length == 0 && parameters.size() != 0) return false;
//     for (CtClass clazz : method.getParameterTypes()) {
//         if (!parameters.contains(clazz.getName())) return false;
//     }
//     return true;
// }
// 
// Path: easymvp-weaver/src/main/java/easymvp/weaver/JavassistUtils.java
// static CtClass[] stringToCtClass(ClassPool pool, String[] params)
//         throws NotFoundException {
//     CtClass[] ctClasses = new CtClass[params.length];
//     for (int i = 0; i < params.length; i++) {
//         ctClasses[i] = pool.get(params[i]);
//     }
//     return ctClasses;
// }


// Path: easymvp-weaver/src/main/java/easymvp/weaver/ViewDelegateBinder.java
import weaver.processor.WeaverProcessor;
import static easymvp.weaver.JavassistUtils.ctClassToString;
import static easymvp.weaver.JavassistUtils.sameSignature;
import static easymvp.weaver.JavassistUtils.stringToCtClass;
import easymvp.annotation.ActivityView;
import easymvp.annotation.CustomView;
import easymvp.annotation.FragmentView;
import easymvp.annotation.conductor.ConductorController;
import java.util.Arrays;
import java.util.Set;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
import weaver.common.WeaveEnvironment;
import weaver.instrumentation.injection.ClassInjector;

/*
 * Copyright (C) 2016 6thSolution.
 *
 * 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 easymvp.weaver;



/**
 * Bytecode weaver processor.
 * <p>
 * After generating <code>ViewDelegate</code> classes, this processor will run and do these things:
 * <ol>
 * <li>Finds all classes that annotated with {@link ActivityView} ,{@link FragmentView} and {@link
 * CustomView}.</li>
 * <li>For each view, Adds a field correspond to his generated <code>ViewDelegate</code>
 * class.</li>
 * <li>Invokes all methods from injected field in the right place of view lifecycle.</li>
 * </ol>
 *
 * @author Saeed Masoumi (saeed@6thsolution.com)
 */
public class ViewDelegateBinder extends WeaverProcessor {

    private static final String FIELD_VIEW_DELEGATE = "$$viewDelegate";
    private static final String FIELD_PRESENTER_PROVIDER = "$$presenterProvider";

    private static final String BUNDLE_CLASS = "android.os.Bundle";
    private static final String LAYOUT_INFLATER_CLASS = "android.view.LayoutInflater";
    private static final String VIEW_GROUP_CLASS = "android.view.ViewGroup";
    private static final String VIEW_CLASS = "android.view.View";

    private static final String POINT_CUT = "org.aspectj.lang.JoinPoint";

    private static final String STATEMENT_CALL_INITIALIZE =
            "$s." + FIELD_VIEW_DELEGATE + ".initialize($s);";
    private static final String STATEMENT_CALL_INITIALIZE_WITH_FACTORY =
            "$s." + FIELD_VIEW_DELEGATE + ".initialize($s, $s." + FIELD_PRESENTER_PROVIDER + ");";
    private static final String STATEMENT_CALL_ATTACH =
            "$s." + FIELD_VIEW_DELEGATE + ".attachView($s);";
    private static final String STATEMENT_CALL_DETACH =
            "$s." + FIELD_VIEW_DELEGATE + ".detachView();";
    private static final String STATEMENT_CALL_DESTROY =
            "$s." + FIELD_VIEW_DELEGATE + ".destroy($s);";

    private static final String ASPECTJ_GEN_METHOD = "_aroundBody";
    private ClassPool pool;