java - 如何使用Reflection类动态调用setter和getter方法?

标签 java reflection

假设我有类 AccountPojoGetAccountPojo 及其 setter 和 getter 方法,如下所示。

public class AccountPojo {

    private String dataList;
    private String dataSet;

    public String getDataList() {
        return dataList;
    }

    public void setDataList(String dataList) {
        this.dataList = dataList;
    }

    public String getDataSet() {
        return dataSet;
    }

    public void setDataSet(String dataSet) {
        this.dataSet = dataSet;
    }
} 

public class GetAccountsPojo {

    private String accountId;
    private int noOfAccounts;

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public int getNoOfAccounts() {
        return noOfAccounts;
    }

    public void setNoOfAccounts(int noOfAccounts) {
        this.noOfAccounts = noOfAccounts;
    }
}

现在我有类Test如下

public Class Test {

    public static void main(String[] args) {

        Class cls = Class.forName("com.org.temp."+ClassName); // ClassName(AccountPojo/GetAccountPojo) here I know already which class is getting called.

        Object clsInstance = (Object) cls.newInstance();

        System.out.println("The cls is==" + cls+" and classInstance is=="+clsInstance);

    // Here I want to access getter and setter methods of AccountPojo and GetAcoountPojo dynamically, no hard coding

    }   
}

最佳答案

您是否尝试过获取被调用类的所有方法并按名称过滤出 getter 方法并调用它们?

 Method[] methods =  cls.getClass().getDeclaredMethods();

             for (Method m: methods) {
                 if(m.getName().startsWith("get")) {
                     m.invoke(clsInstance);
                 }
             }

这解决了我们的一半问题,因为 getter 是在没有任何参数的情况下调用的。但如果您需要调用 setter 方法,则需要指定参数。例如,要调用接受字符串参数方法的 setter,如下所示:

m.invoke(clsInstance, "some string argument");

一种解决方案是让所有 setter 接受对象类型值并对其进行类型转换,同时将其分配给实际的类变量。

现在你的 pojo 类将如下所示:

public class AccountPojo {

private String dataList;
private String dataSet;

public String getDataList() {
    return dataList;
}

public void setDataList(Object dataList) {
    this.dataList = (String) dataList;
}

public String getDataSet() {
    return dataSet;
}

public void setDataSet(Object dataSet) {
    this.dataSet = (String)dataSet;
}
} 

public class GetAccountsPojo {

private String accountId;
private int noOfAccounts;

public String getAccountId() {
    return accountId;
}

public void setAccountId(Object accountId) {
    this.accountId = (String) accountId;
}

public int getNoOfAccounts() {
    return noOfAccounts;
}

public void setNoOfAccounts(Object noOfAccounts) {
    this.noOfAccounts = (int) noOfAccounts;
}
}

将以下代码添加到您的主方法中:

for (Method m: methods) {
  if(m.getName().startsWith("get")) {
    m.invoke(clsInstance);
  }
  if(m.getName().startsWith("set")) {
    m.invoke(clsInstance, "any argument to be passed here");
  }

}

关于java - 如何使用Reflection类动态调用setter和getter方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50486389/

相关文章:

java - Apache Commons AsyncClient - 忽略证书 - SSLPeerUnverifiedException

Java 反射性能在打包为 jar 时下降

iphone - 如何使用 plist NSString 项作为 UIImageView 对象名称

c# - 我正在使用反射来获取 ASP.NET 中的属性名称和值需要一些优化建议

java - 我可以在注释中使用类类型参数吗?

java - 将 List<Object[]> 数组转换为 Map

java - 如何在 Grizzly 中设置默认索引页

arrays - Scala Array.apply 的魔力是什么

java - 如何在Java中自动启动Quartz Scheduler

java - 如何以编程方式控制 JScrollPane 的位置?