for example I want to replace before compilation:
#debug("${enclosing_method} this is debug message for " + userName)
with:
if (log.isDebugEnabled())
{
log.debug("<real method name> this is debug message for " + userName);
}
for example I want to replace before compilation:
#debug("${enclosing_method} this is debug message for " + userName)
with:
if (log.isDebugEnabled())
{
log.debug("<real method name> this is debug message for " + userName);
}
I would recommend AspectJ to accomplish your goal. It is not a preprocessor, but an aspect-oriented metaprogramming language for Java. You get all sorts of advantages by using AspectJ, like type safety and IDE support. It is quite a powerful language.
To do what I think you want, I would recommend specifying a pointcut to match everything you would like to put your logging into. For example, this matches all the methods in MyType
(you'll want something specific to your own code):
pointcut loggedMethod() execution(* *(..)) && this(MyType);
Then, you'll want to give advice to inject your logging statements:
before() : loggedMethod() {
Log log = iDontKnowWhereLogComesFrom();
if (log.isDefaultEnabled()) {
String methodName = thisJoinPoint.getKind();
log.debug(methodName + " is the debug message for " + iDontKnowWhereUsernameComesFrom);
}
}
Which looks way wordier, except that you automatically support adding logging to any method that matches the pointcut without any changes, which means your actual methods can just look like:
public void foo() {
// do whatever processing you want here without having to worry about the
// cross-cutting concern of logging, since it is not at all associated with
// your business logic
}
In my personal experience, this is a way more sustainable pattern than trying to implement and maintain your own custom preprocessor for this.
Consider using log4j if your example reflects your primary aim.
Quote needed: Preprocessor usage is bad OO practice could be of interest as well.
Don't. Use slf4j instead which allows you to avoid the check with {}.