2

Sometimes in AOSP sources I see private static final boolean debug flag with false as ïts value. And there are debug output if this flag is true. Something like this:

private static final boolean DEBUG_DRAW = false;
private static final Paint DEBUG_DRAW_PAINT;
static {
    DEBUG_DRAW_PAINT = DEBUG_DRAW ? new Paint() : null;
    if (DEBUG_DRAW_PAINT != null) {
        DEBUG_DRAW_PAINT.setAntiAlias(true);
        DEBUG_DRAW_PAINT.setColor(Color.MAGENTA);
    }
}

Who and how uses it? Is it possible to switch this flag somehow and take debug output of AOSP classes?

Zoe
  • 27,060
  • 21
  • 118
  • 148
tse
  • 5,769
  • 6
  • 38
  • 58

1 Answers1

0

Everything is possible with Java and Reflection

Pros:

  • I don't think there is anything more powerfull than that

Cons:

  • This technique will be executed at runtime so any code executed before this (e.g. classes loading)... well, is already executed. So you won't see the effects

  • Slow at runtime

  • Dangerous. Use it carefully

You can modify any value using this:

Class<?> klass = ...
Field field = klass.getDeclaredField("DEBUG_DRAW");
field.setAccesible(true);
field.set(
    null, // as this is an static attribute we don't need anything here...
    true  // set true as new value
);

As I stated before reflection is a dangerous technique so that snipped will throw several exceptions if used wrong so you will have to handle them

Alberto S.
  • 7,409
  • 6
  • 27
  • 46