JAVADOC clarification needed in Class.getMethod

Jochen Theodorou blackdrag at
Sat Aug 29 14:53:14 UTC 2015

yeah, you are right, I made a mistake. supplying a null argument t a
method call to call a Class varargs taking method is ambiguous. The
compiler will want you to cast to either Class, in which case the null
would be wrapped, or to Class[], in which case there will be no
wrapping. Example:

public class Foo {
  static void test(Class... params) { System.out.println(params==null);}
  public static void main(String[] args) {test((Class[])null);}

javap -c will show for the main method:

>   public static void main(java.lang.String[]);
>     Code:
>        0: aconst_null
>        1: checkcast     #4                  // class "[Ljava/lang/Class;"
>        4: invokestatic  #5                  // Method test:([Ljava/lang/Class;)V
>        7: return

which means no wrapping in an array. But you can bypass the cast through
a different method as well:

public class Foo {
  static void test(Class... params) { System.out.println(params==null);}
  static void bar(Class[] params) {test(params);}
  public static void main(String[] args){bar(null);}

since bar already supplies a Class[], it will be directly taken.

But maybe I did misunderstand the intention of your post. In that cae,
sorry for the noise

Am 29.08.2015 15:40, schrieb Kyung Koo Yoon:
> Hi, Jochen.
> -- 
> --------------------
>   Software Innovation Driver yoonforh at gmail dot com
> Analyze, Reason, Reshape the Question
>   PGP
>> 2015. 8. 29., 오후 10:01, Jochen Theodorou <blackdrag at 
>> <mailto:blackdrag at>> 작성:
>> Am 29.08.2015 10:56, schrieb Kyung Koo Yoon:
>>> Hi, all.
>>> The javadoc comment of java.lang.Class.getMethod API has following 
>>> clause.
>>> @CallerSensitive
>>>     public Method getMethod(String name, Class<?>... parameterTypes)
>>>         throws NoSuchMethodException, SecurityException
>>> "The {@code parameterTypes} parameter is an array of {@code Class}
>>> objects that identify the method's formal parameter types, in declared
>>>  order. If {@code parameterTypes} is {@code null}, it is
>>>  treated as if it were an empty array."
>>> As you know the method signature changed to use varargs since JDK 1.5,
>>> if parameterTypes is given null, the compiler will interpret the 
>>> parameter with “new Class[] { null }”.
>> you are making a wrong assumption here. If null is given there will be
>> no wrapping in an array.  varargs are defined that there is no wrapping
>> if the given argument type is compatible. For Class[] this is Class[]
>> and of course null. Even if you had been right, you could still give
>> produce a call with null by reflection or using the MethodHandles API
> You can check by simply decompiling the generated byte codes.
> vararg is not a JVM feature so the compiler pre-processes and the 
> compiler handles the given null as array’s first element.
> Originally class.getMethod(“methodName”, null) should have meant a 
> static method, but with vararg, it’s not.
>> bye blackdrag
>> --
>> Jochen "blackdrag" Theodorou
>> blog:

Jochen "blackdrag" Theodorou

More information about the core-libs-dev mailing list