1

In my Spring boot project, I have several different methods that are each called with different parameters but they have a common list of elements, actions, which can be one of two types of objects (ActionCommon or ActionInstance) but both implement the same interface Action. However, despite using upper bound generics, I am getting no suitable method found error and I have no idea why.

This is a spring boot project consisting of several modules, this is a new module providing new functionality using existing data model (which I cannot edit). Much of the functionality is the same, the only difference between functions is the amount of arguments.

I've tried using directly the interface instead of generics, but my list returns as a list of specific objects using the interface and I cannot change that.

PriceService.java

public interface PriceService {

    List<Price> parseActionsToPrices(List<? extends Action> actions, Client client, Offer offer)

    List<Price> parseActionsToPrices(List<? extends Action> actions, Client client, Contract contract)

    ....more functions
}

OfferService.java

public class OfferServiceImpl implements OfferService

    @Autowired
    PriceService priceService

    @Override
    public List<Price> getPrices(Client client, Offer offer) {
        List<ActionCommon> actions = offer.getActions();

        return priceService.parseActionsToPrices(actions, client, offer)
    }
}

ContractService.java

public class ContractServiceImpl implements ContractService

    @Autowired
    PriceService priceService

    @Override
    public List<Price> getPrices(Client client, Contract contract) {
        List<ActionInstance> actions = contract.getActions();

        return priceService.parseActionsToPrices(actions, client, contract)
    }
}

Usage in question is

  1. Get a list of either ActionCommon or ActionInstance from a function.

  2. Call interface method with said list and the appropriate arguments .

  3. Expect result of function back

Instead, I get a (for example) no suitable method found for parseActionToPrices(List<ActionCommon>, Client, Offer) error. Same result for all the methods.

Michal Habel
  • 47
  • 1
  • 4
  • Somewhat related: https://stackoverflow.com/questions/5495383/java-generics-wildcard-question-list-extends-a, also https://stackoverflow.com/questions/25619676/java-generic-type-for-wildcard-extends-allow-to-add-only-null – Tom Jun 27 '19 at 11:18
  • Those issues don't really apply to this case, I do not need to add to the list, I just need to read from it so in theory, this should work. But for some reason, it doesn't invoke the right function. – Michal Habel Jun 27 '19 at 13:41

0 Answers0