0

In the source code of Handler.java,I cross below code segment

public Handler(Callback callback, boolean async) {
    if (FIND_POTENTIAL_LEAKS) {
        final Class<? extends Handler> klass = getClass();
        if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
                (klass.getModifiers() & Modifier.STATIC) == 0) {
            Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
                klass.getCanonicalName());
        }
    }

}

From code,I can see FIND_POTENTIAL_LEAKS is used to find potential leaks.However the filed is private and always false.

So when it will be really made use of?

EDIT

From Murat, reflection seems to work but why Android set the value default true ?

JianxinLi
  • 23
  • 4

1 Answers1

0

There is a comment in the docs for that field:

 public class Handler {
65     /*
66      * Set this flag to true to detect anonymous, local or member classes
67      * that extend this Handler class and that are not static. These kind
68      * of classes can potentially create leaks.
69      */
70     private static final boolean FIND_POTENTIAL_LEAKS = false;

I guess it's getting set to true via reflections e.g. described here Change private static final field using Java reflection

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • So why not default `true` ? @Ridcully – JianxinLi Aug 06 '17 at 09:12
  • This is internal SDK code. You can ask the Android developers about it. My guess is, that it is used during development of the SDK to find potential leaks. Since what we have here is a production version of the code, it is set to false. – Ridcully Aug 06 '17 at 10:20