History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/tools/example/debug/bdi/Utils.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/tools/example/debug/bdi/Utils.java+181−0
@@ -0,0 +1,181 @@
1/*
2 * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.example.debug.bdi; //### does it belong here?
27
28import com.sun.jdi.*;
29
30public class Utils {
31
32 /**
33 * Return the thread status description.
34 */
35 public static String getStatus(ThreadReference thr) {
36 int status = thr.status();
37 String result;
38 switch (status) {
39 case ThreadReference.THREAD_STATUS_UNKNOWN:
40 result = "unknown status";
41 break;
42 case ThreadReference.THREAD_STATUS_ZOMBIE:
43 result = "zombie";
44 break;
45 case ThreadReference.THREAD_STATUS_RUNNING:
46 result = "running";
47 break;
48 case ThreadReference.THREAD_STATUS_SLEEPING:
49 result = "sleeping";
50 break;
51 case ThreadReference.THREAD_STATUS_MONITOR:
52 result = "waiting to acquire a monitor lock";
53 break;
54 case ThreadReference.THREAD_STATUS_WAIT:
55 result = "waiting on a condition";
56 break;
57 default:
58 result = "<invalid thread status>";
59 }
60 if (thr.isSuspended()) {
61 result += " (suspended)";
62 }
63 return result;
64 }
65
66 /**
67 * Return a description of an object.
68 */
69 public static String description(ObjectReference ref) {
70 ReferenceType clazz = ref.referenceType();
71 long id = ref.uniqueID(); //### TODO use real id
72 if (clazz == null) {
73 return toHex(id);
74 } else {
75 return "(" + clazz.name() + ")" + toHex(id);
76 }
77 }
78
79 /**
80 * Convert a long to a hexadecimal string.
81 */
82 public static String toHex(long n) {
83 char s1[] = new char[16];
84 char s2[] = new char[18];
85
86 // Store digits in reverse order.
87 int i = 0;
88 do {
89 long d = n & 0xf;
90 s1[i++] = (char)((d < 10) ? ('0' + d) : ('a' + d - 10));
91 } while ((n >>>= 4) > 0);
92
93 // Now reverse the array.
94 s2[0] = '0';
95 s2[1] = 'x';
96 int j = 2;
97 while (--i >= 0) {
98 s2[j++] = s1[i];
99 }
100 return new String(s2, 0, j);
101 }
102
103 /**
104 * Convert hexadecimal strings to longs.
105 */
106 public static long fromHex(String hexStr) {
107 String str = hexStr.startsWith("0x") ?
108 hexStr.substring(2).toLowerCase() : hexStr.toLowerCase();
109 if (hexStr.length() == 0) {
110 throw new NumberFormatException();
111 }
112
113 long ret = 0;
114 for (int i = 0; i < str.length(); i++) {
115 int c = str.charAt(i);
116 if (c >= '0' && c <= '9') {
117 ret = (ret * 16) + (c - '0');
118 } else if (c >= 'a' && c <= 'f') {
119 ret = (ret * 16) + (c - 'a' + 10);
120 } else {
121 throw new NumberFormatException();
122 }
123 }
124 return ret;
125 }
126
127
128 /*
129 * The next two methods are used by this class and by EventHandler
130 * to print consistent locations and error messages.
131 */
132 public static String locationString(Location loc) {
133 return loc.declaringType().name() +
134 "." + loc.method().name() + "(), line=" +
135 loc.lineNumber();
136 }
137
138//### UNUSED.
139/************************
140 private String typedName(Method method) {
141 // TO DO: Use method.signature() instead of method.arguments() so that
142 // we get sensible results for classes without debugging info
143 StringBuffer buf = new StringBuffer();
144 buf.append(method.name());
145 buf.append("(");
146 Iterator it = method.arguments().iterator();
147 while (it.hasNext()) {
148 buf.append(((LocalVariable)it.next()).typeName());
149 if (it.hasNext()) {
150 buf.append(",");
151 }
152 }
153 buf.append(")");
154 return buf.toString();
155 }
156************************/
157
158 public static boolean isValidMethodName(String s) {
159 return isJavaIdentifier(s) ||
160 s.equals("<init>") ||
161 s.equals("<clinit>");
162 }
163
164 public static boolean isJavaIdentifier(String s) {
165 if (s.length() == 0) {
166 return false;
167 }
168 int cp = s.codePointAt(0);
169 if (! Character.isJavaIdentifierStart(cp)) {
170 return false;
171 }
172 for (int i = Character.charCount(cp); i < s.length(); i += Character.charCount(cp)) {
173 cp = s.codePointAt(i);
174 if (! Character.isJavaIdentifierPart(cp)) {
175 return false;
176 }
177 }
178 return true;
179 }
180
181}