blob: fd30a362fa7d79b228f6dfa9ece39c7ed0ddc818 [file] [log] [blame]
Elliott Hughes11e45072011-08-16 17:40:46 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: enh@google.com (Elliott Hughes)
3
buzbeec143c552011-08-20 17:38:58 -07004#include "file.h"
Elliott Hughes11e45072011-08-16 17:40:46 -07005#include "object.h"
buzbeec143c552011-08-20 17:38:58 -07006#include "os.h"
Elliott Hughes11e45072011-08-16 17:40:46 -07007#include "utils.h"
8
9namespace art {
10
buzbeec143c552011-08-20 17:38:58 -070011std::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 Hughes5174fe62011-08-23 15:12:35 -070028std::string PrettyDescriptor(const String* java_descriptor) {
29 std::string descriptor(java_descriptor->ToModifiedUtf8());
30
Elliott Hughes11e45072011-08-16 17:40:46 -070031 // Count the number of '['s to get the dimensionality.
Elliott Hughes5174fe62011-08-23 15:12:35 -070032 const char* c = descriptor.c_str();
Elliott Hughes11e45072011-08-16 17:40:46 -070033 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 Hughes5174fe62011-08-23 15:12:35 -070056 default: return descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -070057 }
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 Hughesa0b8feb2011-08-20 09:50:55 -070078std::string PrettyMethod(const Method* m, bool with_signature) {
79 if (m == NULL) {
80 return "null";
81 }
82 Class* c = m->GetDeclaringClass();
Elliott Hughes5174fe62011-08-23 15:12:35 -070083 std::string result(PrettyDescriptor(c->GetDescriptor()));
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070084 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 Hughes11e45072011-08-16 17:40:46 -070094std::string PrettyType(const Object* obj) {
95 if (obj == NULL) {
96 return "null";
97 }
98 if (obj->GetClass() == NULL) {
99 return "(raw)";
100 }
Elliott Hughes5174fe62011-08-23 15:12:35 -0700101 std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700102 if (obj->IsClass()) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700103 result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -0700104 }
105 return result;
106}
107
Elliott Hughes79082e32011-08-25 12:07:32 -0700108std::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
139std::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
159std::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 Hughes11e45072011-08-16 17:40:46 -0700173} // namespace art