如何修复在java中扩展泛型方法的警告?



在第三个库中有一个接口,不能修改。我需要扩展它。代码如下:

// 3rd interface
interface Client {
void create(String path);
void delete(String path);
List<String> getChildren(String path);
<T> T getChildListener(String path);
xxxx
}
// 3rd abstract
AbstractClient<T> implements Client {
xxx
T getChildListener(String path) {
return xxx;
}
xxx
}

我的代码如下:

class MyClient extends AbstractClient<PathWatcher> {
...
}

编译器警告:

返回类型要求从xxx到T的未检查转换

我尝试添加@SuppressWarnings(xxx)与cast, warning, all。
它不工作。如何修复此警告?
Ican not修改第三个库

我不确定你到底想要什么。但是你真正想从

得到什么呢?
interface Client {
void create(String path);
void delete(String path);
List<String> getChildren(String path);
<T> T getChildListener(String path);
}

以这种方式指定似乎有点奇怪,因为我不认为接口本身知道T到底是什么。

一个更好的接口设置可能如下:

interface Client<T> {
void create(String path);
void delete(String path);
List<String> getChildren(String path);
T getChildListener(String path);
}
public class AbstractClient<T> implements Client<T> {
T getChildListener(String path) {
return xxx;
}
}
public class MyClient extends AbstractClient<PathWatcher> {
...
}

不确定,如果这是你想要的。

一个折衷方案:在MyClient类中添加@SuppressWarnings("unchecked")

最新更新