PageRenderTime 19ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/cmd/src/com/bluemarsh/jswat/command/commands/EvaluateCommand.java

http://jswat.googlecode.com/
Java | 102 lines | 68 code | 7 blank | 27 comment | 12 complexity | 16924e8a5e1df71ccbf579394419b48a MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1/*
  2 * The contents of this file are subject to the terms of the Common Development
  3 * and Distribution License (the License). You may not use this file except in
  4 * compliance with the License.
  5 *
  6 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
  7 * or http://www.netbeans.org/cddl.txt.
  8 *
  9 * When distributing Covered Code, include this CDDL Header Notice in each file
 10 * and include the License file at http://www.netbeans.org/cddl.txt.
 11 * If applicable, add the following below the CDDL Header, with the fields
 12 * enclosed by brackets [] replaced by your own identifying information:
 13 * "Portions Copyrighted [year] [name of copyright owner]"
 14 *
 15 * The Original Software is the JSwat Command Module. The Initial Developer of the
 16 * Software is Nathan L. Fiedler. Portions created by Nathan L. Fiedler
 17 * are Copyright (C) 2009. All Rights Reserved.
 18 *
 19 * Contributor(s): Nathan L. Fiedler.
 20 *
 21 * $Id: EvaluateCommand.java 149 2009-05-03 15:15:16Z nathanfiedler $
 22 */
 23
 24package com.bluemarsh.jswat.command.commands;
 25
 26import com.bluemarsh.jswat.command.AbstractCommand;
 27import com.bluemarsh.jswat.command.CommandArguments;
 28import com.bluemarsh.jswat.command.CommandContext;
 29import com.bluemarsh.jswat.command.CommandException;
 30import com.bluemarsh.jswat.command.MissingArgumentsException;
 31import com.bluemarsh.jswat.core.context.ContextProvider;
 32import com.bluemarsh.jswat.core.context.DebuggingContext;
 33import com.bluemarsh.jswat.core.expr.EvaluationException;
 34import com.bluemarsh.jswat.core.expr.Evaluator;
 35import com.bluemarsh.jswat.core.session.Session;
 36import com.sun.jdi.ClassNotPreparedException;
 37import com.sun.jdi.InvalidStackFrameException;
 38import com.sun.jdi.NativeMethodException;
 39import com.sun.jdi.ObjectCollectedException;
 40import com.sun.jdi.ThreadReference;
 41import java.io.PrintWriter;
 42import org.openide.util.NbBundle;
 43
 44/**
 45 * Evalutes a Java-like expression and displays the result.
 46 *
 47 * @author Nathan Fiedler
 48 */
 49public class EvaluateCommand extends AbstractCommand {
 50
 51    @Override
 52    public String getName() {
 53        return "evaluate";
 54    }
 55
 56    @Override
 57    public void perform(CommandContext context, CommandArguments arguments)
 58            throws CommandException, MissingArgumentsException {
 59
 60        PrintWriter writer = context.getWriter();
 61        Session session = context.getSession();
 62        DebuggingContext dc = ContextProvider.getContext(session);
 63        ThreadReference thread = dc.getThread();
 64        arguments.returnAsIs(true);
 65        String expr = arguments.rest();
 66        Evaluator eval = new Evaluator(expr);
 67        try {
 68            Object o = eval.evaluate(thread, dc.getFrame());
 69            String s = Evaluator.prettyPrint(o, thread);
 70            writer.println(s);
 71        } catch (EvaluationException ee) {
 72            Throwable t = ee.getCause();
 73            if (t instanceof ClassNotPreparedException) {
 74                throw new CommandException(NbBundle.getMessage(getClass(),
 75                        "ERR_ClassNotPrepared"), t);
 76            } else if (t instanceof IllegalThreadStateException) {
 77                throw new CommandException(NbBundle.getMessage(getClass(),
 78                        "ERR_ThreadNoStack"), t);
 79            } else if (t instanceof IndexOutOfBoundsException) {
 80                throw new CommandException(NbBundle.getMessage(getClass(),
 81                        "ERR_InvalidStackFrame"), t);
 82            } else if (t instanceof InvalidStackFrameException) {
 83                throw new CommandException(NbBundle.getMessage(getClass(),
 84                        "ERR_InvalidStackFrame"), t);
 85            } else if (t instanceof NativeMethodException) {
 86                throw new CommandException(NbBundle.getMessage(getClass(),
 87                        "ERR_NativeMethod"), t);
 88            } else if (t instanceof ObjectCollectedException) {
 89                throw new CommandException(NbBundle.getMessage(getClass(),
 90                        "ERR_ObjectCollected"), t);
 91            } else {
 92                throw new CommandException(NbBundle.getMessage(getClass(),
 93                        "ERR_EvaluationError", ee.getMessage()), ee);
 94            }
 95        }
 96    }
 97
 98    @Override
 99    public boolean requiresArguments() {
100        return true;
101    }
102}