下面解释了我尝试制作混合 PyDev/Eclipse 插件项目的尝试。从那以后我发现,即使我创建了一个单独的 PyDev 项目,我也无法通过在插件项目中引用该项目来使用我的基于 PyDev Jython 的 Java 类。另一方面,我可以在不是插件项目的单独项目中使用 PyDev Java 类。
我有一个 Eclipse 插件,我也将它设置为 PyDev 项目。我基于Jython Book v1.0 文档第 10章和第 11章构建了它。
当我如下所示运行 Main 时,我看到
1 BUIDING-A 100 MAIN ST
当我尝试在设置为 PyDev 项目的插件项目中执行相同操作时(右键单击项目-> Pydev-> 设置为 Pydev 项目),我收到 ImportError:没有名为 Building 的模块。似乎该项目的插件性质胜过该项目的 PyDev 性质。
有任何想法吗?
我把 Main 函数放在下面,后面跟着一些支持代码。
package org.jython.book;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jython.book.interfaces.BuildingType;
import org.jython.book.util.JythonObjectFactory;
public class Main {
public static void main(String[] args) {
// Obtain an instance of the object factory
JythonObjectFactory factory = JythonObjectFactory.getInstance();
// Call the createObject() method on the object factory by
// passing the Java interface and the name of the Jython module
// in String format. The returning object is casted to the the same
// type as the Java interface and stored into a variable.
BuildingType building = (BuildingType) factory.createObject(
BuildingType.class, "Building");
// Populate the object with values using the setter methods
building.setBuildingName("BUIDING-A");
building.setBuildingAddress("100 MAIN ST.");
building.setBuildingId(1);
System.out.println(building.getBuildingId() + " " + building.getBuildingName() + " " +
building.getBuildingAddress());
}
}
这是 JythonObjectFactory,它应该与 [Jython Book v1.0 文档第 10 章][3] 中的内容完全一样,当然,更正了拼写错误:-)
package mypackage.files.util;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
/**
* Object factory implementation that is defined
* in a generic fashion.
*
*/
public class JythonObjectFactory {
private static JythonObjectFactory instance = null;
private static PyObject pyObject = null;
protected JythonObjectFactory() {
}
/**
* Create a singleton object. Only allow one instance to be created
*/
public static JythonObjectFactory getInstance(){
if(instance == null){
instance = new JythonObjectFactory();
}
return instance;
}
/**
* The createObject() method is responsible for the actual creation of the
* Jython object into Java bytecode.
*/
public static Object createObject(Object interfaceType, String moduleName){
Object javaInt = null;
// Create a PythonInterpreter object and import our Jython module
// to obtain a reference.
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("from " + moduleName + " import " + moduleName);
pyObject = interpreter.get(moduleName);
try {
// Create a new object reference of the Jython module and
// store into PyObject.
PyObject newObj = pyObject.__call__();
// Call __tojava__ method on the new object along with the interface name
// to create the java bytecode
javaInt = newObj.__tojava__(Class.forName(interfaceType.toString().substring(
interfaceType.toString().indexOf(" ")+1,
interfaceType.toString().length())));
} catch (ClassNotFoundException ex) {
Logger.getLogger(JythonObjectFactory.class.getName()).log(Level.SEVERE, null, ex);
}
return javaInt;
}
}