What is the ambiguity of this Java method call?

I got a "make is ambiguous" compilation error. I don't understand

I have two methods

public static <T> T make(String name,Class<T> parentClass,boolean rethrowRuntimeExceptions,Object... params) throws DLException

 public static <T> T make(String name,Object... params) throws DLException

This line of code is marked as ambiguous

String className = "clsNme";
  String one = "1";
  String two = "2";     
  SimpleFactory.make(className,Object.class,false,one,two);

This is a mistake

both method <T#1>make(String,Class<T#1>,boolean,Object...) in SimpleFactory and method <T#2>make(String,Class<T#2>,Object...) in SimpleFactory match
    [javac]   where T#1,T#2 are type-variables:
    [javac]     T#1 extends Object declared in method <T#1>make(String,Object...)
    [javac]     T#2 extends Object declared in method <T#2>make(String,Object...)

Does the existence of Boolean parameters make the first method closer than the second method?

If important, this is part of the powermock test This is a complete method

public void makeCallsMakeWithFalse() throws Throwable {
  Object expected = mock(Object.class);
  String className = "clsNme";

  String one = "1";
  String two = "2";

  spy(SimpleFactory.class);

  doReturn(expected).when(SimpleFactory.class);
  SimpleFactory.make(className,two);  // causes error

  Object observed = SimpleFactory.make(className,two); // doesn't cause error
  assertEquals(expected,observed);

  verifyStatic();
  SimpleFactory.make(className,two);  // causes error

}

If it helps: I use javac 1.8 0_ 77,Mokito 1.10. 19 and powermock 1.6 three

Solution

The problem is

Object... params

When calling simplefactory Make (classname, two); Java does not know whether to package "false" into a Boolean object and pass it as the first parameter of the "params" varargs array (Boolean extensions object) and use it

make(String name,Object... params)

Or whether to call

make(String name,Object... params)

Because the signature can also accept Boolean values before params varargs

Therefore, why it is not clear that both methods of signature apply

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>