Let's say I have this code that uses some input (like a URL path) to determine which method to run, via reflection:
map.put("/users/*", "viewUser");
map.put("/users", "userIndex");
String methodName = map.get(path);
Method m = Handler.class.getMethod(methodName, ...);
m.invoke(handler, ...);
This uses reflection, so performance can be improved. This can be done as follows:
map.put("/users/*", new Runnable() { public void run() { handler.viewUser(); } });
map.put("/users", new Runnable() { public void run() { handler.userIndex(); } });
Runnable action = map.get(path);
action.run();
But for manually creating all of these Runnable, as this has its problems. I am wondering if I can create them at runtime? Therefore, I will have an input map, as in the first example, and will dynamically create a map of the second example. Of course, generation is just a matter of constructing a string, but what about compiling and loading it?
. , , . , .