Java反射:动态调用具有不同参数的方法

Java反射:动态调用具有不同参数的方法,java,reflection,Java,Reflection,我试图从一个类中动态调用不同的方法 detFunctions.java: package detFunctions; import java.lang.reflect.*; import java.sql.*; public class detFunctions{ public static void main(String[] args) throws Exception { String funcId = ""; String funcName = "";

我试图从一个类中动态调用不同的方法

detFunctions.java:

package detFunctions;

import java.lang.reflect.*;
import java.sql.*;  

public class detFunctions{
    public static void main(String[] args) throws Exception {
    String funcId = "";
    String funcName = "";
    String funcDesc = "";
    String Input = "";
    try{
        System.out.println("Retrieving Function for Execution....");

        Class.forName("oracle.jdbc.driver.OracleDriver");  
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@192.168.168.4:2921:SCRDEV","custom","custom");
        Statement stmt=con.createStatement(); 
        ResultSet rs=stmt.executeQuery("select FUNC_IDENTIFIER,FUNC_NAME,FUNC_DESC from custom.dfmt");
        Input = "10001|10002|10003";
        while(rs.next()){
            funcId = rs.getString("FUNC_IDENTIFIER");
            funcName = rs.getString("FUNC_NAME");
            funcDesc = rs.getString("FUNC_DESC");       
        }   
        con.close();
        System.out.println("Function execution completed!!!");
    } catch (Exception e){ System.out.println(e);} 
    System.out.println(funcId);
    System.out.println(funcName);
    System.out.println(funcDesc);
    System.out.println(Input);

    //Reflection of DETFunctions (Raw Type)
    String classId = "detFunctions.detFuncSet1";
    Class c = Class.forName(classId);
    Method m = c.getDeclaredMethod(funcId, new Class[] {String.class});
    Object i = c.newInstance();
    Object funcOutput = m.invoke(i, Input);
    System.out.println(funcOutput);


    //Reflection of DETFunctions (Generic Type)

  } //End of Main Function
} //End of class
    *
detFuncSet1.java:

package detFunctions;

public class detFuncSet1 {

    public double GL_BALANCE(String GLList) {
        System.out.println("GL_BALANCE Method invoked.......");
        double GLBalance = 5005689.50;
        System.out.println("GL_BALANCE Method execution completed.....");
        return GLBalance;
  }

    public double EFF_BALANCE(String AcctNo,String InDate) {
        System.out.println("EFF_BALANCE Method invoked.......");
        double EFFBalance = 500.50;
        System.out.println("EFF_BALANCE Method execution completed.....");
        return EFFBalance;
  }
}
在这里,我尝试使用refection从detFuncSet1类执行方法。但是getDeclaredMethod是一个原始类型,因此我无法将不同类型的输入传递给这些方法


在上面的代码中,getDeclaredMethod具有参数类型的参数。根据我正在执行的查询,我将导出funcId(这是我的方法名)。由于我的方法(在detFuncSet1类中)具有不同的输入,因此我无法将参数类型动态传递给getDeclaredMethod。有没有一种方法可以动态执行我的方法

使用反射,您将失去类型安全性。调用方法时,只需传递一个
对象[]
数组。

这是什么类型的程序?您没有遵循Java样式指南(例如,小写变量名),这非常令人困惑。
输入变量的作用是什么?它包含一个以“管道”分隔的字符串(
|
)。它应该被分割吗?这是我硬编码的一个输入,用于传递方法GL_BALANCE。很抱歉没有遵循Java样式指南。该方法的名称为
EFF\u BALANCE
,并带有两个参数。我认为这是你计划中最大的问题。您需要找出每个方法要传递多少个参数。一般来说,您试图做的也是一个很大的安全风险。基本上,这是我的要求,bcoz我的应用程序将向我发送方法名称及其输入到我选择的表中。(但这里我已经对输入进行了硬编码),随着应用程序功能的增强,我必须能够在defFuncSet1中添加越来越多的方法。