Reputation: 172
The following Java code will not compile.
public <DO extends ServerDataObject> ProxyDataObject<DO> convertToProxy(Class<DO> doClass) throws Exception{
if(getId()==0)return null;
ProxyDataObject<DO> proxy = (ProxyDataObject<DO>) doClass.newInstance();
proxy.setID(getId());
return proxy;
}
public interface ProxyDataObject<DO extends ServerDataObject> extends ServerDataObject, DataTransferInterface {
public void setID(int id);
@Ignore
public String getIDName();
}
The following message is given:
Bound mismatch: The type DO is not a valid substitute for the bounded parameter DO extends ServerDataObject of the type ProxyDataObject
This doesn't make sense to me.
Upvotes: 0
Views: 207
Reputation: 6707
Strange. It compiles fine for me with Java 1.6.0_26, albeit with a warning.
The first problem is that your method signature should read either:
public <DO extends ServerDataObject> ProxyDataObject<DO> convertToProxy(
Class<? extends ProxyDataObject<DO>> doClass
) throws Exception {
if(getId()==0)return null;
ProxyDataObject<DO> proxy = doClass.newInstance();
proxy.setID(getId());
return proxy;
}
or:
public <DO extends ProxyDataObject<?>> DO convertToProxy(Class<DO> doClass) throws Exception{
if(getId()==0)return null;
DO proxy = doClass.newInstance();
proxy.setID(getId());
return proxy;
}
depending on what you actually meant.
Second, DO extends ServerDataObject, not ProxyDataObject. With the inheritance you have defined it is possible for an instance of ServerDataObject to not be a ProxyDataObject. Therefore you get the error you are seeing.
Upvotes: 0
Reputation: 122429
doClass
is type Class<DO>
, so doClass.newInstance()
has type DO
. How can you possibly cast this to ProxyDataObject<DO>
?
Upvotes: 1