Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame^] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: enh@google.com (Elliott Hughes) |
| 3 | |
| 4 | #include "object.h" |
| 5 | #include "utils.h" |
| 6 | |
| 7 | namespace art { |
| 8 | |
| 9 | std::string PrettyDescriptor(const StringPiece& descriptor) { |
| 10 | // Count the number of '['s to get the dimensionality. |
| 11 | const char* c = descriptor.data(); |
| 12 | size_t dim = 0; |
| 13 | while (*c == '[') { |
| 14 | dim++; |
| 15 | c++; |
| 16 | } |
| 17 | |
| 18 | // Reference or primitive? |
| 19 | if (*c == 'L') { |
| 20 | // "[[La/b/C;" -> "a.b.C[][]". |
| 21 | c++; // Skip the 'L'. |
| 22 | } else { |
| 23 | // "[[B" -> "byte[][]". |
| 24 | // To make life easier, we make primitives look like unqualified |
| 25 | // reference types. |
| 26 | switch (*c) { |
| 27 | case 'B': c = "byte;"; break; |
| 28 | case 'C': c = "char;"; break; |
| 29 | case 'D': c = "double;"; break; |
| 30 | case 'F': c = "float;"; break; |
| 31 | case 'I': c = "int;"; break; |
| 32 | case 'J': c = "long;"; break; |
| 33 | case 'S': c = "short;"; break; |
| 34 | case 'Z': c = "boolean;"; break; |
| 35 | default: return descriptor.ToString(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 40 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 41 | std::string result; |
| 42 | const char* p = c; |
| 43 | while (*p != ';') { |
| 44 | char ch = *p++; |
| 45 | if (ch == '/') { |
| 46 | ch = '.'; |
| 47 | } |
| 48 | result.push_back(ch); |
| 49 | } |
| 50 | // ...and replace the semicolon with 'dim' "[]" pairs: |
| 51 | while (dim--) { |
| 52 | result += "[]"; |
| 53 | } |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | std::string PrettyType(const Object* obj) { |
| 58 | if (obj == NULL) { |
| 59 | return "null"; |
| 60 | } |
| 61 | if (obj->GetClass() == NULL) { |
| 62 | return "(raw)"; |
| 63 | } |
| 64 | std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor())); |
| 65 | if (obj->IsClass()) { |
| 66 | result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">"; |
| 67 | } |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | } // namespace art |