Using Java reflection to create an eval () method

I have a question about reflection. I'm trying to use some kind of eval () method. Therefore, I can call, for example:

eval("test('woohoo')"); 

Now I understand that in java there is no eval method, but there is a reflection. I made the following code:

 String s = "test"; Class cl = Class.forName("Main"); Method method = cl.getMethod(s, String.class); method.invoke(null, "woohoo"); 

This works fine (of course, there is an attempt to lock the lock around this code). It runs a testing method. However, I want to call several methods, all of which have different parameters.

I do not know what parameters are (so not only String.class). But how is this possible? How can I get the types of method parameters? I know about the following method:

 Class[] parameterTypes = method.getParameterTypes(); 

But this will return the paramTypes of the method I just selected! with the following statement:

 Method method = cl.getMethod(s, String.class); 

Any help would be appreciated!

+5
source share
4 answers

You will need to call Class.getMethods() and repeat their search for the correct function.

 For (Method method : clazz.getMethods()) { if (method.getName().equals("...")) { ... } } 

The reason for this is that there may be several methods with the same name and different types of parameters (i.e. the method name is overloaded).

getMethods() returns all public methods in the class, including from superclasses. An alternative is Class.getDeclaredMethods() , which returns all methods in this class only.

+15
source

You can iterate over all class methods with:

 cls.getMethods(); // gets all public methods (from the whole class hierarchy) 

or

 cls.getDeclaredMethods(); // get all methods declared by this class 

.

 for (Method method : cls.getMethods()) { // make your checks and calls here } 
+5
source

You can use getMethods() , which returns an array of all class methods. Inside the loop, you can check the parameters of each method.

 for(Method m : cl.getMethods()) { Class<?>[] params = m.getParameterTypes(); ... } 

Otherwise, you can use getDelcaredMethods() , which allows you to “see” private methods (but not inherited methods). Note: if you want to call private methods, you must first apply setAccessible (boolean flag) on it:

 for(Method m : cl.getDelcaredMethods()) { m.setAccessible(true); Class<?>[] params = m.getParameterTypes(); ... } 
+2
source

Ok, thanks to all the people who answered my question here, the final solution:

 import java.lang.reflect.Method; public class Main { public static void main(String[] args){ String func = "test"; Object arguments[] = {"this is ", "really cool"}; try{ Class cl = Class.forName("Main"); for (Method method : cl.getMethods()){ if(method.getName().equals(func)){ method.invoke(null, arguments); } } } catch (Exception ioe){ System.out.println(ioe); } } public static void test(String s, String b){ System.out.println(s+b); } } 
+2
source

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


All Articles