Note that there are some explanatory texts on larger screens.

plurals
  1. POReflexive calls go wrong because of getClass()
    primarykey
    data
    text
    <p>I'm facing something that I don't think is solvable, but it's worth a try. Assume you have the following classes :</p> <pre><code>package a; public class A { protected A() {} public static void staticMethod(A a) { // ... } } public class B extends A { public B() {} } </code></pre> <p>So you can instantiate <code>B</code> from other packages, but not <code>A</code>. Now assume you want to use <code>Class.getMethod</code>, only you're using some kind of reflexivity framework that goes something like :</p> <pre><code>public Object callStaticMethod(Class c, String methodname, Object[] args) { Class[] signature = new Class[args.length]; for (i = 0 ; i &lt; args.length ; ++i) { signature[i] = args.getClass(); } Method m = c.getMethod("methodname", signature); return m.invoke(null, args) } </code></pre> <p>This method is given to me as-is, and sadly I can not improve it. Now you may want to call the method like this :</p> <pre><code>package b; B b; callStaticMethod(A.class, "staticMethod", b); </code></pre> <p>...Except that will not work, because the signature generated (based on <code>getClass()</code> results will be <code>{B.class}</code>, and there is no method called <code>staticMethod</code> in <code>A</code> which takes a <code>B</code> as parameter. </p> <p>Would any of you know about that ?</p> <p><strong>BONUS</strong> : As an additional challenge, is there a way, with this method, to call <code>staticMethod</code> with a null parameter (which may also be perfectly valid, semantically)? I personally haven't found a way to.</p>
    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.
 

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