blob: 4eb27b1052ac4fe0fc4cde2f46b467ea00cdf7a0 [file] [log] [blame]
Richard Uhlerb730b782015-07-15 16:01:58 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.ahat;
18
19import com.android.tools.perflib.heap.ClassObj;
20import com.android.tools.perflib.heap.Instance;
21import java.net.URI;
22
23/**
24 * Class to render an hprof value to a DocString.
25 */
26class Value {
27
Richard Uhler77ff54b2015-08-31 10:22:56 -070028 // For string literals, we limit the number of characters we show to
29 // kMaxChars in case the string is really long.
30 private static int kMaxChars = 200;
31
Richard Uhlerb730b782015-07-15 16:01:58 -070032 /**
33 * Create a DocString representing a summary of the given instance.
34 */
35 private static DocString renderInstance(Instance inst) {
36 DocString link = new DocString();
37 if (inst == null) {
38 link.append("(null)");
39 return link;
40 }
41
42 // Annotate classes as classes.
43 if (inst instanceof ClassObj) {
44 link.append("class ");
45 }
46
47 link.append(inst.toString());
48
49 // Annotate Strings with their values.
Richard Uhler77ff54b2015-08-31 10:22:56 -070050 String stringValue = InstanceUtils.asString(inst, kMaxChars);
Richard Uhlerb730b782015-07-15 16:01:58 -070051 if (stringValue != null) {
Richard Uhler77ff54b2015-08-31 10:22:56 -070052 link.appendFormat("\"%s", stringValue);
53 link.append(kMaxChars == stringValue.length() ? "..." : "\"");
Richard Uhlerb730b782015-07-15 16:01:58 -070054 }
55
56 // Annotate DexCache with its location.
Richard Uhler77ff54b2015-08-31 10:22:56 -070057 String dexCacheLocation = InstanceUtils.getDexCacheLocation(inst, kMaxChars);
Richard Uhlerb730b782015-07-15 16:01:58 -070058 if (dexCacheLocation != null) {
Richard Uhler77ff54b2015-08-31 10:22:56 -070059 link.appendFormat(" for %s", dexCacheLocation);
60 if (kMaxChars == dexCacheLocation.length()) {
61 link.append("...");
62 }
Richard Uhlerb730b782015-07-15 16:01:58 -070063 }
64
Richard Uhlerc21e4e62015-08-31 16:16:14 -070065 URI objTarget = DocString.formattedUri("object?id=%d", inst.getId());
Richard Uhlerb730b782015-07-15 16:01:58 -070066 DocString formatted = DocString.link(objTarget, link);
67
68 // Annotate bitmaps with a thumbnail.
69 Instance bitmap = InstanceUtils.getAssociatedBitmapInstance(inst);
70 String thumbnail = "";
71 if (bitmap != null) {
Richard Uhlerc21e4e62015-08-31 16:16:14 -070072 URI uri = DocString.formattedUri("bitmap?id=%d", bitmap.getId());
Richard Uhlerb730b782015-07-15 16:01:58 -070073 formatted.appendThumbnail(uri, "bitmap image");
74 }
75 return formatted;
76 }
77
78 /**
79 * Create a DocString summarizing the given value.
80 */
81 public static DocString render(Object val) {
82 if (val instanceof Instance) {
83 return renderInstance((Instance)val);
84 } else {
Richard Uhlerc21e4e62015-08-31 16:16:14 -070085 return DocString.format("%s", val);
Richard Uhlerb730b782015-07-15 16:01:58 -070086 }
87 }
88}