When I compile my code I get 4 warnings - but how do I make them disappear? (and I don't mean with @SuppressWarnings)
PS: Obviously this must have been asked before - but either I cannot find it or I cannot apply it.
Output
warning: [unchecked] unchecked call to BaseWorker(C) as a member of the raw type BaseWorker
[warn] return new BaseWorker(this);
warning: [unchecked] unchecked conversion
[warn] found : com.crashnote.BaseWorker
[warn] required: com.crashnote.BaseWorker<C>
[warn] return new BaseWorker(this);
warning: [unchecked] unchecked call to SubWorker(C) as a member of the raw type SubWorker
[warn] return new SubWorker(this);
warning: [unchecked] unchecked conversion
[warn] found : com.crashnote.SubWorker
[warn] required: com.crashnote.SubWorker<C>
[warn] return new SubWorker(this);
Source Code
BaseConfig : instantiates BaseWorker
public class BaseConfig<C extends BaseConfig> {
public BaseConfig(final Object c) {
}
public BaseWorker<C> getWorker() {
return new BaseWorker(this);
}
}
BaseWorker
public class BaseWorker<C extends BaseConfig> {
public BaseWorker(final C config) {
}
}
SubConfig : instantiates SubWorker
class SubConfig<C extends SubConfig> extends BaseConfig<C> {
public SubConfig(final Object c) {
super(c);
}
@Override
public SubWorker<C> getWorker() {
return new SubWorker(this);
}
}
SubWorker : inherits from SubWorker
public class SubWorker<C extends BaseConfig> extends BaseWorker<C> {
public SubWorker(final C config) {
super(config);
}
}
Edit
I tried the provided solutions but for some reason it fails now.
A)
return new BaseWorker<BaseConfig>(this);
results in
incompatible types
[error] found : BaseWorker<BaseConfig>
[error] required: BaseWorker<C>
[error] return new BaseWorker<BaseConfig>(this);
B)
return new BaseWorker<C>(this);
results in
cannot find symbol
[error] symbol : constructor BaseWorker(BaseConfig<C>)
[error] location: class BaseWorker<C>
[error] return new BaseWorker<C>(this);
Hm, maybe I'm doing something wrong. This is Java 6 by the way, in case it matters.
Instead of returning plain
return new BaseWorker(this);
Make it generic, because your BaseWorker
class is defined as generic.
return new BaseWorker<C>(this);
Same rule applies for SubWorker
also.
return new SubWorker<C>(this);
Update:
Based on exception message, your constructor also missing generics.
Try
return new BaseWorker<C>(this);
and
return new SubWorker<C>(this);
Well, after applying the answers to my code I found out that they are actually wrong. Here is what seems to work.
BaseConfig now has a print() method (just to check we have the right type later) and adds a generic to the instantiation of BaseWorker .
public class BaseConfig<C extends BaseConfig> {
public BaseConfig(final Object c) {
}
public BaseWorker<C> getWorker() {
return new BaseWorker<C>(this);
}
public void print() {
//
}
}
Now this is where it gets interesting, BaseWorker has a constructor with a generic. This seems totally redundant - but it was the only way I found that worked. When you write just <C>
it appears to be a new "local" generic additionally to the one of the class and a compiler error is raised ( cannot find symbol: method print()
).
public class BaseWorker<C extends BaseConfig> {
public <C extends BaseConfig> BaseWorker(final C config) {
config.print();
}
}
SubConfig and SubWorker are changed accordingly.
PS: If this is utter nonsense please tell me :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.