Java Comment Preprocessor


Current version: 4.96b (12 aug 2007), you can download it from here

   You can found yourself in a situation when you need to change a small part of your Java source (but not all source) particularly when you are developing for mobile devices.There is a Preprocessor tool in C/C++ (as well as in C#) but we don't have such one in Java. I read that someone connected a C preprocessor to Java and used the C preprocessor directives to optimize its code before compilation but of course that was not an optiomal way, because you can not to debug and edit your Java code when it contains C preprocessor directives and any Java IDE (IDEA or NetBeans as examples) will tell you that your source has syntax errors.
    I'd like to tell you that in the times when I began to develop for mobile devices (and I began in 2001 when the Motorola A008 was still in Stockholm laboratory of Motorola) there were not a lot of tools and utilities which made development for mobile devices easy and optimal so I was made to develop a such tool myself and I developed the first version of the preprocessor in the summer 2002.
    I needed to use an IDE for Java development so I decided to use Java comment constructions to save my preprocessor directives and because I needed to make global optimisation of sources I made the preprocessor as a two-pass tool (consequently there are possibilities to exclude files from preprocessing and use global variables in the preprocessor).
    You can take a look at the example of a Java source which uses preprocessor directives below..
//#local TESTVAR="TEST LOCAL VARIABLE"
//#assert "TESTVAR="+/*$TESTVAR$*/
//#include ".//test//_MainProcedure.java"

public static final void testproc()
{
System.out.println(/*$VARHELLO$*/);
System.out.println("// Hello commentaries");
//#local counter=10
//#while counter!=0
System.out.println("Number /*$counter$*/");
//#local counter=counter-1
//#end
System.out.println("Current file name is /*$SRV_CUR_FILE$*/");
System.out.println("Output dir is /*$SRV_OUT_DIR$*/");
//#if strhas("Hello","Hello world")
System.out.println("Substring found");
//#endif
}
May be the preprocessor is only what supports loops in its directives.

I would like to show you the preprocessor directive list to explain you its possibilities. The list is commented shortly because there is a more informative document (but the document can have a bit worse english, sorry) and you can find the document inside of the java comment preprocessor distributive archive.

Directive list

DirectiveReference
//#action EXPR0,EXPR1...EXPRnTo make an action event and process it with a preprocessor action listener
//#global NAME=EXPRTo define a global variable (the variable is globally visibiled, from all preprocessed files)
//#local NAME=EXPRTo define a local variable (the variable will be visibled only in preprocessed file and included files)
//#define NAMETo define a local logical variable as TRUE (it works as //#local NAME=true)
//#if BOOLEAN_EXPRThe start a #if..#else..#endif block
//#ifdefined VARIABLE_NAMEThe start a #ifdefined..#else..#endif block, TRUE if the variable is exists else FALSE
//#elseTo change the flag for current #if block
//#endifTo end current #if block
//#include STRING_EXPRTo include and preprocess other source file which is signed as the string result of the expression
//#excludeif BOOLEAN_EXPRTo exclude the file (where the directive is found) from the preprocessor output list if the expression is true (it very useful when you need to break a big file for a small preprocessed parts but don't need to get the parts as independed preprocessed sources)
//#exitTo abort current source file preprocessing 
//#exitif BOOLEAN_EXPRTo abort current source file preprocessing if the expression result is true
//#while BOOLEAN_EXPRTo start a //#while..//#end block
//#breakTo break current //#while..//#end block
//#continueTo continue //#while..//#end construction
//#endEnd of //#while..//#end construction
//#//To make the next string line after the directive as commented by "//" in the result file (will be processd only the next string line)
/*-*/To give up the tail of the string after the directive
/*$EXPR$*/This directive will be replaced by a string representation of the expression result
//#-To turn off the output of preprocessed data in the output stream
//#+To turn on the output of preprocessed data in the output stream
//$To output the string line tail (after this directive) into the output stream without comments "//$Hello"==>"Hello"
//$$To output the string line tail (after this directive) into the output stream without comments and without (!) processing of /*$..$*/ directives
//#assert STR_EXPRTo output the string expression result at the console (usually I use it for information messages)
//#prefix+All strings after the directive will be added at the start of a result file (it is very useful to make the "import" list for Java sources)
//#prefix-To reset the prefix output mode
//#postfix+All strings after the directive will be added at the end of a result file
//#postfix-To reset the postfix output mode
//#outdir STRING_EXPRTo set a destination directory for the result file of current preprocessed source
//#outname STRING_EXPRTo set a name for the result file of current preprocessed source
//#flushTo flush inside buffer in //#outdir as //#outname and reset the buffer
//#_if BOOLEAN_EXPRTo start a #_if..#_else..#_endif block (it processed at the preprocessing start when the utility finds all global variables at all sources)
//#_elseTo change the flag for current #_if block
//#_endifTo end current #_if block

Expression operators

Arithmetic *, /, +, %
Logical && (and)
|| (or)
! (not)
^ (xor)
Comparison ==,!=,<,>,>=,<=
Brakes (,)

Functions

FLOAT|INTEGER abs(FLOAT|INTEGER)To get the absolute value of an int or float value.
INTEGER round(FLOAT|INTEGER)To get the closest int to the argument.
STRING str2web(STRING)To convert a string to a web compatible string.
INTEGER strlen(STRING)To get the length of a string
INTEGER str2int(STRING)To parse a string as an integer
INTEGER xml_open(STRING)To open an xml document for the path (which given as an argument), it returns the ID of the ioened document
INTEGER xml_getDocumentElement(INTEGER)To get  the root document element of an opened xml document
INTEGER xml_getElementsForName(INTEGER,STRING)To get an element list for an element name from an opened XML document
INTEGER xml_elementsNumber(INTEGER)To get the length of an element list
INTEGER xml_elementAt(INTEGER)To get an indexed element from an element list (the first index is 0)
STRING xml_elementName(INTEGER)To get the name of an element
STRING xml_getAttribute(INTEGER,STRING)To get an attribute value from the element
STRING xml_getElementText(STRING)To get the text from an element (it means <element>Element text</element>)
VALUE $user_name(EXPR0,EXPR1..EXPRn)To call an user defined function
BOOLEAN strhas(STRING,STRING)To find a substring (1th argument) inside of a string (2th argument), TRUE if found, else FALSE

Examples

You can take a look at two java files below which actively use preprocessor and I used them in mobile developments.

LoadingThread.java, SoundManager.java


© 2003-2010 Igor A. Maznitsa. All Rights reserved.