1 22 23 24 package com.mchange.v1.jvm; 25 26 public final class InternalNameUtils 27 { 28 public static String dottifySlashesAndDollarSigns(String str) 29 { return _dottifySlashesAndDollarSigns( str ).toString(); } 30 31 public static String decodeType(String internalrep) throws TypeFormatException 32 { return _decodeType(internalrep).toString(); } 33 34 public static String decodeTypeList(String internalrep) throws TypeFormatException 35 { 36 StringBuffer sb = new StringBuffer (64); 37 _decodeTypeList(internalrep, 0, sb); 38 return sb.toString(); 39 } 40 41 public static boolean isPrimitive(char rep) 42 { 43 return (rep == 'Z' || 44 rep == 'B' || 45 rep == 'C' || 46 rep == 'S' || 47 rep == 'I' || 48 rep == 'J' || 49 rep == 'F' || 50 rep == 'D' || 51 rep == 'V'); 52 } 53 54 private static void _decodeTypeList(String typeList, int start_pos, StringBuffer appendTo) throws TypeFormatException 55 { 56 if (appendTo.length() != 0) 57 appendTo.append(' '); 58 59 char c = typeList.charAt(start_pos); 60 if (isPrimitive(c)) 61 { 62 appendTo.append( _decodeType( typeList.substring(start_pos, start_pos + 1) ) ); 63 ++start_pos; 64 } 65 else 66 { 67 int stop_index; 68 69 if (c == '[') 70 { 71 int finger = start_pos + 1; 72 while (typeList.charAt(finger) == '[') 73 ++finger; 74 if (typeList.charAt(finger) == 'L') 75 { 76 ++finger; 77 while (typeList.charAt( finger ) != ';') 78 ++finger; 79 } 80 stop_index = finger; 81 } 82 else 83 { 84 stop_index = typeList.indexOf(';', start_pos); 85 if (stop_index < 0) 86 throw new TypeFormatException(typeList.substring(start_pos) + " is neither a primitive nor semicolon terminated!"); 87 } 88 89 appendTo.append( _decodeType( typeList.substring( start_pos, (start_pos = stop_index + 1) ) ) ); 90 } 91 if (start_pos < typeList.length()) 92 { 93 appendTo.append(','); 94 _decodeTypeList(typeList, start_pos, appendTo); 95 } 96 } 97 98 private static StringBuffer _decodeType(String type) throws TypeFormatException 99 { 100 102 int array_level = 0; 103 StringBuffer out; 104 105 char c = type.charAt(0); 106 107 switch (c) 108 { 109 case 'Z': 110 out = new StringBuffer ("boolean"); 111 break; 112 case 'B': 113 out = new StringBuffer ("byte"); 114 break; 115 case 'C': 116 out = new StringBuffer ("char"); 117 break; 118 case 'S': 119 out = new StringBuffer ("short"); 120 break; 121 case 'I': 122 out = new StringBuffer ("int"); 123 break; 124 case 'J': 125 out = new StringBuffer ("long"); 126 break; 127 case 'F': 128 out = new StringBuffer ("float"); 129 break; 130 case 'D': 131 out = new StringBuffer ("double"); 132 break; 133 case 'V': 134 out = new StringBuffer ("void"); 135 break; 136 case '[': 137 ++array_level; 138 out = _decodeType(type.substring(1)); 139 break; 140 case 'L': 141 out = _decodeSimpleClassType(type); 142 break; 143 default: 144 throw new TypeFormatException(type + " is not a valid inernal type name."); 145 } 146 for (int i = 0; i < array_level; ++i) 147 out.append("[]"); 148 return out; 149 } 150 151 private static StringBuffer _decodeSimpleClassType(String type) throws TypeFormatException 152 { 153 int len = type.length(); 154 if (type.charAt(0) != 'L' || type.charAt( len - 1 ) != ';') 155 throw new TypeFormatException(type + " is not a valid representation of a simple class type."); 156 157 return _dottifySlashesAndDollarSigns( type.substring(1, len - 1) ); 158 } 159 160 private static StringBuffer _dottifySlashesAndDollarSigns(String s) 161 { 162 StringBuffer sb = new StringBuffer ( s ); 163 for (int i = 0, len = sb.length(); i < len; ++i) 164 { 165 char c = sb.charAt(i); 166 if ( c == '/' || c == '$') 167 sb.setCharAt(i, '.'); 168 } 169 return sb; 170 } 171 172 private InternalNameUtils() 173 {} 174 175 public static void main(String [] argv) 176 { 177 try 178 { 179 System.out.println(decodeTypeList(argv[0])); 181 } 182 catch (Exception e) 183 { e.printStackTrace(); } 184 } 185 186 190 199 } 200 201 202 203 204 205 206 207 208 209 210 211 212 | Popular Tags |