mercredi 11 mai 2016

How to parse a String that consists of a static method so that we can call it?

I'm trying to write a function that receives a string that consists of a static method with a string array as an argument.

For example, let's imagine we have this class with a static method:

package com.stack.examples;

public class Example {

   public static void testMethod() {
       System.out.println("method executed");
    }
}

Now, our function would be in another class, as follows:

package com.stack.functions;

public class Functions {

   public void parseCommand(String command) {
      //TODO, this is where my doubts lie
      //The given string is always composed of a sequence of Java identifiers 
        //separated by the character ’.’, representing the full qualified name of 
          //a Java method (package.class.method)
       command.execute(); //Would this work? Perhaps reflection would be ideal

   }
}

My objective is to parse the string given as an argument in parseCommand so that

parseCommand("com.stack.examples.Example.testMethod()");

actually calls the static method with the given arguments (in this example case, the method would only print out "message executed").





Aucun commentaire:

Enregistrer un commentaire