Re: JAVADOC clarification needed in Class.getMethod
Hi, Jochen. -- -------------------- Software Innovation Driver yoonforh at gmail dot com Analyze, Reason, Reshape the Question PGP http://www.javadom.com/personal/yoonforhatgmaildotcom.asc <http://www.javadom.com/personal/yoonforhatgmaildotcom.asc>
2015. 8. 29., 오후 10:01, Jochen Theodorou <blackdrag@gmx.org> 작성:
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: http://blackdragsview.blogspot.com/ <http://blackdragsview.blogspot.com/>
2015. 8. 29., 오후 10:40, Kyung Koo Yoon <yoonforh@gmail.com> 작성:
Hi, Jochen.
-- -------------------- Software Innovation Driver yoonforh at gmail dot com Analyze, Reason, Reshape the Question PGP http://www.javadom.com/personal/yoonforhatgmaildotcom.asc <http://www.javadom.com/personal/yoonforhatgmaildotcom.asc>
2015. 8. 29., 오후 10:01, Jochen Theodorou <blackdrag@gmx.org <mailto:blackdrag@gmx.org>> 작성:
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.
Sorry, static is not correct. should have meant a void method.
bye blackdrag
-- Jochen "blackdrag" Theodorou blog: http://blackdragsview.blogspot.com/ <http://blackdragsview.blogspot.com/>
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 http://www.javadom.com/personal/yoonforhatgmaildotcom.asc
2015. 8. 29., 오후 10:01, Jochen Theodorou <blackdrag@gmx.org <mailto:blackdrag@gmx.org>> 작성:
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:http://blackdragsview.blogspot.com/
-- Jochen "blackdrag" Theodorou blog: http://blackdragsview.blogspot.com/
participants (2)
-
Jochen Theodorou
-
Kyung Koo Yoon