2014-09-16 10:17:13 +00:00
|
|
|
'''
|
|
|
|
signatures.py
|
|
|
|
=============
|
|
|
|
|
|
|
|
A handy API for writing JNI signatures easily
|
|
|
|
|
|
|
|
Author: chrisjrn
|
|
|
|
|
2014-09-17 07:45:07 +00:00
|
|
|
This module aims to provide a more human-friendly API for
|
|
|
|
wiring up Java proxy methods in PyJnius.
|
|
|
|
|
|
|
|
You can use the signature function to produce JNI method
|
|
|
|
signautures for methods; passing PyJnius JavaClass classes
|
|
|
|
as return or argument types; provided here are annotations
|
|
|
|
representing Java's primitive and array times.
|
|
|
|
|
|
|
|
Methods can return just a standard primitive type:
|
|
|
|
|
|
|
|
>>> signature(jint, ())
|
|
|
|
'()I'
|
|
|
|
|
|
|
|
>>> s.signature(jvoid, [jint])
|
|
|
|
'(I)V'
|
|
|
|
|
|
|
|
Or you can use autoclass proxies to specify Java classes
|
|
|
|
for return types.
|
|
|
|
|
|
|
|
>>> from jnius import autoclass
|
|
|
|
>>> String = autoclass("java.lang.String")
|
|
|
|
>>> signature(String, ())
|
|
|
|
'()Ljava/lang/String;'
|
|
|
|
|
2014-09-16 10:17:13 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
__version__ = '0.0.1'
|
|
|
|
|
|
|
|
from . import JavaClass
|
|
|
|
from . import java_method
|
|
|
|
|
|
|
|
|
|
|
|
''' Type specifiers for primitives '''
|
|
|
|
|
2014-09-17 07:50:31 +00:00
|
|
|
class _JavaSignaturePrimitive(object):
|
2014-09-16 10:17:13 +00:00
|
|
|
_spec = ""
|
|
|
|
|
|
|
|
def _MakeSignaturePrimitive(name, spec):
|
2014-09-17 07:50:31 +00:00
|
|
|
class __Primitive(_JavaSignaturePrimitive):
|
2014-09-16 10:17:13 +00:00
|
|
|
''' PyJnius signature for Java %s type ''' % name
|
|
|
|
_name = name
|
|
|
|
_spec = spec
|
2014-09-17 07:50:31 +00:00
|
|
|
__Primitive.__name__ = "j" + name
|
|
|
|
|
2014-09-16 10:17:13 +00:00
|
|
|
return __Primitive
|
|
|
|
|
|
|
|
jboolean = _MakeSignaturePrimitive("boolean", "Z")
|
|
|
|
jbyte = _MakeSignaturePrimitive("byte", "B")
|
|
|
|
jchar = _MakeSignaturePrimitive("char", "C")
|
|
|
|
jdouble = _MakeSignaturePrimitive("double", "D")
|
|
|
|
jfloat = _MakeSignaturePrimitive("float", "F")
|
|
|
|
jint = _MakeSignaturePrimitive("int", "I")
|
|
|
|
jlong = _MakeSignaturePrimitive("long", "J")
|
|
|
|
jshort = _MakeSignaturePrimitive("short", "S")
|
|
|
|
jvoid = _MakeSignaturePrimitive("void", "V")
|
|
|
|
|
|
|
|
def JArray(of_type):
|
2014-09-17 07:45:07 +00:00
|
|
|
''' Signature helper for identifying arrays of a given object or
|
|
|
|
primitive type. '''
|
2014-09-17 07:30:37 +00:00
|
|
|
|
2014-09-16 10:17:13 +00:00
|
|
|
spec = "[" + _jni_type_spec(of_type)
|
|
|
|
return _MakeSignaturePrimitive("array", spec)
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
def with_signature(returns, takes):
|
2014-09-16 10:17:13 +00:00
|
|
|
''' Alternative version of @java_method that takes JavaClass
|
|
|
|
objects to produce the method signature. '''
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
sig = signature(returns, takes)
|
2014-09-16 10:17:13 +00:00
|
|
|
return java_method(sig)
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
def signature(returns, takes):
|
2014-09-17 07:45:07 +00:00
|
|
|
''' Produces a JNI method signature, taking the provided arguments
|
|
|
|
and returning the given return type. '''
|
|
|
|
|
2014-09-16 10:17:13 +00:00
|
|
|
out_takes = []
|
|
|
|
for arg in takes:
|
|
|
|
out_takes.append(_jni_type_spec(arg))
|
|
|
|
|
|
|
|
return "(" + "".join(out_takes) + ")" + _jni_type_spec(returns)
|
|
|
|
|
|
|
|
def _jni_type_spec(jclass):
|
|
|
|
''' Produces a JNI type specification string for the given argument.
|
|
|
|
If the argument is a jnius.JavaClass, it produces the JNI type spec
|
|
|
|
for the class. Signature primitives return their stored type spec.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if issubclass(jclass, JavaClass):
|
|
|
|
return "L" + jclass.__javaclass__ + ";"
|
2014-09-17 07:51:56 +00:00
|
|
|
elif issubclass(jclass, _JavaSignaturePrimitive):
|
2014-09-16 10:17:13 +00:00
|
|
|
return jclass._spec
|
|
|
|
|