简体   繁体   English

编译时出现Java通用错误:“类型参数不在类型变量T的范围内”

[英]Java Generic error in compilation time: “type argument is not within bounds of type-variable T”

I'm developing a proxy adapter to convert a request/response into another request/response using generics in Java. 我正在开发一个代理适配器,以使用Java中的泛型将请求/响应转换为另一个请求/响应。

I have a interface: 我有一个界面:

public interface ProxyAdapter<FROM, TO> {

    TO adapt(FROM o);
}

Implementation of adapter (using only the request case as example): 适配器的实现(仅以请求为例):

public interface RequestProxyAdapter<FROM, TO> extends ProxyAdapter<RequestEntity<FROM>, RequestEntity<TO>> { }

Abstract class: 抽象类:

public abstract class ProxyAdapterResolver<U extends HttpEntity<?>, T extends ProxyAdapter<U, U>> {

   private final Map<String, T> adapters;

   private final T defaultAdapter;

   public ProxyAdapterResolver(Collection<T> adapters, T defaultAdapter) {
       this.adapters = adapters.stream()
            .collect(Collectors.toMap(this::proxyNameResolver, Function.identity()));

       this.defaultAdapter = defaultAdapter;
   }
   // other methods...
}

Concrete class: 具体课程:

@Component
public class RequestProxyAdapterResolver extends ProxyAdapterResolver<RequestEntity<?>, RequestProxyAdapter<?, ?>> {
    @Autowired
    public RequestProxyAdapterResolver(Collection<RequestProxyAdapter<?, ?>> allAdapters) {
       super(allAdapters, a -> a);
    }
}

The exception happens in compilation time, into concrete class RequestProxyAdapterResolver . 例外发生在编译时,进入具体类RequestProxyAdapterResolver Here's the stack trace: 这是堆栈跟踪:

Error:(11, 108) java: type argument br.com.afferolab.attendancelist.core.proxy.service.RequestProxyAdapter<?,?> is not within bounds of type-variable T

obs: class RequestEntity is from org.springframework.http package obs:类RequestEntity来自org.springframework.http包

The exception is clear and I did read some questions about this issue in stack overflow. 异常很明显,我确实在堆栈溢出中阅读了有关此问题的一些问题。 However I couldn't find a solution for my case. 但是我找不到适合我的情况的解决方案。 Ex: 例如:

Java generics issue: Class "not within bounds of type-variable" error. Java泛型问题:类“不在类型变量的范围内”错误。

Type argument T is not within bounds of type-variable T(Java Generic) 类型实参T不在类型变量T(Java通用)的范围内

Inferring a generic type from a generic type in Java (compile time error) 从Java中的通用类型推断通用类型(编译时错误)

I spent about three days in that problem. 我花了大约三天时间解决这个问题。 Any ideas? 有任何想法吗? Thanks. 谢谢。

I think the problem is that the RequestEntity<?> given as the first parameter and the RequestEntity<?> implied upper bounds of the wildcard parameters to RequestProxyAdapater aren't being considered as the exact same type, but you're requiring them all to be type U . 我认为问题是, RequestEntity<?>作为第一个参数和RequestEntity<?>通配符参数的隐含上限RequestProxyAdapater不被认为是完全相同的类型,但你他们都需要到键入U

I find that when I'm having trouble getting generics to work together, a ? extends 我发现当我无法让泛型一起工作时,一个? extends ? extends or ? super ? extends还是? super ? super will often fix it. ? super经常会修复它。 Try this: 尝试这个:

public abstract class ProxyAdapterResolver<U extends HttpEntity<?>, T extends ProxyAdapter<? extends U, ? extends U>> {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM