tags:

views:

45

answers:

4

I am writing a desktop and j2me application. I have debug statements in program. Currently to avoid those getting compiled i use as following. We are doing this to reduce size of jar. (Specifically for mobile)

ConstantFile.java
Boolean ConstantFile.DebugEnabled = false;

if(ConstantFile.DebugEnabled) {
log.debug("msg");
}

But this is sort of hard coding. Is there an alternative like C where we have pre-compiled directives. Can Annotation help here ? Or something else i should look for ?

+1  A: 

No, there is no precompile in Java, but instead of using the constants file like you are currently using, try Log4j or some other logging package that supports an isDebugEnabled() operation. This will avoid the hardcoding in favor of a config file or VM argument.

Robin
A: 

Annotations could work, or you could look at AOP. AOP would allow you to automatically insert code into your methods at specific points. Either way should get you what you want.

TMN
A: 

while AOP will work, I'd try and get maven to pre-process the source files to, for instance, remove any line that starts with

log.
this, along with the convention of declaring your log4j loggers as
private final static Logger log = Logger.getLogger(...)
might work, wont require AOP (might require a custom maven plugin), and will keep the logging statements visually where they belong. this should filter out the logging statements from code like this:


for(int i=0; i<1000; i++) {
   //the below line, if removed entirely, will still produce proper syntax
   log.debug("debugging from within a loop is not a good idea");
   //do some useful work
}

note that you'd need to keep all your logging comments in a single line

hatchetman82
A: 

I got exact answer i wanted. Preprocessor. Similar to hatchetman82 suggested, There is wtkpreprocess task available. And its specifically developed for mobile. (Though it can be used for other java part also.) And it works perfectly. It comments out line so byte code remains lighter.

Jigar Shah