简体   繁体   中英

How to Dynamically call and setter and getter methods using Reflection class?

Say I have class AccountPojo and GetAccountPojo with its setter and getter methods as below.

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;
    }
}

Now I have class Test as below

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

    }   
}

Have you tried getting all the methods of the invoked class and filtering out only the getter methods by name and invoking them?

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

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

This solves our half problem, as getters are invoked without any arguments. But if you need to invoke a setter method you need to specify arguments. Ex, To invoke a setter which accepts string argument method as below:

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

One solution to could be make all the setters accept an object type value and typecast them while assigning it to actual class variables.

Now your pojo classes will look as below:

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;
}
}

Add below code to your main method:

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");
  }

}

Don't use raw class. If you know which class is called already, use typed class.

    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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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