I would like to avoid "unchecked or unsafe operations" message from java compiler, but I have no idea how to do it, when I have a generic method, and would like to return the values from a generic Map. The Map's values should be the same generic type as the generic method, but I couldn't figure it out how to achieve.
I've created a bitbucket project to show the problem: https://bitbucket.org/ren-hoek/avoidsuppresswarning/overview
You can check the following snippet as a quick overview, but the best to clone the project and check it there.
public class VehicleServiceImplementation implements VehicleService {
private static Map<Long, VehicleResult> resultMap = new HashMap<Long, VehicleResult>();
static {
resultMap.put(5L, new VehicleResult<Car>(new Car(5L)));
}
public <CAR extends Car> VehicleResult<CAR> retrieve(final CarRequest request) {
return resultMap.get(request.getId());
}}
Please note that I have no access to the interface itself, so I can't change it's signature
Please note that it's not about how to deal with Object returning methods, but about how to deal with generic methods.