From: James H. <jh...@pa...> - 2001-06-15 22:29:06
|
Hi, So far I think Jython is pretty cool... but I've got a problem/question... First off, here's my sample code: "MyMap.java" =========================================== import java.io.Serializable; import java.util.HashMap; public class MyMap { private HashMap hash = new HashMap(10); public void MyMap() { } public void setAttribute(String key, Object attr) { hash.put(key, attr); } public Object getAtrribute(String key) { return hash.get(key); } public static void main(String[] args) { Properties props = new Properties(); props.setProperty("jython.home", "/jython"); PythonInterpreter.initialize(System.getProperties(),props,new String[0]); MyMap theMap = new MyMap(); theMap.setAttribute("myAttr", "myAttr is cool"); PythonInterpreter interp = new PythonInterpreter(); interp.set("theMap", theMap); interp.exec("print theMap.getAtrribute(\"myAttr\")"); interp.exec("theMap.setAttribute(\"crapKey\", \"crapVal\")"); MyMap newMap = (MyMap)interp.get("theMap", MyMap.class); System.err.println("GotBack: " + (String)newMap.getAtrribute("crapKey")); } } ======================================== This works just fine, as intended by printing the output: myAttr is cool GotBack: crapVal My problem is that I really need the method void setAttribute(String key, Object attr) to be this: void setAttribute(String key, Serializable attr) But when I make that change, rather than the expected output, I get: myAttr is cool Exception in thread "main" java.lang.ClassCastException: org.python.core.PyString at com.partnet.prototypes.mops.unittest.MyMap.main(MyMap.java:72) Can someone explain this to me? I really need to restrict the input objects to be of type Serializable. Thanks, James |