Use a precompiler "à la C" with JavaTag(s): Environment
Open source packages
Check this list of what is available.Also take a look at JEnable
Ant
For simple need, Ant can be used to do substitution in your sources.We insert into the code a special tag to delimit code that need to be stripped by the Ant script. Let's say we use //@STARTDEBUG@// and //@ENDDEBUG@//.
package com.rgagnon.howto;
import javax.swing.JFrame;
public class Example {
public static void main(String args[]){
JFrame f = new JFrame();
f.setSize(300,200);
f.setVisible(true);
f.setTitle("HowTo");
//@STARTDEBUG@//
f.setTitle(f.getTitle() + " DEBUG version");
//@ENDDEBUG@//
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
The Ant script to remove the debugging code is :
<project default="buildme">
<target name="compileprod">
<copy todir="../out" includeEmptyDirs="false">
<filterchain>
<tokenfilter>
<replacestring from="//@STARTDEBUG@//" to="/*" />
<replacestring from="//@ENDDEBUG@//" to="*/" />
</tokenfilter>
</filterchain>
<fileset dir=".">
<include name="**/*.java" />
</fileset>
</copy>
<javac srcdir="../out" />
</target>
<target name="compiledebug">
<javac srcdir="." />
</target>
<target name="buildme" depends="compileprod" />
</project>
package com.rgagnon.howto;
import javax.swing.JFrame;
public class Example {
public static void main(String args[]){
JFrame f = new JFrame();
f.setSize(300,200);
f.setVisible(true);
f.setTitle("HowTo");
/*
f.setTitle(f.getTitle() + " DEBUG version");
*/
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Simple boolean flag
See this HowTo. This technique relies on the compiler optimization which remove code in the bytecode generated because it will never be executed.
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com