简体   繁体   中英

Apache Log4j Ambiguous Overloaded Method

The interface org.apache.logging.log4j.Logger in Apache Log4j 2.12.1 has the following 2 methods:

void info(String message, Object... params);

void info(String message, Supplier<?>... paramSuppliers);

In my code, my intention was to call the second method and the second arg was a lambda paramSupplier. Surprisingly, at runtime, the first method was called, which printed the lambda's obj reference instead of the actual param value.

I am puzzled why these methods are not flagged as ambiguous by the compiler. This is on Java 11. I also see many other methods, in the same Logger interface, where the difference in method signature is just that one method takes an Object and the other method takes a Supplier<?> in the same position in the args list, with the types and order of all other method args matching.

I found the following two questions re: overloaded methods disambiguation but those answers did not seem to explain this.

  1. Ambiguous method call when overloading method with generics and lambdas
  2. (Java 8) java.util.function.Supplier

By your description, I can suppose that issue is in incorrect package name for Supplier interface, as rule we are using java.util.function.Supplier , but org.apache.logging.log4j.Logger interface expects Supplier interface from org.apache.logging.log4j.util.Supplier package, the second method will be called after changing the package name.

If everything is as I said above, the question of ambiguity is simple, signatures do not match for the second method and void info(String message, Object... params); method will be called as default for unmatched types of parameters, because any class in Java has been extended by Object class and any lambda expression is an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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