Search in sources :

Example 1 with Field

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

the class ShortestPathFinder method visitClassInstance.

private void visitClassInstance(LeakNode node) {
    ClassInstance classInstance = (ClassInstance) node.instance;
    Map<String, Exclusion> ignoredFields = new LinkedHashMap<>();
    ClassObj superClassObj = classInstance.getClassObj();
    Exclusion classExclusion = null;
    while (superClassObj != null) {
        Exclusion params = excludedRefs.classNames.get(superClassObj.getClassName());
        if (params != null) {
            // true overrides null or false.
            if (classExclusion == null || !classExclusion.alwaysExclude) {
                classExclusion = params;
            }
        }
        Map<String, Exclusion> classIgnoredFields = excludedRefs.fieldNameByClassName.get(superClassObj.getClassName());
        if (classIgnoredFields != null) {
            ignoredFields.putAll(classIgnoredFields);
        }
        superClassObj = superClassObj.getSuperClassObj();
    }
    if (classExclusion != null && classExclusion.alwaysExclude) {
        return;
    }
    for (ClassInstance.FieldValue fieldValue : classInstance.getValues()) {
        Exclusion fieldExclusion = classExclusion;
        Field field = fieldValue.getField();
        if (field.getType() != Type.OBJECT) {
            continue;
        }
        Instance child = (Instance) fieldValue.getValue();
        String fieldName = field.getName();
        Exclusion params = ignoredFields.get(fieldName);
        // If we found a field exclusion and it's stronger than a class exclusion
        if (params != null && (fieldExclusion == null || (params.alwaysExclude && !fieldExclusion.alwaysExclude))) {
            fieldExclusion = params;
        }
        enqueue(fieldExclusion, node, child, fieldName, INSTANCE_FIELD);
    }
}
Also used : ClassObj(com.squareup.haha.perflib.ClassObj) Field(com.squareup.haha.perflib.Field) Instance(com.squareup.haha.perflib.Instance) ClassInstance(com.squareup.haha.perflib.ClassInstance) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) ClassInstance(com.squareup.haha.perflib.ClassInstance) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Field

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

the class HahaHelperTest method defaultToZeroStringOffsetWhenReadingMPreview2HeapDump.

@Test
public void defaultToZeroStringOffsetWhenReadingMPreview2HeapDump() {
    buffer.setIntsToRead(COUNT_VALUE, OFFSET_VALUE, VALUE_ARRAY_INSTANCE_ID);
    buffer.setStringsToRead("abcdef");
    addStringClassToSnapshotWithFields(snapshot, new Field[] { new Field(Type.INT, "count"), new Field(Type.INT, "offset"), new Field(Type.OBJECT, "value") });
    ClassInstance stringInstance = createStringInstance();
    createCharArrayValueInstance_M_Preview2();
    String actual = HahaHelper.asString(stringInstance);
    assertTrue(actual.equals("abcde"));
}
Also used : Field(com.squareup.haha.perflib.Field) ClassInstance(com.squareup.haha.perflib.ClassInstance) Test(org.junit.Test)

Example 3 with Field

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

the class HahaHelperTest method throwExceptionWhenMissingCharArrayValueForStringInMPreview2HeapDump.

@Test
public void throwExceptionWhenMissingCharArrayValueForStringInMPreview2HeapDump() {
    buffer.setIntsToRead(COUNT_VALUE, OFFSET_VALUE, VALUE_ARRAY_INSTANCE_ID);
    buffer.setStringsToRead("abcdef");
    addStringClassToSnapshotWithFields(snapshot, new Field[] { new Field(Type.INT, "count"), new Field(Type.INT, "offset"), new Field(Type.OBJECT, "value") });
    ClassInstance stringInstance = createStringInstance();
    createObjectValueInstance_M_Preview2();
    try {
        HahaHelper.asString(stringInstance);
        fail("this test should have thrown UnsupportedOperationException");
    } catch (UnsupportedOperationException uoe) {
        String message = uoe.getMessage();
        assertTrue(message.equals("Could not find char array in " + stringInstance));
    }
}
Also used : Field(com.squareup.haha.perflib.Field) ClassInstance(com.squareup.haha.perflib.ClassInstance) Test(org.junit.Test)

Example 4 with Field

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

the class ShortestPathFinder method visitClassObj.

private void visitClassObj(LeakNode node) {
    ClassObj classObj = (ClassObj) node.instance;
    Map<String, Exclusion> ignoredStaticFields = excludedRefs.staticFieldNameByClassName.get(classObj.getClassName());
    for (Map.Entry<Field, Object> entry : classObj.getStaticFieldValues().entrySet()) {
        Field field = entry.getKey();
        if (field.getType() != Type.OBJECT) {
            continue;
        }
        String fieldName = field.getName();
        if (fieldName.equals("$staticOverhead")) {
            continue;
        }
        Instance child = (Instance) entry.getValue();
        boolean visit = true;
        if (ignoredStaticFields != null) {
            Exclusion params = ignoredStaticFields.get(fieldName);
            if (params != null) {
                visit = false;
                if (!params.alwaysExclude) {
                    enqueue(params, node, child, fieldName, STATIC_FIELD);
                }
            }
        }
        if (visit) {
            enqueue(null, node, child, fieldName, STATIC_FIELD);
        }
    }
}
Also used : ClassObj(com.squareup.haha.perflib.ClassObj) Field(com.squareup.haha.perflib.Field) Instance(com.squareup.haha.perflib.Instance) ClassInstance(com.squareup.haha.perflib.ClassInstance) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with Field

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

the class HahaHelperTest method readStringOffsetFromHeapDumpInstance.

@Test
public void readStringOffsetFromHeapDumpInstance() {
    buffer.setIntsToRead(COUNT_VALUE, OFFSET_VALUE, VALUE_ARRAY_INSTANCE_ID);
    buffer.setStringsToRead("abcdef");
    addStringClassToSnapshotWithFields(snapshot, new Field[] { new Field(Type.INT, "count"), new Field(Type.INT, "offset"), new Field(Type.OBJECT, "value") });
    ClassInstance stringInstance = createStringInstance();
    createCharArrayValueInstance();
    String actual = HahaHelper.asString(stringInstance);
    assertTrue(actual.equals("bcdef"));
}
Also used : Field(com.squareup.haha.perflib.Field) ClassInstance(com.squareup.haha.perflib.ClassInstance) Test(org.junit.Test)

Aggregations

ClassInstance (com.squareup.haha.perflib.ClassInstance)7 Field (com.squareup.haha.perflib.Field)7 Test (org.junit.Test)4 ArrayInstance (com.squareup.haha.perflib.ArrayInstance)3 ClassObj (com.squareup.haha.perflib.ClassObj)3 Instance (com.squareup.haha.perflib.Instance)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 THashMap (com.squareup.haha.trove.THashMap)1 HahaHelper.asString (com.squareup.leakcanary.HahaHelper.asString)1 HahaHelper.fieldToString (com.squareup.leakcanary.HahaHelper.fieldToString)1 HahaHelper.hasField (com.squareup.leakcanary.HahaHelper.hasField)1 ArrayList (java.util.ArrayList)1