Browse Source

exclusion from guessing for non-annotation member types

support for PyClass
support for name pattern for INIT
minor cleanup
Abhinav Sarkar 11 years ago
parent
commit
f4c4ef73bb

+ 2
- 1
.classpath View File

@@ -3,6 +3,7 @@
3 3
 	<classpathentry kind="src" path="src/main/java"/>
4 4
 	<classpathentry kind="src" path="src/example/java"/>
5 5
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6
-	<classpathentry kind="var" path="JYTHON_HOME/jython.jar" sourcepath="/JYTHON_HOME/Doc/javadoc"/>
6
+	<classpathentry kind="var" path="JYTHON_HOME/jython.jar" sourcepath="/JYTHON_HOME/src"/>
7
+	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
7 8
 	<classpathentry kind="output" path="bin"/>
8 9
 </classpath>

+ 23
- 17
.project View File

@@ -1,17 +1,23 @@
1
-<?xml version="1.0" encoding="UTF-8"?>
2
-<projectDescription>
3
-	<name>jywrapper</name>
4
-	<comment></comment>
5
-	<projects>
6
-	</projects>
7
-	<buildSpec>
8
-		<buildCommand>
9
-			<name>org.eclipse.jdt.core.javabuilder</name>
10
-			<arguments>
11
-			</arguments>
12
-		</buildCommand>
13
-	</buildSpec>
14
-	<natures>
15
-		<nature>org.eclipse.jdt.core.javanature</nature>
16
-	</natures>
17
-</projectDescription>
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<projectDescription>
3
+	<name>jywrapper</name>
4
+	<comment></comment>
5
+	<projects>
6
+	</projects>
7
+	<buildSpec>
8
+		<buildCommand>
9
+			<name>org.python.pydev.PyDevBuilder</name>
10
+			<arguments>
11
+			</arguments>
12
+		</buildCommand>
13
+		<buildCommand>
14
+			<name>org.eclipse.jdt.core.javabuilder</name>
15
+			<arguments>
16
+			</arguments>
17
+		</buildCommand>
18
+	</buildSpec>
19
+	<natures>
20
+		<nature>org.eclipse.jdt.core.javanature</nature>
21
+		<nature>org.python.pydev.pythonNature</nature>
22
+	</natures>
23
+</projectDescription>

+ 92
- 89
src/main/java/net/abhinavsarkar/jywrapper/JyWrapper.java View File

