source: src/test/java/geniusweb/clienttest/JythonObjectFactory.java@ 31

Last change on this file since 31 was 31, checked in by bart, 3 years ago

Geniusweb 2.0.3. MOPAC protocol translated to Python. Small fixes.

File size: 2.1 KB
RevLine 
[31]1package geniusweb.clienttest;
2
3import org.python.core.Py;
4import org.python.core.PyObject;
5import org.python.core.PySystemState;
6
7/**
8 * Jython Object Factory using PySystemState
9 */
10public class JythonObjectFactory {
11
12 private final Class interfaceType;
13 private final PyObject klass;
14
15 // Constructor obtains a reference to the importer, module, and the class
16 // name
17 public JythonObjectFactory(PySystemState state, Class interfaceType, String moduleName, String className) {
18 this.interfaceType = interfaceType;
19 PyObject importer = state.getBuiltins().__getitem__(Py.newString("__import__"));
20 PyObject module = importer.__call__(Py.newString(moduleName));
21 klass = module.__getattr__(className);
22 System.err.println("module=" + module + ",class=" + klass);
23 }
24
25 // This constructor passes through to the other constructor
26 public JythonObjectFactory(Class interfaceType, String moduleName, String className) {
27 this(new PySystemState(), interfaceType, moduleName, className);
28 }
29
30 // All of the followng methods return
31 // a coerced Jython object based upon the pieces of information
32 // that were passed into the factory. The differences are
33 // between them are the number of arguments that can be passed
34 // in as arguents to the object.
35
36 public Object createObject() {
37 return klass.__call__().__tojava__(interfaceType);
38 }
39
40 public Object createObject(Object arg1) {
41 return klass.__call__(Py.java2py(arg1)).__tojava__(interfaceType);
42 }
43
44 public Object createObject(Object arg1, Object arg2) {
45 return klass.__call__(Py.java2py(arg1), Py.java2py(arg2)).__tojava__(interfaceType);
46 }
47
48 public Object createObject(Object arg1, Object arg2, Object arg3) {
49 return klass.__call__(Py.java2py(arg1), Py.java2py(arg2), Py.java2py(arg3)).__tojava__(interfaceType);
50 }
51
52 public Object createObject(Object args[], String keywords[]) {
53 PyObject convertedArgs[] = new PyObject[args.length];
54 for (int i = 0; i < args.length; i++) {
55 convertedArgs[i] = Py.java2py(args[i]);
56 }
57
58 return klass.__call__(convertedArgs, keywords).__tojava__(interfaceType);
59 }
60
61 public Object createObject(Object... args) {
62 return createObject(args, Py.NoKeywords);
63 }
64
65}
Note: See TracBrowser for help on using the repository browser.