1、一个可以和 Jython 对象映射的 Java 借口类
2、一个从 Jython 文件中获取 Java对象、Jython对象和 Jython函数对象的工厂方法类
3、Jython脚本文件 Employee.py
4、使用上述类的方法
- package com.newbee;
- public interface EmployeeType {
- public String getEmployeeFirst();
- public String getEmployeeLast();
- public String getEmployeeId();
- public void setEmployeeId();
- }
2、一个从 Jython 文件中获取 Java对象、Jython对象和 Jython函数对象的工厂方法类
- 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.exec(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.exec(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.execfile(pathToJythonModule);
- }
- return interpreter;
- }
- }
- # 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
- 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)));
- }
- }