Пример #1
0
 def __init__(self, class_name):
     '''Initialize to wrap a class name
     
     :param class_name: name of class in dotted form, e.g. java.lang.Integer
     '''
     STATIC = J.get_static_field("java/lang/reflect/Modifier", "STATIC", "I")
     self.cname = class_name.replace(".", "/")
     self.klass = J.get_class_wrapper(J.class_for_name(class_name), True)
     self.static_methods = {}
     env = J.get_env()
     jmethods = env.get_object_array_elements(self.klass.getMethods())
     methods = {}
     for jmethod in jmethods:
         if (J.call(jmethod, "getModifiers", "()I") & STATIC) != STATIC:
             continue
         method = J.get_method_wrapper(jmethod)
         name = method.getName()
         if name not in methods:
             methods[name] = []
             fn = lambda naame=name: lambda *args: self.__call_static(naame, *args)
             fn = fn()
             fn.__doc__ = J.to_string(jmethod)
             setattr(self, name, fn)
         else:
             fn = getattr(self, name)
             fn.__doc__ = fn.__doc__ +"\n"+J.to_string(jmethod)
         methods[name].append(method)
     self.methods = methods
Пример #2
0
 def __init__(self, o):
     '''Initialize the JWrapper with a Java object
     
     :param o: a Java object (class = JB_Object)
     '''
     STATIC = J.get_static_field("java/lang/reflect/Modifier", "STATIC", "I")
     self.o = o
     self.class_wrapper = J.get_class_wrapper(o)
     env = J.get_env()
     jmethods = env.get_object_array_elements(self.class_wrapper.getMethods())
     methods = {}
     for jmethod in jmethods:
         if (J.call(jmethod, "getModifiers", "()I") & STATIC) == STATIC:
             continue
         method = J.get_method_wrapper(jmethod)
         name = method.getName()
         if name not in methods:
             methods[name] = []
             fn = lambda naame=name: lambda *args: self.__call(naame, *args)
             fn = fn()
             fn.__doc__ = J.to_string(jmethod)
             setattr(self, name, fn)
         else:
             fn = getattr(self, name)
             fn.__doc__ = fn.__doc__ +"\n"+J.to_string(jmethod)
         methods[name].append(method)
     self.methods = methods
Пример #3
0
 def test_03_07_cw_get_fields(self):
     c = javabridge.get_class_wrapper('java.lang.String')
     fields = c.getFields()
     fields = javabridge.get_env().get_object_array_elements(fields)
     self.assertEqual(len(fields), 1)
     self.assertEqual(javabridge.call(fields[0], 'getName', '()Ljava/lang/String;'),
                      "CASE_INSENSITIVE_ORDER")
Пример #4
0
        def allowOpenToCheckType(self, allow):
            '''Allow the "isThisType" function to open files

            For the cluster, you want to tell potential file formats
            not to open the image file to test if it's their format.
            '''
            if not hasattr(self, "allowOpenToCheckType_method"):
                self.allowOpenToCheckType_method = None
                class_wrapper = jutil.get_class_wrapper(self.o)
                methods = class_wrapper.getMethods()
                for method in jutil.get_env().get_object_array_elements(methods):
                    m = jutil.get_method_wrapper(method)
                    if m.getName() in ('allowOpenToCheckType', 'setAllowOpenFiles'):
                        self.allowOpenToCheckType_method = m
            if self.allowOpenToCheckType_method is not None:
                object_class = env.find_class('java/lang/Object')
                jexception = jutil.get_env().exception_occurred()
                if jexception is not None:
                    raise jutil.JavaException(jexception)

                boolean_value = jutil.make_instance('java/lang/Boolean',
                                                    '(Z)V', allow)
                args = jutil.get_env().make_object_array(1, object_class)
                jexception = jutil.get_env().exception_occurred()
                if jexception is not None:
                    raise jutil.JavaException(jexception)
                jutil.get_env().set_object_array_element(args, 0, boolean_value)
                jexception = jutil.get_env().exception_occurred()
                if jexception is not None:
                    raise jutil.JavaException(jexception)
                self.allowOpenToCheckType_method.invoke(self.o, args)
Пример #5
0
def get_plugin(classname):
    '''Return an instance of the named plugin'''
    if classname.startswith("ij."):
        cls = J.class_for_name(classname)
    else:
        cls = J.class_for_name(classname, get_user_loader())
    cls = J.get_class_wrapper(cls, True)
    constructor = J.get_constructor_wrapper(cls.getConstructor(None))
    return constructor.newInstance(None)
Пример #6
0
 def test_03_09_cw_get_method(self):
     sclass = javabridge.class_for_name('java.lang.String')
     iclass = javabridge.get_static_field('java/lang/Integer', 'TYPE', 
                                 'Ljava/lang/Class;')
     c = javabridge.get_class_wrapper('java.lang.String')
     m = c.getMethod('charAt', [ iclass ])
     self.assertEqual(javabridge.to_string(javabridge.call(m, 'getReturnType', '()Ljava/lang/Class;')), 'char')
     m = c.getMethod('concat', [ sclass])
     self.assertEqual(javabridge.to_string(javabridge.call(m, 'getReturnType', '()Ljava/lang/Class;')), 
                      'class java.lang.String')
Пример #7
0
 def test_03_06_cw_get_constructors(self):
     c = javabridge.get_class_wrapper('java.lang.String')
     constructors = c.getConstructors()
     constructors = javabridge.get_env().get_object_array_elements(constructors)
     self.assertEqual(len(constructors), 15)
