/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

  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. package com.bluemarsh.jswat.command.commands;
  24. import com.bluemarsh.jswat.command.AbstractCommand;
  25. import com.bluemarsh.jswat.command.CommandArguments;
  26. import com.bluemarsh.jswat.command.CommandContext;
  27. import com.bluemarsh.jswat.command.CommandException;
  28. import com.bluemarsh.jswat.command.MissingArgumentsException;
  29. import com.bluemarsh.jswat.core.context.ContextProvider;
  30. import com.bluemarsh.jswat.core.context.DebuggingContext;
  31. import com.bluemarsh.jswat.core.expr.EvaluationException;
  32. import com.bluemarsh.jswat.core.expr.Evaluator;
  33. import com.bluemarsh.jswat.core.session.Session;
  34. import com.sun.jdi.ClassNotPreparedException;
  35. import com.sun.jdi.InvalidStackFrameException;
  36. import com.sun.jdi.NativeMethodException;
  37. import com.sun.jdi.ObjectCollectedException;
  38. import com.sun.jdi.ThreadReference;
  39. import java.io.PrintWriter;
  40. import org.openide.util.NbBundle;
  41. /**
  42. * Evalutes a Java-like expression and displays the result.
  43. *
  44. * @author Nathan Fiedler
  45. */
  46. public class EvaluateCommand extends AbstractCommand {
  47. @Override
  48. public String getName() {
  49. return "evaluate";
  50. }
  51. @Override
  52. public void perform(CommandContext context, CommandArguments arguments)
  53. throws CommandException, MissingArgumentsException {
  54. PrintWriter writer = context.getWriter();
  55. Session session = context.getSession();
  56. DebuggingContext dc = ContextProvider.getContext(session);
  57. ThreadReference thread = dc.getThread();
  58. arguments.returnAsIs(true);
  59. String expr = arguments.rest();
  60. Evaluator eval = new Evaluator(expr);
  61. try {
  62. Object o = eval.evaluate(thread, dc.getFrame());
  63. String s = Evaluator.prettyPrint(o, thread);
  64. writer.println(s);
  65. } catch (EvaluationException ee) {
  66. Throwable t = ee.getCause();
  67. if (t instanceof ClassNotPreparedException) {
  68. throw new CommandException(NbBundle.getMessage(getClass(),
  69. "ERR_ClassNotPrepared"), t);
  70. } else if (t instanceof IllegalThreadStateException) {
  71. throw new CommandException(NbBundle.getMessage(getClass(),
  72. "ERR_ThreadNoStack"), t);
  73. } else if (t instanceof IndexOutOfBoundsException) {
  74. throw new CommandException(NbBundle.getMessage(getClass(),
  75. "ERR_InvalidStackFrame"), t);
  76. } else if (t instanceof InvalidStackFrameException) {
  77. throw new CommandException(NbBundle.getMessage(getClass(),
  78. "ERR_InvalidStackFrame"), t);
  79. } else if (t instanceof NativeMethodException) {
  80. throw new CommandException(NbBundle.getMessage(getClass(),
  81. "ERR_NativeMethod"), t);
  82. } else if (t instanceof ObjectCollectedException) {
  83. throw new CommandException(NbBundle.getMessage(getClass(),
  84. "ERR_ObjectCollected"), t);
  85. } else {
  86. throw new CommandException(NbBundle.getMessage(getClass(),
  87. "ERR_EvaluationError", ee.getMessage()), ee);
  88. }
  89. }
  90. }
  91. @Override
  92. public boolean requiresArguments() {
  93. return true;
  94. }
  95. }