// Path: library/src/main/java/com/danikula/aibolit/InjectingException.java
// public class InjectingException extends RuntimeException {
// 
//     private static final long serialVersionUID = -1L;
// 
//     public InjectingException(String message) {
//         super(message);
//     }
// 
//     public InjectingException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public InjectingException(Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: library/src/main/java/com/danikula/aibolit/InjectionContext.java
// public interface InjectionContext {
// 
//     View getRootView();
// 
//     Context getAndroidContext();
// 
// }
// 
// Path: library/src/main/java/com/danikula/aibolit/MethodInvocationHandler.java
// public class MethodInvocationHandler implements InvocationHandler {
// 
//     private Object methodOwner;
// 
//     private Method targetMethod;
// 
//     private Method sourceMethod;
// 
//     /**
//      * Constructs invokation handler. 
//      * 
//      * @param methodOwner Object object that contains fields or methods that should be injected
//      * @param sourceMethod Method method to be called
//      * @param targetMethod Method method of listener
//      */
//     public MethodInvocationHandler(Object methodOwner, Method sourceMethod, Method targetMethod) {
//         this.methodOwner = methodOwner;
//         this.targetMethod = targetMethod;
//         this.sourceMethod = sourceMethod;
//     }
// 
//     @Override
//     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//         if (method.getName().equals(targetMethod.getName())) {
//             sourceMethod.setAccessible(true);
//             return sourceMethod.invoke(methodOwner, args);
//         }
//         // call only one target method, ignore other
//         return null;
//     }
// }


import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import android.view.View;
import com.danikula.aibolit.InjectingException;
import com.danikula.aibolit.InjectionContext;
import com.danikula.aibolit.MethodInvocationHandler;
/*
 * Copyright (C) 2011 Alexey Danilov
 *
 * 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 com.danikula.aibolit.injector;




/**
 * Injects event handler 
 * 
 * @author Alexey Danilov
 * 
 * @param <A> type of corresponding annotation
 */
public abstract class AbstractMethodInjector<A extends Annotation> extends AbstractInjector<A> {

    /**
     * Injects event handler
     * @param methodOwner Objects object that contain method
     * @param injectionContext InjectionContext object to be used for retrieving information about injection context
     * @param sourceMethod method to be invoked as event handler
     * @param annotation T annotation fir providing data for injection
     */
    public abstract void doInjection(Object methodOwner, InjectionContext injectionContext, Method sourceMethod, A annotation);

    protected void checkIsViewAssignable(Class<? extends View> expectedClass, Class<? extends View> actualClass) {
        if (!expectedClass.isAssignableFrom(actualClass)) {
            String errorPattern = "Injecting is allowable only for view with type %s, but not for %s";
// Path: library/src/main/java/com/danikula/aibolit/InjectingException.java
// public class InjectingException extends RuntimeException {
// 
//     private static final long serialVersionUID = -1L;
// 
//     public InjectingException(String message) {
//         super(message);
//     }
// 
//     public InjectingException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public InjectingException(Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: library/src/main/java/com/danikula/aibolit/InjectionContext.java
// public interface InjectionContext {
// 
//     View getRootView();
// 
//     Context getAndroidContext();
// 
// }
// 
// Path: library/src/main/java/com/danikula/aibolit/MethodInvocationHandler.java
// public class MethodInvocationHandler implements InvocationHandler {
// 
//     private Object methodOwner;
// 
//     private Method targetMethod;
// 
//     private Method sourceMethod;
// 
//     /**
//      * Constructs invokation handler. 
//      * 
//      * @param methodOwner Object object that contains fields or methods that should be injected
//      * @param sourceMethod Method method to be called
//      * @param targetMethod Method method of listener
//      */
//     public MethodInvocationHandler(Object methodOwner, Method sourceMethod, Method targetMethod) {
//         this.methodOwner = methodOwner;
//         this.targetMethod = targetMethod;
//         this.sourceMethod = sourceMethod;
//     }
// 
//     @Override
//     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//         if (method.getName().equals(targetMethod.getName())) {
//             sourceMethod.setAccessible(true);
//             return sourceMethod.invoke(methodOwner, args);
//         }
//         // call only one target method, ignore other
//         return null;
//     }
// }


// Path: library/src/main/java/com/danikula/aibolit/injector/AbstractMethodInjector.java
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import android.view.View;
import com.danikula.aibolit.InjectingException;
import com.danikula.aibolit.InjectionContext;
import com.danikula.aibolit.MethodInvocationHandler;

/*
 * Copyright (C) 2011 Alexey Danilov
 *
 * 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 com.danikula.aibolit.injector;




/**
 * Injects event handler 
 * 
 * @author Alexey Danilov
 * 
 * @param <A> type of corresponding annotation
 */
public abstract class AbstractMethodInjector<A extends Annotation> extends AbstractInjector<A> {

    /**
     * Injects event handler
     * @param methodOwner Objects object that contain method
     * @param injectionContext InjectionContext object to be used for retrieving information about injection context
     * @param sourceMethod method to be invoked as event handler
     * @param annotation T annotation fir providing data for injection
     */
    public abstract void doInjection(Object methodOwner, InjectionContext injectionContext, Method sourceMethod, A annotation);

    protected void checkIsViewAssignable(Class<? extends View> expectedClass, Class<? extends View> actualClass) {
        if (!expectedClass.isAssignableFrom(actualClass)) {
            String errorPattern = "Injecting is allowable only for view with type %s, but not for %s";
            throw new InjectingException(String.format(errorPattern, expectedClass.getName(), actualClass.getName()));