javaclass

Annotated runclass.py

176:fb5bfdb1cb04
2005-02-21 Paul Boddie Attempted to add more sophisticated circular import handling.
paul@118 1
#!/usr/bin/env python
paul@118 2
paul@122 3
"A program to run Java class files."
paul@118 4
paul@136 5
import javaclass.classhook
paul@123 6
import java.lang
paul@118 7
paul@160 8
# NOTE: Simple __this__ package loader to potentially avoid repeated import
paul@160 9
# NOTE: issues exposed by test.py.
paul@160 10
paul@160 11
def load_classes(class_names):
paul@160 12
paul@160 13
    "Load the classes with the given 'class_names'."
paul@160 14
paul@160 15
    module = __import__("__this__", globals(), locals(), class_names)
paul@160 16
    objs = []
paul@160 17
    for class_name in class_names:
paul@160 18
        objs.append(getattr(module, class_name))
paul@160 19
    return objs
paul@160 20
paul@160 21
# The more general class loader.
paul@160 22
paul@118 23
def load_class(class_name):
paul@118 24
paul@118 25
    "Load the class with the given 'class_name'."
paul@118 26
paul@118 27
    class_name_parts = class_name.split(".")
paul@118 28
    if len(class_name_parts) == 1:
paul@118 29
        module = __import__("__this__", globals(), locals(), [class_name])
paul@118 30
        obj = getattr(module, class_name)
paul@118 31
    else:
paul@120 32
        class_module = ".".join(class_name_parts[:-1])
paul@120 33
        obj = __import__(class_module, globals(), locals())
paul@118 34
        for part in class_name_parts[1:]:
paul@118 35
            obj = getattr(obj, part)
paul@118 36
paul@118 37
    return obj
paul@118 38
paul@118 39
def run_class(cls, args):
paul@123 40
    cls.main([java.lang.String(arg) for arg in args])
paul@118 41
paul@118 42
if __name__ == "__main__":
paul@118 43
    import sys
paul@118 44
    cls = load_class(sys.argv[1])
paul@118 45
    run_class(cls, sys.argv[2:])
paul@118 46
paul@118 47
# vim: tabstop=4 expandtab shiftwidth=4