@@ -1,89 +1,92 @@
1
-package net.abhinavsarkar.jywrapper;
2
-
3
-import static net.abhinavsarkar.jywrapper.Messages._;
4
-
5
-
6
-import net.abhinavsarkar.jywrapper.annotation.Wraps;
7
-import net.abhinavsarkar.jywrapper.exception.PythonImportNotFoundException;
8
-
9
-import org.python.core.PyFunction;
10
-import org.python.core.PyModule;
11
-import org.python.core.PyObject;
12
-import org.python.core.PyType;
13
-
14
-/**
15
- * @author Abhinav Sarkar <abhinav@abhinavsarkar.net>
16
- *
17
- * @param <T>	The type of the java class to wrap the Python class/module with.
18
- */
19
-public final class JyWrapper {
20
-	
21
-	private JyWrapper() {
22
-	}
23
-
24
-	public static <T> T wrap(final Class<T> javaClass) {
25
-		final Wraps annotation = javaClass.getAnnotation(Wraps.class);
26
-		if (annotation == null) {
27
-			throw new PythonImportNotFoundException(_("JyWrapper.7", javaClass));  //$NON-NLS-1$
28
-		}
29
-		
30
-		return wrap(javaClass, annotation.value());
31
-	}
32
-
33
-	/**
34
-	 * @param pyImportName	The full import name of the Python class/module
35
-	 * 		to wrap.
36
-	 * @return	An instance of {@link UninitedPyObjectWrapper}, ready to be 
37
-	 * 		initialized.
38
-	 * @throws	IllegalStateException Thrown if the java Class to be used to
39
-	 * 		wrap the Python module/class, has not been supplied by earlier 
40
-	 * 		calling {@link JyWrapper#with(Class)}.
41
-	 * @throws IllegalArgumentException Thrown if the pyImportName parameter
42
-	 * 		is null.	
43
-	 */
44
-	public static <T> T wrap(final Class<T> javaClass, final String pyImportName) {
45
-		if (javaClass == null) {
46
-			throw new IllegalStateException(_("JyWrapper.6", "javaClass"));   //$NON-NLS-1$ //$NON-NLS-2$
47
-		}
48
-		if (pyImportName == null) {
49
-			throw new IllegalArgumentException(_("JyWrapper.6", "pyImportName"));   //$NON-NLS-1$ //$NON-NLS-2$
50
-		}
51
-		
52
-		final PyObject pyImport = PyImportLoader.loadPyImport(pyImportName);
53
-		if (!(pyImport instanceof PyType || pyImport instanceof PyModule)) {
54
-			throw new IllegalArgumentException(_("JyWrapper.5", pyImportName));  //$NON-NLS-1$
55
-		}
56
-		return Util.py2Java(pyImport, javaClass);
57
-	}
58
-	
59
-	/**
60
-	 * @param <T>			The return type of the {@link PyCallable} instance. 
61
-	 * @param pyImportName	The full import name of the Python function to wrap.
62
-	 * @param returnType	The class of the return type.
63
-	 * @return				An instance of {@link PyCallable} which wraps the
64
-	 * 		Python function given in parameter.
65
-	 * @throws	IllegalArgumentException Thrown if the any of the parameters 
66
-	 * 		supplied are null or if the pyImportName parameter supplied does not 
67
-	 * 		correspond to a Python function.
68
-	 */
69
-	public static <T> PyCallable<T> wrapPyFunction(
70
-			final String pyImportName, final Class<T> returnType) {
71
-		if (pyImportName == null) {
72
-			throw new IllegalArgumentException(_("JyWrapper.6", "pyImportName"));   //$NON-NLS-1$ //$NON-NLS-2$
73
-		}
74
-		if (returnType == null) {
75
-			throw new IllegalArgumentException(_("JyWrapper.6", "returnType"));   //$NON-NLS-1$ //$NON-NLS-2$
76
-		}
77
-		
78
-		final PyObject pyImport = PyImportLoader.loadPyImport(pyImportName);
79
-		if (!(pyImport instanceof PyFunction)) {
80
-			throw new IllegalArgumentException(_("JyWrapper.0", pyImportName));  //$NON-NLS-1$
81
-		}
82
-		
83
-		@SuppressWarnings("unchecked")
84
-		final PyCallable<T> newInstance = PyObjectProxy.newInstance(
85
-				pyImport, PyCallable.class);
86
-		return newInstance;
87
-	}
88
-	
89
-}
1
+package net.abhinavsarkar.jywrapper;
2
+
3
+import static net.abhinavsarkar.jywrapper.Messages._;
4
+
5
+
6
+import net.abhinavsarkar.jywrapper.annotation.Wraps;
7
+import net.abhinavsarkar.jywrapper.exception.PythonImportNotFoundException;
8
+
9
+import org.python.core.PyClass;
10
+import org.python.core.PyFunction;
11
+import org.python.core.PyModule;
12
+import org.python.core.PyObject;
13
+import org.python.core.PyType;
14
+
15
+/**
16
+ * @author Abhinav Sarkar <abhinav@abhinavsarkar.net>
17
+ *
18
+ * @param <T>	The type of the java class to wrap the Python class/module with.
19
+ */
20
+public final class JyWrapper {
21
+	
22
+	private JyWrapper() {
23
+	}
24
+
25
+	public static <T> T wrap(final Class<T> javaClass) {
26
+		final Wraps annotation = javaClass.getAnnotation(Wraps.class);
27
+		if (annotation == null) {
28
+			throw new PythonImportNotFoundException(_("JyWrapper.7", javaClass));  //$NON-NLS-1$
29
+		}
30
+		
31
+		return wrap(javaClass, annotation.value());
32
+	}
33
+
34
+	/**
35
+	 * @param pyImportName	The full import name of the Python class/module
36
+	 * 		to wrap.
37
+	 * @return	An instance of {@link UninitedPyObjectWrapper}, ready to be 
38
+	 * 		initialized.
39
+	 * @throws	IllegalStateException Thrown if the java Class to be used to
40
+	 * 		wrap the Python module/class, has not been supplied by earlier 
41
+	 * 		calling {@link JyWrapper#with(Class)}.
42
+	 * @throws IllegalArgumentException Thrown if the pyImportName parameter
43
+	 * 		is null.	
44
+	 */
45
+	public static <T> T wrap(final Class<T> javaClass, final String pyImportName) {
46
+		if (javaClass == null) {
47
+			throw new IllegalStateException(_("JyWrapper.6", "javaClass"));   //$NON-NLS-1$ //$NON-NLS-2$
48
+		}
49
+		if (pyImportName == null) {
50
+			throw new IllegalArgumentException(_("JyWrapper.6", "pyImportName"));   //$NON-NLS-1$ //$NON-NLS-2$
51
+		}
52
+		
53
+		final PyObject pyImport = PyImportLoader.loadPyImport(pyImportName);
54
+		if (!(pyImport instanceof PyType 
55
+				|| pyImport instanceof PyModule 
56
+				|| pyImport instanceof PyClass)) {
57
+			throw new IllegalArgumentException(_("JyWrapper.5", pyImportName));  //$NON-NLS-1$
58
+		}
59
+		return Util.py2Java(pyImport, javaClass);
60
+	}
61
+	
62
+	/**
63
+	 * @param <T>			The return type of the {@link PyCallable} instance. 
64
+	 * @param pyImportName	The full import name of the Python function to wrap.
65
+	 * @param returnType	The class of the return type.
66
+	 * @return				An instance of {@link PyCallable} which wraps the
67
+	 * 		Python function given in parameter.
68
+	 * @throws	IllegalArgumentException Thrown if the any of the parameters 
69
+	 * 		supplied are null or if the pyImportName parameter supplied does not 
70
+	 * 		correspond to a Python function.
71
+	 */
72
+	public static <T> PyCallable<T> wrapPyFunction(
73
+			final String pyImportName, final Class<T> returnType) {
74
+		if (pyImportName == null) {
75
+			throw new IllegalArgumentException(_("JyWrapper.6", "pyImportName"));   //$NON-NLS-1$ //$NON-NLS-2$
76
+		}
77
+		if (returnType == null) {
78
+			throw new IllegalArgumentException(_("JyWrapper.6", "returnType"));   //$NON-NLS-1$ //$NON-NLS-2$
79
+		}
80
+		
81
+		final PyObject pyImport = PyImportLoader.loadPyImport(pyImportName);
82
+		if (!(pyImport instanceof PyFunction)) {
83
+			throw new IllegalArgumentException(_("JyWrapper.0", pyImportName));  //$NON-NLS-1$
84
+		}
85
+		
86
+		@SuppressWarnings("unchecked")
87
+		final PyCallable<T> newInstance = PyObjectProxy.newInstance(
88
+				pyImport, PyCallable.class);
89
+		return newInstance;
90
+	}
91
+	
92
+}

