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?

2
  • 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. Commented Aug 17, 2017 at 14:09
  • There are already questions you can refer below post. best-way-of-invoking-getter-by-reflection Commented Aug 17, 2017 at 14:12

1 Answer 1

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) {}
}
Sign up to request clarification or add additional context in comments.

3 Comments

urielSilva:- What is getterMethod in the following statement getterMethod = commonBean.getClass().getMethod("getMediaUrl"+i);
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.
updated again, you must pass the object whose method is being called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.