gpt4 book ai didi

java - 从 Kotlin 使用的 Java 库中的泛型和继承

转载 作者:行者123 更新时间:2023-12-02 13:16:21 28 4
gpt4 key购买 nike

我在实现一个使用来自 MockMvc 库的 Java 定义的泛型/接口(interface)类的 Kotlin 类时遇到问题。
库类:

public interface ConfigurableMockMvcBuilder<B extends ConfigurableMockMvcBuilder<B>> extends MockMvcBuilder {

// ...

<T extends B> T defaultRequest(RequestBuilder requestBuilder);

// ...

}

@FunctionalInterface
public interface MockMvcBuilderCustomizer {

/**
* Customize the given {@code builder}.
* @param builder the builder
*/
void customize(ConfigurableMockMvcBuilder<?> builder);

}
我想实现这个(在Java中):
@Component
public class ApiKeyHeaderMockMvcBuilderCustomizer implements MockMvcBuilderCustomizer {

@Override
public void customize(ConfigurableMockMvcBuilder<?> builder) {
RequestBuilder apiKeyRequestBuilder = MockMvcRequestBuilders.get("any")
.header("api-key-header", "apikeyvalue");
builder.defaultRequest(apiKeyRequestBuilder);
}
}
但是,当我尝试在 Kotlin 中重写上述类时,我无法弄清楚我应该在 customize 中使用什么通用约束。的参数和调用时 builder.defaultRequest
@Component
class ApiKeyHeaderMockMvcBuilderCustomizer : MockMvcBuilderCustomizer {
// is this generic right?
override fun customize(builder: ConfigurableMockMvcBuilder<*>) {
val apiKeyRequestBuilder: RequestBuilder =
MockMvcRequestBuilders.get("http://any")
.header("api-key-header", "apikeyvalue")

// won't compile, using Nothing compiles but fails at runtime
builder.defaultRequest<*>(apiKeyRequestBuilder)
}
}
在 IntelliJ Idea 中从 Java 复制粘贴代码将转换为以下 Kotlin 代码:
@Component
class ApiKeyHeaderMockMvcBuilderCustomizer : MockMvcBuilderCustomizer {
override fun customize(builder: ConfigurableMockMvcBuilder<*>) {
val apiKeyRequestBuilder: RequestBuilder = MockMvcRequestBuilders.get("any")
.header("api-key-header", "apikeyvalue")
builder.defaultRequest(apiKeyRequestBuilder)
}
}
defaultRequest 下出现错误:
Type inference failed: Not enough information to infer parameter T in 
fun <T : Nothing!> defaultRequest(requestBuilder: RequestBuilder) : T
Please specify it explicitly.
当我尝试使用 NothingNothing?作为 defaultRequest 的通用参数, 我得到运行时 NullPointerException代替方法调用。 defaultRequest 方法的实现是:
public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
extends MockMvcBuilderSupport implements ConfigurableMockMvcBuilder<B> {

@Override
public final <T extends B> T defaultRequest(RequestBuilder requestBuilder) {
this.defaultRequestBuilder = requestBuilder;
return self();
}

@SuppressWarnings("unchecked")
protected <T extends B> T self() {
return (T) this;
}

}

最佳答案

编译器要求显式类型:

Type inference failed: Not enough information to infer parameter T in 
fun <T : Nothing!> defaultRequest(requestBuilder: RequestBuilder) : T
Please specify it explicitly.
你可以像这样指定它:
@Component
class ApiKeyHeaderMockMvcBuilderCustomizer<T :Nothing?> : MockMvcBuilderCustomizer {
override fun customize(builder: ConfigurableMockMvcBuilder<*>) {
val apiKeyRequestBuilder: RequestBuilder = MockMvcRequestBuilders.get("any")
.header("api-key-header", "apikeyvalue")
builder.defaultRequest<T>(apiKeyRequestBuilder)
}
}

关于java - 从 Kotlin 使用的 Java 库中的泛型和继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63807526/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com