Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.osgi; |
| 2 | |
| 3 | import java.util.*; |
| 4 | |
| 5 | import aQute.libg.generics.*; |
| 6 | |
| 7 | public class Descriptors { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 8 | Map<String,TypeRef> typeRefCache = Create.map(); |
| 9 | Map<String,Descriptor> descriptorCache = Create.map(); |
| 10 | Map<String,PackageRef> packageCache = Create.map(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | |
| 12 | // MUST BE BEFORE PRIMITIVES, THEY USE THE DEFAULT PACKAGE!! |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 13 | final static PackageRef DEFAULT_PACKAGE = new PackageRef(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | final static PackageRef PRIMITIVE_PACKAGE = new PackageRef(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 15 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 16 | final static TypeRef VOID = new ConcreteRef("V", "void", PRIMITIVE_PACKAGE); |
| 17 | final static TypeRef BOOLEAN = new ConcreteRef("Z", "boolean", PRIMITIVE_PACKAGE); |
| 18 | final static TypeRef BYTE = new ConcreteRef("B", "byte", PRIMITIVE_PACKAGE); |
| 19 | final static TypeRef CHAR = new ConcreteRef("C", "char", PRIMITIVE_PACKAGE); |
| 20 | final static TypeRef SHORT = new ConcreteRef("S", "short", PRIMITIVE_PACKAGE); |
| 21 | final static TypeRef INTEGER = new ConcreteRef("I", "int", PRIMITIVE_PACKAGE); |
| 22 | final static TypeRef LONG = new ConcreteRef("J", "long", PRIMITIVE_PACKAGE); |
| 23 | final static TypeRef DOUBLE = new ConcreteRef("D", "double", PRIMITIVE_PACKAGE); |
| 24 | final static TypeRef FLOAT = new ConcreteRef("F", "float", PRIMITIVE_PACKAGE); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 25 | |
| 26 | { |
| 27 | packageCache.put("", DEFAULT_PACKAGE); |
| 28 | } |
| 29 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 30 | public interface TypeRef extends Comparable<TypeRef> { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 31 | String getBinary(); |
| 32 | |
| 33 | String getFQN(); |
| 34 | |
| 35 | String getPath(); |
| 36 | |
| 37 | boolean isPrimitive(); |
| 38 | |
| 39 | TypeRef getComponentTypeRef(); |
| 40 | |
| 41 | TypeRef getClassRef(); |
| 42 | |
| 43 | PackageRef getPackageRef(); |
| 44 | |
| 45 | String getShortName(); |
| 46 | |
| 47 | boolean isJava(); |
| 48 | |
| 49 | boolean isObject(); |
| 50 | |
| 51 | String getSourcePath(); |
| 52 | |
| 53 | String getDottedOnly(); |
| 54 | |
| 55 | } |
| 56 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 57 | public static class PackageRef implements Comparable<PackageRef> { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 58 | final String binaryName; |
| 59 | final String fqn; |
| 60 | final boolean java; |
| 61 | |
| 62 | private PackageRef(String binaryName) { |
| 63 | this.binaryName = fqnToBinary(binaryName); |
| 64 | this.fqn = binaryToFQN(binaryName); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 65 | this.java = this.fqn.startsWith("java."); // && |
| 66 | // !this.fqn.equals("java.sql)" |
| 67 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 68 | // For some reason I excluded java.sql but the classloader will |
| 69 | // delegate anyway. So lost the understanding why I did it?? |
| 70 | } |
| 71 | |
| 72 | private PackageRef() { |
| 73 | this.binaryName = ""; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 74 | this.fqn = "."; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 75 | this.java = false; |
| 76 | } |
| 77 | |
| 78 | public PackageRef getDuplicate() { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 79 | return new PackageRef(binaryName + Constants.DUPLICATE_MARKER); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 80 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 81 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 82 | public String getFQN() { |
| 83 | return fqn; |
| 84 | } |
| 85 | |
| 86 | public String getBinary() { |
| 87 | return binaryName; |
| 88 | } |
| 89 | |
| 90 | public String getPath() { |
| 91 | return binaryName; |
| 92 | } |
| 93 | |
| 94 | public boolean isJava() { |
| 95 | return java; |
| 96 | } |
| 97 | |
| 98 | public String toString() { |
| 99 | return fqn; |
| 100 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 101 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 102 | boolean isDefaultPackage() { |
| 103 | return this.fqn.equals("."); |
| 104 | } |
| 105 | |
| 106 | boolean isPrimitivePackage() { |
| 107 | return this == PRIMITIVE_PACKAGE; |
| 108 | } |
| 109 | |
| 110 | public int compareTo(PackageRef other) { |
| 111 | return fqn.compareTo(other.fqn); |
| 112 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 113 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 114 | public boolean equals(Object o) { |
| 115 | assert o instanceof PackageRef; |
| 116 | return o == this; |
| 117 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 118 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 119 | public int hashCode() { |
| 120 | return super.hashCode(); |
| 121 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 122 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 123 | /** |
| 124 | * Decide if the package is a metadata package. |
| 125 | * |
| 126 | * @param pack |
| 127 | * @return |
| 128 | */ |
| 129 | public boolean isMetaData() { |
| 130 | if (isDefaultPackage()) |
| 131 | return true; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 132 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 133 | for (int i = 0; i < Constants.METAPACKAGES.length; i++) { |
| 134 | if (fqn.startsWith(Constants.METAPACKAGES[i])) |
| 135 | return true; |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | |
| 142 | // We "intern" the |
| 143 | private static class ConcreteRef implements TypeRef { |
| 144 | final String binaryName; |
| 145 | final String fqn; |
| 146 | final boolean primitive; |
| 147 | final PackageRef packageRef; |
| 148 | |
| 149 | ConcreteRef(PackageRef packageRef, String binaryName) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 150 | if (packageRef.getFQN().length() < 2) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 151 | System.err.println("in default pack? " + binaryName); |
| 152 | this.binaryName = binaryName; |
| 153 | this.fqn = binaryToFQN(binaryName); |
| 154 | this.primitive = false; |
| 155 | this.packageRef = packageRef; |
| 156 | } |
| 157 | |
| 158 | ConcreteRef(String binaryName, String fqn, PackageRef pref) { |
| 159 | this.binaryName = binaryName; |
| 160 | this.fqn = fqn; |
| 161 | this.primitive = true; |
| 162 | this.packageRef = pref; |
| 163 | } |
| 164 | |
| 165 | public String getBinary() { |
| 166 | return binaryName; |
| 167 | } |
| 168 | |
| 169 | public String getPath() { |
| 170 | return binaryName + ".class"; |
| 171 | } |
| 172 | |
| 173 | public String getSourcePath() { |
| 174 | return binaryName + ".java"; |
| 175 | } |
| 176 | |
| 177 | public String getFQN() { |
| 178 | return fqn; |
| 179 | } |
| 180 | |
| 181 | public String getDottedOnly() { |
| 182 | return fqn.replace('$', '.'); |
| 183 | } |
| 184 | |
| 185 | public boolean isPrimitive() { |
| 186 | return primitive; |
| 187 | } |
| 188 | |
| 189 | public TypeRef getComponentTypeRef() { |
| 190 | return null; |
| 191 | } |
| 192 | |
| 193 | public TypeRef getClassRef() { |
| 194 | return this; |
| 195 | } |
| 196 | |
| 197 | public PackageRef getPackageRef() { |
| 198 | return packageRef; |
| 199 | } |
| 200 | |
| 201 | public String getShortName() { |
| 202 | int n = binaryName.lastIndexOf('/'); |
| 203 | return binaryName.substring(n + 1); |
| 204 | } |
| 205 | |
| 206 | public boolean isJava() { |
| 207 | return packageRef.isJava(); |
| 208 | } |
| 209 | |
| 210 | public String toString() { |
| 211 | return fqn; |
| 212 | } |
| 213 | |
| 214 | public boolean isObject() { |
| 215 | return fqn.equals("java.lang.Object"); |
| 216 | } |
| 217 | |
| 218 | public boolean equals(Object other) { |
| 219 | assert other instanceof TypeRef; |
| 220 | return this == other; |
| 221 | } |
| 222 | |
| 223 | public int compareTo(TypeRef other) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 224 | if (this == other) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 225 | return 0; |
| 226 | return fqn.compareTo(other.getFQN()); |
| 227 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 228 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | private static class ArrayRef implements TypeRef { |
| 232 | final TypeRef component; |
| 233 | |
| 234 | ArrayRef(TypeRef component) { |
| 235 | this.component = component; |
| 236 | } |
| 237 | |
| 238 | public String getBinary() { |
| 239 | return "[" + component.getBinary(); |
| 240 | } |
| 241 | |
| 242 | public String getFQN() { |
| 243 | return component.getFQN() + "[]"; |
| 244 | } |
| 245 | |
| 246 | public String getPath() { |
| 247 | return component.getPath(); |
| 248 | } |
| 249 | |
| 250 | public String getSourcePath() { |
| 251 | return component.getSourcePath(); |
| 252 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 253 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 254 | public boolean isPrimitive() { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | public TypeRef getComponentTypeRef() { |
| 259 | return component; |
| 260 | } |
| 261 | |
| 262 | public TypeRef getClassRef() { |
| 263 | return component.getClassRef(); |
| 264 | } |
| 265 | |
| 266 | public boolean equals(Object other) { |
| 267 | if (other == null || other.getClass() != getClass()) |
| 268 | return false; |
| 269 | |
| 270 | return component.equals(((ArrayRef) other).component); |
| 271 | } |
| 272 | |
| 273 | public PackageRef getPackageRef() { |
| 274 | return component.getPackageRef(); |
| 275 | } |
| 276 | |
| 277 | public String getShortName() { |
| 278 | return component.getShortName() + "[]"; |
| 279 | } |
| 280 | |
| 281 | public boolean isJava() { |
| 282 | return component.isJava(); |
| 283 | } |
| 284 | |
| 285 | public String toString() { |
| 286 | return component.toString() + "[]"; |
| 287 | } |
| 288 | |
| 289 | public boolean isObject() { |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | public String getDottedOnly() { |
| 294 | return component.getDottedOnly(); |
| 295 | } |
| 296 | |
| 297 | public int compareTo(TypeRef other) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 298 | if (this == other) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 299 | return 0; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 300 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 301 | return getFQN().compareTo(other.getFQN()); |
| 302 | } |
| 303 | |
| 304 | } |
| 305 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 306 | public TypeRef getTypeRef(String binaryClassName) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 307 | assert !binaryClassName.endsWith(".class"); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 308 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 309 | TypeRef ref = typeRefCache.get(binaryClassName); |
| 310 | if (ref != null) |
| 311 | return ref; |
| 312 | |
| 313 | if (binaryClassName.startsWith("[")) { |
| 314 | ref = getTypeRef(binaryClassName.substring(1)); |
| 315 | ref = new ArrayRef(ref); |
| 316 | } else { |
| 317 | if (binaryClassName.length() >= 1) { |
| 318 | switch (binaryClassName.charAt(0)) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 319 | case 'V' : |
| 320 | return VOID; |
| 321 | case 'B' : |
| 322 | return BYTE; |
| 323 | case 'C' : |
| 324 | return CHAR; |
| 325 | case 'I' : |
| 326 | return INTEGER; |
| 327 | case 'S' : |
| 328 | return SHORT; |
| 329 | case 'D' : |
| 330 | return DOUBLE; |
| 331 | case 'F' : |
| 332 | return FLOAT; |
| 333 | case 'J' : |
| 334 | return LONG; |
| 335 | case 'Z' : |
| 336 | return BOOLEAN; |
| 337 | case 'L' : |
| 338 | binaryClassName = binaryClassName.substring(1, binaryClassName.length() - 1); |
| 339 | break; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 340 | } |
| 341 | // falls trough for other 1 letter class names |
| 342 | } |
| 343 | ref = typeRefCache.get(binaryClassName); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 344 | if (ref != null) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 345 | return ref; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 346 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 347 | PackageRef pref; |
| 348 | int n = binaryClassName.lastIndexOf('/'); |
| 349 | if (n < 0) |
| 350 | pref = DEFAULT_PACKAGE; |
| 351 | else |
| 352 | pref = getPackageRef(binaryClassName.substring(0, n)); |
| 353 | |
| 354 | ref = new ConcreteRef(pref, binaryClassName); |
| 355 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 356 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 357 | typeRefCache.put(binaryClassName, ref); |
| 358 | return ref; |
| 359 | } |
| 360 | |
| 361 | public PackageRef getPackageRef(String binaryPackName) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 362 | if (binaryPackName.indexOf('.') >= 0) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 363 | binaryPackName = binaryPackName.replace('.', '/'); |
| 364 | } |
| 365 | PackageRef ref = packageCache.get(binaryPackName); |
| 366 | if (ref != null) |
| 367 | return ref; |
| 368 | |
| 369 | ref = new PackageRef(binaryPackName); |
| 370 | packageCache.put(binaryPackName, ref); |
| 371 | return ref; |
| 372 | } |
| 373 | |
| 374 | public Descriptor getDescriptor(String descriptor) { |
| 375 | Descriptor d = descriptorCache.get(descriptor); |
| 376 | if (d != null) |
| 377 | return d; |
| 378 | d = new Descriptor(descriptor); |
| 379 | descriptorCache.put(descriptor, d); |
| 380 | return d; |
| 381 | } |
| 382 | |
| 383 | public class Descriptor { |
| 384 | final TypeRef type; |
| 385 | final TypeRef[] prototype; |
| 386 | final String descriptor; |
| 387 | |
| 388 | private Descriptor(String descriptor) { |
| 389 | this.descriptor = descriptor; |
| 390 | int index = 0; |
| 391 | List<TypeRef> types = Create.list(); |
| 392 | if (descriptor.charAt(index) == '(') { |
| 393 | index++; |
| 394 | while (descriptor.charAt(index) != ')') { |
| 395 | index = parse(types, descriptor, index); |
| 396 | } |
| 397 | index++; // skip ) |
| 398 | prototype = types.toArray(new TypeRef[types.size()]); |
| 399 | types.clear(); |
| 400 | } else |
| 401 | prototype = null; |
| 402 | |
| 403 | index = parse(types, descriptor, index); |
| 404 | type = types.get(0); |
| 405 | } |
| 406 | |
| 407 | int parse(List<TypeRef> types, String descriptor, int index) { |
| 408 | char c; |
| 409 | StringBuilder sb = new StringBuilder(); |
| 410 | while ((c = descriptor.charAt(index++)) == '[') { |
| 411 | sb.append('['); |
| 412 | } |
| 413 | |
| 414 | switch (c) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 415 | case 'L' : |
| 416 | while ((c = descriptor.charAt(index++)) != ';') { |
| 417 | // TODO |
| 418 | sb.append(c); |
| 419 | } |
| 420 | break; |
| 421 | |
| 422 | case 'V' : |
| 423 | case 'B' : |
| 424 | case 'C' : |
| 425 | case 'I' : |
| 426 | case 'S' : |
| 427 | case 'D' : |
| 428 | case 'F' : |
| 429 | case 'J' : |
| 430 | case 'Z' : |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 431 | sb.append(c); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 432 | break; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 433 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 434 | default : |
| 435 | throw new IllegalArgumentException("Invalid type in descriptor: " + c + " from " + descriptor + "[" |
| 436 | + index + "]"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 437 | } |
| 438 | types.add(getTypeRef(sb.toString())); |
| 439 | return index; |
| 440 | } |
| 441 | |
| 442 | public TypeRef getType() { |
| 443 | return type; |
| 444 | } |
| 445 | |
| 446 | public TypeRef[] getPrototype() { |
| 447 | return prototype; |
| 448 | } |
| 449 | |
| 450 | public boolean equals(Object other) { |
| 451 | if (other == null || other.getClass() != getClass()) |
| 452 | return false; |
| 453 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 454 | return Arrays.equals(prototype, ((Descriptor) other).prototype) && type == ((Descriptor) other).type; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | public int hashCode() { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 458 | return prototype == null ? type.hashCode() : type.hashCode() ^ Arrays.hashCode(prototype); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | public String toString() { |
| 462 | return descriptor; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Return the short name of a FQN |
| 468 | */ |
| 469 | |
| 470 | public static String getShortName(String fqn) { |
| 471 | assert fqn.indexOf('/') < 0; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 472 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 473 | int n = fqn.lastIndexOf('.'); |
| 474 | if (n >= 0) { |
| 475 | return fqn.substring(n + 1); |
| 476 | } |
| 477 | return fqn; |
| 478 | } |
| 479 | |
| 480 | public static String binaryToFQN(String binary) { |
| 481 | StringBuilder sb = new StringBuilder(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 482 | for (int i = 0, l = binary.length(); i < l; i++) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 483 | char c = binary.charAt(i); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 484 | |
| 485 | if (c == '/') |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 486 | sb.append('.'); |
| 487 | else |
| 488 | sb.append(c); |
| 489 | } |
| 490 | String result = sb.toString(); |
| 491 | assert result.length() > 0; |
| 492 | return result; |
| 493 | } |
| 494 | |
| 495 | public static String fqnToBinary(String binary) { |
| 496 | return binary.replace('.', '/'); |
| 497 | } |
| 498 | |
| 499 | public static String getPackage(String binaryNameOrFqn) { |
| 500 | int n = binaryNameOrFqn.lastIndexOf('/'); |
| 501 | if (n >= 0) |
| 502 | return binaryNameOrFqn.substring(0, n).replace('/', '.'); |
| 503 | |
| 504 | n = binaryNameOrFqn.lastIndexOf("."); |
| 505 | if (n >= 0) |
| 506 | return binaryNameOrFqn.substring(0, n); |
| 507 | |
| 508 | return "."; |
| 509 | } |
| 510 | |
| 511 | public static String fqnToPath(String s) { |
| 512 | return fqnToBinary(s) + ".class"; |
| 513 | } |
| 514 | |
| 515 | public TypeRef getTypeRefFromFQN(String fqn) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 516 | if (fqn.equals("boolean")) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 517 | return BOOLEAN; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 518 | |
| 519 | if (fqn.equals("byte")) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 520 | return BOOLEAN; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 521 | |
| 522 | if (fqn.equals("char")) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 523 | return CHAR; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 524 | |
| 525 | if (fqn.equals("short")) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 526 | return SHORT; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 527 | |
| 528 | if (fqn.equals("int")) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 529 | return INTEGER; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 530 | |
| 531 | if (fqn.equals("long")) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 532 | return LONG; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 533 | |
| 534 | if (fqn.equals("float")) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 535 | return FLOAT; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 536 | |
| 537 | if (fqn.equals("double")) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 538 | return DOUBLE; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 539 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 540 | return getTypeRef(fqnToBinary(fqn)); |
| 541 | } |
| 542 | |
| 543 | public TypeRef getTypeRefFromPath(String path) { |
| 544 | assert path.endsWith(".class"); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 545 | return getTypeRef(path.substring(0, path.length() - 6)); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 546 | } |
| 547 | } |