jdk/src/share/classes/com/sun/tools/example/debug/tty/Env.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1998-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.example.debug.tty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jdi.request.StepRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jdi.request.MethodEntryRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jdi.request.MethodExitRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
class Env {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static EventRequestSpecList specList = new EventRequestSpecList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static VMConnection connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static SourceMapper sourceMapper = new SourceMapper("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static List<String> excludes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final int SOURCE_CACHE_SIZE = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static List<SourceCode> sourceCache = new LinkedList<SourceCode>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static HashMap<String, Value> savedValues = new HashMap<String, Value>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static Method atExitMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static void init(String connectSpec, boolean openNow, int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        connection = new VMConnection(connectSpec, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (!connection.isLaunch() || openNow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            connection.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static VMConnection connection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static VirtualMachine vm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return connection.vm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        shutdown(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static void shutdown(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (connection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                connection.disposeVM();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            } catch (VMDisconnectedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                // Shutting down after the VM has gone away. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // not an error, and we just ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (message != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            MessageOutput.lnprint(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            MessageOutput.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static void setSourcePath(String srcPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        sourceMapper = new SourceMapper(srcPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        sourceCache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    92
    static void setSourcePath(List<String> srcList) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        sourceMapper = new SourceMapper(srcList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        sourceCache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static String getSourcePath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return sourceMapper.getSourcePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static private List<String> excludes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (excludes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            setExcludes("java.*, javax.*, sun.*, com.sun.*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return excludes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static String excludesString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        StringBuffer buffer = new StringBuffer();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   110
        for (String pattern : excludes()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            buffer.append(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            buffer.append(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static void addExcludes(StepRequest request) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   118
        for (String pattern : excludes()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            request.addClassExclusionFilter(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static void addExcludes(MethodEntryRequest request) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   124
        for (String pattern : excludes()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            request.addClassExclusionFilter(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static void addExcludes(MethodExitRequest request) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   130
        for (String pattern : excludes()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            request.addClassExclusionFilter(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    static void setExcludes(String excludeString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        StringTokenizer t = new StringTokenizer(excludeString, " ,;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        List<String> list = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        while (t.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            list.add(t.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        excludes = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static Method atExitMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return atExitMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static void setAtExitMethod(Method mmm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        atExitMethod = mmm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Return a Reader cooresponding to the source of this location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Return null if not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Note: returned reader must be closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    static BufferedReader sourceReader(Location location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return sourceMapper.sourceReader(location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    static synchronized String sourceLine(Location location, int lineNumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                          throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (lineNumber == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            String fileName = location.sourceName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   170
            Iterator<SourceCode> iter = sourceCache.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            SourceCode code = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            while (iter.hasNext()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   173
                SourceCode candidate = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (candidate.fileName().equals(fileName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    code = candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (code == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                BufferedReader reader = sourceReader(location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                if (reader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    throw new FileNotFoundException(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                code = new SourceCode(fileName, reader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                if (sourceCache.size() == SOURCE_CACHE_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    sourceCache.remove(sourceCache.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            sourceCache.add(0, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return code.sourceLine(lineNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } catch (AbsentInformationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /** Return a description of an object. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    static String description(ObjectReference ref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        ReferenceType clazz = ref.referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        long id = ref.uniqueID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (clazz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return toHex(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return MessageOutput.format("object description and hex id",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                        new Object [] {clazz.name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                                       toHex(id)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /** Convert a long to a hexadecimal string. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    static String toHex(long n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        char s1[] = new char[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        char s2[] = new char[18];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        /* Store digits in reverse order. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            long d = n & 0xf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            s1[i++] = (char)((d < 10) ? ('0' + d) : ('a' + d - 10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } while ((n >>>= 4) > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        /* Now reverse the array. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        s2[0] = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        s2[1] = 'x';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int j = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        while (--i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            s2[j++] = s1[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return new String(s2, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /** Convert hexadecimal strings to longs. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    static long fromHex(String hexStr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        String str = hexStr.startsWith("0x") ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            hexStr.substring(2).toLowerCase() : hexStr.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (hexStr.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throw new NumberFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        long ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        for (int i = 0; i < str.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            int c = str.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (c >= '0' && c <= '9') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                ret = (ret * 16) + (c - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            } else if (c >= 'a' && c <= 'f') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                ret = (ret * 16) + (c - 'a' + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                throw new NumberFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    static ReferenceType getReferenceTypeFromToken(String idToken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        ReferenceType cls = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (Character.isDigit(idToken.charAt(0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            cls = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        } else if (idToken.startsWith("*.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // This notation saves typing by letting the user omit leading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // package names. The first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // loaded class whose name matches this limited regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // expression is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        idToken = idToken.substring(1);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   264
        for (ReferenceType type : Env.vm().allClasses()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (type.name().endsWith(idToken)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                cls = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // It's a class name
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   272
            List<ReferenceType> classes = Env.vm().classesByName(idToken);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (classes.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                // TO DO: handle multiples
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   275
                cls = classes.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   281
    static Set<String> getSaveKeys() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return savedValues.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    static Value getSavedValue(String key) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   286
        return savedValues.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    static void setSavedValue(String key, Value value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        savedValues.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    static class SourceCode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        private String fileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        private List<String> sourceLines = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        SourceCode(String fileName, BufferedReader reader)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            this.fileName = fileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                String line = reader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                while (line != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    sourceLines.add(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    line = reader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                reader.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        String fileName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return fileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        String sourceLine(int number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            int index = number - 1; // list is 0-indexed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (index >= sourceLines.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   319
                return sourceLines.get(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
}