1 19 20 package org.netbeans.modules.classfile; 21 22 import java.io.ObjectStreamException ; 23 import java.io.Serializable ; 24 import java.lang.ref.WeakReference ; 25 import java.util.Comparator ; 26 import java.util.WeakHashMap ; 27 28 40 public final class ClassName implements Comparable , Comparator , Serializable { 41 42 static final long serialVersionUID = -8444469778945723553L; 43 44 private final String type; 45 private final transient String internalName; 46 private volatile transient String externalName; 47 private volatile transient String packageName; 48 private volatile transient String simpleName; 49 50 private final static WeakHashMap <String ,WeakReference <ClassName>> cache = 51 new WeakHashMap <String ,WeakReference <ClassName>>(); 52 53 89 public static ClassName getClassName(String classType) { 90 if (classType == null || classType.length() == 0) 93 return null; 94 95 ClassName cn = getCacheEntry(classType); 96 if (cn == null) 97 synchronized (cache) { 98 cn = getCacheEntry(classType); 99 if (cn == null) { 100 int i = classType.indexOf('L'); 102 String _type; 103 char lastChar = classType.charAt(classType.length()-1); 104 if (i != -1 && lastChar == ';') { 105 _type = classType.substring(i+1, classType.length()-1); 107 if (i > 0) 108 _type = classType.substring(0, i) + _type; 110 cn = getCacheEntry(_type); 111 if (cn != null) 112 return cn; 113 } else { 114 _type = classType; 115 } 116 117 cn = new ClassName(_type); 118 cache.put(_type, new WeakReference <ClassName>(cn)); 119 } 120 } 121 return cn; 122 } 123 124 private static ClassName getCacheEntry(String key) { 125 WeakReference ref = cache.get(key); 126 return ref != null ? (ClassName)ref.get() : null; 127 } 128 129 133 private ClassName(String type) { 134 this.type = type; 135 136 int i = type.lastIndexOf('['); 138 internalName = (i > -1) ? type.substring(i+1) : type; 139 } 140 141 146 public String getType() { 147 return type; 148 } 149 150 159 public String getInternalName() { 160 return internalName; 161 } 162 163 173 public String getExternalName() { 174 return getExternalName(false); 175 } 176 177 187 public String getExternalName(boolean suppressArrays) { 188 initExternalName(); 189 int i; 190 if (suppressArrays && (i = externalName.indexOf('[')) != -1) 191 return externalName.substring(0, i); 192 return externalName; 193 } 194 195 private synchronized void initExternalName() { 196 if (externalName == null) 197 externalName = externalizeClassName(); 198 } 199 200 203 public String getPackage() { 204 if (packageName == null) 205 initPackage(); 206 return packageName; 207 } 208 209 private synchronized void initPackage() { 210 int i = internalName.lastIndexOf('/'); 211 packageName = (i != -1) ? 212 internalName.substring(0, i).replace('/', '.') : ""; 213 } 214 215 218 public String getSimpleName() { 219 if (simpleName == null) 220 initSimpleName(); 221 return simpleName; 222 } 223 224 private synchronized void initSimpleName() { 225 String pkg = getPackage(); 226 int i = pkg.length(); 227 String extName = getExternalName(); 228 if (i == 0) 229 simpleName = extName; else 231 simpleName = extName.substring(i + 1); 232 } 233 234 public boolean equals(Object obj) { 235 if (this == obj) 236 return true; 237 return (obj instanceof ClassName) ? 238 (type.equals(((ClassName)obj).type)) : false; 239 } 240 241 257 public int compareTo(Object obj) { 258 return type.compareTo(((ClassName)obj).type); 261 } 262 263 276 public int compare(Object o1, Object o2) { 277 return ((ClassName)o1).compareTo(o2); 278 } 279 280 public int hashCode() { 281 return type.hashCode(); 282 } 283 284 public String toString() { 285 return getExternalName(); 286 } 287 288 private String externalizeClassName() { 290 StringBuffer sb = new StringBuffer (type); 291 int arrays = 0; 292 boolean atBeginning = true; 293 int length = sb.length(); 294 for (int i = 0; i < length; i++) { 295 char ch = sb.charAt(i); 296 switch (ch) { 297 case '[': 298 if (atBeginning) 299 arrays++; 300 break; 301 302 case '/': 303 case '$': 304 sb.setCharAt(i, '.'); 305 atBeginning = false; 306 break; 307 308 default: 309 atBeginning = false; 310 } 311 } 312 313 if (arrays > 0) { 314 sb.delete(0, arrays); 315 for (int i = 0; i < arrays; i++) 316 sb.append("[]"); 317 } 318 319 return sb.toString(); 320 } 321 322 325 static void clearCache() { 326 cache.clear(); 327 } 328 329 334 private Object readResolve() throws ObjectStreamException { 335 return getClassName(internalName); 336 } 337 } 338 | Popular Tags |