Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>//prior to edit:</p> <p>Your problem is the fact that <code>getMethod</code> looks for a <code>public</code> member.</p> <p>From the <a href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getMethod%28java.lang.String,%20java.lang.Class...%29" rel="noreferrer"><code>Class.getMethod</code></a> (emphasis mine):</p> <blockquote> <p>Returns a <code>Method</code> object that reflects the specified <strong>public</strong> member method of the class or interface represented by this Class object</p> </blockquote> <p>So you have two options:</p> <ul> <li>Make <code>public void foo(String... s)</code> and use <code>getMethod</code></li> <li>Use <code>getDeclaredMethod</code> instead</li> </ul> <p>Note that the same difference exists for <code>getField/s</code> vs <code>getDeclaredField/s</code> and <code>getConstructor/s</code> vs <code>getDeclaredConstructor/s</code>.</p> <hr> <p>//<code>invoke</code> problem</p> <p>This is particularly nasty, but what happens is that <code>invoke(Object obj, Object... args)</code> makes it tricky if you need to pass an array of reference type as an only argument, because it is cast-able to <code>Object[]</code>, even though it should be wrapped inside a <code>new Object[1]</code> instead.</p> <p>You can do:</p> <pre><code>m.invoke(this, new Object[] {a}); // Bohzo's solution </code></pre> <p>This bypasses the vararg mechanism. More succinctly you can also do:</p> <pre><code>m.invoke(this, (Object) a); </code></pre> <p>The cast to <code>Object</code> makes the vararg mechanism do the work of creating the array for you.</p> <p>The trick is also needed when passing a <code>null</code> as an argument to varargs, and has nothing to do with reflection.</p> <pre><code>public void foo(String... ss) { System.out.println(ss[0]); } foo(null); // causes NullPointerException foo((String) null); // prints "null" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload