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 | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 28 | std::string PrettyDescriptor(const String* java_descriptor) { |
| 29 | std::string descriptor(java_descriptor->ToModifiedUtf8()); |
| 30 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 31 | // Count the number of '['s to get the dimensionality. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 32 | const char* c = descriptor.c_str(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 33 | size_t dim = 0; |
| 34 | while (*c == '[') { |
| 35 | dim++; |
| 36 | c++; |
| 37 | } |
| 38 | |
| 39 | // Reference or primitive? |
| 40 | if (*c == 'L') { |
| 41 | // "[[La/b/C;" -> "a.b.C[][]". |
| 42 | c++; // Skip the 'L'. |
| 43 | } else { |
| 44 | // "[[B" -> "byte[][]". |
| 45 | // To make life easier, we make primitives look like unqualified |
| 46 | // reference types. |
| 47 | switch (*c) { |
| 48 | case 'B': c = "byte;"; break; |
| 49 | case 'C': c = "char;"; break; |
| 50 | case 'D': c = "double;"; break; |
| 51 | case 'F': c = "float;"; break; |
| 52 | case 'I': c = "int;"; break; |
| 53 | case 'J': c = "long;"; break; |
| 54 | case 'S': c = "short;"; break; |
| 55 | case 'Z': c = "boolean;"; break; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 56 | default: return descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 61 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 62 | std::string result; |
| 63 | const char* p = c; |
| 64 | while (*p != ';') { |
| 65 | char ch = *p++; |
| 66 | if (ch == '/') { |
| 67 | ch = '.'; |
| 68 | } |
| 69 | result.push_back(ch); |
| 70 | } |
| 71 | // ...and replace the semicolon with 'dim' "[]" pairs: |
| 72 | while (dim--) { |
| 73 | result += "[]"; |
| 74 | } |
| 75 | return result; |
| 76 | } |
| 77 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 78 | std::string PrettyMethod(const Method* m, bool with_signature) { |
| 79 | if (m == NULL) { |
| 80 | return "null"; |
| 81 | } |
| 82 | Class* c = m->GetDeclaringClass(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 83 | std::string result(PrettyDescriptor(c->GetDescriptor())); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 84 | result += '.'; |
| 85 | result += m->GetName()->ToModifiedUtf8(); |
| 86 | if (with_signature) { |
| 87 | // TODO: iterate over the signature's elements and pass them all to |
| 88 | // PrettyDescriptor? We'd need to pull out the return type specially, too. |
| 89 | result += m->GetSignature()->ToModifiedUtf8(); |
| 90 | } |
| 91 | return result; |
| 92 | } |
| 93 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 94 | std::string PrettyType(const Object* obj) { |
| 95 | if (obj == NULL) { |
| 96 | return "null"; |
| 97 | } |
| 98 | if (obj->GetClass() == NULL) { |
| 99 | return "(raw)"; |
| 100 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 101 | std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 102 | if (obj->IsClass()) { |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 103 | result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 104 | } |
| 105 | return result; |
| 106 | } |
| 107 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame^] | 108 | std::string MangleForJni(const std::string& s) { |
| 109 | std::string result; |
| 110 | size_t char_count = CountModifiedUtf8Chars(s.c_str()); |
| 111 | const char* cp = &s[0]; |
| 112 | for (size_t i = 0; i < char_count; ++i) { |
| 113 | uint16_t ch = GetUtf16FromUtf8(&cp); |
| 114 | if (ch == '$' || ch > 127) { |
| 115 | StringAppendF(&result, "_0%04x", ch); |
| 116 | } else { |
| 117 | switch (ch) { |
| 118 | case '_': |
| 119 | result += "_1"; |
| 120 | break; |
| 121 | case ';': |
| 122 | result += "_2"; |
| 123 | break; |
| 124 | case '[': |
| 125 | result += "_3"; |
| 126 | break; |
| 127 | case '/': |
| 128 | result += "_"; |
| 129 | break; |
| 130 | default: |
| 131 | result.push_back(ch); |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | std::string JniShortName(const Method* m) { |
| 140 | Class* declaring_class = m->GetDeclaringClass(); |
| 141 | |
| 142 | std::string class_name(declaring_class->GetDescriptor()->ToModifiedUtf8()); |
| 143 | // Remove the leading 'L' and trailing ';'... |
| 144 | CHECK(class_name[0] == 'L') << class_name; |
| 145 | CHECK(class_name[class_name.size() - 1] == ';') << class_name; |
| 146 | class_name.erase(0, 1); |
| 147 | class_name.erase(class_name.size() - 1, 1); |
| 148 | |
| 149 | std::string method_name(m->GetName()->ToModifiedUtf8()); |
| 150 | |
| 151 | std::string short_name; |
| 152 | short_name += "Java_"; |
| 153 | short_name += MangleForJni(class_name); |
| 154 | short_name += "_"; |
| 155 | short_name += MangleForJni(method_name); |
| 156 | return short_name; |
| 157 | } |
| 158 | |
| 159 | std::string JniLongName(const Method* m) { |
| 160 | std::string long_name; |
| 161 | long_name += JniShortName(m); |
| 162 | long_name += "__"; |
| 163 | |
| 164 | std::string signature(m->GetSignature()->ToModifiedUtf8()); |
| 165 | signature.erase(0, 1); |
| 166 | signature.erase(signature.begin() + signature.find(')'), signature.end()); |
| 167 | |
| 168 | long_name += MangleForJni(signature); |
| 169 | |
| 170 | return long_name; |
| 171 | } |
| 172 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 173 | } // namespace art |