Javaでジェネリックを使用して要求/応答を別の要求/応答に変換するプロキシアダプタを開発しています。 (例としてのみ要求ケースを使用して)、アダプタのJavaコンパイル時の一般的なエラー:「型引数が型変数Tの境界内にありません」
public interface ProxyAdapter<FROM, TO> {
TO adapt(FROM o);
}
実装:
Iインターフェイスを有する
public interface RequestProxyAdapter<FROM, TO> extends ProxyAdapter<RequestEntity<FROM>, RequestEntity<TO>> { }
抽象クラス:
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...
}
具象クラス:
@Component
public class RequestProxyAdapterResolver extends ProxyAdapterResolver<RequestEntity<?>, RequestProxyAdapter<?, ?>> {
@Autowired
public RequestProxyAdapterResolver(Collection<RequestProxyAdapter<?, ?>> allAdapters) {
super(allAdapters, a -> a);
}
}
例外はコンパイル時に具体クラスRequestProxyAdapterResolverに発生します。ここでスタックトレースです:
Error:(11, 108) java: type argument br.com.afferolab.attendancelist.core.proxy.service.RequestProxyAdapter<?,?> is not within bounds of type-variable T
OBS:クラスはorg.springframework.httpパッケージ
例外は明確であると私は、スタックオーバーフローで、この問題に関するいくつかの質問を読んでなかったからであるRequestEntity。しかし、私の場合は解決策を見つけることができませんでした。例:
Java generics issue: Class "not within bounds of type-variable" error.
Type argument T is not within bounds of type-variable T(Java Generic)
Inferring a generic type from a generic type in Java (compile time error)
私はその問題に約3日間を過ごしました。何か案は?ありがとう。
あなたの抽象クラスは、 'T'は' ProxyAdapter'タイプでなければならず、 'FROM'と' TO'タイプは同じでなければならないと言います。しかし、あなたの 'RequestProxyAdapterResolver'は、' FROM'と 'TO'タイプの' ProxyAdapter'をワイルドカード(任意のタイプを意味する)として宣言して、その契約を変更します。 – tsolakp