Пример #8
0
 def test_03_05_cw_get_annotations(self):
     c = javabridge.get_class_wrapper('java.security.Identity')
     annotations = c.getAnnotations()
     annotations = javabridge.get_env().get_object_array_elements(annotations)
     self.assertEqual(len(annotations), 1)
     self.assertTrue(javabridge.to_string(annotations[0]).startswith('@java.lang.Deprecated'))
Пример #9
0
 def test_03_04_cw_get_annotation(self):
     c = javabridge.get_class_wrapper('java.security.Identity')
     annotation = c.getAnnotation(javabridge.class_for_name('java.lang.Deprecated'))
     self.assertTrue(annotation is not None)
Пример #10
0
 def test_03_03_cw_get_classes(self):
     c = javabridge.get_class_wrapper('java.lang.Number')
     classes = c.getClasses()
     self.assertEqual(len(javabridge.get_env().get_object_array_elements(classes)), 0)
Пример #11
0
 def test_04_03_field_type(self):
     c = javabridge.get_class_wrapper('java.lang.Byte')
     f = javabridge.get_field_wrapper(c.getField('MAX_VALUE'))
     t = f.getType()
     self.assertEqual(javabridge.to_string(t), 'byte')
Пример #12
0
 def test_04_02_field_name(self):
     c = javabridge.get_class_wrapper('java.lang.Byte')
     f = javabridge.get_field_wrapper(c.getField('MAX_VALUE'))
     self.assertEqual(f.getName(), 'MAX_VALUE')
Пример #13
0
 def test_03_02_cw_from_string(self):
     '''Get a class wrapper from a string'''
     c = javabridge.get_class_wrapper("java.lang.Number")
Пример #14
0
 def test_04_01_field_get(self):
     c = javabridge.get_class_wrapper('java.lang.Byte')
     f = javabridge.get_field_wrapper(c.getField('MAX_VALUE'))
     v = f.get(None)
     self.assertEqual(javabridge.to_string(v), '127')
Пример #15
0
 def test_03_06_cw_get_constructors(self):
     c = javabridge.get_class_wrapper('java.lang.String')
     constructors = c.getConstructors()
     constructors = javabridge.get_env().get_object_array_elements(
         constructors)
     self.assertEqual(len(constructors), 15)
Пример #16
0
 def test_03_04_cw_get_annotation(self):
     c = javabridge.get_class_wrapper('java.security.Identity')
     annotation = c.getAnnotation(
         javabridge.class_for_name('java.lang.Deprecated'))
     self.assertTrue(annotation is not None)
Пример #17
0
 def test_03_03_cw_get_classes(self):
     c = javabridge.get_class_wrapper('java.lang.Number')
     classes = c.getClasses()
     self.assertEqual(
         len(javabridge.get_env().get_object_array_elements(classes)), 0)
Пример #18
0
 def test_03_11_cw_get_constructor(self):
     c = javabridge.get_class_wrapper('java.lang.String')
     sclass = javabridge.class_for_name('java.lang.String')
     constructor = c.getConstructor([sclass])
     self.assertEqual(javabridge.call(constructor, 'getName', '()Ljava/lang/String;'),
                      'java.lang.String')
Пример #19
0
 def test_03_08_cw_get_field(self):
     c = javabridge.get_class_wrapper('java.lang.String')
     field = c.getField('CASE_INSENSITIVE_ORDER')
     modifiers = javabridge.call(field, 'getModifiers', '()I')
     static = javabridge.get_static_field('java/lang/reflect/Modifier','STATIC','I')
     self.assertEqual((modifiers & static), static)
Пример #20
0
 def test_03_01_cw_from_class(self):
     '''Get a class wrapper from a class'''
     c = javabridge.get_class_wrapper(
         javabridge.make_instance('java/lang/Integer', '(I)V', 14))
Пример #21
0
 def test_03_10_cw_get_methods(self):
     c = javabridge.get_class_wrapper('java.lang.String')
     mmm = javabridge.get_env().get_object_array_elements(c.getMethods())
     self.assertTrue(any([javabridge.call(m, 'getName', '()Ljava/lang/String;') == 'concat'
                          for m in mmm]))
Пример #22
0
 def test_03_01_cw_from_class(self):
     '''Get a class wrapper from a class'''
     c = javabridge.get_class_wrapper(javabridge.make_instance('java/lang/Integer', '(I)V',
                                             14))
Пример #23
0
 def test_04_01_field_get(self):
     c = javabridge.get_class_wrapper('java.lang.Byte')
     f = javabridge.get_field_wrapper(c.getField('MAX_VALUE'))
     v = f.get(None)
     self.assertEqual(javabridge.to_string(v), '127')
Пример #24
0
 def test_03_02_cw_from_string(self):
     '''Get a class wrapper from a string'''
     c = javabridge.get_class_wrapper("java.lang.Number")
Пример #25
0
 def test_04_03_field_type(self):
     c = javabridge.get_class_wrapper('java.lang.Byte')
     f = javabridge.get_field_wrapper(c.getField('MAX_VALUE'))
     t = f.getType()
     self.assertEqual(javabridge.to_string(t), 'byte')
Пример #26
0
 def test_04_02_field_name(self):
     c = javabridge.get_class_wrapper('java.lang.Byte')
     f = javabridge.get_field_wrapper(c.getField('MAX_VALUE'))
     self.assertEqual(f.getName(), 'MAX_VALUE')