Search in sources :

Example 6 with ClassObj

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

the class HahaHelper method extendsThread.

static boolean extendsThread(ClassObj clazz) {
    boolean extendsThread = false;
    ClassObj parentClass = clazz;
    while (parentClass.getSuperClassObj() != null) {
        if (clazz.getClassName().equals(Thread.class.getName())) {
            extendsThread = true;
            break;
        }
        parentClass = parentClass.getSuperClassObj();
    }
    return extendsThread;
}
Also used : ClassObj(com.squareup.haha.perflib.ClassObj)

Example 7 with ClassObj

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

the class HeapAnalyzer method buildLeakElement.

private LeakTraceElement buildLeakElement(LeakNode node) {
    if (node.parent == null) {
        // Ignore any root node.
        return null;
    }
    Instance holder = node.parent.instance;
    if (holder instanceof RootObj) {
        return null;
    }
    LeakTraceElement.Type type = node.referenceType;
    String referenceName = node.referenceName;
    LeakTraceElement.Holder holderType;
    String className;
    String extra = null;
    List<String> fields = describeFields(holder);
    className = getClassName(holder);
    if (holder instanceof ClassObj) {
        holderType = CLASS;
    } else if (holder instanceof ArrayInstance) {
        holderType = ARRAY;
    } else {
        ClassObj classObj = holder.getClassObj();
        if (extendsThread(classObj)) {
            holderType = THREAD;
            String threadName = threadName(holder);
            extra = "(named '" + threadName + "')";
        } else if (className.matches(ANONYMOUS_CLASS_NAME_PATTERN)) {
            String parentClassName = classObj.getSuperClassObj().getClassName();
            if (Object.class.getName().equals(parentClassName)) {
                holderType = OBJECT;
                try {
                    // This is an anonymous class implementing an interface. The API does not give access
                    // to the interfaces implemented by the class. We check if it's in the class path and
                    // use that instead.
                    Class<?> actualClass = Class.forName(classObj.getClassName());
                    Class<?>[] interfaces = actualClass.getInterfaces();
                    if (interfaces.length > 0) {
                        Class<?> implementedInterface = interfaces[0];
                        extra = "(anonymous implementation of " + implementedInterface.getName() + ")";
                    } else {
                        extra = "(anonymous subclass of java.lang.Object)";
                    }
                } catch (ClassNotFoundException ignored) {
                }
            } else {
                holderType = OBJECT;
                // Makes it easier to figure out which anonymous class we're looking at.
                extra = "(anonymous subclass of " + parentClassName + ")";
            }
        } else {
            holderType = OBJECT;
        }
    }
    return new LeakTraceElement(referenceName, type, holderType, className, extra, node.exclusion, fields);
}
Also used : ClassObj(com.squareup.haha.perflib.ClassObj) Instance(com.squareup.haha.perflib.Instance) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) ClassInstance(com.squareup.haha.perflib.ClassInstance) HahaHelper.fieldToString(com.squareup.leakcanary.HahaHelper.fieldToString) HahaHelper.asString(com.squareup.leakcanary.HahaHelper.asString) RootObj(com.squareup.haha.perflib.RootObj) ArrayInstance(com.squareup.haha.perflib.ArrayInstance)

Example 8 with ClassObj

use of com.squareup.haha.perflib.ClassObj 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 9 with ClassObj

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

the class HahaHelperTest method addStringClassToSnapshotWithFields.

private void addStringClassToSnapshotWithFields(Snapshot snapshot, Field[] fields) {
    ClassObj charArrayClass = new ClassObj(0, null, "char[]", 0);
    snapshot.addClass(CHAR_ARRAY_CLASS_ID, charArrayClass);
    ClassObj stringClass = new ClassObj(0, null, "string", 0);
    stringClass.setFields(fields);
    snapshot.addClass(STRING_CLASS_ID, stringClass);
}
Also used : ClassObj(com.squareup.haha.perflib.ClassObj)

Example 10 with ClassObj

use of com.squareup.haha.perflib.ClassObj 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

ClassObj (com.squareup.haha.perflib.ClassObj)10 ArrayInstance (com.squareup.haha.perflib.ArrayInstance)8 ClassInstance (com.squareup.haha.perflib.ClassInstance)7 Instance (com.squareup.haha.perflib.Instance)6 HahaHelper.asString (com.squareup.leakcanary.HahaHelper.asString)5 HahaHelper.fieldToString (com.squareup.leakcanary.HahaHelper.fieldToString)5 Field (com.squareup.haha.perflib.Field)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 HprofParser (com.squareup.haha.perflib.HprofParser)1 RootObj (com.squareup.haha.perflib.RootObj)1 Snapshot (com.squareup.haha.perflib.Snapshot)1 HprofBuffer (com.squareup.haha.perflib.io.HprofBuffer)1 MemoryMappedFileBuffer (com.squareup.haha.perflib.io.MemoryMappedFileBuffer)1 THashMap (com.squareup.haha.trove.THashMap)1 HahaHelper.hasField (com.squareup.leakcanary.HahaHelper.hasField)1