I may have just hit a nasty JDK varargs issue. If you have two constructors with the same number of non-vararg parameters and one of them has an additional varargs parameter, you need to be very careful which is called. Here is the code:
public void foo(Class klass, String str, String... names); public void foo(Object obj, String str);
It looks like Java favors matching on parameter numbers and not types. If you invoke this method like this:
It invokes the SECOND version and NOT the first! Looks like the JDK matches on the number of parameters (2) and ignores the fact that the first version of the method matches better because of typing and the fact that varargs is empty.
Thanks to a commenter, I found the section in the JLS that determines if variable argument methods are invoked:
“The first phase (Â§22.214.171.124) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.”
This states that in order to keep backwards compatibility, if first ignored methods with variable arguments (variable arity). This is ONLY for backwards compatibility and if JDK 5.0 was really a major release with no compatibility than my initial assessment of this being a bug would be true.