Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is because your class isn't named <code>CodeCircuit</code>. Remove that <code>if</code> condition and it will work.</p> <p>Also remove the line <code>Constructor c=clazz.getConstructor();</code> as it throws an exception.</p> <p>After making these changes, your own code prints</p> <pre><code>&gt;&gt; inner classes &gt;&gt; MyInnerClass inner class methods &gt;&gt; wantedMethod </code></pre> <h2>EDIT</h2> <p>Use this code to execute the method.</p> <pre><code> Class&lt;?&gt;[] classes = MyBigClass.class.getDeclaredClasses(); for (Class&lt;?&gt; clazz : classes) { if(clazz.getSimpleName().equals("MyInnerClass")) { Method method = clazz.getDeclaredMethod("wantedMethod", new Class[] {}); method.setAccessible(true); method.invoke(clazz.getDeclaredConstructor(MyBigClass.class).newInstance(new MyBigClass()), new Object[] {}); } } </code></pre> <p>The syntax is a bit strange, making you to use the outer class for getting hold of the inner class constructor. It behaves as if you have a constructor with the below signature defined in your inner class:</p> <pre><code>public MyInnerClass(MyBigClass bigClass) { } </code></pre> <p>But, I assume that's how Java handles inner (nested) classes using reflection.</p> <p>Note that you'll have to provide a <code>public</code> constructor for your inner class.</p> <pre><code>public class MyBigClass { private class MyInnerClass { public MyInnerClass() { System.out.println("hello"); } private void wantedMethod() { System.out.println("world"); } } } </code></pre> <p>You'll also have to <code>setAccessible(true)</code> on the private method in order to be able to invoke it.</p> <h2>EDIT 2</h2> <p>Upon further investigation, when I decompiled the generated <code>MyBigClass$MyInnerClass.class</code> class, I found that my hunch was right:</p> <pre><code>public class MyBigClass$MyInnerClass { public MyBigClass$MyInnerClass(MyBigClass paramMyBigClass) { System.out.println("hello"); } private void wantedMethod() { System.out.println("world"); } } </code></pre> <p><strong>Would be really glad if someone can throw some light into this behaviour</strong> </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.
    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.
 

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