|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 Square, Inc. |
| 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 | +package com.squareup.leakcanary; |
| 17 | + |
| 18 | +import com.squareup.haha.perflib.ArrayInstance; |
| 19 | +import com.squareup.haha.perflib.ClassInstance; |
| 20 | +import com.squareup.haha.perflib.ClassObj; |
| 21 | +import com.squareup.haha.perflib.Field; |
| 22 | +import com.squareup.haha.perflib.Heap; |
| 23 | +import com.squareup.haha.perflib.Instance; |
| 24 | +import com.squareup.haha.perflib.Type; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +import static com.squareup.leakcanary.Preconditions.checkNotNull; |
| 31 | +import static java.util.Arrays.asList; |
| 32 | + |
| 33 | +public final class HahaHelper { |
| 34 | + |
| 35 | + private static final Set<String> WRAPPER_TYPES = new HashSet<>( |
| 36 | + asList(Boolean.class.getName(), Character.class.getName(), Float.class.getName(), |
| 37 | + Double.class.getName(), Byte.class.getName(), Short.class.getName(), |
| 38 | + Integer.class.getName(), Long.class.getName())); |
| 39 | + |
| 40 | + static String fieldToString(Map.Entry<Field, Object> entry) { |
| 41 | + return fieldToString(entry.getKey(), entry.getValue()); |
| 42 | + } |
| 43 | + |
| 44 | + static String fieldToString(ClassInstance.FieldValue fieldValue) { |
| 45 | + return fieldToString(fieldValue.getField(), fieldValue.getValue()); |
| 46 | + } |
| 47 | + |
| 48 | + static String fieldToString(Field field, Object value) { |
| 49 | + return field.getName() + " = " + value; |
| 50 | + } |
| 51 | + |
| 52 | + static String threadName(Instance holder) { |
| 53 | + List<ClassInstance.FieldValue> values = classInstanceValues(holder); |
| 54 | + Object nameField = fieldValue(values, "name"); |
| 55 | + return asString(nameField); |
| 56 | + } |
| 57 | + |
| 58 | + static boolean extendsThread(ClassObj clazz) { |
| 59 | + boolean extendsThread = false; |
| 60 | + ClassObj parentClass = clazz; |
| 61 | + while (parentClass.getSuperClassObj() != null) { |
| 62 | + if (clazz.getClassName().equals(Thread.class.getName())) { |
| 63 | + extendsThread = true; |
| 64 | + break; |
| 65 | + } |
| 66 | + parentClass = parentClass.getSuperClassObj(); |
| 67 | + } |
| 68 | + return extendsThread; |
| 69 | + } |
| 70 | + |
| 71 | + static String asString(Object stringObject) { |
| 72 | + Instance instance = (Instance) stringObject; |
| 73 | + List<ClassInstance.FieldValue> values = classInstanceValues(instance); |
| 74 | + |
| 75 | + Integer count = fieldValue(values, "count"); |
| 76 | + Object value = fieldValue(values, "value"); |
| 77 | + Integer offset; |
| 78 | + ArrayInstance charArray; |
| 79 | + if (isCharArray(value)) { |
| 80 | + charArray = (ArrayInstance) value; |
| 81 | + offset = fieldValue(values, "offset"); |
| 82 | + } else { |
| 83 | + // In M preview 2+, the underlying char buffer resides in the heap with ID equalling the |
| 84 | + // String's ID + 16. |
| 85 | + // https://android-review.googlesource.com/#/c/160380/2/android/src/com/android/tools/idea/ |
| 86 | + // editors/hprof/descriptors/InstanceFieldDescriptorImpl.java |
| 87 | + Heap heap = instance.getHeap(); |
| 88 | + Instance inlineInstance = heap.getInstance(instance.getId() + 16); |
| 89 | + if (isCharArray(inlineInstance)) { |
| 90 | + charArray = (ArrayInstance) inlineInstance; |
| 91 | + offset = 0; |
| 92 | + } else { |
| 93 | + throw new UnsupportedOperationException("Could not find char array in " + instance); |
| 94 | + } |
| 95 | + } |
| 96 | + checkNotNull(count, "count"); |
| 97 | + checkNotNull(charArray, "charArray"); |
| 98 | + checkNotNull(offset, "offset"); |
| 99 | + |
| 100 | + if (count == 0) { |
| 101 | + return ""; |
| 102 | + } |
| 103 | + |
| 104 | + char[] chars = charArray.asCharArray(offset, count); |
| 105 | + |
| 106 | + return new String(chars); |
| 107 | + } |
| 108 | + |
| 109 | + public static boolean isPrimitiveWrapper(Object value) { |
| 110 | + if (!(value instanceof ClassInstance)) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + return WRAPPER_TYPES.contains(((ClassInstance) value).getClassObj().getClassName()); |
| 114 | + } |
| 115 | + |
| 116 | + public static boolean isPrimitiveOrWrapperArray(Object value) { |
| 117 | + if (!(value instanceof ArrayInstance)) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + ArrayInstance arrayInstance = (ArrayInstance) value; |
| 121 | + if (arrayInstance.getArrayType() != Type.OBJECT) { |
| 122 | + return true; |
| 123 | + } |
| 124 | + return WRAPPER_TYPES.contains(arrayInstance.getClassObj().getClassName()); |
| 125 | + } |
| 126 | + |
| 127 | + private static boolean isCharArray(Object value) { |
| 128 | + return value instanceof ArrayInstance && ((ArrayInstance) value).getArrayType() == Type.CHAR; |
| 129 | + } |
| 130 | + |
| 131 | + static List<ClassInstance.FieldValue> classInstanceValues(Instance instance) { |
| 132 | + ClassInstance classInstance = (ClassInstance) instance; |
| 133 | + return classInstance.getValues(); |
| 134 | + } |
| 135 | + |
| 136 | + static <T> T fieldValue(List<ClassInstance.FieldValue> values, String fieldName) { |
| 137 | + for (ClassInstance.FieldValue fieldValue : values) { |
| 138 | + if (fieldValue.getField().getName().equals(fieldName)) { |
| 139 | + //noinspection unchecked |
| 140 | + return (T) fieldValue.getValue(); |
| 141 | + } |
| 142 | + } |
| 143 | + throw new IllegalArgumentException("Field " + fieldName + " does not exists"); |
| 144 | + } |
| 145 | + |
| 146 | + private HahaHelper() { |
| 147 | + throw new AssertionError(); |
| 148 | + } |
| 149 | +} |
0 commit comments