Since Java has no concept of preprocessing, the solution is to create your own.
You can think about using a standard C preprocessor or personalized code and compile the pre-processed output, but this has the disadvantage of duplicating files, so that the project will become more complex and support from the development environment (for example, the ability to switch to a syntax error).
My suggestion is to use annotations through comments that the user preprocessor will direct, and let it do the substitution before compilation.
For example,
public static void main(String[] args) { int nDisks = 3; doTowers(nDisks, 'A', 'B', 'C'); }
will become
public static void main(String[] args) { int nDisks = 3 ; doTowers(nDisks, 'A', 'B', 'C'); }
Then your preprocessor will have a definition file, for example
NDISKS 5
turning code into
public static void main(String[] args) { int nDisks = 5 ; doTowers(nDisks, 'A', 'B', 'C'); }
Similarly, you can emulate conditional code compilation with
doTowers(topN - 1, from, to, inter); System.out.println("Disk " + topN + " from " + from + " to " + to); doTowers(topN - 1, inter, from, to);
which can be converted by a preprocessor (with PRINT OFF type definition) to
doTowers(topN - 1, from, to, inter); /*!PRINT System.out.println("Disk " + topN + " from " + from + " to " + to); */ doTowers(topN - 1, inter, from, to);
You can use alternative syntaxes, but the main ideas
- that the annotated code remains compiled,
- that subprocess substitutions are reversible.