blob: c24504e75f552c71543cff6e27797f75e52ee68f [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
108} // namespace art