blob: 67d57808654ebb7fe281d6c79ec53d71a0fb17b7 [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
4#include "object.h"
5#include "utils.h"
6
7namespace art {
8
9std::string PrettyDescriptor(const StringPiece& descriptor) {
10 // Count the number of '['s to get the dimensionality.
11 const char* c = descriptor.data();
12 size_t dim = 0;
13 while (*c == '[') {
14 dim++;
15 c++;
16 }
17
18 // Reference or primitive?
19 if (*c == 'L') {
20 // "[[La/b/C;" -> "a.b.C[][]".
21 c++; // Skip the 'L'.
22 } else {
23 // "[[B" -> "byte[][]".
24 // To make life easier, we make primitives look like unqualified
25 // reference types.
26 switch (*c) {
27 case 'B': c = "byte;"; break;
28 case 'C': c = "char;"; break;
29 case 'D': c = "double;"; break;
30 case 'F': c = "float;"; break;
31 case 'I': c = "int;"; break;
32 case 'J': c = "long;"; break;
33 case 'S': c = "short;"; break;
34 case 'Z': c = "boolean;"; break;
35 default: return descriptor.ToString();
36 }
37 }
38
39 // At this point, 'c' is a string of the form "fully/qualified/Type;"
40 // or "primitive;". Rewrite the type with '.' instead of '/':
41 std::string result;
42 const char* p = c;
43 while (*p != ';') {
44 char ch = *p++;
45 if (ch == '/') {
46 ch = '.';
47 }
48 result.push_back(ch);
49 }
50 // ...and replace the semicolon with 'dim' "[]" pairs:
51 while (dim--) {
52 result += "[]";
53 }
54 return result;
55}
56
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070057std::string PrettyMethod(const Method* m, bool with_signature) {
58 if (m == NULL) {
59 return "null";
60 }
61 Class* c = m->GetDeclaringClass();
62 std::string result(PrettyDescriptor(c->GetDescriptor()->ToModifiedUtf8()));
63 result += '.';
64 result += m->GetName()->ToModifiedUtf8();
65 if (with_signature) {
66 // TODO: iterate over the signature's elements and pass them all to
67 // PrettyDescriptor? We'd need to pull out the return type specially, too.
68 result += m->GetSignature()->ToModifiedUtf8();
69 }
70 return result;
71}
72
Elliott Hughes11e45072011-08-16 17:40:46 -070073std::string PrettyType(const Object* obj) {
74 if (obj == NULL) {
75 return "null";
76 }
77 if (obj->GetClass() == NULL) {
78 return "(raw)";
79 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070080 std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor()->ToModifiedUtf8()));
Elliott Hughes11e45072011-08-16 17:40:46 -070081 if (obj->IsClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070082 result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()->ToModifiedUtf8()) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -070083 }
84 return result;
85}
86
87} // namespace art