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/tty/Env.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/tools/example/debug/tty/Env.java+322−0
@@ -0,0 +1,322 @@
1/*
2 * Copyright (c) 1998, 2008, 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.tty;
27
28import com.sun.jdi.*;
29import com.sun.jdi.request.StepRequest;
30import com.sun.jdi.request.MethodEntryRequest;
31import com.sun.jdi.request.MethodExitRequest;
32import java.util.*;
33import java.io.*;
34
35
36class Env {
37
38 static EventRequestSpecList specList = new EventRequestSpecList();
39
40 private static VMConnection connection;
41
42 private static SourceMapper sourceMapper = new SourceMapper("");
43 private static List<String> excludes;
44
45 private static final int SOURCE_CACHE_SIZE = 5;
46 private static List<SourceCode> sourceCache = new LinkedList<SourceCode>();
47
48 private static HashMap<String, Value> savedValues = new HashMap<String, Value>();
49 private static Method atExitMethod;
50
51 static void init(String connectSpec, boolean openNow, int flags) {
52 connection = new VMConnection(connectSpec, flags);
53 if (!connection.isLaunch() || openNow) {
54 connection.open();
55 }
56 }
57
58 static VMConnection connection() {
59 return connection;
60 }
61
62 static VirtualMachine vm() {
63 return connection.vm();
64 }
65
66 static void shutdown() {
67 shutdown(null);
68 }
69
70 static void shutdown(String message) {
71 if (connection != null) {
72 try {
73 connection.disposeVM();
74 } catch (VMDisconnectedException e) {
75 // Shutting down after the VM has gone away. This is
76 // not an error, and we just ignore it.
77 }
78 }
79 if (message != null) {
80 MessageOutput.lnprint(message);
81 MessageOutput.println();
82 }
83 System.exit(0);
84 }
85
86 static void setSourcePath(String srcPath) {
87 sourceMapper = new SourceMapper(srcPath);
88 sourceCache.clear();
89 }
90
91 static void setSourcePath(List<String> srcList) {
92 sourceMapper = new SourceMapper(srcList);
93 sourceCache.clear();
94 }
95
96 static String getSourcePath() {
97 return sourceMapper.getSourcePath();
98 }
99
100 static private List<String> excludes() {
101 if (excludes == null) {
102 setExcludes("java.*, javax.*, sun.*, com.sun.*");
103 }
104 return excludes;
105 }
106
107 static String excludesString() {
108 StringBuffer buffer = new StringBuffer();
109 for (String pattern : excludes()) {
110 buffer.append(pattern);
111 buffer.append(",");
112 }
113 return buffer.toString();
114 }
115
116 static void addExcludes(StepRequest request) {
117 for (String pattern : excludes()) {
118 request.addClassExclusionFilter(pattern);
119 }
120 }
121
122 static void addExcludes(MethodEntryRequest request) {
123 for (String pattern : excludes()) {
124 request.addClassExclusionFilter(pattern);
125 }
126 }
127
128 static void addExcludes(MethodExitRequest request) {
129 for (String pattern : excludes()) {
130 request.addClassExclusionFilter(pattern);
131 }
132 }
133
134 static void setExcludes(String excludeString) {
135 StringTokenizer t = new StringTokenizer(excludeString, " ,;");
136 List<String> list = new ArrayList<String>();
137 while (t.hasMoreTokens()) {
138 list.add(t.nextToken());
139 }
140 excludes = list;
141 }
142
143 static Method atExitMethod() {
144 return atExitMethod;
145 }
146
147 static void setAtExitMethod(Method mmm) {
148 atExitMethod = mmm;
149 }
150
151 /**
152 * Return a Reader cooresponding to the source of this location.
153 * Return null if not available.
154 * Note: returned reader must be closed.
155 */
156 static BufferedReader sourceReader(Location location) {
157 return sourceMapper.sourceReader(location);
158 }
159
160 static synchronized String sourceLine(Location location, int lineNumber)
161 throws IOException {
162 if (lineNumber == -1) {
163 throw new IllegalArgumentException();
164 }
165
166 try {
167 String fileName = location.sourceName();
168
169 Iterator<SourceCode> iter = sourceCache.iterator();
170 SourceCode code = null;
171 while (iter.hasNext()) {
172 SourceCode candidate = iter.next();
173 if (candidate.fileName().equals(fileName)) {
174 code = candidate;
175 iter.remove();
176 break;
177 }
178 }
179 if (code == null) {
180 BufferedReader reader = sourceReader(location);
181 if (reader == null) {
182 throw new FileNotFoundException(fileName);
183 }
184 code = new SourceCode(fileName, reader);
185 if (sourceCache.size() == SOURCE_CACHE_SIZE) {
186 sourceCache.remove(sourceCache.size() - 1);
187 }
188 }
189 sourceCache.add(0, code);
190 return code.sourceLine(lineNumber);
191 } catch (AbsentInformationException e) {
192 throw new IllegalArgumentException();
193 }
194 }
195
196 /** Return a description of an object. */
197 static String description(ObjectReference ref) {
198 ReferenceType clazz = ref.referenceType();
199 long id = ref.uniqueID();
200 if (clazz == null) {
201 return toHex(id);
202 } else {
203 return MessageOutput.format("object description and hex id",
204 new Object [] {clazz.name(),
205 toHex(id)});
206 }
207 }
208
209 /** Convert a long to a hexadecimal string. */
210 static String toHex(long n) {
211 char s1[] = new char[16];
212 char s2[] = new char[18];
213
214 /* Store digits in reverse order. */
215 int i = 0;
216 do {
217 long d = n & 0xf;
218 s1[i++] = (char)((d < 10) ? ('0' + d) : ('a' + d - 10));
219 } while ((n >>>= 4) > 0);
220
221 /* Now reverse the array. */
222 s2[0] = '0';
223 s2[1] = 'x';
224 int j = 2;
225 while (--i >= 0) {
226 s2[j++] = s1[i];
227 }
228 return new String(s2, 0, j);
229 }
230
231 /** Convert hexadecimal strings to longs. */
232 static long fromHex(String hexStr) {
233 String str = hexStr.startsWith("0x") ?
234 hexStr.substring(2).toLowerCase() : hexStr.toLowerCase();
235 if (hexStr.length() == 0) {
236 throw new NumberFormatException();
237 }
238
239 long ret = 0;
240 for (int i = 0; i < str.length(); i++) {
241 int c = str.charAt(i);
242 if (c >= '0' && c <= '9') {
243 ret = (ret * 16) + (c - '0');
244 } else if (c >= 'a' && c <= 'f') {
245 ret = (ret * 16) + (c - 'a' + 10);
246 } else {
247 throw new NumberFormatException();
248 }
249 }
250 return ret;
251 }
252
253 static ReferenceType getReferenceTypeFromToken(String idToken) {
254 ReferenceType cls = null;
255 if (Character.isDigit(idToken.charAt(0))) {
256 cls = null;
257 } else if (idToken.startsWith("*.")) {
258 // This notation saves typing by letting the user omit leading
259 // package names. The first
260 // loaded class whose name matches this limited regular
261 // expression is selected.
262 idToken = idToken.substring(1);
263 for (ReferenceType type : Env.vm().allClasses()) {
264 if (type.name().endsWith(idToken)) {
265 cls = type;
266 break;
267 }
268 }
269 } else {
270 // It's a class name
271 List<ReferenceType> classes = Env.vm().classesByName(idToken);
272 if (classes.size() > 0) {
273 // TO DO: handle multiples
274 cls = classes.get(0);
275 }
276 }
277 return cls;
278 }
279
280 static Set<String> getSaveKeys() {
281 return savedValues.keySet();
282 }
283
284 static Value getSavedValue(String key) {
285 return savedValues.get(key);
286 }
287
288 static void setSavedValue(String key, Value value) {
289 savedValues.put(key, value);
290 }
291
292 static class SourceCode {
293 private String fileName;
294 private List<String> sourceLines = new ArrayList<String>();
295
296 SourceCode(String fileName, BufferedReader reader) throws IOException {
297 this.fileName = fileName;
298 try {
299 String line = reader.readLine();
300 while (line != null) {
301 sourceLines.add(line);
302 line = reader.readLine();
303 }
304 } finally {
305 reader.close();
306 }
307 }
308
309 String fileName() {
310 return fileName;
311 }
312
313 String sourceLine(int number) {
314 int index = number - 1; // list is 0-indexed
315 if (index >= sourceLines.size()) {
316 return null;
317 } else {
318 return sourceLines.get(index);
319 }
320 }
321 }
322}