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
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
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")
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)
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)
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')
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)
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'))
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)
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)
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')
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')
def test_03_02_cw_from_string(self): '''Get a class wrapper from a string''' c = javabridge.get_class_wrapper("java.lang.Number")
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')
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)
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)
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)
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')
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)
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))
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]))
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))