vendredi 26 février 2016

how to return an object after setting its property using reflection

I am using java reflection to create object and then using BeanUtils to set the property of object. I want to ask, how can I return the same object on which I have used BeanUtils to set property.

My code:

public Object createBOFromJSON(JSONObject jsonData,String strBeanName,String[] strParametrNames)
 {
  Object object = null;
  try {
      Class classDefinition = Class.forName(strBeanName);
      object = classDefinition.newInstance();
  } catch (InstantiationException e) {
      System.out.println(e);
  } catch (IllegalAccessException e) {
      System.out.println(e);
  } catch (ClassNotFoundException e) {
      System.out.println(e);
  }
  try {
        for(int j=0;j<strParametrNames.length;j++){
            String value=jsonData.getString(strParametrNames[j]);
            BeanUtils.setProperty(object, strParametrNames[j], value);      
        }

} catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
  catch(Exception ex){

  }
  return object;
 }

I am using this method like below code:

RWReportCustomerDB objRWReportCustomerDB=(RWReportCustomerDB) rwUtils.createBOFromJSON(requestedJSONObject, strBeanName, strBeanFields);


System.out.println(objRWReportCustomerDB.getDbLogin()+"   ***********");

in system.out.println I am getting null. why is it so. is it not the same object. how can i resolve to get same object.





Aucun commentaire:

Enregistrer un commentaire