Hi There,
I have a class which maps a name to a class like so:
class Mapping {
private String name;
private Class runner;
public Mapping(String name, Class runnerClass){
m_name = name;
m_runner = runnerClass;
}
}
What I want to be able to do is to change that so the runnerClass can
be defined as only being the Class object of a Class which extends a
specific class ("EditorRunner" in this case).
Something like this compiles, but doesn't give the required results:
class Mapping {
private String name;
private Class<EditorRunner> runner;
public Mapping(String name, Class<EditorRunner> runnerClass){
m_name = name;
m_runner = runnerClass;
}
}
With this latter code, it works for EditorRunner.class as a parameter
but not for the .class member of any subclass of EditorRunner.
Anyone know how I can get the desired effect?
Cheers,
Peter.
Michael Rauscher - 18 Sep 2006 01:36 GMT
Peter Ashford schrieb:
> class Mapping {
> private String name;
[quoted text clipped - 11 lines]
>
> Anyone know how I can get the desired effect?
Use <? extends EditorRunner>.
To be more generic (I replaced runner because to me it's
indistinguishable how a "Mapping" is related to a "runner")
class Mapping<T> {
private String name;
private Class<? extends T> clazz;
public Mapping( String name, Class<? extends T> clazz ) {
this.name = name;
this.clazz = clazz;
}
}
Bye
Michael
Robert Klemme - 18 Sep 2006 02:21 GMT
> Use <? extends EditorRunner>.
>
[quoted text clipped - 10 lines]
> }
> }
I'd probably also consider to make Mapping the complete mapping (i.e.
all name - class pairs). Depending on the OP's problem to solve that
might or might not be more useful.
Kind regards
robert