How do I invoke a Java method when given the method name as a string?

前端 未结 21 2160
耶瑟儿~
耶瑟儿~ 2020-11-21 04:50

If I have two variables:

Object obj;
String methodName = \"getName\";

Without knowing the class of obj, how can I call the met

相关标签:
21条回答
  • 2020-11-21 05:07

    Coding from the hip, it would be something like:

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

    The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodName).

    Then you invoke that method by calling

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

    Again, leave out the arguments in .invoke, if you don't have any. But yeah. Read about Java Reflection

    0 讨论(0)
  • 2020-11-21 05:10
    //Step1 - Using string funClass to convert to class
    String funClass = "package.myclass";
    Class c = Class.forName(funClass);
    
    //Step2 - instantiate an object of the class abov
    Object o = c.newInstance();
    //Prepare array of the arguments that your function accepts, lets say only one string here
    Class[] paramTypes = new Class[1];
    paramTypes[0]=String.class;
    String methodName = "mymethod";
    //Instantiate an object of type method that returns you method name
     Method m = c.getDeclaredMethod(methodName, paramTypes);
    //invoke method with actual params
    m.invoke(o, "testparam");
    
    0 讨论(0)
  • 2020-11-21 05:10

    Please refer following code may help you.

    public static Method method[];
    public static MethodClass obj;
    public static String testMethod="A";
    
    public static void main(String args[]) 
    {
        obj=new MethodClass();
        method=obj.getClass().getMethods();
        try
        {
            for(int i=0;i<method.length;i++)
            {
                String name=method[i].getName();
                if(name==testMethod)
                {   
                    method[i].invoke(name,"Test Parameters of A");
                }
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }
    

    Thanks....

    0 讨论(0)
  • 2020-11-21 05:18

    Student.java

    class Student{
        int rollno;
        String name;
    
        void m1(int x,int y){
            System.out.println("add is" +(x+y));
        }
    
        private void m3(String name){
            this.name=name;
            System.out.println("danger yappa:"+name);
        }
        void m4(){
            System.out.println("This is m4");
        }
    }
    

    StudentTest.java

    import java.lang.reflect.Method;
    public class StudentTest{
    
         public static void main(String[] args){
    
            try{
    
                Class cls=Student.class;
    
                Student s=(Student)cls.newInstance();
    
    
                String x="kichha";
                Method mm3=cls.getDeclaredMethod("m3",String.class);
                mm3.setAccessible(true);
                mm3.invoke(s,x);
    
                Method mm1=cls.getDeclaredMethod("m1",int.class,int.class);
                mm1.invoke(s,10,20);
    
            }
            catch(Exception e){
                e.printStackTrace();
            }
         }
    }
    
    0 讨论(0)
  • 2020-11-21 05:20

    Use method invocation from reflection:

    Class<?> c = Class.forName("class name");
    Method method = c.getDeclaredMethod("method name", parameterTypes);
    method.invoke(objectToInvokeOn, params);
    

    Where:

    • "class name" is the name of the class
    • objectToInvokeOn is of type Object and is the object you want to invoke the method on
    • "method name" is the name of the method you want to call
    • parameterTypes is of type Class[] and declares the parameters the method takes
    • params is of type Object[] and declares the parameters to be passed to the method
    0 讨论(0)
  • 2020-11-21 05:21

    Indexing (faster)

    You can use FunctionalInterface to save methods in a container to index them. You can use array container to invoke them by numbers or hashmap to invoke them by strings. By this trick, you can index your methods to invoke them dynamically faster.

    @FunctionalInterface
    public interface Method {
        double execute(int number);
    }
    
    public class ShapeArea {
        private final static double PI = 3.14;
    
        private Method[] methods = {
            this::square,
            this::circle
        };
    
        private double square(int number) {
            return number * number;
        }
    
        private double circle(int number) {
            return PI * number * number;
        }
    
        public double run(int methodIndex, int number) {
            return methods[methodIndex].execute(number);
        }
    }
    

    Lambda syntax

    You can also use lambda syntax:

    public class ShapeArea {
        private final static double PI = 3.14;
    
        private Method[] methods = {
            number -> {
                return number * number;
            },
            number -> {
                return PI * number * number;
            },
        };
    
        public double run(int methodIndex, int number) {
            return methods[methodIndex].execute(number);
        }
    }
    
    0 讨论(0)
提交回复
热议问题