Skip to content
Advertisement

Nested generic types in static method

I was trying to develop a generic method that could convert a JSON to a generic object that would have another instantiable generic object inside so I could use it as a parser in several places in my application.

I had thought of the following solution, but it doesn’t work:

public static <T, K> T<K> jsonToObjectType(String json, TypeReference<T<K>> type) {
    // More code
}

Is there any way to be able to perform such a method?

Advertisement

Answer

public static <T, K> T<K>

Your T has no bounds, meaning, T can be anything. It could be String.

String has no generics, so how can T<K> make sense? It doesn’t, hence, java doesn’t let you compile this.

I guess you could conceive of the notion of: “T is some specific type, could be anything, as long as it has exactly 1 generics param”, but java doesn’t have this, and never will, because that is structural type and java doesn’t do that.

However, note that a generics param can be any type, notably include types that are themselves parameterized. Here is a trival example:

public static <T> T coalesce(T a, T b) {
    return a == null ? b : a;
}

This method can be used like so:

String a = null;
String b = "Hello";

coalesce(a, b).toLowerCase();

There is absolutely no problem feeding it 2 List<String>, at which point the expression coalesce(listA, listB) would be of type List<String>. And that’s just with <T>, not with this <T, K> stuff.

I don’t quite know what jsonToObjectType is supposed to do, but assuming that it is supposed to take a string that contains JSON + some super-type-token (you can search the web for that term), which I’m 99.9% certain you have, then just remove K from it all, and you get precisely what you wanted:

public static <T> T jsonToObjectType(String json, TypeReference<T> type) {
   // code here 
}

and you can call it like so:

String json = "["Hello", "World!"]";
List<String> list = jsonToObjectType(json, new TypeReference<List<String>>() {});

and it’ll compile without warnings or errors and works.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement