예제 #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')