javaclass

Annotated runclass.py

142:feba07e568f9
2005-01-22 Paul Boddie Added more test programs.
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@118 8
def load_class(class_name):
paul@118 9
paul@118 10
    "Load the class with the given 'class_name'."
paul@118 11
paul@118 12
    class_name_parts = class_name.split(".")
paul@118 13
    if len(class_name_parts) == 1:
paul@118 14
        module = __import__("__this__", globals(), locals(), [class_name])
paul@118 15
        obj = getattr(module, class_name)
paul@118 16
    else:
paul@120 17
        class_module = ".".join(class_name_parts[:-1])
paul@120 18
        obj = __import__(class_module, globals(), locals())
paul@118 19
        for part in class_name_parts[1:]:
paul@118 20
            obj = getattr(obj, part)
paul@118 21
paul@118 22
    return obj
paul@118 23
paul@118 24
def run_class(cls, args):
paul@123 25
    cls.main([java.lang.String(arg) for arg in args])
paul@118 26
paul@118 27
if __name__ == "__main__":
paul@118 28
    import sys
paul@118 29
    cls = load_class(sys.argv[1])
paul@118 30
    run_class(cls, sys.argv[2:])
paul@118 31
paul@118 32
# vim: tabstop=4 expandtab shiftwidth=4