01: /*
02: * JavaDefineClassCmd.java --
03: *
04: * This class implements the built-in "java::defineclass" command.
05: *
06: * Copyright (c) 1997 by Sun Microsystems, Inc.
07: *
08: * See the file "license.terms" for information on usage and redistribution
09: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10: *
11: * RCS: @(#) $Id: JavaDefineClassCmd.java,v 1.5 2006/02/08 23:53:47 mdejong Exp $
12: */
13:
14: package tcl.lang;
15:
16: class JavaDefineClassCmd implements Command {
17:
18: /*
19: *----------------------------------------------------------------------
20: *
21: * cmdProc --
22: *
23: * This procedure is invoked to process the "java::defineclass"
24: * Tcl comamnd. See the user documentation for details on what
25: * it does.
26: *
27: * Results:
28: * None.
29: *
30: * Side effects:
31: * A standard Tcl result is stored in the interpreter.
32: *
33: *----------------------------------------------------------------------
34: */
35:
36: public void cmdProc(Interp interp, // Current interpreter.
37: TclObject argv[]) // Argument list.
38: throws TclException // A standard Tcl exception.
39: {
40: byte[] classData = null;
41: Class result;
42:
43: if (argv.length != 2) {
44: throw new TclNumArgsException(interp, 1, argv, "classbytes");
45: }
46:
47: TclObject classBytesObj = argv[1];
48:
49: // If the classbytes argument is a ReflectObject
50: // that contains a byte[] then unwrap it and
51: // use the bytes directly. Creating a TclByteArray
52: // actually creates a copy of the passed in array,
53: // so passing the byte[] directly is faster.
54:
55: if (classBytesObj.getInternalRep() instanceof ReflectObject) {
56: Object obj = ReflectObject.get(interp, classBytesObj);
57: if (obj instanceof byte[]) {
58: classData = (byte[]) obj;
59: }
60: }
61:
62: if (classData == null) {
63: // FIXME: It would be better if the TclByteArray class
64: // was available in both Tcl Blend and Jacl so that we
65: // could query bytes directly instead of converting to
66: // a string and then converting back to bytes.
67:
68: String str = classBytesObj.toString();
69: final int str_length = str.length();
70: classData = new byte[str_length];
71: for (int i = 0; i < str_length; i++) {
72: classData[i] = (byte) str.charAt(i);
73: }
74: }
75:
76: // Use TclClassLoader defined on a per-interp basis
77: TclClassLoader tclClassLoader = (TclClassLoader) interp
78: .getClassLoader();
79:
80: result = tclClassLoader.defineClass(null, classData);
81:
82: interp.setResult(ReflectObject.newInstance(interp, Class.class,
83: result));
84: }
85:
86: } // end JavaNewCmd
|