1 /*
   2  * Copyright (c) 1998, 2011, 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 
  26 /*
  27  * This source code is provided to illustrate the usage of a given feature
  28  * or technique and has been deliberately simplified. Additional steps
  29  * required for a production-quality application, such as security checks,
  30  * input validation and proper error handling, might not be present in
  31  * this sample code.
  32  */
  33 
  34 
  35 package com.sun.tools.example.debug.bdi;   //### does it belong here?
  36 
  37 import com.sun.jdi.*;
  38 
  39 public class Utils {
  40 
  41     /**
  42      * Return the thread status description.
  43      */
  44     public static String getStatus(ThreadReference thr) {
  45         int status = thr.status();
  46         String result;
  47         switch (status) {
  48           case ThreadReference.THREAD_STATUS_UNKNOWN:
  49             result = "unknown status";
  50             break;
  51           case ThreadReference.THREAD_STATUS_ZOMBIE:
  52             result = "zombie";
  53             break;
  54           case ThreadReference.THREAD_STATUS_RUNNING:
  55             result = "running";
  56             break;
  57           case ThreadReference.THREAD_STATUS_SLEEPING:
  58             result = "sleeping";
  59             break;
  60           case ThreadReference.THREAD_STATUS_MONITOR:
  61             result = "waiting to acquire a monitor lock";
  62             break;
  63           case ThreadReference.THREAD_STATUS_WAIT:
  64             result = "waiting on a condition";
  65             break;
  66           default:
  67             result = "<invalid thread status>";
  68         }
  69         if (thr.isSuspended()) {
  70             result += " (suspended)";
  71         }
  72         return result;
  73     }
  74 
  75     /**
  76      * Return a description of an object.
  77      */
  78     public static String description(ObjectReference ref) {
  79         ReferenceType clazz = ref.referenceType();
  80         long id = ref.uniqueID();  //### TODO use real id
  81         if (clazz == null) {
  82             return toHex(id);
  83         } else {
  84             return "(" + clazz.name() + ")" + toHex(id);
  85         }
  86     }
  87 
  88     /**
  89      * Convert a long to a hexadecimal string.
  90      */
  91     public static String toHex(long n) {
  92         char s1[] = new char[16];
  93         char s2[] = new char[18];
  94 
  95         // Store digits in reverse order.
  96         int i = 0;
  97         do {
  98             long d = n & 0xf;
  99             s1[i++] = (char)((d < 10) ? ('0' + d) : ('a' + d - 10));
 100         } while ((n >>>= 4) > 0);
 101 
 102         // Now reverse the array.
 103         s2[0] = '0';
 104         s2[1] = 'x';
 105         int j = 2;
 106         while (--i >= 0) {
 107             s2[j++] = s1[i];
 108         }
 109         return new String(s2, 0, j);
 110     }
 111 
 112     /**
 113      * Convert hexadecimal strings to longs.
 114      */
 115     public static long fromHex(String hexStr) {
 116         String str = hexStr.startsWith("0x") ?
 117             hexStr.substring(2).toLowerCase() : hexStr.toLowerCase();
 118         if (hexStr.length() == 0) {
 119             throw new NumberFormatException();
 120         }
 121 
 122         long ret = 0;
 123         for (int i = 0; i < str.length(); i++) {
 124             int c = str.charAt(i);
 125             if (c >= '0' && c <= '9') {
 126                 ret = (ret * 16) + (c - '0');
 127             } else if (c >= 'a' && c <= 'f') {
 128                 ret = (ret * 16) + (c - 'a' + 10);
 129             } else {
 130                 throw new NumberFormatException();
 131             }
 132         }
 133         return ret;
 134     }
 135 
 136 
 137     /*
 138      * The next two methods are used by this class and by EventHandler
 139      * to print consistent locations and error messages.
 140      */
 141     public static String locationString(Location loc) {
 142         return  loc.declaringType().name() +
 143             "." + loc.method().name() + "(), line=" +
 144             loc.lineNumber();
 145     }
 146 
 147 //### UNUSED.
 148 /************************
 149     private String typedName(Method method) {
 150         // TO DO: Use method.signature() instead of method.arguments() so that
 151         // we get sensible results for classes without debugging info
 152         StringBuffer buf = new StringBuffer();
 153         buf.append(method.name());
 154         buf.append("(");
 155         Iterator it = method.arguments().iterator();
 156         while (it.hasNext()) {
 157             buf.append(((LocalVariable)it.next()).typeName());
 158             if (it.hasNext()) {
 159                 buf.append(",");
 160             }
 161         }
 162         buf.append(")");
 163         return buf.toString();
 164     }
 165 ************************/
 166 
 167     public static boolean isValidMethodName(String s) {
 168         return isJavaIdentifier(s) ||
 169                s.equals("<init>") ||
 170                s.equals("<clinit>");
 171     }
 172 
 173     public static boolean isJavaIdentifier(String s) {
 174         if (s.length() == 0) {
 175             return false;
 176         }
 177         int cp = s.codePointAt(0);
 178         if (! Character.isJavaIdentifierStart(cp)) {
 179             return false;
 180         }
 181         for (int i = Character.charCount(cp); i < s.length(); i += Character.charCount(cp)) {
 182             cp = s.codePointAt(i);
 183             if (! Character.isJavaIdentifierPart(cp)) {
 184                 return false;
 185             }
 186         }
 187         return true;
 188     }
 189 
 190 }