package org.telegram.actors;

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

/**
 * Created by ex3ndr on 24.03.14.
 */
public abstract class ReflectedActor extends Actor {

    private HashMap<String, Method> eventMethods;

    public ReflectedActor(ActorSystem system, String actorName, String threadName) {
        super(system, actorName, threadName);
    }

    @Override
    protected void registerMethods() {
        eventMethods = new HashMap<String, Method>();
        Method[] methods = getClass().getDeclaredMethods();
        for (Method m : methods) {
            String methodName = m.getName();
            if (methodName.startsWith("on") && methodName.endsWith("Message")) {
                m.setAccessible(true);
                String stateName = methodName.substring(2, methodName.length() - 7);
                stateName = stateName.substring(0, 1).toLowerCase() + stateName.substring(1);
                registerKind(stateName, m.getParameterTypes());
                eventMethods.put(stateName, m);
            }
//            if (methodName.startsWith("on") && methodName.endsWith("Async")) {
//                m.setAccessible(true);
//                String stateName = methodName.substring(2, methodName.length() - 7);
//                // Async methods
//            }
        }
    }

    @Override
    protected void process(String name, Object[] args, ActorReference sender) throws Exception {
        eventMethods.get(name).invoke(this, args);
    }
}
@@ -227,16 +227,78 @@
 tMethods
+ = new HashMap%3CString, Method%3E();%0A    private Method%5B%5D methods
 ;%0A%0A    p
@@ -438,45 +438,25 @@
     
-@Override%0A    protected void register
+private void load
 Meth
@@ -471,30 +471,29 @@
         
-eventM
+if (m
 ethods =
  new Has
@@ -488,88 +488,154 @@
 ds =
- new HashMap%3CString, Method%3E();%0A        Method%5B%5D methods = getClass().getDeclare
+= null) %7B%0A            methods = getClass().getDeclaredMethods();%0A        %7D%0A    %7D%0A%0A    @Override%0A    protected void registerMethods() %7B%0A        loa
 dMet
@@ -1147,18 +1147,197 @@
       %7D%0A
-//
+        %7D%0A    %7D%0A%0A    protected ActorMessageDesc registerMethod(String name) %7B%0A        loadMethods();%0A        for (Method m : methods) %7B%0A            String methodName = m.getName();%0A
         
@@ -1400,21 +1400,21 @@
 th(%22
-Async
+Message
 %22)) %7B%0A
-//
     
@@ -1448,18 +1448,16 @@
 (true);%0A
-//
         
@@ -1533,18 +1533,16 @@
 ) - 7);%0A
-//
         
@@ -1553,27 +1553,263 @@
     
-// Async methods%0A//
+stateName = stateName.substring(0, 1).toLowerCase() + stateName.substring(1);%0A                if (stateName.equals(name)) %7B%0A                    eventMethods.put(stateName, m);%0A                    return registerKind(stateName, m.getParameterTypes());%0A    
     
@@ -1814,32 +1814,37 @@
           %7D%0A
+%0A%0A   
         
 %7D%0A    %7D%0A%0A   
@@ -1823,33 +1823,137 @@
  %7D%0A%0A%0A           
-%7D
+ %7D%0A        %7D%0A        throw new UnsupportedOperationException(%22Unable to find method for '%22 + name + %22'%22);
 %0A    %7D%0A%0A    @Ove

Support for manual reflected actor init