KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > base > Options


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22
23 package org.aspectj.debugger.base;
24
25 import java.util.*;
26
27 /**
28  * Options.java
29  *
30  *
31  * Created: Thu Sep 28 09:31:33 2000
32  *
33  * @author <a HREF="mailto:palm@parc.xerox.com"Jeffrey Palm</a>
34  */

35
36 public class Options {
37
38     public Options() {
39         set("extra");
40     }
41
42     public final static String JavaDoc helpString =
43         "Usage: ajdb <options> <class> <arguments>\n" +
44         "\n" +
45         "where options include:\n" +
46         " -help print out this message and exit\n" +
47         " -sourcepath <directory>\n" +
48         " directory in which to look for source files\n" +
49 // " -attach <address>\n" +
50
// " attach to a running VM at the specified\n" +
51
// " address using standard connector\n" +
52
// " -listen <address>\n" +
53
// " wait for a running VM to connect at the\n" +
54
// " specified address using standard connector\n" +
55
// " -listenany\n" +
56
// " wait for a running VM to connect at any available\n" +
57
// " address using standard connector\n" +
58
// " -launch\n" +
59
// " launch VM immediately instead of waiting for 'run' command\n" +
60
// " -connect <connector-name>:<name1>=<value1>,...\n" +
61
// " connect to target VM using named connector with\n" +
62
// " listed argument values\n" +
63
// " -dbgtrace [flags] print info for debugging jdb\n" +
64
// " -thotspot run the application in the Hotspot(tm) Performance Engine\n" +
65
// " -tclassic run the application in the Classic VM\n" +
66
" -read <file> read the startup file\n" +
67         " -gui start up in GUI mode\n" +
68         " -extra enable extra features unique to ajdb\n" +
69         " -workingdir set the user's workingdir" +
70         "\n" +
71         "options forwarded to debuggee process:\n" +
72         " -v -verbose[:class|gc|jni]\n" +
73         " turn on verbose mode\n" +
74         " -D<name>=<value> set a system property\n" +
75         " -classpath <directories separated by \";\">\n" +
76         " list directories in which to look for classes\n" +
77         " -X<option> non-standard target VM option\n" +
78         "\n" +
79         "<class> is the name of the class to begin debugging\n" +
80         "<arguments> are the arguments passed to the main() method of <class>\n" +
81         "\n" +
82         "For command help type 'help' at jdb prompt\n" +
83         "";
84     
85     public final static String JavaDoc helpString() {
86         return helpString;
87     }
88     
89     private HashMap opts = new HashMap();
90
91     private String JavaDoc className = "";
92     public String JavaDoc getClassName() {
93         return className;
94     }
95
96     private String JavaDoc commandLine = "";
97     public String JavaDoc getCommandLine() {
98         return commandLine;
99     }
100
101     public String JavaDoc getFullCommandLine() {
102         String JavaDoc line = "";
103         if (!empty(className)) {
104             line += className;
105         }
106         if (!empty(commandLine)) {
107             line += " " + commandLine;
108         }
109         line = line.trim();
110         System.out.println("line="+ line);
111         return line;
112     }
113
114     private static boolean empty(String JavaDoc str) {
115         return str == null || "".equals(str.trim());
116     }
117
118     public final static String JavaDoc[] javaArgs = {
119         "classpath",
120         "v",
121         "verbose",
122         "verbose:class",
123         "verbose:gc",
124         "verbose:jni",
125         "X",
126         "D",
127     };
128
129     public void fill(String JavaDoc str) {
130         Vector v = new Vector();
131         StringTokenizer tok = new StringTokenizer(str);
132         String JavaDoc t;
133         while (tok.hasMoreTokens()) {
134             t = tok.nextToken();
135             if (t.startsWith("\"")) {
136                 String JavaDoc end;
137                 boolean done = t.endsWith("\"");
138                 while(!done && tok.hasMoreTokens()) {
139                     t += (end = tok.nextToken().trim());
140                     if (end.endsWith("\"")) {
141                         done = true;
142                     }
143                 }
144             }
145             v.add(t);
146         }
147         String JavaDoc[] args = new String JavaDoc[v.size()];
148         Iterator iter = v.iterator();
149         for (int i = 0; i < args.length; i++) {
150             args[i] = v.get(i) + "";
151         }
152         fill(args);
153         
154     }
155
156     public void fill(String JavaDoc[] args) {
157         realFill(removeNoWarn(args));
158         Debug.setOptions(this);
159     }
160
161     private String JavaDoc[] removeNoWarn(String JavaDoc[] args) {
162         for (int i = 0; i < args.length; i++) {
163             String JavaDoc opt = args[i];
164             if (opt(opt, "nowarn")) {
165                 String JavaDoc[] newArgs = new String JavaDoc[args.length - 1];
166                 System.arraycopy(args, 0, newArgs, 0, i);
167                 System.arraycopy(args, i+1, newArgs, i, args.length-i-1);
168                 set("nowarn");
169                 return newArgs;
170             }
171         }
172         return args;
173     }
174
175     private void realFill(String JavaDoc[] args) {
176         int i = 0;
177         String JavaDoc opt = "";
178         String JavaDoc arg = "";
179         while (i < args.length) {
180             opt = args[i++];
181             if (opt(opt, "extra")) {
182                 set("extra");
183             } else if (opt(opt, "now")) {
184                 set("now");
185             } else if (opt(opt, "noextra")) {
186                 unset("extra");
187             } else if (opt(opt, "nowarn")) {
188                 set("nowarn");
189             } else if (opt(opt, "help")) {
190                 help();
191             } else if (opt(opt, "sourcepath")) {
192                 check(args, i, "-sourcepath");
193                 set("sourcepath", args[i++]);
194             } else if (opt(opt, "attach")) {
195                 check(args, i, "-attach");
196                 set("attach", args[i++]);
197             } else if (opt(opt, "listen")) {
198                 check(args, i, "-listen");
199                 set("listen", args[i++]);
200             } else if (opt(opt, "listenany")) {
201                 set("listenany");
202             } else if (opt(opt, "launch")) {
203                 set("launch");
204             } else if (opt(opt, "connect")) {
205                 check(args, i, "-connect");
206                 set("connect", args[i++]);
207             } else if (opt(opt, "dbgtrace")) {
208                 if (isArg(args, i+1)) {
209                     set("dbgtrace", args[i++]);
210                 } else {
211                     set("dbgtrace");
212                 }
213             } else if (opt(opt, "thotspot")) {
214                 set("thotspot");
215             } else if (opt(opt, "tclassic")) {
216                 set("tclassic");
217             } else if (opt(opt, "v")) {
218                 set("v");
219             } else if (opt(opt, "verbose")) {
220                 set("verbose");
221             } else if (opt(opt, "verbose:class")) {
222                 set("verbose:class");
223             } else if (opt(opt, "verbose:gc")) {
224                 set("verbose:gc");
225             } else if (opt(opt, "verbose:jni")) {
226                 set("verbose:jni");
227             } else if (opt(opt, "demo")) {
228                 set("demo");
229                 Debugger.demoMode = true;
230             } else if (opt(opt, "classpath")) {
231                 check(args, i, "-classpath");
232                 set("classpath", args[i++]);
233             } else if (opt(opt, "workingdir")) {
234                 check(args, i, "-workingdir");
235                 set("workingdir", args[i++]);
236             } else if (opt(opt, "read")) {
237                 check(args, i, "-read");
238                 set("read", args[i++]);
239             } else if (opt(opt, "debug")) {
240                 set("debug");
241             } else if (opt.startsWith("-D")) {
242                 arg = strip("-D", opt, "<name>=<value>");
243                 if (arg.indexOf("\"") != -1) {
244                     while (i < args.length) {
245                         String JavaDoc next = args[i++];
246                         arg += " " + next;
247                         if (next.endsWith("\"")) {
248                             break;
249                         }
250                     }
251                 }
252                 set("D", arg);
253                 // fyi D, X resolve to setListOption(String, String)
254
} else if (opt.startsWith("-X")) {
255                 arg = strip("-X", opt, "<option>");
256                 set("X", arg);
257             } else if (isArg(opt)) {
258                 warn(opt + " is not a valid option");
259                 set(deopt(opt));
260             } else {
261                 className = opt;
262                 //commandLine = opt;
263
while (i < args.length) {
264                     commandLine += " " + args[i++];
265                 }
266             }
267         }
268     }
269
270     private boolean opt(String JavaDoc opt, String JavaDoc str) {
271         return opt.equals("-" + str) || opt.equals("--" + str);
272     }
273
274     public void setClassName(String JavaDoc className) {
275         this.className = className;
276     }
277
278     public void setMainClass(String JavaDoc mainClass) {
279         setClassName(mainClass);
280     }
281
282     public void setClassPath(String JavaDoc classPath) {
283         set("classpath", classPath);
284     }
285
286     public void setSourcePath(String JavaDoc sourcePath) {
287         set("sourcepath", sourcePath);
288     }
289
290     public String JavaDoc getJavaArgs() {
291         String JavaDoc args = "";
292         if (isSet("thotspot")) {
293             args += "-hotspot";
294         } else if (isSet("tclassic")) {
295             args += "-classic";
296         }
297         for (int i = 0; i < javaArgs.length; i++) {
298             if (isSet(javaArgs[i])) {
299                 args += " " + format(javaArgs[i]);
300             }
301         }
302         return args.trim();
303     }
304
305     /**
306      * warning: may not work with List-based options
307      */

308     public String JavaDoc deopt(String JavaDoc opt) {
309         for (int i = 0; i < opt.length(); i++) {
310             if (opt.charAt(i) != '-' && opt.length() >= i) {
311                 return opt.substring(i);
312             }
313         }
314         return opt;
315     }
316
317     public String JavaDoc opt(String JavaDoc opt) {
318         return "-" + opt;
319     }
320
321     /**
322      * Return a String with the formatted version of the option.
323      * The result may have spaces and may resolve to multiple
324      * options if opt (the option key) refers to a list.
325      * @param opt the String rendition of the option key to format
326      * @return a String with the formatted value for the option key.
327      */

328     public String JavaDoc format(String JavaDoc opt) {
329         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
330         if (isSet(opt)) {
331             if (isListOption(opt)) {
332                 List list = (List) get(opt);
333                 if (null == list) {
334                     warn(opt + " set but no list");
335                 } else {
336                     for (Iterator it = list.iterator(); it.hasNext();) {
337                         sb.append(" -" + opt + it.next());
338                     }
339                 }
340             } else {
341                 sb.append(opt(opt));
342                 String JavaDoc value = getOpt(opt);
343                 if ((null != value) && (0 < value.length())) {
344                     sb.append(" ");
345                     sb.append(value);
346                 }
347             }
348         }
349         return sb.toString();
350     }
351
352     public boolean isSet(String JavaDoc opt) {
353         return (get(opt) != null);
354     }
355
356     /**
357      * Set the value of an option.
358      * If the option is a list option, this resolves to
359      * <code>setListOption(opt, arg)</code>.
360      * If the option has been set and is not a list option,
361      * it will be overwritten (and a warning issued).
362      */

363     public void set(String JavaDoc opt, String JavaDoc arg) {
364         if (isListOption(opt)) {
365             setListOption(opt, arg);
366         } else {
367             if (hasWhitespace(arg)) {
368                 arg = quote(arg);
369             }
370             Object JavaDoc value = get(opt);
371             if ((value != null) && (!("debugging-for-jeff".equals(value)))) {
372                 warn("Resetting option '" + opt + "' to '" + arg + "'.");
373             }
374             opts.put(opt, arg);
375         }
376     }
377
378     /**
379      * @param opt the String (case-sensitive) referring to the option key
380      * (exclusive of any leading '-').
381      * @return true if opt (the option key) refers to an option
382      * whose values are represented in a List
383      */

384     public boolean isListOption(String JavaDoc opt) {
385         return ("D".equals(opt) || "X".equals(opt));
386     }
387
388     /**
389      * Set the value of an option represented as a List.
390      * (Does nothing but warn if opt key is not a list option.)
391      * @param opt the String key of the option
392      * @param arg the String value to set, taken as one instance for the List.
393      */

394     public void setListOption(String JavaDoc opt, String JavaDoc arg) {
395         if (!isListOption(opt)) {
396             String JavaDoc args = "(\"" + opt + "\", \"" + arg + "\")";
397             warn("Options.setListOption" + args + ": not a list option");
398             // todo: call set(opt, arg) instead of returning?
399
return;
400         }
401         if (hasWhitespace(arg)) {
402             arg = quote(arg);
403         }
404         Object JavaDoc value = opts.get(opt);
405         if (null == value) {
406             value = new ArrayList();
407             opts.put(opt, value);
408         }
409         List list = (List) value;
410         list.add(arg);
411     }
412
413     public void set(String JavaDoc opt) {
414         set(opt, "");
415     }
416
417     public void unset(String JavaDoc opt) {
418         if (isListOption(opt)) {
419             warn("Options.unset(\"" + opt + "\"): unsetting list");
420         }
421         set(opt, null);
422     }
423
424     public String JavaDoc quote(String JavaDoc arg) {
425         return "\"" + arg + "\"";
426     }
427
428     public boolean hasWhitespace(String JavaDoc str) {
429         for (int i = 0; i < str.length(); i++) {
430             if (Character.isWhitespace(str.charAt(i))) {
431                 return true;
432             }
433         }
434         return false;
435     }
436
437     public Object JavaDoc get(String JavaDoc opt) {
438         return opts.get(opt);
439     }
440
441     public String JavaDoc getOpt(String JavaDoc opt) {
442         return get(opt) + "";
443     }
444
445
446     public String JavaDoc toString() {
447         return opts + "";
448     }
449
450     String JavaDoc strip(String JavaDoc start, String JavaDoc opt, String JavaDoc syntax) {
451         int pos = start.length();
452         String JavaDoc str = "";
453         if (opt.length() < pos) {
454             fail("Correct syntax: " + opt + syntax);
455         } else {
456             str = opt.substring(pos);
457         }
458         return str;
459     }
460
461     boolean isArg(String JavaDoc arg) {
462         return (arg != null && arg.startsWith("-"));
463     }
464
465     boolean isArg(String JavaDoc[] args, int i) {
466         return (args != null && isArg(args[i]));
467     }
468
469     void check(String JavaDoc[] args, int i, String JavaDoc s) {
470         if (args == null ||
471             args.length <= i ||
472             args[i] == null ||
473             isArg(args[i])) {
474             fail("The option '" + s + "' needs an argument.");
475         }
476     }
477
478     void fail(Object JavaDoc o) {
479         outln(o);
480         exit(1);
481     }
482
483     void warn(Object JavaDoc o) {
484         if (false && !isSet("nowarn")) {
485             outln("Warning: " + o);
486         }
487     }
488
489     void exit(int i) {
490         System.exit(i);
491     }
492
493     void help() {
494         outln(helpString());
495     }
496
497     void outln(Object JavaDoc o) {
498         System.err.println(o);
499     }
500 }
501
Popular Tags