Java 如何使用反射类动态调用和setter及getter方法?
假设我有classJava 如何使用反射类动态调用和setter及getter方法?,java,reflection,Java,Reflection,假设我有classAccountPojo和GetAccountPojo,其setter和getter方法如下 public class AccountPojo { private String dataList; private String dataSet; public String getDataList() { return dataList; } public void setDataList(String dataList
AccountPojo
和GetAccountPojo
,其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;
}
}
现在我有如下的类测试
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
}
}
不要使用原始类。如果您知道已经调用了哪个类,请使用类型化类
try {
AccountPojo obj = AccountPojo.class.newInstance();
Method setDataList = AccountPojo.class.getMethod("setDataList");
setDataList.setAccessible(true); // This is important if you want to access protected or private method. For public method you can skip
setDataList.invoke(obj, "123");
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
您是否尝试过获取被调用类的所有方法并按名称筛选出getter方法并调用它们
Method[] methods = cls.getDeclaredMethods();
for (Method m: methods) {
if(m.getName().startsWith("get")) {
m.invoke(clsInstance);
}
}
这解决了一半的问题,因为调用getter时没有任何参数。但如果需要调用setter方法,则需要指定参数。例如,调用接受字符串参数方法的setter,如下所示:
m.invoke(clsInstance, "some string argument");
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;
}
}
一种解决方案是让所有的setter接受一个对象类型值,并在将其赋给实际的类变量时对其进行类型转换
现在,您的pojo类将如下所示:
m.invoke(clsInstance, "some string argument");
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");
}
}
您可以尝试cls.getField(“dataList”).set(clsInstance,“newString”)代码>。如果属性是私有的,请尝试使用setter方法:cls.getMethod(“setDataList”).invoke(clsInstance,“newString”)代码>我有70多个POJO类,所以AccountPojo obj=AccountPojo.class.newInstance();不会帮我的。对于每个pojo,我必须调用不同的setter/getterb,但是您知道要事先使用哪个类和哪个方法吗?例如,您是否知道要使用参数a1、a2、a3
在类Aclass
上调用setA()
?