Commits

Michał Bendowski committed 9bbbcc1

First attempt at generating classes based on JVM info.

Comments (0)

Files changed (5)

pypy/module/jvm/__init__.py

     """
 
     appleveldefs = {
-        'app_level_hello': 'app_level.hello'
+#        'app_level_hello': 'app_level.hello'
     }
 
     interpleveldefs = {
-        'interp_level_hello': 'interp_level.hello'
+        'make_instance': 'interp_level.make_instance'
     }

pypy/module/jvm/app_level.py

-def hello(name):
-    return "Hello, {0}, this is app-level!".format(name)

pypy/module/jvm/app_level_private.py

+
+class _JavaObject(object):
+    def __init__(self, class_name, full_name):
+        self._class_name = class_name
+        self._full_name = full_name
+
+def make_java_class(full_name, inst_methods, inst_fields, static_methods, static_fields):
+    class_name = full_name.split('.')[-1]
+
+    members = {}
+
+    def make_method(name):
+        def method(self):
+            return 'This was method %s' % name
+        return method
+
+    for method_name in inst_methods:
+        members[method_name] = make_method(method_name)
+
+    cls = type('Java' + class_name, (_JavaObject,), members)
+
+    def __init__(self):
+        super(JavaObject, self).__init__(class_name, full_name)
+
+    cls.__init__ = __init__
+
+    return cls

pypy/module/jvm/interp_level.py

-def hello(space, w_name):
-    res = "Hello, {0}, this is interp-level!".format(space.str_w(w_name))
-    return space.wrap(res)
+import os
+from pypy.interpreter.gateway import ApplevelClass, unwrap_spec
+from pypy.rlib.rjvm import java
+
+path, _ = os.path.split(__file__)
+app_level_file = os.path.join(path, 'app_level_private.py')
+app_level = ApplevelClass(file(app_level_file).read())
+del path, app_level_file
+
+w_make_java_class = app_level.interphook('make_java_class')
+
+def wrap_list_of_strings(space, lst):
+    list_w = [space.wrap(s) for s in lst]
+    return space.newlist(list_w)
+
+@unwrap_spec(class_name=str)
+def make_instance(space, class_name):
+    java_cls = java.lang.Class.forName(class_name)
+    method_names = {m.getName() for m in java_cls.getMethods()}
+    empty = space.newlist([])
+    w_cls = w_make_java_class(space, space.wrap(class_name),
+        wrap_list_of_strings(space, method_names), empty, empty, empty)
+
+    new_name = 'Java' + class_name.split('.')[-1]
+
+    space.setattr(space.builtin, space.wrap(new_name), w_cls)
+
+    w_obj = space.call(w_cls, empty)
+    return w_obj

pypy/module/jvm/test/test_jvm.py

         space = gettestobjspace(usemodules=('jvm',))
         cls.space = space
 
-    def test_app_hello(self):
+    def test_make_instance(self):
         import jvm
-        res = jvm.app_level_hello('Guido')
-        assert 'Guido' in res
-        assert 'app-level' in res
+        obj = jvm.make_instance('java.lang.Object')
 
-    def test_interp_hello(self):
-        import jvm
-        res = jvm.interp_level_hello('Guido')
-        assert 'Guido' in res
-        assert 'interp-level' in res
+        expected_methods = {'getClass', 'notifyAll', 'equals', 'hashCode',
+                            'toString', 'notify', 'wait'}
+
+        for name in expected_methods:
+            assert hasattr(obj, name)
+            assert name in getattr(obj, name)()
+
+        for name in [m for m in dir(obj) if not m.startswith('_')]:
+            assert name in expected_methods
+
+        assert obj._class_name == 'Object'
+        assert obj._full_name == 'java.lang.Object'
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.