Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<T> cannot be resolved to a type?

I want to convert a json string to a List<Someclass> using jackson json library.

public static List<T> toList(String json, Class<T> type, ObjectMapperProperties objectMapperProperties){

        ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper(objectMapperProperties);

        try {
            return objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, type));
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

So if I pass Attribute.class as type, then it must return a List<Attribute>.

However, this gives me a compile time error

T cannot be resolved to a type

I guess generics part is not clear to me here. Any help is appreciated.

like image 453
Siddharth Trikha Avatar asked Feb 25 '16 08:02

Siddharth Trikha


1 Answers

you need to declare T first in your generic method, In your case it would be :

public static <T> List<T> toList(String json, Class<T> type,  ObjectMapperProperties objectMapperProperties)

for more info please check oracle documentation for generic methods:

https://docs.oracle.com/javase/tutorial/extra/generics/methods.html

like image 90
Sachin Gupta Avatar answered Sep 21 '22 01:09

Sachin Gupta