Question:
I am looking for a way to get the javac compiler to generate a warning message that I specify in my code when it is compiling my code. I use the #warning
directive in gcc to tell me about things I need to come back to later and fix up, but there seem to be no compiler directives for javac. Is there any way around this?
Answer:
Unlike C, Java has no preprocessor and cannot make use of preprocessor directives such as gcc’s #warning
. The closest you can come to achieving the same effect is to write a shellscript that preprocesses your .java
files. I would suggest using a unique comment like:
/*warning: Don't hard code the constant. */int foobar = 5;
The shell script should scan through a .java
looking for the specially formatted comments, and output their line numbers and contents. Perl would do nicely for this task. Then set up your build procedure to call your shell script immediately before invoking theJava compiler. That way you’ll get your warnings at compile time. It’s extremely klugey, but it appears to be the only way to do what you want with Java.