+ 28
- 28
src/main/java/net/abhinavsarkar/jywrapper/Messages.java View File

@@ -1,28 +1,28 @@
1
-package net.abhinavsarkar.jywrapper;
2
-
3
-import java.text.MessageFormat;
4
-import java.util.MissingResourceException;
5
-import java.util.ResourceBundle;
6
-
7
-final class Messages {
8
-	private static final String BUNDLE_NAME = "net.abhinavsarkar.jywrapper.messages"; //$NON-NLS-1$
9
-
10
-	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
11
-			.getBundle(BUNDLE_NAME);
12
-
13
-	private Messages() {
14
-	}
15
-
16
-	public static String getString(String key) {
17
-		try {
18
-			return RESOURCE_BUNDLE.getString(key);
19
-		} catch (MissingResourceException e) {
20
-			return '!' + key + '!';
21
-		}
22
-	}
23
-
24
-	public static String _(final String messageKey, final Object... arguments) {
25
-		return new MessageFormat(getString(messageKey))
26
-			.format(arguments, new StringBuffer(), null).toString();
27
-	}
28
-}
1
+package net.abhinavsarkar.jywrapper;
2
+
3
+import java.text.MessageFormat;
4
+import java.util.MissingResourceException;
5
+import java.util.ResourceBundle;
6
+
7
+final class Messages {
8
+	private static final String BUNDLE_NAME = "net.abhinavsarkar.jywrapper.messages"; //$NON-NLS-1$
9
+
10
+	private static final ResourceBundle RESOURCE_BUNDLE =
11
+		ResourceBundle.getBundle(BUNDLE_NAME);
12
+
13
+	private Messages() {
14
+	}
15
+
16
+	public static String getString(final String key) {
17
+		try {
18
+			return RESOURCE_BUNDLE.getString(key);
19
+		} catch (final MissingResourceException e) {
20
+			return '!' + key + '!';
21
+		}
22
+	}
23
+
24
+	public static String _(final String messageKey, final Object... arguments) {
25
+		return new MessageFormat(getString(messageKey))
26
+			.format(arguments, new StringBuffer(), null).toString();
27
+	}
28
+}

