package io.tjeubaoit.android.mvc.annotation;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import io.tjeubaoit.android.mvc.message.Message;

/**
 * TODO: Class description here.
 *
 * @author <a href="https://github.com/tjeubaoit">tjeubaoit</a>
 */
public class OnActionScanner {

    public static Map<String, Method> getMethodsAnnotatedAsAction(Object obj) {
        Message msg = new Message.Builder().build();
        Map<String, Method> methodMap = new HashMap<>();

        // scan methods for mapping
        for (Method method : obj.getClass().getDeclaredMethods()) {
            if (!method.isAnnotationPresent(OnAction.class)) continue;

            Class<?>[] paramTypes = method.getParameterTypes();
            if (paramTypes.length != 1 || !paramTypes[0].isInstance(msg)) continue;

            for (String action : method.getAnnotation(OnAction.class).value()) {
                methodMap.put(action, method);
            }
        }

        return methodMap;
    }
}

@@ -169,16 +169,61 @@
 Message;
+%0Aimport io.tjeubaoit.android.mvc.util.Logger;
 %0A%0A/**%0A *
@@ -356,16 +356,99 @@
 nner %7B%0A%0A
+    private static final Logger LOGGER = Logger.getLogger(OnActionScanner.class);%0A%0A
     publ
@@ -908,17 +908,43 @@
 gth 
-!= 1
+== 0
  %7C%7C 
-!
+(paramTypes.length == 1 && 
 para
@@ -969,28 +969,24 @@
 ce(msg))
- continue;%0A%0A
+) %7B%0A    
         
@@ -1074,16 +1074,20 @@
         
+    
 methodMa
@@ -1109,16 +1109,250 @@
 ethod);%0A
+                    LOGGER.debug(String.format(%22Map action %25s with method %25s%22, action, method.getName()));%0A                %7D%0A            %7D else %7B%0A                throw new RuntimeException(%22Parameters invalid for method: %22 + method);%0A
         

Throw exception if target method marked with OnAction has invalid parameters