Sunday, August 1, 2010

Re: [android-developers] Re: Android preprocessor, //#ifdef...

It's not a feature of the language, nor of Android, it's just part of
any sane Java compiler. I have yet to come across one that doesn't do
it. The Sun (Oracle) Java compiler in particular does it. The Eclipse
compiler does it as well. You can test for yourself with your compiler
by compiling a simple class like this one:

class Test {
private static final boolean DEBUG = false;

public void foo() {
if (DEBUG) System.out.println("Debug mode");
System.out.println("Normal mode");
}
}

Then decompile it using javap -c Test:

class Test extends java.lang.Object{
Test();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return

public void foo();
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String Normal mode
5: invokevirtual #4; //Method
java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}

You can see that the if (DEBUG) statement was not compiled in the
foo() method. If you were to compile with DEBUG set to true, you'd see
that the call to System.out.println is compiled, but the if is not.

On Sun, Aug 1, 2010 at 1:12 PM, RichardC <richard.critten@googlemail.com> wrote:
> Thank you, that's going to make my life easier.
>
> As I am still learning Java, can you tell me if the removal of "dead
> code" is a feature of the Java language or is it unique to the Android
> build chain?
>
> On Aug 1, 8:50 pm, Romain Guy <romain...@android.com> wrote:
>> > I have had to learn to live without conditional compliation.  The only
>> > area where I really miss having a lanugage constuct like "#ifdef" is
>> > when I need to remove instrumentation and/or debugging code.
>>
>> You can achieve this in Java using static final fields. The compiler
>> is smart enough to remove if block whose condition evaluates to false
>> at compile time (we use this a lot in Android's source code :).
>>
>> --
>> Romain Guy
>> Android framework engineer
>> romain...@android.com
>>
>> Note: please don't send private questions to me, as I don't have time
>> to provide private support.  All such questions should be posted on
>> public forums, where I and others can see and answer them
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

--
Romain Guy
Android framework engineer
romainguy@android.com

Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment