Compiling differently for debug and release

I want to produce slightly different .class files for my debug and release compilations. For debug, methods like toString() should return rich information which should not be present in the release version. In fact, I want most of my debug specific code to not be included in the final release to keep ti's file size down.

I realise I can do this by creating something like

publicinterface MyGlobals{

staticfinalboolean DEBUG =false;

}

and implementing this on any class that needs to do debug specific work. However, I'm in a bit of a chicken and egg scenerio. This file needs to exist during development time so that every class that needs it can implement it - however, this would prevent me from dynamically changing it based on whether I'm doing a debug or release build. On the other hand, if the file is generated automatically at compile time, all the classes that need to refer to it will need to refer to a non-existant source file, which will cause problems in my NetBeans IDE.

Is there a way around this? It would be nice if there was some sort of a configuration file you could specify to to alter how the code is generated. Something similar to C++ #ifdef.

[1471 byte] By [Mark_McKaya] at [2007-11-26 13:52:42]
# 1
Typically its done by creating a variable (something @DEBUG@) and replace it by build tool like ant, use replace task: http://ant.apache.org/manual/CoreTasks/replace.html.
AjaySingh516a at 2007-7-8 1:30:32 > top of Java-index,Java Essentials,Java Programming...
# 2

Interesting aproach, but I have two questions:

Won't this technique overwrite my source files whenever I run the ant task?

@DEBUG@ isn't a valid java token or annotation, so it appears as an error in my editor.

Is there a purer solution to this problem, or is this standard practice?

Mark_McKaya at 2007-7-8 1:30:32 > top of Java-index,Java Essentials,Java Programming...
# 3
The file name containing these kind of tokens can't be compiled as it is. Its 2 step process:1. create the file with extension other than .java, something like .java.template would do.2. Run the ant task so that it would create the .java file that can be used hence fourth
AjaySingh516a at 2007-7-8 1:30:32 > top of Java-index,Java Essentials,Java Programming...
# 4
> is this standard practice?I have seen a lot of open source projects following this.
AjaySingh516a at 2007-7-8 1:30:32 > top of Java-index,Java Essentials,Java Programming...
# 5

> I want to produce slightly different .class files for

> my debug and release compilations. For debug,

> methods like toString() should return rich

> information which should not be present in the

> release version. In fact, I want most of my debug

> specific code to not be included in the final release

> to keep ti's file size down.

I'd advise against that. It's unlikely that the file size will be significantly different, and it's better to test the exact same code that you release.

jverda at 2007-7-8 1:30:32 > top of Java-index,Java Essentials,Java Programming...
# 6
or use a system property that could be set at runtimejava -Ddebug=true
dmbdmba at 2007-7-8 1:30:32 > top of Java-index,Java Essentials,Java Programming...
# 7
> or use a system property that could be set at> runtime> > java -Ddebug=trueOf course, to be thorough, you'd test with that option enabled, which kind of defeats the purpose (but not entirely, I guess).
jverda at 2007-7-8 1:30:32 > top of Java-index,Java Essentials,Java Programming...
# 8

> I realise I can do this by creating something like

>

> [code]

> public interface MyGlobals {

>static final boolean DEBUG = false;

> /code]

>

You should be careful about this, by the way, rember that this kind of constant value gets compiled in to classes that reference it, and java doesn't always pick up the need to recompile when the source of MyGlobals changes.

On the other hand, if you don't declare it final then the compiler can't optimise out code that you want it to exclude for you.

malcolmmca at 2007-7-8 1:30:32 > top of Java-index,Java Essentials,Java Programming...
# 9
You could use a precompiler (as I sometimes do for JME projects(1)). But I would recommend you don't unless you really need to (i.e. targeting memory-poor devices).1) Google Java Precompiler, or use a C/C++ compiler with the correct options.
mlka at 2007-7-8 1:30:32 > top of Java-index,Java Essentials,Java Programming...