For simple needs, 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 academic.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);
}
}
If you execute this code, the JFrame title will have the word "DEBUG" appended to it.
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>
After running this script, the source (in the ..\out directory)
package academic.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); } }
public class Debug { public static final boolean RELEASE = true; }
In your source, when you need some debugging codes, you included them in a if statement like
if (Debug.RELEASE) { System.out.println("The value of i is " + i); }
public class Debug { public static final boolean RELEASE = false; }
REFERENCE: rgagnon.com/..java-0164.html rgagnon.com/..java-0130.html
No comments:
Post a Comment