You are currently browsing the Philipp Meier's weblog blog archives for January, 2005


report

Use static method to initialize classes before super class constructor is called.

Java does not allow to call any method before linking super() in a constructor for good reasons. Sometime this limitation hurst. Consider to the following case: In a subclass of a hypothetical class Message you want to mangle the message before using the super constructor. The following fails:


public class BrokenSpecialMessage extends Message {
public BrokenSpecialMessage(String name) {
Calendar cal = Calendar.getInstance();
cal.roll(Calendar.DAY_OF_YEAR, -1);
String newName = MessageFormat.format(
"Yesterday at {0,date}, the message {1} was created",
new Object[]{cal, name});
super("At " + newName);
}
}


/java/BrokenSpecialMessage.java
Error: line (11) Message(java.lang.String) in Message cannot be applied to ()
Error: line (17) call to super must be first statement in constructor

This is annoying because beside this simple case the “pre”-super logic cannot be collapsed into a easy statement. The solution is to use a static class method:


class SpecialMessage extends Message {
public SpecialMessage(String message) {
super("At " + formatMessage(message));
}

private static String formatMessage(String name) {
Calendar cal = Calendar.getInstance();
cal.roll(Calendar.DAY_OF_YEAR, -1);
String newName = MessageFormat.format(
“Yesterday at {0,date}, the message {1} was created”,
new Object[]{cal, name});
return newName;
}

}

search
content

Simple Log: The Simple Way To Log

I need to check this (simple-log) out. Simple log is 3 classes in a 18.2k .jar that might make happy:

The following example is stolen from the simple log website:

public class
HelloWorld
{
// Create a SimpleLogger:
private static final SimpleLogger log = new SimpleLogger(HelloWorld.class);

public static void
main(String[] argv)
{
try
{
// Use it!
log.entry(“main()”);
log.debug(“About to print ‘Hello World!'”);
String helloWorldString = “‘Hello World!'”;
log.dbo(DebugLevel.L6_VERBOSE, “helloWorldString”, helloWorldString);
log.db(DebugLevel.L7_LUDICROUS,
“I can’t believe this library has a level called ‘Ludicrous’!”);

System.out.println(helloWorldString);

log.debug(“Printed ‘Hello World!'”);
log.info(“Did you get that?”);
log.warn(“This example is very contrived.”);
}
catch (Throwable t)
{
// Just in case…
log.fatal(“Something really unexpected dropped by.”);
log.dbe(DebugLevel.L1_FATAL, t);
}
log.exit(“main()”);
}

What I like is the log.entry log.exit thing to log method entries and exits. A sample output look like

Fri 2004/11/26 21:10:32.618|>>>|main|HelloWorld|main()
Fri 2004/11/26 21:10:32.618| |main|HelloWorld|About to print ‘Hello World!’
Fri 2004/11/26 21:10:32.618|—|main|HelloWorld|helloWorldString|’Hello World!’
Fri 2004/11/26 21:10:32.618| |main|HelloWorld|I can’t believe this library has a level called ‘Ludicrous’!
‘Hello World!’
Fri 2004/11/26 21:10:32.618| |main|HelloWorld|Printed ‘Hello World!’
Fri 2004/11/26 21:10:32.618| |main|HelloWorld|Did you get that?
Fri 2004/11/26 21:10:32.618| |main|HelloWorld|This example is very contrived.
Fri 2004/11/26 21:10:32.618|< <<|main|HelloWorld|main()

support
marketing
Bear