Java Preprocessor Using Eclipse Part 1 PDF Print E-mail
Written by Administrator   
Sunday, 03 January 2010 15:03

Java Preprocessing Using Eclipse Part 1

 So, you would like to macros like  

 

#ifdef PRO_VERSION

   System.out.println("Welcome to the PRO version of this wonderful software.  :) ");

#else

   System.out.println("Sorry, you are using the lousy, crippled, and worthless version of our otherwise wonderful software. :( ");

#endif

 

The Explanation of Why I would Ever Want Icky C-style macros in my Java code

   Now, searching for a java preprocessor on the web, you will find lots of people who are simply horrified that anybody would want to put such backward, impure things in their code.   And the above could certainly be done without preprocessing.    But there are sometimes you just really really want to use IFDEF type macros.   In my case, it was because I have a really huge library which is now filled with lines which generate log data using various combinations of  String.format("blah blah %d %f", i, fx); type stuff.   Then because I was going to be running on an embedded platform, all those strings were getting generated on the heap even if the log lines weren't executing.   This was very bad.  I didn't want to remote all the log lines because I need them for debugging.    In my case this library was a big port from c# code.    I had to do a lot of checking by putting the same log lines in the C# code, running that, and then putting the same output lines in the Java code and comparing the output. 

 

 Prebop to the Rescue

 http://prebop.sourceforge.net/

Prebop is an open source java preprocessor which uses Ant and is fairly easy to integrate with Eclipse.    If you don't know what Eclipse is, then I don't see why you are reading this article.   Now if you don't know what Ant is, here is Apache's one-line description: 

Apache Ant is a Java-based build tool. In theory, it is kind of like Make, but without Make's wrinkles.

   But fear not, you will not have to learn everything about Ant.  In fact, you can use Ant in Eclipse without too much pain at all.

   So anyway, to use a preprocessor in your Eclipse Java application, you need to do the following steps:

  1.  Download Prebop zip file. 
  2.  Install the Prebop plug-in in Eclipse
  3.  Create a build.xml file in your Java project
  4.  Make sure your project builds using the build.xml file
  5.  Add some Prebop lines to your build.xml to run the preprocessor before the compiler
  6.  Run the Ant Build wizard and specify a jar file and stuff
  7.  Add some macro-like code to your source
  8.  Enjoy

 

 

 

 

Last Updated on Monday, 04 January 2010 13:10
 

Sponsored Links

Bold Inventions, Powered by Joomla!; Joomla templates by SG web hosting