Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.ahat; |
| 18 | |
| 19 | import com.android.tools.perflib.heap.ClassObj; |
| 20 | import com.android.tools.perflib.heap.Instance; |
| 21 | import java.net.URI; |
| 22 | |
| 23 | /** |
| 24 | * Class to render an hprof value to a DocString. |
| 25 | */ |
| 26 | class 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 Uhler | c21e4e6 | 2015-08-31 16:16:14 -0700 | [diff] [blame] | 48 | link.appendFormat("\"%s\"", stringValue); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 49 | } |
| 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 Uhler | c21e4e6 | 2015-08-31 16:16:14 -0700 | [diff] [blame] | 57 | URI objTarget = DocString.formattedUri("object?id=%d", inst.getId()); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 58 | 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 Uhler | c21e4e6 | 2015-08-31 16:16:14 -0700 | [diff] [blame] | 64 | URI uri = DocString.formattedUri("bitmap?id=%d", bitmap.getId()); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 65 | 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 Uhler | c21e4e6 | 2015-08-31 16:16:14 -0700 | [diff] [blame] | 77 | return DocString.format("%s", val); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |