Search in sources :

Example 6 with RootObj

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

Aggregations

RootObj (com.squareup.haha.perflib.RootObj)6 ArrayInstance (com.squareup.haha.perflib.ArrayInstance)4 ClassInstance (com.squareup.haha.perflib.ClassInstance)4 Instance (com.squareup.haha.perflib.Instance)4 HahaHelper.asString (com.squareup.leakcanary.HahaHelper.asString)2 HahaHelper.fieldToString (com.squareup.leakcanary.HahaHelper.fieldToString)2 ClassObj (com.squareup.haha.perflib.ClassObj)1 Snapshot (com.squareup.haha.perflib.Snapshot)1 THashMap (com.squareup.haha.trove.THashMap)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1