+ 18
- 17
src/main/java/net/abhinavsarkar/jywrapper/PyAttributeType.java View File

@@ -1,17 +1,18 @@
1
-package net.abhinavsarkar.jywrapper;
2
-
3
-import net.abhinavsarkar.jywrapper.PyObjectProxy.MemberType;
4
-
5
-public enum PyAttributeType {
6
-	GETTER(MemberType.GETTER), SETTER(MemberType.SETTER), CONST(MemberType.CONST);
7
-	
8
-	private final MemberType memberType;
9
-
10
-	private PyAttributeType(MemberType memberType) {
11
-		this.memberType = memberType;
12
-	}
13
-
14
-	public MemberType getMemberType() {
15
-		return memberType;
16
-	}
17
-}
1
+package net.abhinavsarkar.jywrapper;
2
+
3
+import net.abhinavsarkar.jywrapper.PyObjectProxy.MemberType;
4
+
5
+public enum PyAttributeType {
6
+
7
+	GETTER(MemberType.GETTER), SETTER(MemberType.SETTER), CONST(MemberType.CONST);
8
+
9
+	private final MemberType memberType;
10
+
11
+	private PyAttributeType(final MemberType memberType) {
12
+		this.memberType = memberType;
13
+	}
14
+
15
+	MemberType getMemberType() {
16
+		return memberType;
17
+	}
18
+}

+ 62
- 62
src/main/java/net/abhinavsarkar/jywrapper/PyImportLoader.java View File

