⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tty.java

📁 《移动Agent技术》一书的所有章节源代码。
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

    void clear(StringTokenizer t) throws Exception {
	if (!t.hasMoreTokens()) {
	    listBreakpoints();
	    return;
	}

	String idClass = null;
	String idMethod = null;
	RemoteClass cls = null;
	try {
	    idClass = t.nextToken(": \t\n\r");
	    try {
	        cls = getClassFromToken(idClass);
            } catch (IllegalArgumentException e) {
                // Try stripping method from class.method token.
                int idot = idClass.lastIndexOf(".");
                if (idot == -1) {
                    out.println("\"" + idClass +
                        "\" is not a valid id or class name.");
                    return;
                }
                idMethod = idClass.substring(idot + 1);
                idClass = idClass.substring(0, idot);
                cls = getClassFromToken(idClass);
                RemoteField method;
                try {
                    method = cls.getMethod(idMethod);
                } catch (NoSuchMethodException nsme) {
		    out.println("\"" + idMethod +
				"\" is not a valid method name of class " +
				cls.getName());
		    return;
		}
		String err = cls.clearBreakpointMethod(method);
	        if (err.length() > 0) {
		    out.println(err);
	        } else {
		    out.println("Breakpoint cleared at " +
				cls.getName() + "." + idMethod);
		}
		return;
            }

	    String idLine = t.nextToken();
	    int lineno = Integer.valueOf(idLine).intValue();

	    String err = cls.clearBreakpointLine(lineno);
	    if (err.length() > 0) {
		out.println(err);
	    } else {
		out.println("Breakpoint cleared at " + cls.getName() +
				   ": " + lineno);
	    }
	} catch (NoSuchElementException e) {
	    out.println("Usage: clear <class>:<line_number>");
	    out.println("   or: clear <class>.<method>");
	} catch (NumberFormatException e) {
	    out.println("Usage: clear <class>:<line_number>");
	    out.println("   or: clear <class>.<method>");
	} catch (IllegalArgumentException e) {
	    out.println("\"" + idClass +
			       "\" is not a valid id or class name.");
	}
    }

    void list(StringTokenizer t) throws Exception {
	RemoteStackFrame frame = null;
	if (currentThread == null) {
	    out.println("No thread specified.");
	    return;
	}
	try {
	    frame = currentThread.getCurrentFrame();
	} catch (IllegalAccessError e) {
	    out.println("Current thread isn't suspended.");
	    return;
	} catch (ArrayIndexOutOfBoundsException e) {
	    out.println("Thread is not running (no stack).");
	    return;
	}

	int lineno;
	if (t.hasMoreTokens()) {
	    String id = t.nextToken();

            // See if token is a line number.
            try {
                lineno = Integer.valueOf(id).intValue();
            } catch (NumberFormatException nfe) {
                // It isn't -- see if it's a method name.
                try {
                    lineno = frame.getRemoteClass().getMethodLineNumber(id);
                } catch (NoSuchMethodException iobe) {
                    out.println(id + " is not a valid line number or " +
                                "method name for class " +
                                frame.getRemoteClass().getName());
                    return;
                } catch (NoSuchLineNumberException nse) {
                    out.println("Line number information not found in " +
                                frame.getRemoteClass().getName());
                    return;
                }
            }
	} else {
	    lineno = frame.getLineNumber();
	}
	int startLine = (lineno > 4) ? lineno - 4 : 1;
	int endLine = startLine + 9;

	InputStream rawSourceFile = frame.getRemoteClass().getSourceFile();
	if (rawSourceFile == null) {
	    out.println("Unable to find " +
                        frame.getRemoteClass().getSourceFileName());
	    return;
	}

	DataInputStream sourceFile = new DataInputStream(rawSourceFile);
	String sourceLine = null;

	/* Skip through file to desired window. */
	for (int i = 1; i <= startLine; i++) {
	    sourceLine = sourceFile.readLine();
	}
	if (sourceLine == null) {
	    out.println(new Integer(lineno).toString() +
                        " is an invalid line number for the file " +
                        frame.getRemoteClass().getSourceFileName());
	}

	/* Print lines */
	for (int i = startLine; i < endLine && sourceLine != null; i++) {
	    out.print(new Integer(i).toString() + "\t" +
			     ((i == lineno) ? "=> " : "   "));
	    out.println(sourceLine);
	    sourceLine = sourceFile.readLine();
	}

    }

    /* Get or set the source file path list. */
    void use(StringTokenizer t) throws Exception {
	if (!t.hasMoreTokens()) {
	    out.println(debugger.getSourcePath());
	} else {
	    debugger.setSourcePath(t.nextToken());
	}
    }

    /* Print a stack variable */
    private void printVar(RemoteStackVariable var) {
        out.print("  " + var.getName());
        if (var.inScope()) {
            RemoteValue val = var.getValue();
            out.println(" = " + (val == null? "null" : val.toString()) );
        } else {
            out.println(" is not in scope");
        }
    }

    /* Print all local variables in current stack frame. */
    void locals() throws Exception {
	if (currentThread == null) {
	    out.println("No default thread specified: " +
			       "use the \"thread\" command first.");
	    return;
	}
        if (!currentThread.isSuspended()) {
            out.println("Thread isn't suspended.");
            return;
        }
	RemoteStackVariable rsv[] = currentThread.getStackVariables();
	if (rsv == null || rsv.length == 0) {
	    out.println("No local variables: try compiling with -g");
	    return;
	}
	out.println("Method arguments:");
	for (int i = 0; i < rsv.length; i++) {
	    if (rsv[i].methodArgument()) {
		printVar(rsv[i]);
	    }
	}
	out.println("Local variables:");
	for (int i = 0; i < rsv.length; i++) {
	    if (!rsv[i].methodArgument()) {
		printVar(rsv[i]);
            }
	}
	return;
    }

    static final String printDelimiters = ".[(";

    /* Print a specified reference.
     * New print() implementation courtesy of S. Blackheath of IBM
     */
    void print(StringTokenizer t, boolean dumpObject) throws Exception {
	if (!t.hasMoreTokens()) {
	    out.println("No objects specified.");
            return;
	}

	int id;
	RemoteValue obj = null;

        while (t.hasMoreTokens()) {
	    String expr = t.nextToken();
	    StringTokenizer pieces =
	       new StringTokenizer(expr, printDelimiters, true);

	    String idToken = pieces.nextToken(); // There will be at least one.
	    if (idToken.startsWith("t@")) {
	        /* It's a thread */
	        setDefaultThreadGroup();
	        RemoteThread tlist[] = currentThreadGroup.listThreads(true);
	        try {
	            id = Integer.valueOf(idToken.substring(2)).intValue();
	        } catch (NumberFormatException e) {
		    id = 0;
	        }
	        if (id <= 0 || id > tlist.length) {
	            out.println("\"" + idToken +
		           "\" is not a valid thread id.");
                    continue;
	        }
	        obj = tlist[id - 1];

	    } else if (idToken.startsWith("$s")) {
	        int slotnum;
	        try {
	            slotnum = Integer.valueOf(idToken.substring(2)).intValue();
	        } catch (NumberFormatException e) {

	            out.println("\"" + idToken + "\" is not a valid slot.");
                    continue;
	        }
	        if (currentThread != null) {
	            RemoteStackVariable rsv[] = currentThread.getStackVariables();
		    if (rsv == null || slotnum >= rsv.length) {
		        out.println("\"" + idToken + "\" is not a valid slot.");
                        continue;
		    }
		    obj = rsv[slotnum].getValue();
	        }

	    } else if (idToken.startsWith("0x") ||
		       Character.isDigit(idToken.charAt(0))) {
	        /* It's an object id. */
	        try {
	            id = RemoteObject.fromHex(idToken);
	        } catch (NumberFormatException e) {
	            id = 0;
	        }
                if (id == 0 || (obj = debugger.get(new Integer(id))) == null) {
	            out.println("\"" + idToken + "\" is not a valid id.");
                    continue;
	        }
	    } else {
	        /* See if it's a local stack variable */
                RemoteStackVariable rsv = null;
	        if (currentThread != null) {
		    rsv = currentThread.getStackVariable(idToken);
		    if (rsv != null && !rsv.inScope()) {
		        out.println(idToken + " is not in scope.");
                        continue;
		    }
		    obj = (rsv == null) ? null : rsv.getValue();
	        }
	        if (rsv == null) {
                    String error = null;
                    /* See if it's an instance variable */
                    String instanceStr = idToken;
                    try {
                        instanceStr = instanceStr + pieces.nextToken("");
                    }
                    catch (NoSuchElementException e) {}

                    if (currentThread != null)
                        rsv = currentThread.getStackVariable("this");
                    if (rsv != null && rsv.inScope()) {
                        obj = rsv.getValue();

                        error = printModifiers(expr,
                              new StringTokenizer("."+instanceStr, printDelimiters, true),
                              dumpObject, obj, true);
                        if (error == null)
                            continue;
                    }

                    // If the above failed, then re-construct the same
                    // string tokenizer we had before.
                    pieces = new StringTokenizer(instanceStr, printDelimiters, true);
                    idToken = pieces.nextToken();

		    /* Try interpreting it as a class */
                    while (true) {
		        obj = debugger.findClass(idToken);
		        if (obj != null)             // break if this is a valid class name
                            break;
                        if (!pieces.hasMoreTokens()) // break if we run out of input
                            break;
                        String dot = pieces.nextToken();
                        if (!dot.equals("."))        // break if this token is not a dot
                            break;
                        if (!pieces.hasMoreTokens())
                            break;
                        // If it is a dot, then add the next token, and loop
                        idToken = idToken + dot + pieces.nextToken();
                    }
                    if (obj == null) {
                        if (error == null)
		            error = "\"" + expr + "\" is not a " + "valid local or class name.";
                    }
                    else {
                        String error2 = printModifiers(expr, pieces, dumpObject, obj, false);
                        if (error2 == null)
                            continue;
                        if (error == null)
                            error = error2;
                    }
                    out.println(error);
                    continue;
	        }
	    }
            String error = printModifiers(expr, pieces, dumpObject, obj, false);
            if (error != null)
                out.println(error);
        }
    }

    String printModifiers(String expr, StringTokenizer pieces, boolean dumpObject, RemoteValue obj,
        boolean could_be_local_or_class)
        throws Exception
    {
        RemoteInt noValue = new RemoteInt(-1);
        RemoteValue rv = noValue;

        // If the object is null, or a non-object type (integer, array, etc...)
        // then the value must be in rv.
        if (obj == null)
            rv = null;
        else
        if (!obj.isObject())
            rv = obj;

	String lastField = "";
	String idToken = pieces.hasMoreTokens() ? pieces.nextToken() : null;
	while (idToken != null) {

	    if (idToken.equals(".")) {
	        if (pieces.hasMoreTokens() == false) {
		    return "\"" + expr + "\" is not a valid expression.";
		}
		idToken = pieces.nextToken();

		if (rv != noValue) {
		    /* attempt made to get a field on a non-object */
		    return "\"" + lastField + "\" is not an object.";
		}
		lastField = idToken;

                /* Rather than calling RemoteObject.getFieldValue(), we do this so that
                 * we can report an error if the field doesn't exist. */
                {
	            RemoteField fields[] = ((RemoteObject)obj).getFields();
                    boolean found = false;
                    for (int i = fields.length-1; i >= 0; i--)
                        if (idToken.equals(fields[i].getName())) {
                            rv = ((RemoteObject)obj).getFieldValue(i);
                            found = true;
                            break;
                        }

                    if (!found) {
                        if (could_be_local_or_class)
                            /* expr is used here instead of idToken, because:
                             *   1. we know that we're processing the first token in the line,
                             *   2. if the user specified a class name with dots in it, 'idToken'
                             *      will only give the first token. */
                            return "\"" + expr + "\" is not a valid local, class name, or field of "
                                + obj.description();
                        else
                            return "\"" + idToken + "\" is not a valid field of "
                                + obj.description();
                    }
                }

                  // don't give long error message next time round the loop
                could_be_local_or_class = false;

		if (rv != null && rv.isObject()) {
		    obj = rv;
		    rv = noValue;
		}
		idToken =
		    pieces.hasMoreTokens() ? pieces.nextToken() : null;

	    } else if (idToken.equals("[")) {
		if (pieces.hasMoreTokens() == false) {
		    return "\"" + expr +
					"\" is not a valid expression.";
		}
		idToken = pieces.nextToken("]");
		try {
		    int index = Integer.valueOf(idToken).intValue();
		    rv = ((RemoteArray)obj).getElement(index);
		} catch (NumberFormatException e) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -