The following explains my attempts at trying to make a hybrid PyDev/Eclipse plugin project. I have since discovered that even if I make a separate PyDev project, I cannot use my PyDev Jython-based Java classes by referencing the project in a plug-in project. On the other hand, I can use the PyDev Java classes in a separate project that is NOT a plugin project.
I've got an eclipse plugin that I've also set as a PyDev project. I built it based on Jython Book v1.0 documentation chapter 10 and chapter 11.
When I run Main as seen below, I see
1 BUIDING-A 100 MAIN ST
When I try to do the same thing within a plug-in project that is set as a PyDev project (right click on project->Pydev->Set as Pydev Project), I get an ImportError: no module named Building. It seems like the plugin nature of the project is trumping the PyDev nature of the project.
Any ideas?
I've put the Main function below followed by some support code.
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());
}
}
Here is the JythonObjectFactory which should be exactly like what is in [Jython Book v1.0 documentation chapter 10][3], with typos corrected, of course :-)
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;
}
}