@@ -1,62 +1,62 @@
1
-package net.abhinavsarkar.jywrapper;
2
-
3
-import static net.abhinavsarkar.jywrapper.Messages._;
4
-
5
-import java.util.concurrent.ConcurrentHashMap;
6
-
7
-import net.abhinavsarkar.jywrapper.exception.PythonImportNotFoundException;
8
-
9
-import org.python.core.Py;
10
-import org.python.core.PyException;
11
-import org.python.core.PyObject;
12
-import org.python.core.PySystemState;
13
-
14
-/**
15
- * @author AbhinavSarkar
16
- *
17
- */
18
-public final class PyImportLoader {
19
-	
20
-	private static final PyObject importer = new PySystemState().getBuiltins()
21
-		.__getitem__(Py.newString("__import__")); //$NON-NLS-1$
22
-
23
-   private static final ConcurrentHashMap<String, PyObject> loadedPyImports = 
24
-		new ConcurrentHashMap<String, PyObject>();
25
-
26
-   private PyImportLoader() {
27
-	}
28
-   
29
-   /**
30
-     * @param fullImportName
31
-     * @return
32
-     * @throws PythonImportNotFoundException
33
-     */
34
-    public static PyObject loadPyImport(String fullImportName)
35
-            throws PythonImportNotFoundException {
36
-    	if (!loadedPyImports.containsKey(fullImportName)) {
37
-    		int i = fullImportName.lastIndexOf('.');
38
-            String errorMsg = _("PyImportLoader.1", fullImportName); //$NON-NLS-1$
39
-            PyObject pyImport;
40
-            if (i == -1) {
41
-                String pyModuleName = fullImportName;
42
-                try {
43
-                	pyImport = importer.__call__(Py.newString(pyModuleName));
44
-                } catch (PyException pye) {
45
-                    throw new PythonImportNotFoundException(errorMsg, pye);
46
-                }
47
-            } else {
48
-                String pyModuleName = fullImportName.substring(0, i);
49
-                String pyClassName = fullImportName.substring(i + 1);
50
-
51
-                try {
52
-                	pyImport = importer.__call__(Py.newString(pyModuleName))
53
-                		.__getattr__(pyClassName);
54
-                } catch (PyException pye) {
55
-                    throw new PythonImportNotFoundException(errorMsg, pye);
56
-                }
57
-            }
58
-			loadedPyImports.putIfAbsent(fullImportName, pyImport);
59
-    	}
60
-		return loadedPyImports.get(fullImportName);
61
-    }
62
-}
1
+package net.abhinavsarkar.jywrapper;
2
+
3
+import static net.abhinavsarkar.jywrapper.Messages._;
4
+
5
+import java.util.concurrent.ConcurrentHashMap;
6
+
7
+import net.abhinavsarkar.jywrapper.exception.PythonImportNotFoundException;
8
+
9
+import org.python.core.Py;
10
+import org.python.core.PyException;
11
+import org.python.core.PyObject;
12
+import org.python.core.PySystemState;
13
+
14
+/**
15
+ * @author AbhinavSarkar
16
+ *
17
+ */
18
+public final class PyImportLoader {
19
+
20
+	private static final PyObject importer = new PySystemState().getBuiltins()
21
+		.__getitem__(Py.newString("__import__")); //$NON-NLS-1$
22
+
23
+	private static final ConcurrentHashMap<String, PyObject> loadedPyImports =
24
+		new ConcurrentHashMap<String, PyObject>();
25
+
26
+	private PyImportLoader() {
27
+	}
28
+
29
+   /**
30
+	 * @param fullImportName
31
+	 * @return
32
+	 * @throws PythonImportNotFoundException
33
+	 */
34
+	public static PyObject loadPyImport(final String fullImportName)
35
+			throws PythonImportNotFoundException {
36
+		if (!loadedPyImports.containsKey(fullImportName)) {
37
+			final int i = fullImportName.lastIndexOf('.');
38
+			final String errorMsg = _("PyImportLoader.1", fullImportName); //$NON-NLS-1$
39
+			PyObject pyImport;
40
+			if (i == -1) {
41
+				final String pyModuleName = fullImportName;
42
+				try {
43
+					pyImport = importer.__call__(Py.newString(pyModuleName));
44
+				} catch (final PyException pye) {
45
+					throw new PythonImportNotFoundException(errorMsg, pye);
46
+				}
47
+			} else {
48
+				final String pyModuleName = fullImportName.substring(0, i);
49
+				final String pyClassName = fullImportName.substring(i + 1);
50
+
51
+				try {
52
+					pyImport = importer.__call__(Py.newString(pyModuleName))
53
+						.__getattr__(pyClassName);
54
+				} catch (final PyException pye) {
55
+					throw new PythonImportNotFoundException(errorMsg, pye);
56
+				}
57
+			}
58
+			loadedPyImports.putIfAbsent(fullImportName, pyImport);
59
+		}
60
+		return loadedPyImports.get(fullImportName);
61
+	}
62
+}

