如何将多个参数传递给java反射中的方法 [英] how to pass multiple parameters to a method in java reflections

查看:178
本文介绍了如何将多个参数传递给java反射中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用反思来实现某些目的。
我已经给了类名,该类的方法名和需要在文件中传递给该方法的参数值(取任何文件,而不是约束)。
我必须使用参数调用该方法。这种方法不会返回任何东西。
这个类和参数列表中有一个很大的方法列表,每个变量都有所不同。



例如: method1(String,String, int,boolean)
method1(String,int,boolean)同样我有不同的排列和组合。
那么我如何才能实现这个。
我已尝试使用不同的switch子句的硬编码,但它是真正的开销和风险的维护。
我们可以动态地做这个事情,就像在飞行中一样从文件中读取方法名称及其参数并调用它。
任何小的代码片段都将有所帮助。
TIA。

解决方案

我已经找到了上述问题的解决方案。以下是示例代码片段。

 软件包反射; 

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

public class ReflectionTest {
public void method1(String str,int number){
System.out.println(str + number);
}

public void method1(String str){
System.out.println(str);
}

public void method1(){
System.out.println(helloworld);
}

public static void main(String [] args)throws ClassNotFoundException,
InstantiationException,IllegalAccessException,
NoSuchMethodException,SecurityException,IllegalArgumentException,
InvocationTargetException {
//步骤1)创建一个对象数组并存储您希望
//的参数传递它。
Object [] obj = {}; // for method1()
// Object [] obj = {hello}; for method1(String str)
// Object [] obj = {hello,1}; for method1(String str,int number)
//步骤2)创建一个类数组,它将保存被调用的
//方法的签名。
Class<?> params [] = new Class [obj.length]; (int i = 0; i< obj.length; i ++){
if(obj [i] instanceof Integer){
params [i] = Integer.TYPE;
} else if(obj [i] instanceof String){
params [i] = String.class;
}
//如果需要,您可以对其他数据类型进行其他检查。
}

String methoName =method1; //方法名称被调用
String className =reflections.ReflectionTest; //类名
Class<?> cls = Class.forName(className);
Object _instance = cls.newInstance();
方法myMethod = cls.getDeclaredMethod(methoName,params);
myMethod.invoke(_instance,obj);
}
}

我希望这样也能帮助别人。 >

Hi i am using reflections to achieve something. I have been given class name, method name of that class and parameter values that needs to be passed to that method in a file(Take any file. Not a constraint). I have to call that method with the parameters. This methods do not return anything. There is a huge list of methods in this classes and parameter list of each varies.

E.g: method1(String, String, int, boolean) method1(String, int, boolean) and likewise i have different permutations and combinations. So how can i achieve this. I have tried hard coding things with different switch clauses but it is a real overhead and risky thing to maintain. Can we dynamically do this thing, like on the fly read the method name and its parameter from the file and call it. Any small code snippet will be helpful. TIA.

解决方案

Hi all i have found the solution to the above question. below is the sample code snippet.

package reflections;

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

public class ReflectionTest {
    public void method1(String str, int number) {
        System.out.println(str + number);
    }

    public void method1(String str) {
        System.out.println(str);
    }

    public void method1() {
        System.out.println("helloworld");
    }

    public static void main(String[] args) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException,
            NoSuchMethodException, SecurityException, IllegalArgumentException,
            InvocationTargetException {
        // Step 1) Make an object array and store the parameters that you wish
        // to pass it.
        Object[] obj = {};// for method1()
        // Object[] obj={"hello"}; for method1(String str)
        // Object[] obj={"hello",1}; for method1(String str,int number)
        // Step 2) Create a class array which will hold the signature of the
        // method being called.
        Class<?> params[] = new Class[obj.length];
        for (int i = 0; i < obj.length; i++) {
            if (obj[i] instanceof Integer) {
                params[i] = Integer.TYPE;
            } else if (obj[i] instanceof String) {
                params[i] = String.class;
            }
            // you can do additional checks for other data types if you want.
        }

        String methoName = "method1"; // methodname to be invoked
        String className = "reflections.ReflectionTest";// Class name
        Class<?> cls = Class.forName(className);
        Object _instance = cls.newInstance();
        Method myMethod = cls.getDeclaredMethod(methoName, params);
        myMethod.invoke(_instance, obj);
    }
}

I hope this will help others too.

这篇关于如何将多个参数传递给java反射中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