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