Java编译器:具有相同名称和不同签名的两个方法如何与一个方法调用匹配? [英] Java compiler: How can two methods with the same name and different signatures match a method call?

查看:175
本文介绍了Java编译器:具有相同名称和不同签名的两个方法如何与一个方法调用匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫做Container的课程:

public class Container {

    private final Map<String, Object> map = new HashMap<>();

    public void put(String name, Object value) {
        map.put(name, value);
    }

    public Container with(String name, Object value) {
        put(name, value);
        return this;
    }

    public Object get(String name) {
        return map.get(name);
    }

    public <R> R get(String name, Function<Object, R> mapper) {

        Object value = get(name);

        if (null == value) {
            return null;
        }

        return mapper
            .apply(value);
    }

    public <R> R get(String name, Class<R> type) {

        Object value = get(name);

        if (null == value) {
            return null;
        }

        if (type.isAssignableFrom(value.getClass())) {
            return type
                .cast(value);
        }

        throw new ClassCastException(String
            .format("%s -> %s", value.getClass(), type));
    }
}

和名为Token的类:

public class Token {

    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Token withValue(String value) {
        setValue(value);
        return this;
    }
}

最后是Token类的测试类

public class TokenTest {

    @Test
    public void verifyToken() {
        verify("bar", new Token()
            .withValue("bar"));
    }

    @Test
    public void verifyContainer() {
        Container tokens = new Container()
            .with("foo", "bar")
            .with("baz", "bat");

        verify("bar", tokens.get("foo", String.class));
        verify("bat", tokens.get("baz", String::valueOf));  // line 21
    }

    private void verify(String expected, String actual) {
        verify(expected, new Token()
            .withValue(actual));
    }

    private void verify(String expected, Token actual) {
        Assert
            .assertEquals(expected, actual.getValue());
    }
}

该测试仅在Eclipse中编译并运行文件.

The test compiles and runs just file in eclipse.

在通用线路上构建时

mvn clean test

出现编译错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:testCompile (default-testCompile) on project ambiguous: Compilation failure
[ERROR] /C:/data/projects/java/ambiguous/src/test/java/ambiguous/TokenTest.java:[21,9] reference to verify is ambiguous
[ERROR]   both method verify(java.lang.String,java.lang.String) in ambiguous.TokenTest and method verify(java.lang.String,ambiguous.Token) in ambiguous.TokenTest match

当我将行21更改为其中之一时,编译也会失败

The compilation also fails when I change line 21 to one of

verify("bat", tokens.get("baz", e -> String.valueOf(e)));
verify("bat", tokens.get("baz", e -> e.toString));

当我将行更改为其中之一

When I change the line to one of

verify("bat", tokens.get("baz", String.class));
verify("bat", tokens.get("baz", Object::toString));

编译成功.

我无法理解为什么会出现这种编译错误.

I cannot undestand why this compiliation error is raised.

我遇到了以下链接:装箱和拆箱多个通用类型和交集类型和此 eclipse编译器错误,但我仍然无法关联原因.

I came across the follwong links boxing and unboxing, multiple generic types and intersection types and this eclipse compiler bug but I still cannot relate to the mentioned causes.

我的问题是,当映射器String::valueOf传递给get方法时,是什么使编译器认为verify方法的两个签名都匹配?

My question is, what makes the compiler think that both signatures of the verify method are matching when the mapper String::valueOf is passed to the get method?

要进行编译,请使用以下jdk(带有maven和gradle):

For compilation the following jdk is used (with maven and gradle):

$ java -version
openjdk version "1.8.0_201-1-ojdkbuild"
OpenJDK Runtime Environment (build 1.8.0_201-1-ojdkbuild-b09)
OpenJDK 64-Bit Server VM (build 25.201-b09, mixed mode)

推荐答案

根据

参数表达式被认为与适用性有关 可能适用的方法m,除非具有以下之一 形式:

An argument expression is considered pertinent to applicability for a potentially applicable method m unless it has one of the following forms:

  • 隐式类型的lambda表达式 1 .
  • 不精确的方法引用表达式 2 .
  • [...]
  • An implicitly typed lambda expression1.
  • An inexact method reference expression2.
  • [...]

因此:

verify("bar", tokens.get("foo", e -> String.valueOf(e)));

在重载解决期间,隐式类型的lambda表达式e -> String.valueOf(e)从适用性检查中跳过-两种verify(...)方法均适用-因此存在歧义.

an implicitly typed lambda expression e -> String.valueOf(e) is skipped from the applicability check during overload resolution - both verify(...) methods become applicable - hence the ambiguity.

相比之下,这是一些有效的示例,因为类型是明确指定的:

In comparison, here are some examples that will work, because the types are specified explicitly:

verify("bar", tokens.get("foo", (Function<Object, String>) e -> String.valueOf(e)));

verify("bar", tokens.get("foo", (Function<Object, String>) String::valueOf));


1-隐式类型的lambda表达式是lambda表达式,其中推断了其所有形式参数的类型.
2-一个不精确的方法引用-一个具有多个重载的对象.


1 - An implicitly typed lambda expression is a lambda expression, where the types of all its formal parameters are inferred.
2 - An inexact method reference - one with multiple overloads.

这篇关于Java编译器:具有相同名称和不同签名的两个方法如何与一个方法调用匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