Java calls Jython

Source: Internet
Author: User
1. A Java excuse class that can be mapped to Jython objects
  1. Package com. newbee;
  2. Public interface EmployeeType {
  3. Public String getEmployeeFirst ();
  4. Public String getEmployeeLast ();
  5. Public String getEmployeeId ();
  6. Public void setEmployeeId ();
  7. }

2. A factory method class for obtaining Java objects, Jython objects, and Jython function objects from Jython files

  1. Package com. newbee;
  2. Import java. util. HashMap;
  3. Import org. python. core. PyFunction;
  4. Import org. python. core. PyObject;
  5. Import org. python. util. PythonInterpreter;
  6. Public class JythonFactory {
  7. Private static final JythonFactory instance = new JythonFactory ();
  8. Private static final HashMap <String, PythonInterpreter> piMap = new HashMap <String, PythonInterpreter> ();
  9. Public static JythonFactory getInstance (){
  10. Return instance;
  11. }
  12. Public Object getJavaObjectFromJythonFile (String interfaceName, String pathToJythonModule ){
  13. Object javaObject = null;
  14. PythonInterpreter interpreter = cacheInterpreter (pathToJythonModule );
  15. String tempName = pathToJythonModule. substring (pathToJythonModule. lastIndexOf ("/") + 1 );
  16. TempName = tempName. substring (0, tempName. indexOf ("."));
  17. System. out. println (tempName );
  18. String instanceName = tempName. toLowerCase ();
  19. String javaClassName = tempName. substring (0, 1). toUpperCase () + tempName. substring (1 );
  20. String objectDef = "=" + javaClassName + "()";
  21. Interpreter.exe c (instanceName + objectDef );
  22. Try {
  23. Class JavaInterface = Class. forName (interfaceName );
  24. JavaObject = interpreter. get (instanceName). _ tojava _ (JavaInterface );
  25. } Catch (ClassNotFoundException ex ){
  26. Ex. printStackTrace (); // Add logging here
  27. }
  28. Return javaObject;
  29. }
  30. Public PyObject getPyObjectFromJythonFile (String typeName, String pathToJythonModule ){
  31. PyObject pyObject = null;
  32. PythonInterpreter interpreter = cacheInterpreter (pathToJythonModule );
  33. String instanceName = typeName. toLowerCase ();
  34. String objectDef = "=" + typeName + "()";
  35. Interpreter.exe c (instanceName + objectDef );
  36. PyObject = interpreter. get (instanceName );
  37. Return pyObject;
  38. }
  39. Public PyFunction getPyFunctionFromJythonFile (String funcName, String pathToJythonModule ){
  40. PyFunction pyFunction = null;
  41. PythonInterpreter interpreter = cacheInterpreter (pathToJythonModule );
  42. PyFunction = (PyFunction) interpreter. get (funcName, PyFunction. class );
  43. Return pyFunction;
  44. }
  45. Private pythoninterpreter cacheinterpreter (string pathtojythonmodule ){
  46. Pythoninterpreter interpreter = NULL;
  47. If (pimap. Get (pathtojythonmodule )! = NULL ){
  48. Interpreter = pimap. Get (pathtojythonmodule );
  49. } Else {
  50. Interpreter = new pythoninterpreter ();
  51. Interpreter.exe cfile (pathtojythonmodule );
  52. }
  53. Return interpreter;
  54. }
  55. }

3. Jython script file Employee. py

  1. # Jython source file
  2. From com. newbee import EmployeeType
  3. Class Employee (EmployeeType ):
  4. Def _ init _ (self ):
  5. Self. first = "Sean"
  6. Self. last = "Guo"
  7. Self. id = "731"
  8. Def getEmployeeFirst (self ):
  9. Return self. first
  10. Def getEmployeeLast (self ):
  11. Return self. last
  12. Def getEmployeeId (self ):
  13. Return self. id
  14. Def setEmployeeId (self, newId ):
  15. Self. id = newId
  16. Def getNunmberValue (seed ):
  17. V0 = 0
  18. For I in range (1, seed + 1 ):
  19. V0 + = I
  20. Return v0

4. Use the methods of the above classes

  1. Package com. newbee;
  2. Import java. util. regex. Matcher;
  3. Import java. util. regex. Pattern;
  4. Import org. python. core. PyException;
  5. Import org. python. core. PyFunction;
  6. Import org. python. core. PyInteger;
  7. Import org. python. core. PyObject;
  8. Import org. python. core. PyString;
  9. Public class SimpleEmbedded {
  10. Public static void main (String [] args) throws PyException {
  11. // Java
  12. EmployeeType eType = (EmployeeType) JythonFactory. getInstance (). getJavaObjectFromJythonFile ("com. newbee. EmployeeType", "jylib/Employee. py ");
  13. System. out. println ("Employee Name:" + eType. getEmployeeFirst () + "" + eType. getEmployeeLast ());
  14. System. out. println ("Employee ID:" + eType. getEmployeeId ());
  15. // Jython
  16. PyObject pyObject = JythonFactory. getInstance (). getPyObjectFromJythonFile ("Employee", "jylib/Employee. py ");
  17. System. out. println ("++ =" + pyObject. invoke ("getEmployeeId "));
  18. PyObject. invoke ("setEmployeeId", new PyString ("1999 "));
  19. System. out. println ("++ =" + pyObject. invoke ("getEmployeeId "));
  20. // Jython Function
  21. PyFunction pyFunction = JythonFactory. getInstance (). getPyFunctionFromJythonFile ("getNunmberValue", "jylib/Employee. py ");
  22. System. out. println ("*** =" + pyFunction. _ call _ (new PyInteger (10 )));
  23. }
  24. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.