Issue222866
Created on 2000-11-18.19:42:11 by bckfnn, last changed 2000-11-23.08:56:09 by bckfnn.
Messages | |||
---|---|---|---|
msg181 (view) | Author: Finn Bock (bckfnn) | Date: 2000-11-18.19:42:11 | |
I wrote a simple container that uses a TreeSet to contain specific types of objects. Basically, the add methods are overridden to ensure that only a specific type is added. The particular method of checking was chosen such that subclasses would not have to override the add methods. All other methods pass through to the TreeSet. The logic that checks the type fails in JPython, although it works fine in java itself. A code snippet is below: class EventHistory extends AbstractSet { private TreeSet data = null; private Class containedClass = Event.class; public Class getContainedClass() { return containedClass; } public static void main(String[] args) { EventHistory eh = new EventHistory(); Event e = new Event(); eh.add(e); } public EventHistory() { this(null, Event.class); } public EventHistory(Collection events) { this(events, Event.class); } protected EventHistory(Collection events, Class c) { data = new TreeSet(new EventComparator()); containedClass = c; if(events != null) { addAll(events); } } // Code includes debugging code added // Final code should start with the if statement public boolean add(Object parm1) { //TODO: Override this java.util.AbstractCollection method System.out.println("Contained class is " + containedClass.getName()); System.out.println("Argument class is " + parm1.getClass().getName()); System.out.println("Result is " + containedClass.isInstance(parm1)); Event e = (Event) parm1; System.out.println("Successfully cast!"); if(!(containedClass.isInstance(parm1))) throw new IllegalArgumentException("Argument must be of type Event"); return data.add(parm1); } public boolean addAll(Collection parm1) { //TODO: Override this java.util.AbstractCollection method Iterator i = parm1.iterator(); while(i.hasNext()) { Object o = i.next(); if(!(containedClass.isInstance(o))) { throw new IllegalArgumentException("Argument must contain only objects of type Event"); } } return data.addAll(parm1); } // Other required methods removed for simplicity } The following is the output of "java EventHistory": c:>java EventHistory Contained class is Event Argument class is Event Result is true Successfully cast! The following is a typed JPython example: c:>jpython JPython 1.1 on java1.2.2 (JIT: NONE) Copyright (C) 1997-1999 Corporation for National Research Initiatives >>> import Event, EventHistory >>> eh = EventHistory() >>> e = Event() >>> eh.add(e) Contained class is Event Argument class is Event Result is false Traceback (innermost last): File "<console>", line 1, in ? java.lang.ClassCastException: Event at EventHistory.add(EventHistory.java:50) at java.lang.reflect.Method.invoke(Native Method) at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java :158) at org.python.core.PyMethod.__call__(PyMethod.java:66) at org.python.core.PyObject.__call__(PyObject.java:272) at org.python.core.PyInstance.invoke(PyInstance.java:273) at org.python.pycode._pyx4.f$0(<console>) at org.python.pycode._pyx4.call_function(<console>) at org.python.core.PyTableCode.call(PyTableCode.java:155) at org.python.core.Py.runCode(Py.java:937) at org.python.core.Py.exec(Py.java:951) at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:121) at org.python.util.InteractiveInterpreter.runcode(InteractiveInterpreter .java:82) at org.python.util.InteractiveInterpreter.runsource(InteractiveInterpret er.java:63) at org.python.util.InteractiveInterpreter.runsource(InteractiveInterpret er.java:42) at org.python.util.InteractiveConsole.push(InteractiveConsole.java:84) at org.python.util.InteractiveConsole.interact(InteractiveConsole.java:6 3) at org.python.util.jpython.main(jpython.java:141) java.lang.ClassCastException: java.lang.ClassCastException: Event Full source code is available if necessary. |
|||
msg182 (view) | Author: Finn Bock (bckfnn) | Date: 2000-11-23.08:56:09 | |
This is fixed with Samuele's java load fix. (The EventHistory class must be public tho). |
History | |||
---|---|---|---|
Date | User | Action | Args |
2000-11-18 19:42:11 | bckfnn | create |
Supported by Python Software Foundation,
Powered by Roundup