title: Create object of a class containing anonymous inner class via reflection
body: <p>I'm trying to create an object of class <strong>Test</strong> containing anonymous inner class - <strong>ActionListener</strong> - via reflection.</p>
  
  <p>The class is loaded properly. I can list any fields, methods, contructors etc. The problem is, when I'm trying to create object with constructor containing ActionListeners for some buttons. Everything works fine, when listeners are commented out.</p>
  
  <p>When I'm using Constructor.newInstance() method, I'm getting:</p>
  
  <pre><code>java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at Engine.createObj(Engine.java:78)
      at Main$3.actionPerformed(Main.java:107)
      at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
      at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
      at java.awt.Component.processMouseEvent(Unknown Source)
      at javax.swing.JComponent.processMouseEvent(Unknown Source)
      at java.awt.Component.processEvent(Unknown Source)
      at java.awt.Container.processEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Window.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
      at java.awt.EventQueue.access$000(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
  Caused by: java.lang.IllegalAccessError: tried to access class Test.Test$2 from class Test.Test
      at Test.Test.&lt;init&gt;(Test.java:123)
      ... 42 more
  </code></pre>
  
  <p>Test.java:123 is where the listener is implemented of course. Test.Test$2 is anonymous inner class file.</p>
  
  <p>Loading class code which looks like working fine:</p>
  
  <pre><code>    public void loadClass(File file) {
      reflectClass = null;
      try {
          FileInputStream fis = null;
          fis = new FileInputStream(file);
          byte[] bytes = new byte[fis.available()];
          int read = fis.read(bytes);
          fis.close();
          if (read != bytes.length) {
              return;
          }
          reflectClass = defineClass(null, bytes, 0, bytes.length);
          resolveClass(reflectClass);
      } catch (Exception e1) {
          e1.printStackTrace();
      }
  }
  </code></pre>
  
  <p>And object creation:</p>
  
  <pre><code>public void createObj(Constructor ct) {
      Class[] types = ct.getParameterTypes();
      //... some params generating code, not important, i'm using non-param constructor.
      try {
          object = ct.newInstance(oParams); // Here is the problem.
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>