KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > bdi > Utils


1 /*
2  * @(#)Utils.java 1.11 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34
35 package com.sun.tools.example.debug.bdi; //### does it belong here?
36

37 import com.sun.jdi.*;
38 import com.sun.tools.jdi.*;
39 import java.util.*;
40 import java.io.*;
41
42 public class Utils {
43
44     /**
45      * Return the thread status description.
46      */

47     public static String JavaDoc getStatus(ThreadReference thr) {
48     int status = thr.status();
49     String JavaDoc result;
50     switch (status) {
51       case ThreadReference.THREAD_STATUS_UNKNOWN:
52         result = "unknown status";
53         break;
54       case ThreadReference.THREAD_STATUS_ZOMBIE:
55         result = "zombie";
56         break;
57       case ThreadReference.THREAD_STATUS_RUNNING:
58         result = "running";
59         break;
60       case ThreadReference.THREAD_STATUS_SLEEPING:
61         result = "sleeping";
62         break;
63       case ThreadReference.THREAD_STATUS_MONITOR:
64         result = "waiting to acquire a monitor lock";
65         break;
66       case ThreadReference.THREAD_STATUS_WAIT:
67         result = "waiting on a condition";
68         break;
69       default:
70         result = "<invalid thread status>";
71     }
72     if (thr.isSuspended()) {
73         result += " (suspended)";
74     }
75     return result;
76     }
77
78     /**
79      * Return a description of an object.
80      */

81     public static String JavaDoc description(ObjectReference ref) {
82         ReferenceType clazz = ref.referenceType();
83         long id = ref.uniqueID(); //### TODO use real id
84
if (clazz == null) {
85             return toHex(id);
86         } else {
87             return "(" + clazz.name() + ")" + toHex(id);
88         }
89     }
90
91     /**
92      * Convert a long to a hexadecimal string.
93      */

94     public static String JavaDoc toHex(long n) {
95     char s1[] = new char[16];
96     char s2[] = new char[18];
97
98     // Store digits in reverse order.
99
int i = 0;
100     do {
101         long d = n & 0xf;
102         s1[i++] = (char)((d < 10) ? ('0' + d) : ('a' + d - 10));
103     } while ((n >>>= 4) > 0);
104
105     // Now reverse the array.
106
s2[0] = '0';
107     s2[1] = 'x';
108     int j = 2;
109     while (--i >= 0) {
110         s2[j++] = s1[i];
111     }
112     return new String JavaDoc(s2, 0, j);
113     }
114
115     /**
116      * Convert hexadecimal strings to longs.
117      */

118     public static long fromHex(String JavaDoc hexStr) {
119     String JavaDoc str = hexStr.startsWith("0x") ?
120         hexStr.substring(2).toLowerCase() : hexStr.toLowerCase();
121     if (hexStr.length() == 0) {
122         throw new NumberFormatException JavaDoc();
123     }
124     
125     long ret = 0;
126     for (int i = 0; i < str.length(); i++) {
127         int c = str.charAt(i);
128         if (c >= '0' && c <= '9') {
129         ret = (ret * 16) + (c - '0');
130         } else if (c >= 'a' && c <= 'f') {
131         ret = (ret * 16) + (c - 'a' + 10);
132         } else {
133         throw new NumberFormatException JavaDoc();
134         }
135     }
136     return ret;
137     }
138
139
140     /*
141      * The next two methods are used by this class and by EventHandler
142      * to print consistent locations and error messages.
143      */

144     public static String JavaDoc locationString(Location loc) {
145     return loc.declaringType().name() +
146         "." + loc.method().name() + "(), line=" +
147         loc.lineNumber();
148     }
149
150 //### UNUSED.
151
/************************
152     private String typedName(Method method) {
153         // TO DO: Use method.signature() instead of method.arguments() so that
154     // we get sensible results for classes without debugging info
155         StringBuffer buf = new StringBuffer();
156         buf.append(method.name());
157         buf.append("(");
158         Iterator it = method.arguments().iterator();
159         while (it.hasNext()) {
160             buf.append(((LocalVariable)it.next()).typeName());
161             if (it.hasNext()) {
162                 buf.append(",");
163             }
164         }
165         buf.append(")");
166         return buf.toString();
167     }
168 ************************/

169
170     public static boolean isValidMethodName(String JavaDoc s) {
171         return isJavaIdentifier(s) ||
172                s.equals("<init>") ||
173                s.equals("<clinit>");
174     }
175
176     public static boolean isJavaIdentifier(String JavaDoc s) {
177         if (s.length() == 0) {
178             return false;
179         }
180         int cp = s.codePointAt(0);
181         if (! Character.isJavaIdentifierStart(cp)) {
182             return false;
183         }
184         for (int i = Character.charCount(cp); i < s.length(); i += Character.charCount(cp)) {
185             cp = s.codePointAt(i);
186             if (! Character.isJavaIdentifierPart(cp)) {
187                 return false;
188             }
189         }
190         return true;
191     }
192
193 }
194
195
Popular Tags