Wednesday, December 18, 2013

getDeclareMethod(String, Class...) never accept null

At one day I was playing around with unit test using Java Reflection. I found it interesting when I pass in null as shown in code snippet below, a warning would be seen in Eclipse IDE.
private Method funcA;

funcA = TheClassA.class.getDeclaredMethod("funcA", null);
funcA.setAccessible(true);
Here is the warning message being shown on Eclipse IDE.
The argument of type null should explicitly be cast to Class<?>[] for the invocation of the varargs method getDeclaredMethod(String, Class<?>...) from type Class<TheTargetClass>. It could alternatively be cast to Class<?> for a varargs invocation
May be I have overlooked on the getDeclaredMethod’s declaration, as it shows:

Method java.lang.Class.getDeclaredMethod(String name, Class... parameterTypes)


The second argument is accepting a class type rather than an object. On top of that, it is a variable number of argument lists. Thus it is still acceptable if I do this.


funcA = TheClassA.class.getDeclaredMethod("funA");

No comments: