1. A Java excuse class that can be mapped to Jython objects
- Package com. newbee;
- Public interface EmployeeType {
- Public String getEmployeeFirst ();
- Public String getEmployeeLast ();
- Public String getEmployeeId ();
- Public void setEmployeeId ();
- }
2. A factory method class for obtaining Java objects, Jython objects, and Jython function objects from Jython files
- Package com. newbee;
- Import java. util. HashMap;
- Import org. python. core. PyFunction;
- Import org. python. core. PyObject;
- Import org. python. util. PythonInterpreter;
- Public class JythonFactory {
- Private static final JythonFactory instance = new JythonFactory ();
- Private static final HashMap <String, PythonInterpreter> piMap = new HashMap <String, PythonInterpreter> ();
- Public static JythonFactory getInstance (){
- Return instance;
- }
- Public Object getJavaObjectFromJythonFile (String interfaceName, String pathToJythonModule ){
- Object javaObject = null;
- PythonInterpreter interpreter = cacheInterpreter (pathToJythonModule );
- String tempName = pathToJythonModule. substring (pathToJythonModule. lastIndexOf ("/") + 1 );
- TempName = tempName. substring (0, tempName. indexOf ("."));
- System. out. println (tempName );
- String instanceName = tempName. toLowerCase ();
- String javaClassName = tempName. substring (0, 1). toUpperCase () + tempName. substring (1 );
- String objectDef = "=" + javaClassName + "()";
- Interpreter.exe c (instanceName + objectDef );
- Try {
- Class JavaInterface = Class. forName (interfaceName );
- JavaObject = interpreter. get (instanceName). _ tojava _ (JavaInterface );
- } Catch (ClassNotFoundException ex ){
- Ex. printStackTrace (); // Add logging here
- }
- Return javaObject;
- }
- Public PyObject getPyObjectFromJythonFile (String typeName, String pathToJythonModule ){
- PyObject pyObject = null;
- PythonInterpreter interpreter = cacheInterpreter (pathToJythonModule );
- String instanceName = typeName. toLowerCase ();
- String objectDef = "=" + typeName + "()";
- Interpreter.exe c (instanceName + objectDef );
- PyObject = interpreter. get (instanceName );
- Return pyObject;
- }
- Public PyFunction getPyFunctionFromJythonFile (String funcName, String pathToJythonModule ){
- PyFunction pyFunction = null;
- PythonInterpreter interpreter = cacheInterpreter (pathToJythonModule );
- PyFunction = (PyFunction) interpreter. get (funcName, PyFunction. class );
- Return pyFunction;
- }
- Private pythoninterpreter cacheinterpreter (string pathtojythonmodule ){
- Pythoninterpreter interpreter = NULL;
- If (pimap. Get (pathtojythonmodule )! = NULL ){
- Interpreter = pimap. Get (pathtojythonmodule );
- } Else {
- Interpreter = new pythoninterpreter ();
- Interpreter.exe cfile (pathtojythonmodule );
- }
- Return interpreter;
- }
- }
3. Jython script file Employee. py
- # Jython source file
- From com. newbee import EmployeeType
- Class Employee (EmployeeType ):
- Def _ init _ (self ):
- Self. first = "Sean"
- Self. last = "Guo"
- Self. id = "731"
- Def getEmployeeFirst (self ):
- Return self. first
- Def getEmployeeLast (self ):
- Return self. last
- Def getEmployeeId (self ):
- Return self. id
- Def setEmployeeId (self, newId ):
- Self. id = newId
- Def getNunmberValue (seed ):
- V0 = 0
- For I in range (1, seed + 1 ):
- V0 + = I
- Return v0
4. Use the methods of the above classes
- Package com. newbee;
- Import java. util. regex. Matcher;
- Import java. util. regex. Pattern;
- Import org. python. core. PyException;
- Import org. python. core. PyFunction;
- Import org. python. core. PyInteger;
- Import org. python. core. PyObject;
- Import org. python. core. PyString;
- Public class SimpleEmbedded {
- Public static void main (String [] args) throws PyException {
- // Java
- EmployeeType eType = (EmployeeType) JythonFactory. getInstance (). getJavaObjectFromJythonFile ("com. newbee. EmployeeType", "jylib/Employee. py ");
- System. out. println ("Employee Name:" + eType. getEmployeeFirst () + "" + eType. getEmployeeLast ());
- System. out. println ("Employee ID:" + eType. getEmployeeId ());
- // Jython
- PyObject pyObject = JythonFactory. getInstance (). getPyObjectFromJythonFile ("Employee", "jylib/Employee. py ");
- System. out. println ("++ =" + pyObject. invoke ("getEmployeeId "));
- PyObject. invoke ("setEmployeeId", new PyString ("1999 "));
- System. out. println ("++ =" + pyObject. invoke ("getEmployeeId "));
- // Jython Function
- PyFunction pyFunction = JythonFactory. getInstance (). getPyFunctionFromJythonFile ("getNunmberValue", "jylib/Employee. py ");
- System. out. println ("*** =" + pyFunction. _ call _ (new PyInteger (10 )));
- }
- }