exclusion from guessing for non-annotation member types

support for PyClass
support for name pattern for INIT
minor cleanup
This commit is contained in:
Abhinav Sarkar 2009-10-31 00:16:42 +05:30
parent cc159354ce
commit f4c4ef73bb
10 changed files with 946 additions and 909 deletions

View File

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

View File

@ -1,17 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>jywrapper</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>jywrapper</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>

View File

@ -1,89 +1,92 @@
package net.abhinavsarkar.jywrapper;
import static net.abhinavsarkar.jywrapper.Messages._;
import net.abhinavsarkar.jywrapper.annotation.Wraps;
import net.abhinavsarkar.jywrapper.exception.PythonImportNotFoundException;
import org.python.core.PyFunction;
import org.python.core.PyModule;
import org.python.core.PyObject;
import org.python.core.PyType;
/**
* @author Abhinav Sarkar <abhinav@abhinavsarkar.net>
*
* @param <T> The type of the java class to wrap the Python class/module with.
*/
public final class JyWrapper {
private JyWrapper() {
}
public static <T> T wrap(final Class<T> javaClass) {
final Wraps annotation = javaClass.getAnnotation(Wraps.class);
if (annotation == null) {
throw new PythonImportNotFoundException(_("JyWrapper.7", javaClass)); //$NON-NLS-1$
}
return wrap(javaClass, annotation.value());
}
/**
* @param pyImportName The full import name of the Python class/module
* to wrap.
* @return An instance of {@link UninitedPyObjectWrapper}, ready to be
* initialized.
* @throws IllegalStateException Thrown if the java Class to be used to
* wrap the Python module/class, has not been supplied by earlier
* calling {@link JyWrapper#with(Class)}.
* @throws IllegalArgumentException Thrown if the pyImportName parameter
* is null.
*/
public static <T> T wrap(final Class<T> javaClass, final String pyImportName) {
if (javaClass == null) {
throw new IllegalStateException(_("JyWrapper.6", "javaClass")); //$NON-NLS-1$ //$NON-NLS-2$
}
if (pyImportName == null) {
throw new IllegalArgumentException(_("JyWrapper.6", "pyImportName")); //$NON-NLS-1$ //$NON-NLS-2$
}
final PyObject pyImport = PyImportLoader.loadPyImport(pyImportName);
if (!(pyImport instanceof PyType || pyImport instanceof PyModule)) {
throw new IllegalArgumentException(_("JyWrapper.5", pyImportName)); //$NON-NLS-1$
}
return Util.py2Java(pyImport, javaClass);
}
/**
* @param <T> The return type of the {@link PyCallable} instance.
* @param pyImportName The full import name of the Python function to wrap.
* @param returnType The class of the return type.
* @return An instance of {@link PyCallable} which wraps the
* Python function given in parameter.
* @throws IllegalArgumentException Thrown if the any of the parameters
* supplied are null or if the pyImportName parameter supplied does not
* correspond to a Python function.
*/
public static <T> PyCallable<T> wrapPyFunction(
final String pyImportName, final Class<T> returnType) {
if (pyImportName == null) {
throw new IllegalArgumentException(_("JyWrapper.6", "pyImportName")); //$NON-NLS-1$ //$NON-NLS-2$
}
if (returnType == null) {
throw new IllegalArgumentException(_("JyWrapper.6", "returnType")); //$NON-NLS-1$ //$NON-NLS-2$
}
final PyObject pyImport = PyImportLoader.loadPyImport(pyImportName);
if (!(pyImport instanceof PyFunction)) {
throw new IllegalArgumentException(_("JyWrapper.0", pyImportName)); //$NON-NLS-1$
}
@SuppressWarnings("unchecked")
final PyCallable<T> newInstance = PyObjectProxy.newInstance(
pyImport, PyCallable.class);
return newInstance;
}
}
package net.abhinavsarkar.jywrapper;
import static net.abhinavsarkar.jywrapper.Messages._;
import net.abhinavsarkar.jywrapper.annotation.Wraps;
import net.abhinavsarkar.jywrapper.exception.PythonImportNotFoundException;
import org.python.core.PyClass;
import org.python.core.PyFunction;
import org.python.core.PyModule;
import org.python.core.PyObject;
import org.python.core.PyType;
/**
* @author Abhinav Sarkar <abhinav@abhinavsarkar.net>
*
* @param <T> The type of the java class to wrap the Python class/module with.
*/
public final class JyWrapper {
private JyWrapper() {
}
public static <T> T wrap(final Class<T> javaClass) {
final Wraps annotation = javaClass.getAnnotation(Wraps.class);
if (annotation == null) {
throw new PythonImportNotFoundException(_("JyWrapper.7", javaClass)); //$NON-NLS-1$
}
return wrap(javaClass, annotation.value());
}
/**
* @param pyImportName The full import name of the Python class/module
* to wrap.
* @return An instance of {@link UninitedPyObjectWrapper}, ready to be
* initialized.
* @throws IllegalStateException Thrown if the java Class to be used to
* wrap the Python module/class, has not been supplied by earlier
* calling {@link JyWrapper#with(Class)}.
* @throws IllegalArgumentException Thrown if the pyImportName parameter
* is null.
*/
public static <T> T wrap(final Class<T> javaClass, final String pyImportName) {
if (javaClass == null) {
throw new IllegalStateException(_("JyWrapper.6", "javaClass")); //$NON-NLS-1$ //$NON-NLS-2$
}
if (pyImportName == null) {
throw new IllegalArgumentException(_("JyWrapper.6", "pyImportName")); //$NON-NLS-1$ //$NON-NLS-2$
}
final PyObject pyImport = PyImportLoader.loadPyImport(pyImportName);
if (!(pyImport instanceof PyType
|| pyImport instanceof PyModule
|| pyImport instanceof PyClass)) {
throw new IllegalArgumentException(_("JyWrapper.5", pyImportName)); //$NON-NLS-1$
}
return Util.py2Java(pyImport, javaClass);
}
/**
* @param <T> The return type of the {@link PyCallable} instance.
* @param pyImportName The full import name of the Python function to wrap.
* @param returnType The class of the return type.
* @return An instance of {@link PyCallable} which wraps the
* Python function given in parameter.
* @throws IllegalArgumentException Thrown if the any of the parameters
* supplied are null or if the pyImportName parameter supplied does not
* correspond to a Python function.
*/
public static <T> PyCallable<T> wrapPyFunction(
final String pyImportName, final Class<T> returnType) {
if (pyImportName == null) {
throw new IllegalArgumentException(_("JyWrapper.6", "pyImportName")); //$NON-NLS-1$ //$NON-NLS-2$
}
if (returnType == null) {
throw new IllegalArgumentException(_("JyWrapper.6", "returnType")); //$NON-NLS-1$ //$NON-NLS-2$
}
final PyObject pyImport = PyImportLoader.loadPyImport(pyImportName);
if (!(pyImport instanceof PyFunction)) {
throw new IllegalArgumentException(_("JyWrapper.0", pyImportName)); //$NON-NLS-1$
}
@SuppressWarnings("unchecked")
final PyCallable<T> newInstance = PyObjectProxy.newInstance(
pyImport, PyCallable.class);
return newInstance;
}
}

