001: /*
002: * JavaFieldCmd.java --
003: *
004: * Implements the built-in "java::field" command.
005: *
006: * Copyright (c) 1997 Sun Microsystems, Inc.
007: *
008: * See the file "license.terms" for information on usage and
009: * redistribution of this file, and for a DISCLAIMER OF ALL
010: * WARRANTIES.
011: *
012: * RCS: @(#) $Id: JavaFieldCmd.java,v 1.2 1999/05/09 21:53:38 dejong Exp $
013: *
014: */
015:
016: package tcl.lang;
017:
018: /*
019: * This class implements the built-in "java::field" command.
020: */
021:
022: class JavaFieldCmd implements Command {
023:
024: /*----------------------------------------------------------------------
025: *
026: * cmdProc --
027: *
028: * This procedure is invoked to process the "java::field" Tcl
029: * command. See the user documentation for details on what they
030: * do.
031: *
032: * Results:
033: * None.
034: *
035: * Side effects:
036: * A standard Tcl result is stored in the interpreter.
037: *
038: *----------------------------------------------------------------------
039: */
040:
041: public void cmdProc(Interp interp, // Current interpreter.
042: TclObject argv[]) // Argument list.
043: throws TclException // A standard Tcl exception.
044: {
045: boolean query;
046: int objIndex;
047: boolean convert;
048:
049: if (argv.length < 3) {
050: throw new TclException(interp, usage(argv[0]));
051: }
052:
053: // Check the validity of the arguments. N.B., the -noconvert flag
054: // is allowed only in value query.
055:
056: String arg1 = argv[1].toString();
057: if ((arg1.length() >= 2) && ("-noconvert".startsWith(arg1))) {
058: convert = false;
059: objIndex = 2;
060:
061: if (argv.length == 4) {
062: query = true;
063: } else {
064: throw new TclException(interp, usage(argv[0]));
065: }
066: } else {
067: convert = true;
068: objIndex = 1;
069:
070: if (argv.length == 3) {
071: query = true;
072: } else if ((argv.length % 2) == 0) {
073: query = false;
074: } else {
075: throw new TclException(interp, usage(argv[0]));
076: }
077: }
078:
079: if (query) {
080: // Query one field.
081:
082: interp.setResult(JavaInvoke.getField(interp,
083: argv[objIndex], argv[objIndex + 1], convert));
084: } else {
085: // Set one or more fields.
086:
087: for (int i = objIndex + 1; i < argv.length; i += 2) {
088: JavaInvoke.setField(interp, argv[objIndex], argv[i],
089: argv[i + 1]);
090: }
091:
092: interp.resetResult();
093: }
094: }
095:
096: /*----------------------------------------------------------------------
097: *
098: * usage --
099: *
100: * Returns the usage string for the java::field command.
101: *
102: * Results:
103: * Returns the usage string for the java::field command.
104: *
105: * Side effects:
106: * None.
107: *
108: *----------------------------------------------------------------------
109: */
110:
111: private static final String usage(TclObject cmd) // The command name.
112: {
113: // FIXME : unneeded extra code
114:
115: // This does not conform to docs
116: /*
117: return "wrong # args: should be \"" +
118: cmd + " ?-noconvert? object field\" or \"" +
119: cmd + " object field value ?field value ...?\"";
120: */
121:
122: /*
123: return "wrong # args: should be \"" + cmd +
124: " ?-noconvert? objOrClass fieldSignature" +
125: " ?value fieldSignature value ...?";
126: */
127:
128: return "wrong # args: should be \"" + cmd
129: + " ?-noconvert? objOrClass field"
130: + " ?value field value ...?";
131:
132: }
133:
134: } // end JavaFieldCmd
|