I want to create a function that gets an List parameter and creates a certain jsonResult. I know in advance that every T object of mine will have a getId() function. So my method
private static <T> JsonObject createJsonAarrayResult(List<T> items, Class<T> type){
JsonObject jsonResult = new JsonObject();
JsonArray jsonResultItem = new JsonArray();
for (T item : items){
JsonObject jsonArrayItem = new JsonObject();
jsonResultItem.addProperty("id", item.getId());
}
return jsonResult;
}
The line that calls item.getId() of course gives me an error. How can I bypass it. I am using type to also to pass the class type to use it for casting. But I still get an error. Is there a way to use T but and call methods from specific object?