javaclass

Annotated java/lang/reflect.py

158:a52e70c3f294
2005-01-23 Paul Boddie Added "safety measures" for exception offset insertion, although better measures are necessary to avoid bizarre JDK 1.4 exception tables.
paul@114 1
#!/usr/bin/env python
paul@114 2
paul@114 3
import java.lang
paul@114 4
paul@114 5
class Class(java.lang.Object):
paul@114 6
    def __init__(self, obj):
paul@114 7
        self.obj = obj
paul@114 8
paul@114 9
    def getMethod(self, name, parameterTypes):
paul@114 10
paul@114 11
        """
paul@114 12
        Find the method on this class with the given 'name' and
paul@114 13
        'parameterTypes'. Return a Method object.
paul@114 14
paul@114 15
        Note that this implementation remembers which object was used to obtain
paul@114 16
        this class. This allows the Method objects to be called in Python.
paul@114 17
        """
paul@114 18
paul@114 19
        # Build the Python name.
paul@114 20
paul@114 21
        types = []
paul@114 22
        for parameterType in parameterTypes:
paul@114 23
            types.append("__".join(parameterType.split(".")))
paul@114 24
        method_name = unicode(name) + "___" + "___".join(types)
paul@114 25
paul@114 26
        # Either return a Method object or raise the appropriate exception.
paul@114 27
        try:
paul@114 28
            return Method(getattr(self.obj, method_name))
paul@114 29
        except AttributeError:
paul@114 30
            raise Exception, java.lang.NoSuchMethodException(name)
paul@114 31
paul@114 32
    getMethod___java__lang__String___java__lang__Class_array_ = getMethod
paul@114 33
paul@114 34
class Method(java.lang.Object):
paul@114 35
    def __init__(self, method):
paul@114 36
        self.method = method
paul@114 37
paul@114 38
    def __call__(self, *args):
paul@114 39
        return self.method(*args)
paul@114 40
paul@114 41
# vim: tabstop=4 expandtab shiftwidth=4