Skip to content

Instantly share code, notes, and snippets.

@leafstorm
Created July 31, 2014 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leafstorm/14787d09876c60ead95c to your computer and use it in GitHub Desktop.
Save leafstorm/14787d09876c60ead95c to your computer and use it in GitHub Desktop.
An implementation of a Jython FastGetter that doesn't work for some reason.
package jesreflect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.python.core.Py;
import org.python.core.PyObject;
/**
* An optimized getter that's designed to invoke a Java class's
* getSomething method, with no arguments, and wrap the return value.
*/
class FastGetter extends PyObject {
private Class<?> owningClass;
private Method method;
private String messagePrefix;
public FastGetter (String className, String methodName)
throws ClassNotFoundException, NoSuchMethodException {
this(className, methodName, "");
}
public FastGetter (String className, String methodName, String messagePrefix)
throws ClassNotFoundException, NoSuchMethodException {
owningClass = Class.forName(className);
// We're getting the method with 0 arguments.
method = owningClass.getMethod(methodName);
messagePrefix = messagePrefix;
}
// Python method overrides
public boolean isCallable () {
return true;
}
@Override
public PyObject __call__ (PyObject[] args, String[] keywords) {
return FastGetter___call__(args, keywords);
}
public PyObject __call__ (PyObject arg) {
return invoke(arg);
}
// Actually calling
public PyObject FastGetter___call__ (PyObject[] args, String[] keywords) {
// Check that we have the right arguments.
if (args.length != 1 && keywords.length != 0) {
throw Py.TypeError(messagePrefix + "Can only be called with one argument");
}
return invoke(args[0]);
}
public PyObject invoke (PyObject pyOwner) {
// Cast the argument they passed to the owning type.
Object owner = pyOwner.__tojava__(owningClass);
if (owner == Py.NoConversion) {
throw Py.TypeError(messagePrefix + "Input is not a " +
owningClass.getSimpleName());
}
// Invoke it!
Object value = null;
try {
value = method.invoke(owner);
} catch (InvocationTargetException exc) {
throw Py.JavaError(exc.getCause());
} catch (IllegalAccessException exc) {
throw Py.JavaError(exc);
}
return Py.java2py(value);
}
}
Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36)
[OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.8.0_05
Type "help", "copyright", "credits" or "license" for more information.
>>> from jesreflect import FastGetter
>>> getter = FastGetter('java.lang.String', 'length')
>>> dir(getter)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__']
>>> getter("Hi!")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'jesreflect.FastGetter' object is not callable
>>> getter
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f1509409f40, pid=12980, tid=139728348034816
#
# JRE version: OpenJDK Runtime Environment (8.0_05-b13) (build 1.8.0_05-b13)
# Java VM: OpenJDK 64-Bit Server VM (25.5-b02 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V [libjvm.so+0x79cf40] JVM_FindSignal+0x161850
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/matthew/groups/gatech/jes/hs_err_pid12980.log
Could not load hsdis-amd64.so; library not loadable; PrintAssembly is disabled
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment