简体   繁体   English

如何在Java中调用参数化方法?

[英]How to call parametrized method in Java?

I have such a method: 我有这样一种方法:

private static <T extends HomerMessage> HomerMessage postRequest(String path, HomerMessage json) throws IOException, HomerDoh
{
    RequestBody body = RequestBody.create(JSON, toJson(json));
    Request request = new Request.Builder().url("http://" + path).post(body).build();

    String response = new OkHttpClient().newCall(request).execute().body().string();
    System.out.println(response);
    JsonNode responseNode = new ObjectMapper().readValue(response, JsonNode.class);
    if(!"200".equals(responseNode.get("status")))
    {
        throw readData(response, new TypeReference<HomerDoh>() {});
    }
    return readData(response, new TypeReference<T>() {});
}

private static <T> T readData(String is, TypeReference<T> ref) throws IOException
{
    return mapper.readValue(is, ref);
}

All works fine, but I could not figure out how to call it... I have tried: 一切正常,但我不知道怎么称呼...我试过了:

     AuthResponse ar = HomerClient.postRequest(url + "/api/v1/session", auth);

The last expression does not compile. 最后一个表达式不编译。

How to call parametrized method in Java? 如何在Java中调用参数化方法?

AuthResponse extends HomerMessage AuthResponse扩展HomerMessage

您需要提供定义static方法的类的名称:

Exception ar = SomeClass.<Exception>getException();

Your code doesn't compile because it's not returning an AuthResponse : it is returning a HomerMessage . 您的代码无法编译,因为它没有返回AuthResponse :它正在返回HomerMessage

You can make the return type AuthResponse if you change the return type of the method to: 如果将方法的返回类型更改为:,则可以使返回类型为AuthResponse

private static <T extends HomerMessage> T postRequest(
    String path, HomerMessage json, TypeReference<T> typeRef)
        throws IOException, HomerDoh

which is the return type of your only normally-completing code path. 这是您唯一正常完成的代码路径的返回类型。


As noted by @SLaks, you can't use TypeReference with generics: 如@SLaks所述,您不能将TypeReference与泛型TypeReference使用:

new TypeReference<T>() {}

Because of erasure, this will be equivalent at runtime to: 由于存在擦除,因此在运行时等效于:

new TypeReference<Object>() {}

which is almost certainly not what you want - otherwise you could just have used that, and not had an issue calling the generic method in the first place. 几乎可以肯定这不是您想要的-否则您可以使用它,而首先调用通用方法就不会有问题。

You need to actually pass in the concrete TypeReference as a parameter: 您实际上需要传递具体的TypeReference作为参数:

private static <T extends HomerMessage> T postRequest(
    String path, HomerMessage json, TypeReference<T> typeRef)
        throws IOException, HomerDoh

then you can call this simply as: 那么您可以将其简单地称为:

AuthResponse response = postRequest(
    url + "/api/v1/session", auth,
    new TypeReference<AuthResponse>() {});

and the type T is inferred from the third parameter. 而类型T是从第三个参数推断出来的。

The expression Exception ar = <Exception>getException(); 表达式Exception ar = <Exception>getException(); cannot compile because the generic method parametrization idiom requires a class name (or variable name if the method wasn't static) prior to parametrization. 无法编译,因为通用方法参数化习惯用法在参数化之前需要一个类名(如果方法不是静态的,则需要一个变量名)。

For instance: 例如:

Exception ar = MyClass.<Exception>getException();

For instance methods: 例如方法:

  • Exception ar = theObject.<Exception>getException();
  • Exception ar = this.<Exception>getException();

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

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