View File

@ -1,28 +1,28 @@
package net.abhinavsarkar.jywrapper;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
final class Messages {
private static final String BUNDLE_NAME = "net.abhinavsarkar.jywrapper.messages"; //$NON-NLS-1$
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
.getBundle(BUNDLE_NAME);
private Messages() {
}
public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
public static String _(final String messageKey, final Object... arguments) {
return new MessageFormat(getString(messageKey))
.format(arguments, new StringBuffer(), null).toString();
}
}
package net.abhinavsarkar.jywrapper;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
final class Messages {
private static final String BUNDLE_NAME = "net.abhinavsarkar.jywrapper.messages"; //$NON-NLS-1$
private static final ResourceBundle RESOURCE_BUNDLE =
ResourceBundle.getBundle(BUNDLE_NAME);
private Messages() {
}
public static String getString(final String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (final MissingResourceException e) {
return '!' + key + '!';
}
}
public static String _(final String messageKey, final Object... arguments) {
return new MessageFormat(getString(messageKey))
.format(arguments, new StringBuffer(), null).toString();
}
}

View File

@ -1,17 +1,18 @@
package net.abhinavsarkar.jywrapper;
import net.abhinavsarkar.jywrapper.PyObjectProxy.MemberType;
public enum PyAttributeType {
GETTER(MemberType.GETTER), SETTER(MemberType.SETTER), CONST(MemberType.CONST);
private final MemberType memberType;
private PyAttributeType(MemberType memberType) {
this.memberType = memberType;
}
public MemberType getMemberType() {
return memberType;
}
}
package net.abhinavsarkar.jywrapper;
import net.abhinavsarkar.jywrapper.PyObjectProxy.MemberType;
public enum PyAttributeType {
GETTER(MemberType.GETTER), SETTER(MemberType.SETTER), CONST(MemberType.CONST);
private final MemberType memberType;
private PyAttributeType(final MemberType memberType) {
this.memberType = memberType;
}
MemberType getMemberType() {
return memberType;
}
}

View File

@ -1,62 +1,62 @@
package net.abhinavsarkar.jywrapper;
import static net.abhinavsarkar.jywrapper.Messages._;
import java.util.concurrent.ConcurrentHashMap;
import net.abhinavsarkar.jywrapper.exception.PythonImportNotFoundException;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyObject;
import org.python.core.PySystemState;
/**
* @author AbhinavSarkar
*
*/
public final class PyImportLoader {
private static final PyObject importer = new PySystemState().getBuiltins()
.__getitem__(Py.newString("__import__")); //$NON-NLS-1$
private static final ConcurrentHashMap<String, PyObject> loadedPyImports =
new ConcurrentHashMap<String, PyObject>();
private PyImportLoader() {
}
/**
* @param fullImportName
* @return
* @throws PythonImportNotFoundException
*/
public static PyObject loadPyImport(String fullImportName)
throws PythonImportNotFoundException {
if (!loadedPyImports.containsKey(fullImportName)) {
int i = fullImportName.lastIndexOf('.');
String errorMsg = _("PyImportLoader.1", fullImportName); //$NON-NLS-1$
PyObject pyImport;
if (i == -1) {
String pyModuleName = fullImportName;
try {
pyImport = importer.__call__(Py.newString(pyModuleName));
} catch (PyException pye) {
throw new PythonImportNotFoundException(errorMsg, pye);
}
} else {
String pyModuleName = fullImportName.substring(0, i);
String pyClassName = fullImportName.substring(i + 1);
try {
pyImport = importer.__call__(Py.newString(pyModuleName))
.__getattr__(pyClassName);
} catch (PyException pye) {
throw new PythonImportNotFoundException(errorMsg, pye);
}
}
loadedPyImports.putIfAbsent(fullImportName, pyImport);
}
return loadedPyImports.get(fullImportName);
}
}
package net.abhinavsarkar.jywrapper;
import static net.abhinavsarkar.jywrapper.Messages._;
import java.util.concurrent.ConcurrentHashMap;
import net.abhinavsarkar.jywrapper.exception.PythonImportNotFoundException;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyObject;
import org.python.core.PySystemState;
/**
* @author AbhinavSarkar
*
*/
public final class PyImportLoader {
private static final PyObject importer = new PySystemState().getBuiltins()
.__getitem__(Py.newString("__import__")); //$NON-NLS-1$
private static final ConcurrentHashMap<String, PyObject> loadedPyImports =
new ConcurrentHashMap<String, PyObject>();
private PyImportLoader() {
}
/**
* @param fullImportName
* @return
* @throws PythonImportNotFoundException
*/
public static PyObject loadPyImport(final String fullImportName)
throws PythonImportNotFoundException {
if (!loadedPyImports.containsKey(fullImportName)) {
final int i = fullImportName.lastIndexOf('.');
final String errorMsg = _("PyImportLoader.1", fullImportName); //$NON-NLS-1$
PyObject pyImport;
if (i == -1) {
final String pyModuleName = fullImportName;
try {
pyImport = importer.__call__(Py.newString(pyModuleName));
} catch (final PyException pye) {
throw new PythonImportNotFoundException(errorMsg, pye);
}
} else {
final String pyModuleName = fullImportName.substring(0, i);
final String pyClassName = fullImportName.substring(i + 1);
try {
pyImport = importer.__call__(Py.newString(pyModuleName))
.__getattr__(pyClassName);
} catch (final PyException pye) {
throw new PythonImportNotFoundException(errorMsg, pye);
}
}
loadedPyImports.putIfAbsent(fullImportName, pyImport);
}
return loadedPyImports.get(fullImportName);
}
}

View File

