简体   繁体   English

如何使用反射调用 java 中的方法

[英]How to invoke a method in java using reflection

How can I invoke a method with parameters using reflection?如何使用反射调用带有参数的方法?

I want to specify the values of those parameters.我想指定这些参数的值。

Here's a simple example of invoking a method using reflection involving primitives. 这是一个使用包含原语的反射调用方法的简单示例。

import java.lang.reflect.*;

public class ReflectionExample {
    public int test(int i) {
        return i + 1;
    }
    public static void main(String args[]) throws Exception {
        Method testMethod = ReflectionExample.class.getMethod("test", int.class);
        int result = (Integer) testMethod.invoke(new ReflectionExample(), 100);
        System.out.println(result); // 101
    }
}

To be robust, you should catch and handle all checked reflection-related exceptions NoSuchMethodException , IllegalAccessException , InvocationTargetException . 为了健壮,您应该捕获并处理所有已检查的与反射相关的异常NoSuchMethodExceptionIllegalAccessExceptionInvocationTargetException

To call a class method using reflection is very simple. 使用反射调用类方法非常简单。 You need to create a class and generate method in it. 您需要在其中创建一个类并生成方法。 like as follows. 如下。

package reflectionpackage;

public class My {
    public My() {
    }

    public void myReflectionMethod() {
        System.out.println("My Reflection Method called");
    }
}

and call this method in another class using reflection. 并使用反射在另一个类中调用此方法。

package reflectionpackage; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 

public class ReflectionClass {

    public static void main(String[] args) 
    throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Class c=Class.forName("reflectionpackage.My");
        Method m=c.getDeclaredMethod("myReflectionMethod");
        Object t = c.newInstance();
        Object o= m.invoke(t);       
    } 
}

Find more details here . 在此处查找更多详情

You can use getClass in any Object to discover its class. 您可以在任何Object中使用getClass来发现它的类。 Then you can use getMethods to discover all the available methods. 然后,您可以使用getMethods来发现所有可用的方法。 Once you have the correct method you can call invoke with any number of parameters 一旦有了正确的方法,就可以使用任意数量的参数调用invoke

this is the easiest way I know of, it needs to be surrounded with try & catch: 这是我所知道的最简单的方法,它需要被try&catch包围:

Method m = .class.getDeclaredMethod("", arg_1.class, arg_2.class, ... arg_n.class); 方法m = .class.getDeclaredMethod(“”,arg_1.class,arg_2.class,... arg_n.class); result = () m.invoke(null,(Object) arg_1, (Object) arg_2 ... (Object) arg_n); result =()m.invoke(null,(Object)arg_1,(Object)arg_2 ...(Object)arg_n);

this is for invoking a static method, if you want to invoke a non static method, you need to replace the first argument of m.invoke() from null to the object the underlying method is invoked from. 这是用于调用静态方法,如果要调用非静态方法,则需要将m.invoke()的第一个参数从null替换为调用基础方法的对象。

don't forget to add an import to java.lang.reflect.*; 不要忘记向java.lang.reflect添加导入。*;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM