I have a generic PostProcessor interface that looks like this:
public interface PostProcessor<T> {
public void process( T t );
}
I'm implementing a PostProcessor for generic lists that will call an appropriate postprocessor for each item in the list.
public class ListPostProcessor implements PostProcessor<List<?>> {
public void process(List<?> t) {
final PostProcessor<?> p = ...;
for( Object o : t )
if(p!=null)
p.process(o); // BUG can't pass type Object to process()
}
}
Ignoring the magic that sets p
(signified with ...
), my problem is that I can't call process(o)
because o
is an Object
, not a ?
. But I can't cast o
to the correct type because I don't know what that type is at compile time.
How can I invoke the process()
method on each item in the list without having to instantiate a separate ListPostProcessor
for every possible set of Types it might be used on?
UPDATE
Following a suggestion from flicken below, I tried using <? extends Object>
:
public class ListPostProcessor implements PostProcessor<List<? extends Object>> {
public void process(List<? extends Object> list) {
final PostProcessor<? extends Object> p = ... ;
if( p!=null )
for( Object o : list )
p.process(o); // BUG still a problem
}
}
But I get the same error from Eclipse:
The method process(capture#3-of ? extends Object) in the type PostProcessor<capture#3-of ? extends Object> is not applicable for the arguments (Object)