Search in sources :

Example 6 with ArrayInstance

use of com.squareup.haha.perflib.ArrayInstance in project leakcanary by square.

the class HahaHelperTest method createCharArrayValueInstance_M_Preview2.

private void createCharArrayValueInstance_M_Preview2() {
    ArrayInstance valueInstance = new ArrayInstance(0, null, Type.CHAR, VALUE_ARRAY_LENGTH, 0);
    snapshot.addInstance(STRING_INSTANCE_ID + 16, valueInstance);
}
Also used : ArrayInstance(com.squareup.haha.perflib.ArrayInstance)

Example 7 with ArrayInstance

use of com.squareup.haha.perflib.ArrayInstance in project leakcanary by square.

the class HahaHelper method asString.

static String asString(Object stringObject) {
    Instance instance = (Instance) stringObject;
    List<ClassInstance.FieldValue> values = classInstanceValues(instance);
    Integer count = fieldValue(values, "count");
    Object value = fieldValue(values, "value");
    Integer offset;
    ArrayInstance charArray;
    if (isCharArray(value)) {
        charArray = (ArrayInstance) value;
        offset = 0;
        // https://android-review.googlesource.com/#/c/83611/
        if (hasField(values, "offset")) {
            offset = fieldValue(values, "offset");
        }
    } else {
        // In M preview 2, the underlying char buffer resides in the heap with ID equaling the
        // String's ID + 16.
        // https://android-review.googlesource.com/#/c/160380/2/android/src/com/android/tools/idea/
        // editors/hprof/descriptors/InstanceFieldDescriptorImpl.java
        // This workaround is only needed for M preview 2, as it has been fixed on the hprof
        // generation end by reintroducing a virtual "value" variable.
        // https://android.googlesource.com/platform/art/+/master/runtime/hprof/hprof.cc#1242
        Heap heap = instance.getHeap();
        Instance inlineInstance = heap.getInstance(instance.getId() + 16);
        if (isCharArray(inlineInstance)) {
            charArray = (ArrayInstance) inlineInstance;
            offset = 0;
        } else {
            throw new UnsupportedOperationException("Could not find char array in " + instance);
        }
    }
    checkNotNull(count, "count");
    checkNotNull(charArray, "charArray");
    checkNotNull(offset, "offset");
    if (count == 0) {
        return "";
    }
    char[] chars = charArray.asCharArray(offset, count);
    return new String(chars);
}
Also used : Instance(com.squareup.haha.perflib.Instance) ClassInstance(com.squareup.haha.perflib.ClassInstance) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) Heap(com.squareup.haha.perflib.Heap)

Example 8 with ArrayInstance

use of com.squareup.haha.perflib.ArrayInstance in project leakcanary by square.

the class HeapAnalyzer method describeFields.

private List<String> describeFields(Instance instance) {
    List<String> fields = new ArrayList<>();
    if (instance instanceof ClassObj) {
        ClassObj classObj = (ClassObj) instance;
        for (Map.Entry<Field, Object> entry : classObj.getStaticFieldValues().entrySet()) {
            Field field = entry.getKey();
            Object value = entry.getValue();
            fields.add("static " + field.getName() + " = " + value);
        }
    } else if (instance instanceof ArrayInstance) {
        ArrayInstance arrayInstance = (ArrayInstance) instance;
        if (arrayInstance.getArrayType() == Type.OBJECT) {
            Object[] values = arrayInstance.getValues();
            for (int i = 0; i < values.length; i++) {
                fields.add("[" + i + "] = " + values[i]);
            }
        }
    } else {
        ClassObj classObj = instance.getClassObj();
        for (Map.Entry<Field, Object> entry : classObj.getStaticFieldValues().entrySet()) {
            fields.add("static " + fieldToString(entry));
        }
        ClassInstance classInstance = (ClassInstance) instance;
        for (ClassInstance.FieldValue field : classInstance.getValues()) {
            fields.add(fieldToString(field));
        }
    }
    return fields;
}
Also used : ClassObj(com.squareup.haha.perflib.ClassObj) Field(com.squareup.haha.perflib.Field) HahaHelper.hasField(com.squareup.leakcanary.HahaHelper.hasField) ArrayList(java.util.ArrayList) HahaHelper.fieldToString(com.squareup.leakcanary.HahaHelper.fieldToString) HahaHelper.asString(com.squareup.leakcanary.HahaHelper.asString) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) THashMap(com.squareup.haha.trove.THashMap) Map(java.util.Map) ClassInstance(com.squareup.haha.perflib.ClassInstance)

Aggregations

ArrayInstance (com.squareup.haha.perflib.ArrayInstance)8 ClassInstance (com.squareup.haha.perflib.ClassInstance)5 ClassObj (com.squareup.haha.perflib.ClassObj)4 Instance (com.squareup.haha.perflib.Instance)4 HahaHelper.asString (com.squareup.leakcanary.HahaHelper.asString)3 HahaHelper.fieldToString (com.squareup.leakcanary.HahaHelper.fieldToString)3 Field (com.squareup.haha.perflib.Field)1 Heap (com.squareup.haha.perflib.Heap)1 RootObj (com.squareup.haha.perflib.RootObj)1 RootType (com.squareup.haha.perflib.RootType)1 Type (com.squareup.haha.perflib.Type)1 THashMap (com.squareup.haha.trove.THashMap)1 HahaHelper.hasField (com.squareup.leakcanary.HahaHelper.hasField)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1