-
-
Save sinistersnare/f2af88d29b6351ea6493 to your computer and use it in GitHub Desktop.
jythonGDX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from com.sinsnare.factories import EmployeeType | |
class Employee(EmployeeType): | |
def __init__(self): | |
pass | |
def getEmployeeFirst(self): | |
return "poop" | |
def getEmployeeLast(self): | |
return "last" | |
def getEmployeeId(self): | |
return "1337" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sinsnare.factories; | |
public interface EmployeeType { | |
public String getEmployeeFirst(); | |
public String getEmployeeLast(); | |
public String getEmployeeId(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
****************** STACKTRACE ******************* | |
Traceback (most recent call last): | |
File "<string>", line 1, in <module> | |
ImportError: No module named employee | |
Sep 24, 2013 12:07:26 AM com.sinsnare.factories.JythonObjectFactory createObject | |
SEVERE: null | |
org.plyjy.exceptions.ModuleNotFoundException: The module 'employee' is not found. ******* END STACKTRACE | |
at com.sinsnare.factories.JythonObjectFactory.createObject(JythonObjectFactory.java:51) | |
at com.sinsnare.factories.Main.main(Main.java:7) | |
org.plyjy.exceptions.ModuleNotFoundException: The module 'employee' is not found. ******* END STACKTRACE | |
at com.sinsnare.factories.JythonObjectFactory.createObject(JythonObjectFactory.java:51) | |
at com.sinsnare.factories.Main.main(Main.java:7) | |
Exception in thread "main" java.lang.NullPointerException | |
at com.sinsnare.factories.JythonObjectFactory.createObject(JythonObjectFactory.java:62) | |
at com.sinsnare.factories.Main.main(Main.java:7) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sinsnare.factories; | |
/** | |
* JythonObjectFactory | |
* | |
* This loosely-coupled object factory implementation allows for use of any | |
* Jython class from within Java code. This particular implementation makes | |
* use of the PythonInterpreter to import and obtain reference to a Jython | |
* module that is somewhere on the sys.path. | |
* | |
* Implementation by Josh Juneau | |
* | |
* @author juneau | |
*/ | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import org.plyjy.exceptions.ModuleNotFoundException; | |
import org.python.core.PyObject; | |
import org.python.util.PythonInterpreter; | |
public class JythonObjectFactory { | |
private static JythonObjectFactory instance = null; | |
private static PyObject pyObject = null; | |
protected JythonObjectFactory() { | |
} | |
public static synchronized JythonObjectFactory getInstance() { | |
if (instance == null) { | |
instance = new JythonObjectFactory(); | |
} | |
return instance; | |
} | |
// todo: implement args. | |
public static Object createObject(Object interfaceType, String moduleName, | |
String className) { | |
Object javaObj = null; | |
PythonInterpreter interpreter = new PythonInterpreter(); | |
try { | |
try { | |
//interpreter.exec("import sys; print 'SYSTEM PATH',sys.path"); | |
interpreter.exec("from " + moduleName + " import " + className); | |
pyObject = interpreter.get(className); | |
} catch (Exception ie) { | |
System.err | |
.println("****************** STACKTRACE *******************"); | |
ie.printStackTrace(); | |
throw new ModuleNotFoundException("The module '" + moduleName | |
+ "' is not found. ******* END STACKTRACE"); | |
} | |
} catch (ModuleNotFoundException ex) { | |
Logger.getLogger(JythonObjectFactory.class.getName()).log( | |
Level.SEVERE, null, ex); | |
ex.printStackTrace(); | |
} | |
try { | |
PyObject newObj = pyObject.__call__(); | |
javaObj = 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 javaObj; | |
} | |
public static Object createObject(Object interfaceType, String moduleName) { | |
return createObject(interfaceType, moduleName, moduleName); | |
} | |
// public static Object createObject(Object interfaceType) { | |
// } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sinsnare.factories; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.ByteBuffer; | |
import java.nio.charset.Charset; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.Properties; | |
import org.python.util.PythonInterpreter; | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
//listFilesForFolder(new File(".")); | |
//System.out.println(System.getProperty("user.dir")); | |
// String employeeFile = readFile("employee.py", | |
// StandardCharsets.UTF_8); | |
PythonInterpreter interp = new PythonInterpreter(); | |
// interp.exec(employeeFile); | |
interp.exec("import sys"); | |
interp.exec("sys.path.append('/home/sinistersnare/Development/testing/jython/factories/py')"); | |
EmployeeType empl = | |
(EmployeeType) JythonObjectFactory.createObject(EmployeeType.class, "employee", "Employee"); | |
System.out.println(empl.getEmployeeFirst()); | |
} | |
public static void listFilesForFolder(final File folder) { | |
for (final File fileEntry : folder.listFiles()) { | |
if (fileEntry.isDirectory()) { | |
listFilesForFolder(fileEntry); | |
} else { | |
System.out.println(fileEntry.getName()); | |
} | |
} | |
} | |
public static String readFile(String path, Charset encoding) | |
throws IOException { | |
byte[] encoded = Files.readAllBytes(Paths.get(path)); | |
return encoding.decode(ByteBuffer.wrap(encoded)).toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sinistersnare@aelita [05:43:44] [~/Development/testing/jython/factories] | |
-> % tree | |
. | |
├── bin | |
│ └── com | |
│ └── sinsnare | |
│ └── factories | |
│ ├── EmployeeType.class | |
│ ├── JythonObjectFactory.class | |
│ └── Main.class | |
├── libs | |
│ ├── cachedir | |
│ │ └── packages | |
│ │ ├── charsets.pkc | |
│ │ ├── dnsns.pkc | |
│ │ ├── java-atk-wrapper.pkc | |
│ │ ├── jce.pkc | |
│ │ ├── jsse.pkc | |
│ │ ├── jython.pkc | |
│ │ ├── localedata.pkc | |
│ │ ├── packages.idx | |
│ │ ├── PlyJy.pkc | |
│ │ ├── pulse-java.pkc | |
│ │ ├── resources.pkc | |
│ │ ├── rhino.pkc | |
│ │ ├── rt.pkc | |
│ │ ├── sunjce_provider.pkc | |
│ │ ├── sunpkcs11.pkc | |
│ │ └── zipfs.pkc | |
│ ├── jython.jar | |
│ └── PlyJy.jar | |
├── py | |
│ ├── employee.py | |
│ └── employee$py.class | |
└── src | |
└── com | |
└── sinsnare | |
└── factories | |
├── employee$py.class | |
├── EmployeeType.java | |
├── JythonObjectFactory.java | |
└── Main.java | |
12 directories, 27 files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment