简体   繁体   English

将类作为参数传递给Java函数

[英]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. 我试图将Java类作为函数中的参数传递,其背后的原因是我有多个针对不同API的pojo类,并且试图为所有API URL和pojo类创建一个解析器实用程序,我尝试了很少或没有成功。

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. 我仍然有<List<JsonGen>>一个硬编码的类名,我正尝试用参数替换它。 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 您可以使用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 : 如果JsonGenMoviesJsonGen扩展/实现了接口或类,例如JsonGenMovies extends Json则可以执行以下操作:

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. T是一个泛型类型,它实际上并不存在,但在运行时将被jsongen arg使用的Class替换。

For example in : 例如在:

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

All T are replaced by String . 所有TString替换。

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) 如果您想保留POJO模式,则可以尝试执行此操作(不确定是否可以使用)

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM