Conditional methods use the @conditional tag embedded within Java documentation comments to conditionally include or exclude entire methods from a source file. When a conditional method is excluded from a source file, the compiler automatically eliminates calls to the method from elsewhere within the program.
Syntax
/** @conditional (expression) */
void someMethod(args)
{
…
}
The expression shown above can be any valid Java expression, using identifiers declared with the #define or #undef compiler directives.
The greatest benefit conditional methods provide is a simple way to include or exclude large amounts of debug information from an application. For example, a number of conditional methods may be created to generate vital diagnostic information during the development process. Once development is complete and the application is ready for release, all conditional methods, the expressions used to include them, and any calls can be easily eliminated from the compiled application by merely changing the value of an identifier.
The following example illustrates use of a conditional method:
#define DEBUG //DEBUG is true
...
public class someClass {
. . .
/** @conditional (DEBUG) */
trackDisplay(int dTrack)
{
System.out.println("Variable dTrack assigned: %d", dTrack);
}
}
In the code above, defining the DEBUG identifier allows the trackDisplay conditional method to be compiled. Once the method is no longer useful to the developer, replacing the #define directive with the #undef directive will cause the compiler to extract the method from compilation of the source file, and nullify any calls made to the method from elsewhere within the program.