3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
7 import java.util.StringTokenizer;
10 import com.sun.jdi.request.*;
11 import jde.debugger.expr.*;
17 * random useful utilities
19 * Created: Thu Jul 8 13:01:24 1999
23 * @version $Revision: 1.3 $
29 * dump a particular object, in whatever way seems appropriate
30 * @param obj The object to dump
32 public static void dump(Object obj) {
33 if (obj instanceof Collection) {
34 Iterator it = ((Collection)obj).iterator();
35 while (it.hasNext()) {
36 System.out.println(it.next());
38 } else if (obj instanceof Object[]) {
39 Object[] k = (Object[])obj;
40 for (int i=0; i<k.length; i++)
43 System.out.println(obj);
49 * Safely convert to a Long, raising an appropriate
50 * JDENumberFormatException if required
52 * @param obj The object to convert. The .toString() is used.
53 * @param type The type of long: used in the exception string if required
54 * @return the Long, or raises exception.
56 public static Long safeGetLong(Object obj, String type)
59 return new Long(obj.toString());
60 } catch (NumberFormatException ex) {
61 throw new JDENumberFormatException(type);
67 * Safely convert to an int, raising an appropriate
68 * JDENumberFormatException if required
70 * @param obj The object to convert. The .toString() is used.
71 * @param type The type of int: used in the exception string if required
72 * @return the Integer, or raises exception.
74 public static int safeGetint(Object obj, String type)
77 return Integer.parseInt(obj.toString());
78 } catch (NumberFormatException ex) {
79 throw new JDENumberFormatException(type);
85 * Evaluate an expression, given a context
87 * @param expr The expression to evaluate
88 * @param frame The stackframe that defines the context
89 * @return a {@link Rep#getValueRep(Value) value}
91 public static Value evaluate(String expr, final StackFrame frame)
93 // System.out.println(expr);
95 ExpressionParser.GetFrame frameGetter = null;
96 frameGetter = new ExpressionParser.GetFrame() {
97 public StackFrame get()
98 throws IncompatibleThreadStateException {
102 return ExpressionParser.evaluate(expr, frame.virtualMachine(),
104 } catch (NativeMethodException ex) {
105 throw new JDEException("Can't access local variables in native methods");
106 } catch (ParseException ex) {
107 throw new JDEException(ex.toString());
108 } catch (InvocationException ex) {
109 throw new JDEException("Exception in expression: "+ex.exception().referenceType().name());
110 } catch (InvalidTypeException ite) {
111 throw new JDEException("Expression contains invalid type");
112 } catch (IncompatibleThreadStateException itse) {
113 throw new JDEException("This expression cannot be evaluated at an arbitrary location");
114 } catch (ClassNotLoadedException tnle) {
115 throw new JDEException("A required class for the evaluation hasn't been loaded");
121 * Parses the list of arguments for thread information.
123 * on_thread_id threadID
124 * on_thread_name "threadName"
126 * Note that the exception is <b>not</b> raised if the tags are
127 * not present: only if what follows the tag is incorrect.
129 * Commands having this argument will raise events only if the event
130 * thread matches the specification. More details in EventHandler.
132 * Shortcuts: <u>-tid</u> and <u>-tname</u>
134 * @return a Long corresponding to the threadID, or the string
136 * @exception JDEException If the information cannot be resolved
138 public static Object getThreadFromArgs(List args)
139 throws JDEException {
141 String threadArg = null;
142 int threadIndex = -1;
143 for (int i = 0; i < args.size(); i++) {
144 String arg = args.get(i).toString().toLowerCase();
145 if (arg.equals("on_thread_id") || arg.equals("on_thread_name")
146 || arg.equals("-tid") || arg.equals("-tname")) {
148 threadArg = args.remove(threadIndex).toString();
152 // at this point, either threadArg = null, or on_thread_id/name. in
153 // that case, threadIndex should now have the index of the argument
154 if (threadArg == null) {
157 if (threadArg.equals("on_thread_id")
158 || threadArg.equals("-tid")) {
159 if (threadIndex == args.size()) {
160 // ie missing argument
161 throw new JDEException("Missing argument to 'on_thread_id'");
164 return new Long(args.remove(threadIndex).toString());
165 } catch (NumberFormatException ex) {
166 throw new JDENumberFormatException("'on_thread_id' argument");
169 } else if (threadArg.equals("on_thread_name")
170 || threadArg.equals("-tname")) {
171 if (threadIndex == args.size()) {
172 throw new JDEException("Missing argument to 'on_thread_name'");
174 return args.remove(threadIndex).toString();
177 throw new JDEException("Should not happen! Contact maintainer");
184 * Parses the list of arguments for expression information.
188 * Note that the exception is <b>not</b> raised if the tag isn't
189 * present: only if what follows the tag is incorrect.
191 * When used, the expression is evaluated at the time of the event,
192 * and the event passed to jde if the expression evaluates to "true".
194 * Shortcut: <u>-e</u>
196 * @return string corresponding to the expression
197 * @exception JDEException If the information cannot be resolved
199 public static String getExprFromArgs(List args)
200 throws JDEException {
202 String exprArg = null;
204 for (int i = 0; i < args.size(); i++) {
205 String arg = args.get(i).toString().toLowerCase();
206 if (arg.equals("if") || arg.equals("-e")) {
208 exprArg = args.remove(exprIndex).toString();
212 // at this point, either exprArg = null, or "if". in
213 // that case, exprIndex should now have the index of the argument
214 if (exprArg == null) {
217 if (exprArg.equals("if") || exprArg.equals("-e")) {
218 if (exprIndex == args.size()) {
219 // ie missing argument
220 throw new JDEException("Missing argument to 'if'");
222 return args.remove(exprIndex).toString();
225 throw new JDEException("Should not happen! Contact maintainer");
232 * Parses the list of arguments for class filter information.
234 * class_filters "classPatternList"
236 * Note that the exception is <b>not</b> raised if the tag isn't
237 * present: only if what follows the tag is incorrect.
239 * the classPatternList should be a list of class patterns, using
240 * space or comma as delimiter.
242 * Shortcut: <u>-cf</u>
244 * This constraint is used to add class filters to events. To quote
247 * Restricts the events generated by this request to those whose
248 * location is in a class whose name matches a restricted regular
249 * expression. Regular expressions are limited to exact matches
250 * and patterns that begin with '*' or end with '*'; for example,
251 * "*.Foo" or "java.*".
253 * @return a List of all the class filters.
254 * @exception JDEException If the information cannot be resolved
256 public static List getClassFiltersFromArgs(List args)
257 throws JDEException {
259 String filterArg = null;
260 int filterIndex = -1;
261 for (int i = 0; i < args.size(); i++) {
262 String arg = args.get(i).toString().toLowerCase();
263 if (arg.equals("class_filters") || arg.equals("-cf")) {
265 filterArg = args.remove(filterIndex).toString();
269 // at this point, either filterArg = null, or "if". in
270 // that case, filterIndex should now have the index of the argument
271 if (filterArg == null) {
274 if (filterArg.equals("class_filters")
275 || filterArg.equals("-cf")) {
276 if (filterIndex == args.size()) {
277 // ie missing argument
278 throw new JDEException("Missing argument to 'class_filters'");
280 List filters = new ArrayList();
281 StringTokenizer tokens = new StringTokenizer(args.remove(filterIndex).toString(), " \t\n\r\f,");
282 while (tokens.hasMoreTokens()) {
283 filters.add(tokens.nextToken());
288 throw new JDEException("Should not happen! Contact maintainer");
295 * Parses the list of arguments for class exclusion filter information.
297 * class_exclusion_filters "classPatternList"
299 * Note that the exception is <b>not</b> raised if the tag isn't
300 * present: only if what follows the tag is incorrect.
302 * the classPatternList should be a list of class patterns, using
303 * space or comma as delimiter.
305 * Shortcut: <u>-cef</u>
307 * This is used to add class exclusion filters to events. To quote
310 * Restricts the events generated by this request to those whose
311 * location is in a class whose name does <b>not</b> match this
313 * regular expression. Regular expressions are limited to exact matches
314 * and patterns that begin with '*' or end with '*'; for example,
315 * "*.Foo" or "java.*".
317 * @return a List of all the class exclusion filters.
318 * @exception JDEException If the information cannot be resolved
320 public static List getClassExFiltersFromArgs(List args)
321 throws JDEException {
323 String filterArg = null;
324 int filterIndex = -1;
325 for (int i = 0; i < args.size(); i++) {
326 String arg = args.get(i).toString().toLowerCase();
327 if (arg.equals("class_exclusion_filters")
328 || arg.equals("-cef")) {
330 filterArg = args.remove(filterIndex).toString();
334 // at this point, either filterArg = null, or "if". in
335 // that case, filterIndex should now have the index of the argument
336 if (filterArg == null) {
339 if (filterArg.equals("class_exclusion_filters")
340 || filterArg.equals("-cef")) {
341 if (filterIndex == args.size()) {
342 // ie missing argument
343 throw new JDEException("Missing argument to 'class_exclusion_filters'");
345 List filters = new ArrayList();
346 StringTokenizer tokens = new StringTokenizer(args.remove(filterIndex).toString(), " \t\n\r\f,");
347 while (tokens.hasMoreTokens()) {
348 filters.add(tokens.nextToken());
353 throw new JDEException("Should not happen! Contact maintainer");
360 * Parses the list of arguments for suspend policy information.
362 * using_suspend_policy policy
364 * Note that the exception is <b>not</b> raised if the tags are
365 * not present: only if what follows the tag is incorrect.
367 * <i>policy</i> is one of "all", "thread", or "none". "all" means the
369 * VM is suspended when the event occurs, "thread" indicates only the
370 * thread on which the event occurs is suspended (only for events
371 * associated with threads), while "none" means nothing is suspended
372 * when the event occurs.
374 * Shortcut: <u>-sp</u>
376 * @return a valid int indicating the suspend policy
377 * @exception JDEException If the information cannot be resolved
379 public static int getSuspendPolicyFromArgs(List args)
380 throws JDEException {
382 String suspendPolicyArg = null;
383 int suspendPolicyIndex = -1;
384 for (int i = 0; i < args.size(); i++) {
385 String arg = args.get(i).toString().toLowerCase();
386 if (arg.equals("using_suspend_policy") || arg.equals("-sp")) {
387 suspendPolicyIndex = i;
388 suspendPolicyArg =args.remove(suspendPolicyIndex).toString();
392 // at this point, either suspendPolicyArg = null, or "if". in
393 // that case, suspendPolicyIndex should now have the index of the
395 if (suspendPolicyArg == null) {
396 return EventRequest.SUSPEND_ALL;
398 if (suspendPolicyArg.equals("using_suspend_policy")
399 || suspendPolicyArg.equals("-sp")) {
400 if (suspendPolicyIndex == args.size()) {
401 // ie missing argument
402 throw new JDEException("Missing argument to 'using_suspend_policy'");
404 String policy = args.remove(suspendPolicyIndex).toString().toLowerCase();
405 if (policy.equals("all")) {
406 return EventRequest.SUSPEND_ALL;
407 } else if (policy.equals("thread")) {
408 return EventRequest.SUSPEND_EVENT_THREAD;
409 } else if (policy.equals("none")) {
410 return EventRequest.SUSPEND_NONE;
412 throw new JDEException("Invalid suspend policy '"+policy+"'");
417 throw new JDEException("Should not happen! Contact maintainer");
424 * Parses the list of arguments for object ID information.
426 * if_object_id objectID
428 * Note that the exception is <b>not</b> raised if the tag isn't
429 * present: only if what follows the tag is incorrect.
431 * Shortcut: <u>-oid</u>
433 * @return a Long corresponding to the object ID.
434 * @exception JDEException If the information cannot be resolved
436 public static Long getObjectIDFromArgs(List args)
437 throws JDEException {
441 for (int i = 0; i < args.size(); i++) {
442 String arg = args.get(i).toString().toLowerCase();
443 if (arg.equals("if_object_id") || arg.equals("-oid")) {
445 idArg = args.remove(idIndex).toString();
449 // at this point, either idArg = null, or "if". in
450 // that case, idIndex should now have the index of the argument
454 if (idArg.equals("if_object_id")
455 || idArg.equals("-oid")) {
456 if (idIndex == args.size()) {
457 // ie missing argument
458 throw new JDEException("Missing argument to 'if_object_id'");
461 return new Long(args.remove(idIndex).toString());
462 } catch (NumberFormatException ex) {
463 throw new JDENumberFormatException("'if_object_id' argument");
467 throw new JDEException("Should not happen! Contact maintainer");
476 * Revision 1.3 2003/01/08 06:53:38 paulk
477 * Integrate Petter Mahlen's updates.