blob: f8a55cefe19f975d3505102ec629cf090dafe570 [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 Hughes11e45072011-08-16 17:40:46 -070028std::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 Hughesa0b8feb2011-08-20 09:50:55 -070076std::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 Hughes11e45072011-08-16 17:40:46 -070092std::string PrettyType(const Object* obj) {
93 if (obj == NULL) {
94 return "null";
95 }
96 if (obj->GetClass() == NULL) {
97 return "(raw)";
98 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070099 std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor()->ToModifiedUtf8()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700100 if (obj->IsClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700101 result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()->ToModifiedUtf8()) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -0700102 }
103 return result;
104}
105
106} // namespace art