+ 20
- 20
src/main/java/net/abhinavsarkar/jywrapper/PyMethodType.java View File

@@ -1,20 +1,20 @@
1
-package net.abhinavsarkar.jywrapper;
2
-
3
-import net.abhinavsarkar.jywrapper.PyObjectProxy.MemberType;
4
-
5
-public enum PyMethodType {
6
-	INIT(MemberType.INIT), 
7
-	DIRECT(MemberType.DIRECT), 
8
-	UNDERSCORED(MemberType.UNDERSCORED), 
9
-	NUMERIC(MemberType.NUMERIC);
10
-	
11
-	private final MemberType memberType;
12
-
13
-	private PyMethodType(MemberType memberType) {
14
-		this.memberType = memberType;
15
-	}
16
-
17
-	public MemberType getMemberType() {
18
-		return memberType;
19
-	}
20
-}
1
+package net.abhinavsarkar.jywrapper;
2
+
3
+import net.abhinavsarkar.jywrapper.PyObjectProxy.MemberType;
4
+
5
+public enum PyMethodType {
6
+	INIT			(MemberType.INIT),
7
+	DIRECT			(MemberType.DIRECT),
8
+	UNDERSCORED		(MemberType.UNDERSCORED),
9
+	NUMERIC			(MemberType.NUMERIC);
10
+
11
+	private final MemberType memberType;
12
+
13
+	private PyMethodType(final MemberType memberType) {
14
+		this.memberType = memberType;
15
+	}
16
+
17
+	MemberType getMemberType() {
18
+		return memberType;
19
+	}
20
+}

+ 598
- 583
src/main/java/net/abhinavsarkar/jywrapper/PyObjectProxy.java
File diff suppressed because it is too large
View File


+ 86
- 75
src/main/java/net/abhinavsarkar/jywrapper/Util.java View File

