I have a bunch of simple interfaces:
interface County extends Line{}
interface Country<C extends Line> extends LineContainer<C> {}
interface Line {}
interface LineContainer <L extends Line> {
public List<L> getLines();
}
And a Service Method
public static <L extends Line,C extends LineContainer<L>> C getContainer( Class<C> containerType, Class<L> lineType ){
...somthing...
Calling the Service Method
Country<County> c = getContainer( Country.class, County.class );
faces no error, but the checker says:
Type safety: The expression of type Country needs unchecked conversion to conform to Country
I don't understand that: By calling the service method with County as the L-LineType and C is the Container for L and C is given by Country as the C-Type, thus, I expected type inference would conclude, that a Country object will be served.
Can anyone explain, why I am wrong and if and how I can achieve what I want to?
Background: The idea is - as a user of the service - i can freely combine containers and lines just as needed (as long as the service provider can serve these)
This is because the compiler isn't sure that
Country.class
matches the signatureCountry<County>
.Country.class
is considered raw type.If you write this:
and:
Obviously this works.
Now imagine I split the same code into another way:
This will again give warning at compile time, because of raw type.