blob: 9b483faa3295f1dc6e8e48332dd629ba4268db61 [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
28 /**
29 * Create a DocString representing a summary of the given instance.
30 */
31 private static DocString renderInstance(Instance inst) {
32 DocString link = new DocString();
33 if (inst == null) {
34 link.append("(null)");
35 return link;
36 }
37
38 // Annotate classes as classes.
39 if (inst instanceof ClassObj) {
40 link.append("class ");
41 }
42
43 link.append(inst.toString());
44
45 // Annotate Strings with their values.
46 String stringValue = InstanceUtils.asString(inst);
47 if (stringValue != null) {
Richard Uhlerc21e4e62015-08-31 16:16:14 -070048 link.appendFormat("\"%s\"", stringValue);
Richard Uhlerb730b782015-07-15 16:01:58 -070049 }
50
51 // Annotate DexCache with its location.
52 String dexCacheLocation = InstanceUtils.getDexCacheLocation(inst);
53 if (dexCacheLocation != null) {
54 link.append(" for " + dexCacheLocation);
55 }
56
Richard Uhlerc21e4e62015-08-31 16:16:14 -070057 URI objTarget = DocString.formattedUri("object?id=%d", inst.getId());
Richard Uhlerb730b782015-07-15 16:01:58 -070058 DocString formatted = DocString.link(objTarget, link);
59
60 // Annotate bitmaps with a thumbnail.
61 Instance bitmap = InstanceUtils.getAssociatedBitmapInstance(inst);
62 String thumbnail = "";
63 if (bitmap != null) {
Richard Uhlerc21e4e62015-08-31 16:16:14 -070064 URI uri = DocString.formattedUri("bitmap?id=%d", bitmap.getId());
Richard Uhlerb730b782015-07-15 16:01:58 -070065 formatted.appendThumbnail(uri, "bitmap image");
66 }
67 return formatted;
68 }
69
70 /**
71 * Create a DocString summarizing the given value.
72 */
73 public static DocString render(Object val) {
74 if (val instanceof Instance) {
75 return renderInstance((Instance)val);
76 } else {
Richard Uhlerc21e4e62015-08-31 16:16:14 -070077 return DocString.format("%s", val);
Richard Uhlerb730b782015-07-15 16:01:58 -070078 }
79 }
80}