@@ -1,75 +1,86 @@
1
-package net.abhinavsarkar.jywrapper;
2
-
3
-import static net.abhinavsarkar.jywrapper.Messages._;
4
-
5
-import org.python.core.Py;
6
-import org.python.core.PyFunction;
7
-import org.python.core.PyModule;
8
-import org.python.core.PyObject;
9
-import org.python.core.PyString;
10
-import org.python.core.PyType;
11
-
12
-final class Util {
13
-	
14
-	private Util() {
15
-	}
16
-
17
-	static <T> T py2Java(final PyObject pyObject, final Class<T> javaClass) {
18
-		final Object javaWrapper = pyObject.__tojava__(javaClass);	
19
-		if (javaWrapper == Py.NoConversion) {
20
-			if (javaClass.isInterface()) {
21
-				return PyObjectProxy.newInstance(pyObject, javaClass);
22
-			} else {
23
-				throw new IllegalArgumentException(
24
-						_("JyWrapper.1", pyObject, javaClass.getName()));  //$NON-NLS-1$
25
-			}
26
-		} else {
27
-			@SuppressWarnings("unchecked")
28
-			final T t = (T) javaWrapper;
29
-			return t;
30
-		}
31
-	}
32
-
33
-	static PyObject[] convertArgs(final Object[] args) {
34
-		if (args == null || args.length == 0) {
35
-			return new PyObject[0];
36
-		}
37
-		if (args.length == 1) {
38
-			return new PyObject[] { Py.java2py(args[0]) };
39
-		}
40
-		
41
-		final PyObject[] pyArgs = new PyObject[args.length];
42
-		for (int i = args.length; --i >= 0;) {
43
-			pyArgs[i] = Py.java2py(args[i]);
44
-		}
45
-		return pyArgs;
46
-	}
47
-
48
-	static String getPyImportName(final PyObject pyObject) {
49
-		synchronized (pyObject) {
50
-			if (pyObject instanceof PyType) {
51
-				final PyType pyType = (PyType) pyObject;
52
-				final PyObject module = pyType.getModule();
53
-		        if (module instanceof PyString 
54
-		        		&& !module.toString().equals("__builtin__")) {  //$NON-NLS-1$
55
-		            return String.format("%s.%s", module.toString(),   //$NON-NLS-1$
56
-		            		pyType.getName());
57
-		        }
58
-		        return pyType.getName();
59
-			} else if (pyObject instanceof PyModule) {
60
-				return ((PyModule) pyObject).toString();
61
-			} else if (pyObject instanceof PyFunction) {
62
-				final PyFunction pyFunction = (PyFunction) pyObject;
63
-				return String.format("%s.%s", pyFunction.__module__,   //$NON-NLS-1$
64
-						pyFunction.__name__); 
65
-			} else {
66
-				return getPyImportName(pyObject.getType());
67
-			}	
68
-		}			
69
-	}
70
-
71
-	static String camelCase2UnderScore(final String word) {
72
-		return word.replaceAll("([A-Z])", "_$1").toLowerCase();   //$NON-NLS-1$ //$NON-NLS-2$
73
-	}
74
-
75
-}
1
+package net.abhinavsarkar.jywrapper;
2
+
3
+import static net.abhinavsarkar.jywrapper.Messages._;
4
+
5
+import org.python.core.Py;
6
+import org.python.core.PyClass;
7
+import org.python.core.PyFunction;
8
+import org.python.core.PyModule;
9
+import org.python.core.PyObject;
10
+import org.python.core.PyString;
11
+import org.python.core.PyType;
12
+
13
+final class Util {
14
+
15
+	private Util() {
16
+	}
17
+
18
+	static <T> T py2Java(final PyObject pyObject, final Class<T> javaClass) {
19
+		final Object javaWrapper = pyObject.__tojava__(javaClass);
20
+		if (javaWrapper == Py.NoConversion) {
21
+			if (javaClass.isInterface()) {
22
+				return PyObjectProxy.newInstance(pyObject, javaClass);
23
+			} else {
24
+				throw new IllegalArgumentException(
25
+						_("JyWrapper.1", pyObject, javaClass.getName()));  //$NON-NLS-1$
26
+			}
27
+		} else {
28
+			@SuppressWarnings("unchecked")
29
+			final T t = (T) javaWrapper;
30
+			return t;
31
+		}
32
+	}
33
+
34
+	static PyObject[] convertArgs(final Object[] args) {
35
+		if (args == null || args.length == 0) {
36
+			return new PyObject[0];
37
+		}
38
+		if (args.length == 1) {
39
+			return new PyObject[] { Py.java2py(args[0]) };
40
+		}
41
+
42
+		final PyObject[] pyArgs = new PyObject[args.length];
43
+		for (int i = args.length; --i >= 0;) {
44
+			pyArgs[i] = Py.java2py(args[i]);
45
+		}
46
+		return pyArgs;
47
+	}
48
+
49
+	static String getPyImportName(final PyObject pyObject) {
50
+		synchronized (pyObject) {
51
+			if (pyObject instanceof PyType) {
52
+				final PyType pyType = (PyType) pyObject;
53
+				final PyObject module = pyType.getModule();
54
+				if (module instanceof PyString
55
+						&& !module.toString().equals("__builtin__")) {  //$NON-NLS-1$
56
+					return String.format("%s.%s", module.toString(),   //$NON-NLS-1$
57
+							pyType.getName());
58
+				}
59
+				return pyType.getName();
60
+			} else if (pyObject instanceof PyClass) {
61
+				final PyClass pyClass = (PyClass) pyObject;
62
+				final PyObject mod = pyClass.__dict__.__finditem__("__module__");
63
+				String smod;
64
+				if (mod == null || !(mod instanceof PyString)) {
65
+					smod = "<unknown>";
66
+				} else {
67
+					smod = ((PyString) mod).toString();
68
+				}
69
+				return smod + "." + pyClass.__name__;
70
+			} else if (pyObject instanceof PyModule) {
71
+				return ((PyModule) pyObject).toString();
72
+			} else if (pyObject instanceof PyFunction) {
73
+				final PyFunction pyFunction = (PyFunction) pyObject;
74
+				return String.format(
75
+						"%s.%s", pyFunction.__module__, pyFunction.__name__); //$NON-NLS-1$
76
+			} else {
77
+				return getPyImportName(pyObject.getType());
78
+			}
79
+		}
80
+	}
81
+
82
+	static String camelCase2UnderScore(final String word) {
83
+		return word.replaceAll("([A-Z])", "_$1").toLowerCase();   //$NON-NLS-1$ //$NON-NLS-2$
84
+	}
85
+
86
+}

