Can I store function names in the final hashmap for execution?

I am creating an administrator controller that works as a terminal emulator in Flex 4.5. The server side is Red5 on the tomcat server using the Java programming language.

When a user enters a command into his text, the command is sent to red5, in red5 I check if the command exists and returns the correct output or error if the command or parameters do not match.

so now i use if (command.equals("..") {} else if (command.equals(...

Is there a way to save a function name or a link to a function that should be executed in each command and execute it?

Example:

 // creating the hasmap HashMap<String,Object> myfunc = new HashMap<String,Object>(); // adding function reference myfunc.put("help",executeHelp); 

or....

 myfunc.put("help", "executeHelp"); // writing the name of the function 

and then

 void receiveCommand(String command, Object params[]( { myfunc.get(command).<somehow execute the referrened function or string name ? > } 

any ideas?

Thank you!

+4
source share
3 answers

You can use reflection, but I suggest a simpler way.

You can create an abstract class or interface with the abstract execute method. Example:

 interface Command { void execute(Object params[]); } class Help implements Command { void execute(Object params[]) { // do the stuff } } 

Now your hash could be:

 // creating the hasmap HashMap<String,Command> myfunc = new HashMap<String,Command>(); // adding function reference myfunc.put("help", new Help()); 

And then:

 void receiveCommand(String command, Object params[]) { myfunc.get(command).execute(params); } 
+7
source

You can execute a function by name as follows:

 java.lang.reflect.Method method; try { method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..); } catch (SecurityException e) { // ... } catch (NoSuchMethodException e) { // ... } 

In the above snippet of param1.class, param2.class are the class types of the method arguments to execute.

Then:

 try { method.invoke(obj, arg1, arg2,...); } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } 

There is a lot of information about this: http://java.sun.com/docs/books/tutorial/reflect/index.html

+5
source

You can define an interface for your functions.

 interface Function { public Object invoke(Object[] arguments); } 

and then publish your code through this interface

 public class Function1 implements Function { public Object invoke(Object[] arguments) { ... } 

}

and save on the map

 map.put("helpCommand", new Function1()); 

or save the link using an anonymous class

 Function theFunction = new Function() { public Object invoke(Object[] arguments) { return theRealMethod(arguments[0], String.valueOf(arguments[1])); } } 

In the second example, I showed how to use an anonymous class as an adapter if the method you want to call has a different signature than your interface.

+3
source

Source: https://habr.com/ru/post/1382120/


All Articles