Source code for clamp
from org.python.clamp import Clamper
__all__ = ['Clamper', 'method', 'throws', 'annotated', 'annotated_argument']
class _ClampedMethod(object):
def __init__(self, func):
self.name = func.func_name
self.return_type = None
self.arg_types = []
self._throws_exceptions = set()
self.method_annotations = {}
self.arg_annotations = [{}]
@property
def throws_exceptions(self):
return list(self._throws_exceptions)
[docs]def method(return_type, arg_types=[], name=None):
"""Decorate a clamp method with Java type information
:param return_type: method return type
:type return_type: java.lang.reflect.Type
:param arg_types: argument types that the method accepts
:type arg_types: array of java.lang.reflect.Type
:param name: alternative method name. You can use this to support
polymorphism.
:type name: str
Examples...
Simple method with Void.TYPE as the return type:
.. code-block:: java
public void returnsVoid()
.. code-block:: python
@clamp.method(Void.TYPE)
def returnsVoid(self):
pass
This polymorphism example will generate a Java method with the following
signature:
.. code-block:: java
public int add(int a, int b)
public long add(long a, long b)
.. code-block:: python
@clamp.method(int.TYPE, [int.TYPE, int.TYPE])
def add(self, a, b):
return a + b
# we override the method name to "add", which will indicate that we want
# create a method with the same name
@clamp.method(long.TYPE, [long.TYPE, long.TYPE], name="add")
def addLong(self, a, b):
return a_long + b_long
"""
def _method(func):
if not hasattr(func, '_clamp'):
func._clamp = _ClampedMethod(func)
if name is not None:
func._clamp.name = name
func._clamp.return_type = return_type
func._clamp.arg_types = arg_types
return func
return _method
[docs]def throws(*exceptions):
"""Declare what Java exceptions this method throws
:param exceptions: accepts a list of exceptions as arguments
:type exceptions: a list of java.lang.Throwable
Example:
.. code-block:: java
public void writeList() throws IOException, NoSuchFieldException
.. code-block:: python
@clamp.throws(IOException, NoSuchFieldException)
@clamp.method(Void.TYPE)
def writeList(self):
raise IOException("message")
raise NoSuchFieldException("other message")
"""
def _throws(func):
if not hasattr(func, '_clamp'):
func._clamp = _ClampedMethod(func)
func._clamp._throws_exceptions.update(exceptions)
return func
return _throws
[docs]def annotated(annotation, **kwargs):
"""Annotate the Java version of the method with Java annotation
:param annotation: Java annotation
:type annotation: java.lang.annotation.Annotation
:param kwargs: keword arguments to pass to the annotation
:type kwargs: java.lang.Object
Example annotation:
.. code-block:: java
@junit.Test
public void test()
.. code-block:: python
@clamp.annotated(Test)
@clamp.method(Void.TYPE)
def test(self):
pass
"""
def _annotate(func):
if not hasattr(func, '_clamp'):
func._clamp = _ClampedMethod(func)
if kwargs:
func._clamp.method_annotations[annotation] = kwargs
else:
func._clamp.method_annotations[annotation] = None
return func
return _annotate
def annotated_argument(arg, annotations):
def _argument_annotations(func):
if not hasattr(func, '_clamp'):
func._clamp = _ClampedMethod(func)
func._clamp.arg_annotations[arg] = annotations
return func
return _argument_annotations