+ 17
- 17
src/main/java/net/abhinavsarkar/jywrapper/messages.properties View File

@@ -1,17 +1,17 @@
1
-JyWrapper.0="{0}" is not a Python Function
2
-JyWrapper.1=Cannot convert Python object: {0} to a Java object of class {1}
3
-JyWrapper.2=Cannot convert Python Type {0} to Java Class {1}
4
-JyWrapper.3=Cannot not instantiate the Python type: {0}
5
-JyWrapper.4=Python module {0} can be instantiated statically only
6
-JyWrapper.5={0} is not a Python Type or Module
7
-JyWrapper.6=parameter "{0}" is null
8
-JyWrapper.7=No Python import name annotated on the Java Class {0}
9
-JyWrapper.8={0} is not an Interface
10
-JyWrapper.9=Cannot instantiate a Python non type
11
-JyWrapper.10=Instance method "{0}" called on a Python Type
12
-JyWrapper.11=No comparison methods found in the backing Python object
13
-JyWrapper.12=Trying to get instance attribute "{0}" from a Python Type
14
-JyWrapper.13=Can''t set attribute: {0}
15
-JyWrapper.14=Trying to set instance attribute "{0}" in a python class
16
-JyWrapper.15=No {0} named "{1}" in the backing Python Type {2}
17
-PyImportLoader.1=Python Import "{0}" not found in pythonpath
1
+JyWrapper.0="{0}" is not a Python Function
2
+JyWrapper.1=Cannot convert Python object: {0} to a Java object of class {1}
3
+JyWrapper.2=Cannot convert Python Type {0} to Java Class {1}
4
+JyWrapper.3=Cannot not instantiate the Python type: {0}
5
+JyWrapper.4=Python module {0} can be instantiated statically only
6
+JyWrapper.5={0} is not a Python Type, Class or Module
7
+JyWrapper.6=parameter "{0}" is null
8
+JyWrapper.7=No Python import name annotated on the Java Class {0}
9
+JyWrapper.8={0} is not an Interface
10
+JyWrapper.9=Cannot instantiate a non Python Type or Class
11
+JyWrapper.10=Instance method "{0}" called on a Python Type
12
+JyWrapper.11=No comparison methods found in the backing Python object
13
+JyWrapper.12=Trying to get instance attribute "{0}" from a Python Type
14
+JyWrapper.13=Can''t set attribute: {0}
15
+JyWrapper.14=Trying to set instance attribute "{0}" in a python class
16
+JyWrapper.15=No {0} named "{1}" in the backing Python Type {2}
17
+PyImportLoader.1=Python Import "{0}" not found in pythonpath

Loading…
Cancel
Save