private static transient JavaPlugin myPlugin = null;
public SomeClass(JavaPlugin plugin) {
if (myPlugin == null) myPlugin = plugin;
}
public <T> T getPlugin(Class<? extends JavaPlugin> plugin) {
return (T)myPlugin; // return casted version of "myPlugin" form above
}
在下面的行中调用它就可以了,并且尝试使用不扩展 JavaPlugin 的类将引发编译时错误。但是我怎样才能做到以上功能不需要@SuppressWarnings("unchecked")
?
MyPlugin nc = this.getPlugin(my.main.package.MyPlugin.class);