JDK-4209315 : (reflect) error in invoking inner class method thru reflection
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 1.1.6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1999-02-05
  • Updated: 2012-09-28
  • Resolved: 2002-04-24
Related Reports
Duplicate :  
Description

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)
======================================================================

Comments
WORK AROUND Name: dbT83986 Date: 02/05/99 see works portion of the example. you need to specify a non-innerclass object to call the method on. ======================================================================
11-06-2004

EVALUATION Probably a duplicate of 4207233. william.maddox@Eng 1999-02-11 $ java -version java version "1.4.1-beta" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b10) Java HotSpot(TM) Client VM (build 1.4.1-beta-b10, mixed mode) $ java ReflectionBug list model class name = javax.swing.DefaultListModel Size of the model = 3 list model class name = javax.swing.JList$1 doesNotWork failed java.lang.IllegalAccessException: Class ReflectionBug can not access a member of class javax.swing.JList$1 with modifiers "public" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57) at java.lang.reflect.Method.invoke(Method.java:317) at ReflectionBug.doesNotWork(ReflectionBug.java:29) at ReflectionBug.main(ReflectionBug.java:41) Yes, this is a duplicate of 4270733 which is a duplicate of 4071957. Closing as a duplicate of the later bug. -- iag@sfbay 2002-04-24
24-04-2002