1

I have a problem with generic List. I spent few hours searching for a solution and the best I could do is below. Here is my code:

private <T extends SonarContainPaging> T getSonarObjectFromPage(String url, Map<String, Object> uriVariables, Class<T> klass, int page) {
    String json = //create the Json

    return objectMappingFromJson(json, klass);
}

private <T extends SonarContainPaging> void getSonarListContainingPaging(String url, Map<String, Object> uriVariables, Class<T> klass, List<T> list) {
    int page = 1;

    SonarContainPaging sonarObject = getSonarObjectFromPage(url, uriVariables, klass, page);
    page++;

    list.add(sonarObject);
    while (sonarObject.canContinueLooping(page)) {
        sonarObject = getSonarObjectFromPage(url, uriVariables, klass, page);
        page++;

        list.add(sonarObject);
    }
}

the method objectMappingFromJson call gson.fromJson(json, classOfObject); and return a generic object T.

When I tried to add the object sonarObject in the list, I get the following error:

The method add(T) in the type List is not applicable for the arguments (SonarContainPaging)

I think casting the sonarObject to the type (T) would solve the problem. But it is strange.
I declared that <T extends SonarContainPaging>, I don't get why there is this error.


EDIT: Like I was told by @Andy Turner, I changed the method to:

private <T extends SonarContainPaging> void getSonarListContainingPaging(String url, Map<String, Object> uriVariables, Class<T> klass,
                                                                         List<? super SonarContainPaging> list);

Which solve the problem of adding an object in the list. But it created an another one:

List<SonarComponentTree> list = new ArrayList<SonarComponentTree>();
getSonarListContainingPaging(urlBuilder.toString(), uriVariables, SonarComponentTree.class, list);

I cannot call the method getSonarListContainingPaging with a List of SonarComponentTree.

SonarComponentTree implements SonarContainPaging.

The method getSonarListContainingPaging(String, Map<String,Object>, Class<T>, List<? super SonarContainPaging>)
in the type SonarRestApiServiceImpl is not applicable for the arguments
(String, Map<String,Object>, Class<SonarComponentTree>, List<SonarComponentTree>)

If I change back the list parameter to List<? extends SonarContainPaging> list the issue is resolved, but I am back where I was at the beginning.
I can't add an another object in the list.

I read @Michael Myers solution in the link given by @Andy Turner and it seems that I should use neither extends nor super, actually, it may be a conception error.


Final Edit:

The first answer actually works, you just need to cast the object to (T)sonarObject. You just need to bear the warning.

Hamza Ince
  • 604
  • 17
  • 46
  • 1
    ``, but that can be *anything* that extends `SonarContainPaging`, not necessarily `SonarContainPaging` itself. If you want to add a `SonarContainPaging` to `list`, it needs to be `List` or `List super SonarContainPaging>`. – Andy Turner May 07 '19 at 11:43
  • Thank you, I will try to change method with something else. ^^ Should I mark the question as answered? – Hamza Ince May 07 '19 at 12:25
  • There is no mechanism for you to mark the question answered. – Andy Turner May 07 '19 at 12:25

0 Answers0