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 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame^] | 4 | #include "file.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 5 | #include "object.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame^] | 6 | #include "os.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 7 | #include "utils.h" |
| 8 | |
| 9 | namespace art { |
| 10 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame^] | 11 | std::string ReadFileToString(const char* file_name) { |
| 12 | scoped_ptr<File> file(OS::OpenFile(file_name, false)); |
| 13 | CHECK(file != NULL); |
| 14 | |
| 15 | std::string contents; |
| 16 | char buf[8 * KB]; |
| 17 | while (true) { |
| 18 | int64_t n = file->Read(buf, sizeof(buf)); |
| 19 | CHECK_NE(-1, n); |
| 20 | if (n == 0) { |
| 21 | break; |
| 22 | } |
| 23 | contents.append(buf, n); |
| 24 | } |
| 25 | return contents; |
| 26 | } |
| 27 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 28 | std::string PrettyDescriptor(const StringPiece& descriptor) { |
| 29 | // Count the number of '['s to get the dimensionality. |
| 30 | const char* c = descriptor.data(); |
| 31 | size_t dim = 0; |
| 32 | while (*c == '[') { |
| 33 | dim++; |
| 34 | c++; |
| 35 | } |
| 36 | |
| 37 | // Reference or primitive? |
| 38 | if (*c == 'L') { |
| 39 | // "[[La/b/C;" -> "a.b.C[][]". |
| 40 | c++; // Skip the 'L'. |
| 41 | } else { |
| 42 | // "[[B" -> "byte[][]". |
| 43 | // To make life easier, we make primitives look like unqualified |
| 44 | // reference types. |
| 45 | switch (*c) { |
| 46 | case 'B': c = "byte;"; break; |
| 47 | case 'C': c = "char;"; break; |
| 48 | case 'D': c = "double;"; break; |
| 49 | case 'F': c = "float;"; break; |
| 50 | case 'I': c = "int;"; break; |
| 51 | case 'J': c = "long;"; break; |
| 52 | case 'S': c = "short;"; break; |
| 53 | case 'Z': c = "boolean;"; break; |
| 54 | default: return descriptor.ToString(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 59 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 60 | std::string result; |
| 61 | const char* p = c; |
| 62 | while (*p != ';') { |
| 63 | char ch = *p++; |
| 64 | if (ch == '/') { |
| 65 | ch = '.'; |
| 66 | } |
| 67 | result.push_back(ch); |
| 68 | } |
| 69 | // ...and replace the semicolon with 'dim' "[]" pairs: |
| 70 | while (dim--) { |
| 71 | result += "[]"; |
| 72 | } |
| 73 | return result; |
| 74 | } |
| 75 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 76 | std::string PrettyMethod(const Method* m, bool with_signature) { |
| 77 | if (m == NULL) { |
| 78 | return "null"; |
| 79 | } |
| 80 | Class* c = m->GetDeclaringClass(); |
| 81 | std::string result(PrettyDescriptor(c->GetDescriptor()->ToModifiedUtf8())); |
| 82 | result += '.'; |
| 83 | result += m->GetName()->ToModifiedUtf8(); |
| 84 | if (with_signature) { |
| 85 | // TODO: iterate over the signature's elements and pass them all to |
| 86 | // PrettyDescriptor? We'd need to pull out the return type specially, too. |
| 87 | result += m->GetSignature()->ToModifiedUtf8(); |
| 88 | } |
| 89 | return result; |
| 90 | } |
| 91 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 92 | std::string PrettyType(const Object* obj) { |
| 93 | if (obj == NULL) { |
| 94 | return "null"; |
| 95 | } |
| 96 | if (obj->GetClass() == NULL) { |
| 97 | return "(raw)"; |
| 98 | } |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 99 | std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor()->ToModifiedUtf8())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 100 | if (obj->IsClass()) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 101 | result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()->ToModifiedUtf8()) + ">"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 102 | } |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | } // namespace art |