Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4209315

(reflect) error in invoking inner class method thru reflection

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.6
    • core-libs



      Name: dbT83986 Date: 02/05/99


      Examine the two different constructions of JList (and the subsequent Method calls)
      within the "works" and "doesNotWork" methods in the following code. "doesNotWork"
      method throws a java.lang.IllegalAccessException.

      import java.lang.reflect.*;
      import com.sun.java.swing.*;

      public class ReflectionBug {
          public static String[] data = {"1", "2", "3"};

      public static void works()
      throws Exception {
      //======== 1st Construction =============
              DefaultListModel listModel = new DefaultListModel();
              for (int inx = 0; inx < data.length; inx++) {
                  listModel.addElement(data[inx]);
              }
              JList lst = new JList(listModel);
      //===================================
              System.out.println("list model class name = " + listModel.getClass().getName());

              Method m = listModel.getClass().getMethod("getSize", null);
              System.out.println("Size of the model = " + m.invoke(listModel, null));
      }

      public static void doesNotWork()
      throws Exception {
      //========= 2nd Construction ===========
              JList lst = new JList(data);
              ListModel listModel = lst.getModel();
      //==================================
              System.out.println("list model class name = " + listModel.getClass().getName());
              Method m = listModel.getClass().getMethod("getSize", null);
              System.out.println("Size of the model = " + m.invoke(listModel, null));
      }

      public static void main(String[] args) {
      try {
      works();
      } catch (Exception ew) {
      System.out.println("works failed");
      ew.printStackTrace();
      }

      try {
      doesNotWork();
      } catch (Exception enw) {
      System.out.println("doesNotWork failed");
      enw.printStackTrace();
      }

          }
      }
      (Review ID: 43547)
      ======================================================================

            iris Iris Clark
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: