paul@118 | 1 | #!/usr/bin/env python |
paul@118 | 2 | |
paul@186 | 3 | """ |
paul@186 | 4 | A program to run Java class files. |
paul@186 | 5 | |
paul@186 | 6 | Copyright (C) 2004, 2005, 2006, 2011 Paul Boddie <paul@boddie.org.uk> |
paul@186 | 7 | |
paul@186 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@186 | 9 | the terms of the GNU Lesser General Public License as published by the Free |
paul@186 | 10 | Software Foundation; either version 3 of the License, or (at your option) any |
paul@186 | 11 | later version. |
paul@186 | 12 | |
paul@186 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@186 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@186 | 15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paul@186 | 16 | details. |
paul@186 | 17 | |
paul@186 | 18 | You should have received a copy of the GNU Lesser General Public License along |
paul@186 | 19 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@186 | 20 | """ |
paul@118 | 21 | |
paul@136 | 22 | import javaclass.classhook |
paul@123 | 23 | import java.lang |
paul@118 | 24 | |
paul@160 | 25 | # NOTE: Simple __this__ package loader to potentially avoid repeated import |
paul@160 | 26 | # NOTE: issues exposed by test.py. |
paul@160 | 27 | |
paul@160 | 28 | def load_classes(class_names): |
paul@160 | 29 | |
paul@160 | 30 | "Load the classes with the given 'class_names'." |
paul@160 | 31 | |
paul@160 | 32 | module = __import__("__this__", globals(), locals(), class_names) |
paul@160 | 33 | objs = [] |
paul@160 | 34 | for class_name in class_names: |
paul@160 | 35 | objs.append(getattr(module, class_name)) |
paul@160 | 36 | return objs |
paul@160 | 37 | |
paul@160 | 38 | # The more general class loader. |
paul@160 | 39 | |
paul@118 | 40 | def load_class(class_name): |
paul@118 | 41 | |
paul@118 | 42 | "Load the class with the given 'class_name'." |
paul@118 | 43 | |
paul@118 | 44 | class_name_parts = class_name.split(".") |
paul@118 | 45 | if len(class_name_parts) == 1: |
paul@118 | 46 | module = __import__("__this__", globals(), locals(), [class_name]) |
paul@118 | 47 | obj = getattr(module, class_name) |
paul@118 | 48 | else: |
paul@120 | 49 | class_module = ".".join(class_name_parts[:-1]) |
paul@120 | 50 | obj = __import__(class_module, globals(), locals()) |
paul@118 | 51 | for part in class_name_parts[1:]: |
paul@118 | 52 | obj = getattr(obj, part) |
paul@118 | 53 | |
paul@118 | 54 | return obj |
paul@118 | 55 | |
paul@118 | 56 | def run_class(cls, args): |
paul@123 | 57 | cls.main([java.lang.String(arg) for arg in args]) |
paul@118 | 58 | |
paul@118 | 59 | if __name__ == "__main__": |
paul@118 | 60 | import sys |
paul@118 | 61 | cls = load_class(sys.argv[1]) |
paul@118 | 62 | run_class(cls, sys.argv[2:]) |
paul@118 | 63 | |
paul@118 | 64 | # vim: tabstop=4 expandtab shiftwidth=4 |