vendredi 1 mars 2019

Calling private instance variable from other applcation whose getter setter not avaliable

I am reading string xml data from other application in my project using JAXB.

They have provided their appdata.jar. (I dont have control on their classes). They have MessgeBody. In MessageBody they have defined one private instance variable without getter and setter. And because of this I am not able to obtain instance of perticular class.Remaining fields with getter n setter are accessible. I tried Java reflection , it is not working. Here is my code -

Class Test{

public void parseXMLStr(){
 String xmlStr="<xml><first></first><second></second><third></third></xml>"

 //I am able to get values of first and second as getter and setter defined.  
   First f =getMessageBody().getFirst();
   f.dosomething()..  //working
   Second s =getMessageBody().getSecond();
   s.dosomething()...//working
}

appdata.jar(other application) contains MessageBody as,

 class MessageBody{
  private First first;//getter setter provided
  private Second second;//getter setter provided
  private Third third;//getter setter NOT Provided
}

How I can obtain Third class instance and access variables inside it.

I tried Java reflection as below but I am getting null pointer exception(although data is there )

   Field f = obj.getClass().getDeclaredField("third"); 
   f.setAccessible(true);
   Third t = (Third) f.get(obj); 
   t.getName();//here getting nullpointer exception 





Aucun commentaire:

Enregistrer un commentaire