views:

40

answers:

1

Hi,

what, if at all possible, would be a good solution to implement the following desired functionality where I want to:

  • Cast the return type of a routine to the type of the routine parameter, e.g.,

// Foo and Bar are two types of Common
interface Common{}
interface Foo extends Common {}
interface Bar extends Common {}

// Example interface
public List<? extends Common> getFeatureByType(Class<? extends Common> clazz);


// Example of what I want to achieve by using the (or a similar) interface. 
// Can this be achieve without using typecasting to (List<Bar>) or (List<Foo>) 
// and @SuppressWarning("unchecked") ?
List<Bar> bars = getFeatureByType(Bar.class);
List<Foo> foos = getFeatureByType(Foo.class);
+5  A: 
public <T extends Common> List<T> getFeatureByType(Class<T> clazz);
John Kugelman
That's awesome, thanks!
Johan Sjöberg