1

I have a method, which uses generics as follows

  public <T> ReturnThis<T> doSomething(Request req) {
        .
        .
     T result = ObjectMapperFactory.getMapper().readValue(req, new TypeReference<T>() {});
        .
        .
     return ReturnThis<T>;
  }

I do understand that there are @JsonSubTypes that can be used to explicitly let the Jackson know about type. But that is not my use case over here.

Alternatively, this could also be done

 public <T> ReturnThis<T> doSomething(Request req, TypeReference<T> type) {
       .
       .
    T result = ObjectMapperFactory.getMapper().readValue(req, type);
       .
       .
    return ReturnThis<T>;
 }

Is the below one more preferred or more safer?

chaitra.kear
  • 67
  • 3
  • 10
  • 2
    It depends. Do you want it to be generic? Use second method. Altough, first method makes no sense to me because you are not specifying the generic type anywhere in the signature. – Aniket Sahrawat Oct 12 '19 at 02:45
  • @AniketSahrawat The generic type is specified the same way as in method two. – Tom Oct 12 '19 at 02:49
  • 2
    @Tom It's redundant. It's not being used. – Aniket Sahrawat Oct 12 '19 at 02:50
  • @AniketSahrawat Both method bodies only have two lines of code each and you ignored them all? The generic type _is_ used in both version. – Tom Oct 12 '19 at 02:52
  • Actually my concern was about passing around the TypeReference vs initialising it. – chaitra.kear Oct 12 '19 at 02:55
  • 2
    @chaitra.kear As I have already mentioned, it depends on the needs. – Aniket Sahrawat Oct 12 '19 at 02:56
  • 2
    @Tom I can't find any reason for specifying a `T`. I can use `public ReturnThis` because I already know the type. I bet some IDE will raise a warning on method number 1. I hope you understand it now. – Aniket Sahrawat Oct 12 '19 at 02:58
  • @AniketSahrawat So you say you don't need `T`, but then create the generic type `SomeType`? I guess you meant `public ReturnThis`? – Tom Oct 12 '19 at 03:04
  • @Tom Yeah, that's what I mean. Sorry for the typo. But you got the point. – Aniket Sahrawat Oct 12 '19 at 03:05
  • What could help compare the scenarios better is to understand the type inference when the method generics are in use. In the former example, the type `T` while the mapping takes place is *solely inferred* based on the context from which the method `doSomething` would be called, while in the latter the inference would also *also* be based around the `TypeReference` passed down as an argument to that method. – Naman Oct 12 '19 at 04:35
  • On another note, might be that you can simplify the code in question to `public T doSomething(Request req) throws IOException { return new ObjectMapper().readValue( String.valueOf(req), new TypeReference() { }); }` and `public T doSomething(Request req, TypeReference type) throws IOException { return new ObjectMapper(). readValue(String.valueOf(req), type); }` with sample class `class Request { }` – Naman Oct 12 '19 at 04:36
  • Take a look at: [How do I parametrize response parsing in Java?](https://stackoverflow.com/questions/57581859/how-do-i-parametrize-response-parsing-in-java), [Deserializing or serializing any type of object using Jackson ObjectMapper and handling exceptions](https://stackoverflow.com/questions/56299558/deserializing-or-serializing-any-type-of-object-using-jackson-objectmapper-and-h), [Refactor method to use generic for deserialization](https://stackoverflow.com/questions/57975814/refactor-method-to-use-generic-for-deserialization) – Michał Ziober Oct 12 '19 at 11:28

0 Answers0