Oli*_*ire 3 java generics guava
我有以下简短的自包含代码,在编译时显示错误.我拼命想要编译它.我通常不再遇到仿制药了,但是我放弃了这个,我请求团队的帮助.
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
public class CacheWithGenericsDoesNotCompile {
// Class definition can't be modified
static class Resource<T extends Resource<T>> {}
// Class definition can't be modified
static class ResourceType<T extends Resource<T>> {
public ResourceType(Class<T> type) {}
}
// Variable definition may be modified
static LoadingCache<Class<? extends Resource<?>>, ResourceType<? extends Resource<?>>> cache = CacheBuilder.newBuilder().build(
new CacheLoader<Class<? extends Resource<?>>, ResourceType<? extends Resource<?>>>() {
@Override public ResourceType<? extends Resource<?>> load(Class<? extends Resource<?>> key) throws Exception {
return new ResourceType<? extends Resource<?>>(key);
}
});
// Method definition can't be modified, method content may.
@SuppressWarnings("unchecked")
static <T extends Resource<T>> ResourceType<T> getResourceType(Class<T> type) {
return (ResourceType<T>)cache.getUnchecked(type);
}
}
Run Code Online (Sandbox Code Playgroud)
无法编译的行是:
return new ResourceType<? extends Resource<?>>(key);
Run Code Online (Sandbox Code Playgroud)
我知道它失败的原因:我可能不会new Xxxx<...>用问号(?)写.我不能以不同的方式编写这一行来编译其他行.
我有一个后备解决方案,在Resource没有泛型的情况下,但在可能的限制,我想Resource与泛型保持一致.
我对其中的泛型没有任何限制,LoadingCache除了我需要它被称为getResourceType(Class).
那么......我该如何修复这段代码呢?
一个忽略警告的解决方案:
static LoadingCache<Class<? extends Resource<?>>, ResourceType<? extends Resource<?>>> cache = CacheBuilder.newBuilder().build(
new CacheLoader<Class<? extends Resource<?>>, ResourceType<? extends Resource<?>>>() {
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override public ResourceType<? extends Resource<?>> load(Class<? extends Resource<?>> key) throws Exception {
return new ResourceType(key);
}
});
Run Code Online (Sandbox Code Playgroud)
正如@John B在评论中所建议的那样 .由于类型擦除,此代码在运行时与问题中的代码没有区别.
| 归档时间: |
|
| 查看次数: |
1608 次 |
| 最近记录: |