Java調用Jython的方法

來源:互聯網
上載者:User
1、一個可以和 Jython 對象映射的 Java 借口類
  1. package com.newbee;
  2. public interface EmployeeType {
  3.     
  4.     public String getEmployeeFirst();
  5.     public String getEmployeeLast();
  6.     public String getEmployeeId();
  7.     public void setEmployeeId();
  8.     
  9. }

2、一個從 Jython 檔案中擷取 Java對象、Jython對象和 Jython函數對象的Factory 方法類

  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.     
  8.     private static final JythonFactory instance = new JythonFactory();
  9.     private static final HashMap<String, PythonInterpreter> piMap = new HashMap<String, PythonInterpreter>();
  10.     public static JythonFactory getInstance() {
  11.         return instance;
  12.     }
  13.     public Object getJavaObjectFromJythonFile(String interfaceName, String pathToJythonModule) {
  14.         Object javaObject = null;
  15.         
  16.         PythonInterpreter interpreter = cacheInterpreter(pathToJythonModule);
  17.         
  18.         String tempName = pathToJythonModule.substring(pathToJythonModule.lastIndexOf("/") + 1);
  19.         tempName = tempName.substring(0, tempName.indexOf("."));
  20.         System.out.println(tempName);
  21.         String instanceName = tempName.toLowerCase();
  22.         String javaClassName = tempName.substring(0, 1).toUpperCase() + tempName.substring(1);
  23.         String objectDef = "=" + javaClassName + "()";
  24.         interpreter.exec(instanceName + objectDef);
  25.         try {
  26.             Class JavaInterface = Class.forName(interfaceName);
  27.             javaObject = interpreter.get(instanceName).__tojava__(JavaInterface);
  28.         } catch (ClassNotFoundException ex) {
  29.             ex.printStackTrace(); // Add logging here
  30.         }
  31.         return javaObject;
  32.     }
  33.     public PyObject getPyObjectFromJythonFile(String typeName, String pathToJythonModule) {
  34.         PyObject pyObject = null;
  35.         PythonInterpreter interpreter = cacheInterpreter(pathToJythonModule);
  36.         
  37.         String instanceName = typeName.toLowerCase();
  38.         String objectDef = "=" + typeName + "()";
  39.         interpreter.exec(instanceName + objectDef);
  40.         pyObject = interpreter.get(instanceName);
  41.         return pyObject;
  42.     }
  43.     
  44.     public PyFunction getPyFunctionFromJythonFile(String funcName, String pathToJythonModule) {
  45.         PyFunction pyFunction = null;
  46.         PythonInterpreter interpreter = cacheInterpreter(pathToJythonModule);
  47.         pyFunction = (PyFunction)interpreter.get(funcName,PyFunction.class);  
  48.         return pyFunction;
  49.     }
  50.     
  51.     private PythonInterpreter cacheInterpreter(String pathToJythonModule) {
  52.         PythonInterpreter interpreter = null;
  53.         if (piMap.get(pathToJythonModule)!=null) {
  54.             interpreter = piMap.get(pathToJythonModule);
  55.         } else {
  56.             interpreter = new PythonInterpreter();
  57.             interpreter.execfile(pathToJythonModule);
  58.         }
  59.         return interpreter;
  60.     }
  61.     
  62. }

3、Jython指令檔 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.   
  15.     def setEmployeeId(self, newId):
  16.         self.id = newId
  17.         
  18. def getNunmberValue(seed):
  19.     v0 = 0
  20.     for i in range(1,seed+1):
  21.         v0 +=i
  22.     return v0

4、使用上述類的方法

  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.         
  12.         // Java
  13.         EmployeeType eType = (EmployeeType) JythonFactory.getInstance().getJavaObjectFromJythonFile("com.newbee.EmployeeType", "jylib/Employee.py");
  14.         System.out.println("Employee Name: " + eType.getEmployeeFirst() + " " + eType.getEmployeeLast());
  15.         System.out.println("Employee ID: " + eType.getEmployeeId());
  16.         
  17.         // Jython
  18.         PyObject pyObject = JythonFactory.getInstance().getPyObjectFromJythonFile("Employee", "jylib/Employee.py");
  19.         System.out.println("+++="+pyObject.invoke("getEmployeeId"));
  20.         pyObject.invoke("setEmployeeId",new PyString("1999"));
  21.         System.out.println("+++="+pyObject.invoke("getEmployeeId"));
  22.         
  23.         // Jython Function
  24.         PyFunction pyFunction = JythonFactory.getInstance().getPyFunctionFromJythonFile("getNunmberValue", "jylib/Employee.py");
  25.         System.out.println("***="+pyFunction.__call__(new PyInteger(10)));
  26.     }
  27. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.