Daily WTF: ehcache startup code

I found this gem in the ehcache library while debugging my own code and using the thrown and uncaught exception breakpoint filter from Eclipse (in ConfigurationHelper:

public final BootstrapCacheLoader createBootstrapCacheLoader(
        CacheConfiguration.BootstrapCacheLoaderFactoryConfiguration factoryConfiguration) throws CacheException {
    String className = null;
    BootstrapCacheLoader bootstrapCacheLoader = null;
    try {
        className = factoryConfiguration.fullyQualifiedClassPath;
    } catch (Throwable t) {
        //No class created because the config was missing
    }
    if (className == null || className.length() == 0) {
        LOG.debug("No BootstrapCacheLoaderFactory class specified. Skipping...");
    } else {
        BootstrapCacheLoaderFactory factory = (BootstrapCacheLoaderFactory)
                ClassLoaderUtil.createNewInstance(className);
        Properties properties = PropertyUtil.parseProperties(factoryConfiguration.getProperties());
        return factory.createBootstrapCacheLoader(properties);
    }
    return bootstrapCacheLoader;
}

Notice that the parameter given to this method can be null. I always suspected that using an if-statement to check for null-ness was an over-engineered approach.

2 thoughts on “Daily WTF: ehcache startup code

  1. Very nice catch – this is hardly a ‘gem’ though and is really just an all too common programming error….

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>