Java调用Jython的方法
来源:互联网 发布:实验数据作图软件 编辑:程序博客网 时间:2024/05/01 13:25
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)));
- }
- }
- Java调用Jython的方法
- Java调用jython
- Jython调用Sikuli库方法
- Jython 调用 Java 碰壁全纪录
- Jython和Java相互调用
- Jython 调用 Java 碰壁全纪录
- Java通过JYTHON调用python
- Learning Python -- Java 通过JyThon调用Python实现的规则
- Python与Java之间的相互调用——Jython
- java调用python(jython)的报错
- Java调用python——Jython
- eclipse中无法使用jython调用java
- jython学习笔记(一)-----Java调用python
- JAVA调用jython执行python文件
- [java]Java调用python工具类(非jython)
- (Python编程)Jython:Java的Python
- Jython:Java和Python代码的粘合剂
- Jython连接Java和Python的桥梁
- 如何修改Ubuntu 8.04的启动选项界面
- 自已动手制作ubuntu的GRUB启动画面
- JSP页面超链接的绝对路径和相对路径问题
- (转)session运作机制
- 经商十忌
- Java调用Jython的方法
- 收集的一点pc资料,作为一个备份吧
- asp.net身份验证方式|windows身份验证|forms验证
- 西安的互联网缺啥那
- 用vim处理字符的大小写转换
- 顺风自驾游
- ASP.NET 2.0 获取Web.config文件中的标签值
- httpRuntime
- ARM开发板初接触