01: /*
02: * Copyright (c) 2005 Advanced Micro Devices, Inc.
03: *
04: * See the file "license.amd" for information on usage and
05: * redistribution of this file, and for a DISCLAIMER OF ALL
06: * WARRANTIES.
07: *
08: * RCS: @(#) $Id: TJCPackageCmd.java,v 1.4 2006/06/04 20:35:21 mdejong Exp $
09: *
10: */
11:
12: package tcl.lang;
13:
14: public class TJCPackageCmd implements Command {
15:
16: // Implementation of TJC::package used to load
17: // packages at runtime via a Java package name.
18:
19: public void cmdProc(Interp interp, TclObject[] objv)
20: throws TclException {
21: if (objv.length != 2) {
22: throw new TclNumArgsException(interp, 1, objv, "package");
23: }
24: String pkgname = objv[1].toString();
25: String classname = (pkgname.equals("default") ? "TJCExtension"
26: : pkgname + ".TJCExtension");
27:
28: // Create instance of Extension
29: Class c = JavaInvoke.getClassByName(interp, classname);
30:
31: Object o = null;
32: try {
33: o = c.newInstance();
34: } catch (InstantiationException ie) {
35: throw new TclException(interp, "class " + classname
36: + " could not be created");
37: } catch (IllegalAccessException iae) {
38: throw new TclException(interp, "class " + classname
39: + " could not be created");
40: }
41: if (!(o instanceof Extension)) {
42: throw new TclException(interp, "class " + classname
43: + " must extend Extension");
44: }
45: Extension ext = (Extension) o;
46: ext.init(interp);
47: interp.resetResult();
48: return;
49: }
50: }
|