2

I have a bean class and i want to fetch MediaContentType0, MediaContentType1, MediaContentType2, MediaContentType3 in a loop i have get json string and convert it into java class using gson

       Gson gson=new Gson();
    CommonBean commonBean=gson.fromJson("JsonString",CommonBean.class)
    for(int i=0;i<numberOfMedia;i++)
            {
     commonBean.getMediaUrl0();
    commonBean.getMediaUrl1();
  commonBean.getMediaUrl2();
  //but i want it to fetch dynamically by iTh element.
 like-
 commonBean.getMediaUrl+i+();

How Could this possibe? Please Suggest. Thanks }

mybean class is following :- 
public class CommonBean {
public String to;
public String from;
public String body;
public String numMedia;
public String MediaContentType0 ;
public String MediaContentType1 ;
public String MediaContentType2 ;
public String MediaContentType3;
public String getTo() {
    return to;
}
public void setTo(String to) {
    this.to = to;
}

public String getFrom() {
    return from;
}
public void setFrom(String from) {
    this.from = from;
}
public String getBody() {
    return body;
}
public void setBody(String body) {
    this.body = body;
}
public String getNumMedia() {
    return numMedia;
}
public void setNumMedia(String numMedia) {
    this.numMedia = numMedia;
}
public String getMediaContentType0() {
    return MediaContentType0;
}
public void setMediaContentType0(String mediaContentType0) {
    MediaContentType0 = mediaContentType0;
}
public String getMediaContentType1() {
    return MediaContentType1;
}
public void setMediaContentType1(String mediaContentType1) {
    MediaContentType1 = mediaContentType1;
}
public String getMediaContentType2() {
    return MediaContentType2;
}
public void setMediaContentType2(String mediaContentType2) {
    MediaContentType2 = mediaContentType2;
}
public String getMediaContentType3() {
    return MediaContentType3;
}
public void setMediaContentType3(String mediaContentType3) {
    MediaContentType3 = mediaContentType3;
}
}

Please suggest how to fetch these element dynamically using getter method?

shriram
  • 189
  • 4
  • 12
  • The **Reflection API** lets you dynamically call methods of classes (and a bunch of other useful dynamic things). The usage is very easy, just start with something of type `Class`, for example by `commonBean.getClass()` and then you have access to some useful methods. – Zabuzard Aug 17 '17 at 14:09
  • There are already questions you can refer below post. [best-way-of-invoking-getter-by-reflection](https://stackoverflow.com/questions/2638590/best-way-of-invoking-getter-by-reflection) – Deepak Singh Aug 17 '17 at 14:12

1 Answers1

6

You can use the Java Reflection API

for(int i=0;i<numberOfMedia;i++) {
  try {
    Method getterMethod = commonBean.getClass().getMethod("getMediaUrl"+i);
    getterMethod.invoke(commonBean);
  } catch(Exception e) {}
}
urielSilva
  • 400
  • 1
  • 11
  • urielSilva:- What is getterMethod in the following statement getterMethod = commonBean.getClass().getMethod("getMediaUrl"+i); – shriram Aug 17 '17 at 14:37
  • urielSilva:- The method invoke(Object, Object...) in the type Method is not applicable for the arguments (). why this not call method without argument getterMethod.invoke(); it gives compile time error. – shriram Aug 17 '17 at 15:12
  • updated again, you must pass the object whose method is being called. – urielSilva Aug 17 '17 at 16:10