简体   繁体   中英

Passing class as parameter in java function

I am trying to pass Java class as parameter in function, reason behind it it that I have several pojo classes for different APIs and I am trying to create a single parser utility for all the API URL and pojo class, I have tried with little or no success.

below is the code example -

   public class util {

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
        List<JsonGenMovies> jsongen = null;
        String url = "http://www.dishanywhere.com/radish/v20/dol/movies/carousels/featured.json?nkey=0e1345ee597cf280c8a2cde367b6b894";
        getMovieParser(jsongen, url);

        List<JsonGenShow> jsongenShow = null;
        String URL = "http://www.dishanywhere.com/radish/v20/dol/shows/carousels/featured.json";
        getShowParser(jsongenShow, URL);
    }

    public static String[] getMovieParser (List<JsonGenMovies> jsongen, String url ) throws JsonParseException, JsonMappingException, IOException
    {
        URL jsonUrl = new URL(url);
        ObjectMapper objmapper = new ObjectMapper();
        jsongen = objmapper.readValue(jsonUrl, new TypeReference<List<JsonGenMovies>>() {});
        String[] shows = new String [jsongen.size()]; 
        int i = 0;
        for(JsonGenMovies element : jsongen) {
            shows[i++]=element.getName();
        }
        for(int j =0; j<shows.length;j++)
        {
            System.out.println(shows[j]);
        }
        return shows;   
    }

    public static String[] getShowParser (List<JsonGen> jsongenShow, String URL ) throws JsonParseException, JsonMappingException, IOException
    {
        URL jsonUrl = new URL(URL);
        ObjectMapper objmapper = new ObjectMapper();
        jsongenShow = objmapper.readValue(jsonUrl, new TypeReference<List<JsonGen>>() {});
        String[] shows = new String [jsongenShow.size()]; 
        int i = 0;
        for(JsonGen element : jsongenShow) {
            shows[i++]=element.getName();
        }
        for(int j =0; j<shows.length;j++)
        {
            System.out.println(shows[j]);
        }
        return shows;   
    }
}

in the line -

jsongen = objmapper.readValue(jsonUrl, new TypeReference<List<JsonGen>>() {});

I still have <List<JsonGen>> a hard coded class name which I am trying to replace with arguments. please if you can help.

Please reply with little explanation, one liner might not be my thing.

Regards Shek

If i understand correctly you want to have one function instead of two or more.
You can do that with generic type

Eg

public static <T> String[] getParser(List<T> jsongen, String url) throws JsonParseException, JsonMappingException, IOException {
  URL jsonUrl = new URL(url);
  ObjectMapper objmapper = new ObjectMapper();
  jsongen = objmapper.readValue(jsonUrl, new TypeReference<List<T>>() {
  });

  String[] shows = new String[jsongen.size()];
  int i = 0;
  for (T element : jsongen) {
    shows[i++] = element.getName();
  }

  for (int j = 0; j < shows.length; j++) {
    System.out.println(shows[j]);
  }
  return shows;
 }

If both JsonGenMovies and JsonGen extends/implements an interface or class, Eg JsonGenMovies extends Json then you can do this :

public static <T extends Json> String[] getParser(List<T> jsongen, String url)

T is a generic type, it does not really exist but it will be replaced at runtime by Class used for jsongen arg.

For example in :

List<String> myList;
getParser(myList, "http://blablabla.blalb.com");

All T are replaced by String .

I hope this is what you are looking for.
you can found more explanation about generic type here with better explanation than mine.

EDIT:

If you want to keep your POJO pattern then you can try this (not sure it will works)

public static String[] getParser(List<?> jsongen, String url) throws Exception {
  URL jsonUrl = new URL(url);
  ObjectMapper objmapper = new ObjectMapper();
  jsongen = objmapper.readValue(jsonUrl, new TypeReference<List<?>>() {
  });
  String[] shows = new String[jsongen.size()];
  int i = 0;
  for (Object element : jsongen) {
    Method method = element.getClass().getMethod("getName");
    shows[i++] = (String) method.invoke(element);
  }

  for (int j = 0; j < shows.length; j++) {
    System.out.println(shows[j]);
  }

  return shows;
}

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