@ -1,20 +1,20 @@
package net.abhinavsarkar.jywrapper;
import net.abhinavsarkar.jywrapper.PyObjectProxy.MemberType;
public enum PyMethodType {
INIT(MemberType.INIT),
DIRECT(MemberType.DIRECT),
UNDERSCORED(MemberType.UNDERSCORED),
NUMERIC(MemberType.NUMERIC);
private final MemberType memberType;
private PyMethodType(MemberType memberType) {
this.memberType = memberType;
}
public MemberType getMemberType() {
return memberType;
}
}
package net.abhinavsarkar.jywrapper;
import net.abhinavsarkar.jywrapper.PyObjectProxy.MemberType;
public enum PyMethodType {
INIT (MemberType.INIT),
DIRECT (MemberType.DIRECT),
UNDERSCORED (MemberType.UNDERSCORED),
NUMERIC (MemberType.NUMERIC);
private final MemberType memberType;
private PyMethodType(final MemberType memberType) {
this.memberType = memberType;
}
MemberType getMemberType() {
return memberType;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,75 +1,86 @@
package net.abhinavsarkar.jywrapper;
import static net.abhinavsarkar.jywrapper.Messages._;
import org.python.core.Py;
import org.python.core.PyFunction;
import org.python.core.PyModule;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyType;
final class Util {
private Util() {
}
static <T> T py2Java(final PyObject pyObject, final Class<T> javaClass) {
final Object javaWrapper = pyObject.__tojava__(javaClass);
if (javaWrapper == Py.NoConversion) {
if (javaClass.isInterface()) {
return PyObjectProxy.newInstance(pyObject, javaClass);
} else {
throw new IllegalArgumentException(
_("JyWrapper.1", pyObject, javaClass.getName())); //$NON-NLS-1$
}
} else {
@SuppressWarnings("unchecked")
final T t = (T) javaWrapper;
return t;
}
}
static PyObject[] convertArgs(final Object[] args) {
if (args == null || args.length == 0) {
return new PyObject[0];
}
if (args.length == 1) {
return new PyObject[] { Py.java2py(args[0]) };
}
final PyObject[] pyArgs = new PyObject[args.length];
for (int i = args.length; --i >= 0;) {
pyArgs[i] = Py.java2py(args[i]);
}
return pyArgs;
}
static String getPyImportName(final PyObject pyObject) {
synchronized (pyObject) {
if (pyObject instanceof PyType) {
final PyType pyType = (PyType) pyObject;
final PyObject module = pyType.getModule();
if (module instanceof PyString
&& !module.toString().equals("__builtin__")) { //$NON-NLS-1$
return String.format("%s.%s", module.toString(), //$NON-NLS-1$
pyType.getName());
}
return pyType.getName();
} else if (pyObject instanceof PyModule) {
return ((PyModule) pyObject).toString();
} else if (pyObject instanceof PyFunction) {
final PyFunction pyFunction = (PyFunction) pyObject;
return String.format("%s.%s", pyFunction.__module__, //$NON-NLS-1$
pyFunction.__name__);
} else {
return getPyImportName(pyObject.getType());
}
}
}
static String camelCase2UnderScore(final String word) {
return word.replaceAll("([A-Z])", "_$1").toLowerCase(); //$NON-NLS-1$ //$NON-NLS-2$
}
}
package net.abhinavsarkar.jywrapper;
import static net.abhinavsarkar.jywrapper.Messages._;
import org.python.core.Py;
import org.python.core.PyClass;
import org.python.core.PyFunction;
import org.python.core.PyModule;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyType;
final class Util {
private Util() {
}
static <T> T py2Java(final PyObject pyObject, final Class<T> javaClass) {
final Object javaWrapper = pyObject.__tojava__(javaClass);
if (javaWrapper == Py.NoConversion) {
if (javaClass.isInterface()) {
return PyObjectProxy.newInstance(pyObject, javaClass);
} else {
throw new IllegalArgumentException(
_("JyWrapper.1", pyObject, javaClass.getName())); //$NON-NLS-1$
}
} else {
@SuppressWarnings("unchecked")
final T t = (T) javaWrapper;
return t;
}
}
static PyObject[] convertArgs(final Object[] args) {
if (args == null || args.length == 0) {
return new PyObject[0];
}
if (args.length == 1) {
return new PyObject[] { Py.java2py(args[0]) };
}
final PyObject[] pyArgs = new PyObject[args.length];
for (int i = args.length; --i >= 0;) {
pyArgs[i] = Py.java2py(args[i]);
}
return pyArgs;
}
static String getPyImportName(final PyObject pyObject) {
synchronized (pyObject) {
if (pyObject instanceof PyType) {
final PyType pyType = (PyType) pyObject;
final PyObject module = pyType.getModule();
if (module instanceof PyString
&& !module.toString().equals("__builtin__")) { //$NON-NLS-1$
return String.format("%s.%s", module.toString(), //$NON-NLS-1$
pyType.getName());
}
return pyType.getName();
} else if (pyObject instanceof PyClass) {
final PyClass pyClass = (PyClass) pyObject;
final PyObject mod = pyClass.__dict__.__finditem__("__module__");
String smod;
if (mod == null || !(mod instanceof PyString)) {
smod = "<unknown>";
} else {
smod = ((PyString) mod).toString();
}
return smod + "." + pyClass.__name__;
} else if (pyObject instanceof PyModule) {
return ((PyModule) pyObject).toString();
} else if (pyObject instanceof PyFunction) {
final PyFunction pyFunction = (PyFunction) pyObject;
return String.format(
"%s.%s", pyFunction.__module__, pyFunction.__name__); //$NON-NLS-1$
} else {
return getPyImportName(pyObject.getType());
}
}
}
static String camelCase2UnderScore(final String word) {
return word.replaceAll("([A-Z])", "_$1").toLowerCase(); //$NON-NLS-1$ //$NON-NLS-2$
}
}

View File

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