FileDocCategorySizeDatePackage
DebugUtils.javaAPI DocAndroid 5.1 API4982Thu Mar 12 22:22:10 GMT 2015android.util

DebugUtils

public class DebugUtils extends Object

Various utilities for debugging and logging.

Fields Summary
Constructors Summary
public DebugUtils()

hide

Methods Summary
public static voidbuildShortClassTag(java.lang.Object cls, java.lang.StringBuilder out)

hide

        if (cls == null) {
            out.append("null");
        } else {
            String simpleName = cls.getClass().getSimpleName();
            if (simpleName == null || simpleName.isEmpty()) {
                simpleName = cls.getClass().getName();
                int end = simpleName.lastIndexOf('.");
                if (end > 0) {
                    simpleName = simpleName.substring(end+1);
                }
            }
            out.append(simpleName);
            out.append('{");
            out.append(Integer.toHexString(System.identityHashCode(cls)));
        }
    
public static booleanisObjectSelected(java.lang.Object object)

Filters objects against the ANDROID_OBJECT_FILTER environment variable. This environment variable can filter objects based on their class name and attribute values.

Here is the syntax for ANDROID_OBJECT_FILTER:

ClassName@attribute1=value1@attribute2=value2...

Examples:

  • Select TextView instances: TextView
  • Select TextView instances of text "Loading" and bottom offset of 22: TextView@text=Loading.*@bottom=22

The class name and the values are regular expressions.

This class is useful for debugging and logging purpose:

if (DEBUG) {
if (DebugUtils.isObjectSelected(childView) && LOGV_ENABLED) {
Log.v(TAG, "Object " + childView + " logged!");
}
}

NOTE: This method is very expensive as it relies heavily on regular expressions and reflection. Calls to this method should always be stripped out of the release binaries and avoided as much as possible in debug mode.

param
object any object to match against the ANDROID_OBJECT_FILTER environement variable
return
true if object is selected by the ANDROID_OBJECT_FILTER environment variable, false otherwise

        boolean match = false;
        String s = System.getenv("ANDROID_OBJECT_FILTER");
        if (s != null && s.length() > 0) {
            String[] selectors = s.split("@");
            // first selector == class name
            if (object.getClass().getSimpleName().matches(selectors[0])) {
                // check potential attributes
                for (int i = 1; i < selectors.length; i++) {
                    String[] pair = selectors[i].split("=");
                    Class<?> klass = object.getClass();
                    try {
                        Method declaredMethod = null;
                        Class<?> parent = klass;
                        do {
                            declaredMethod = parent.getDeclaredMethod("get" +
                                    pair[0].substring(0, 1).toUpperCase(Locale.ROOT) +
                                    pair[0].substring(1),
                                    (Class[]) null);
                        } while ((parent = klass.getSuperclass()) != null &&
                                declaredMethod == null);

                        if (declaredMethod != null) {
                            Object value = declaredMethod
                                    .invoke(object, (Object[])null);
                            match |= (value != null ?
                                    value.toString() : "null").matches(pair[1]);
                        }
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return match;