1 7 34 35 package com.sun.tools.example.debug.bdi; 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 47 public static String getStatus(ThreadReference thr) { 48 int status = thr.status(); 49 String 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 81 public static String description(ObjectReference ref) { 82 ReferenceType clazz = ref.referenceType(); 83 long id = ref.uniqueID(); if (clazz == null) { 85 return toHex(id); 86 } else { 87 return "(" + clazz.name() + ")" + toHex(id); 88 } 89 } 90 91 94 public static String toHex(long n) { 95 char s1[] = new char[16]; 96 char s2[] = new char[18]; 97 98 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 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 (s2, 0, j); 113 } 114 115 118 public static long fromHex(String hexStr) { 119 String str = hexStr.startsWith("0x") ? 120 hexStr.substring(2).toLowerCase() : hexStr.toLowerCase(); 121 if (hexStr.length() == 0) { 122 throw new NumberFormatException (); 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 (); 134 } 135 } 136 return ret; 137 } 138 139 140 144 public static String locationString(Location loc) { 145 return loc.declaringType().name() + 146 "." + loc.method().name() + "(), line=" + 147 loc.lineNumber(); 148 } 149 150 169 170 public static boolean isValidMethodName(String s) { 171 return isJavaIdentifier(s) || 172 s.equals("<init>") || 173 s.equals("<clinit>"); 174 } 175 176 public static boolean isJavaIdentifier(String 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 |