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 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame^] | 57 | std::string PrettyMethod(const Method* m, bool with_signature) { |
| 58 | if (m == NULL) { |
| 59 | return "null"; |
| 60 | } |
| 61 | Class* c = m->GetDeclaringClass(); |
| 62 | std::string result(PrettyDescriptor(c->GetDescriptor()->ToModifiedUtf8())); |
| 63 | result += '.'; |
| 64 | result += m->GetName()->ToModifiedUtf8(); |
| 65 | if (with_signature) { |
| 66 | // TODO: iterate over the signature's elements and pass them all to |
| 67 | // PrettyDescriptor? We'd need to pull out the return type specially, too. |
| 68 | result += m->GetSignature()->ToModifiedUtf8(); |
| 69 | } |
| 70 | return result; |
| 71 | } |
| 72 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 73 | std::string PrettyType(const Object* obj) { |
| 74 | if (obj == NULL) { |
| 75 | return "null"; |
| 76 | } |
| 77 | if (obj->GetClass() == NULL) { |
| 78 | return "(raw)"; |
| 79 | } |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 80 | std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor()->ToModifiedUtf8())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 81 | if (obj->IsClass()) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 82 | result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()->ToModifiedUtf8()) + ">"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 83 | } |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | } // namespace art |