-1

In the code below I want to pass the 'Races.class' as an argument to the method 'getAllRaces()'. How do I do this?

public static void getAllRaces() throws ClientProtocolException, IOException 
{
    Response myReponse = APIExecutor.getRequest("/races");
    GsonBuilder builder  = new GsonBuilder();
    Gson gson = builder.serializeNulls().setPrettyPrinting().create();

    Races races = gson.fromJson(myReponse.getReqResponse(), Races.class);
    ...
}
dave
  • 11,641
  • 5
  • 47
  • 65
Hashili
  • 113
  • 16
  • What's the question / issue? Looks like you've already done it.... – Brick Sep 21 '17 at 00:23
  • All the relevant info is provided by the [javadocs](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html). –  Sep 21 '17 at 00:25
  • I have updated my question. Here i want to pass the 'Races.class' as an argument to the method 'getAllRaces()'. – Hashili Sep 21 '17 at 00:26

2 Answers2

3

Change your method to:

public static void getAllRaces(Class<?> clazz) {
    ...
    Races races = gson.fromJson(myReponse.getReqResponse(), clazz);
    ...
}

You can call it thus:

getAllRaces(Races.class)
dave
  • 11,641
  • 5
  • 47
  • 65
1

Races.class as an object of type Class<Races>. In your case, you can use a ? capture to pass a class of unknown type:

public static void getAllRaces(Class<?> raceClass) {
    //...
}
rhobincu
  • 906
  • 1
  • 7
  • 22