users@hk2.java.net

RuntimeException silently turned into Nullpointer Exception

From: buko <buko_at_chiubarobot.com>
Date: Fri, 3 Jan 2014 00:58:33 -0500

Discovered another case where hk2 is silently suppressing an
exception/hiding it behind another exception. Each time this happens it's
pretty frustrating because a good chunk of time must be spent debugging the
container trying to figure out what, if anything it's doing.

In this case I created a Factory implementation that throws a
RuntimeException from the Factory#provide() method. (It's not clear, BTW,
why Factory#provide() doesn't allow an exception to be thrown.) The
exception boils up to Cache#compute(final K key). The
catch(ExecutionException ex) block then rethrows the exception to
SingletonContext#findOrCreate catch(RuntimeException re) block. This method
rethrows the exception in a MultiException and calls
RuntimeException.getCause(). MultiException(Throwable th) ctor requires
that Throwable not be null but it does not check that condition. It just
assumes it and the result is a NullPointerException that very effectively
hides the underlying exception. If MultiException simply did a check it
might raise a more informative error that preserves the stack trace. It's
also not clear why SingletonContext#findOrCreate doesn't preserve the
runtimeException and instead chooses to unwrap it by calling
RuntimeException.getCause().

One issue to get around this would be better logging. hk2 seems to do
little logging that doesn't provide much insight into what's happening or
what went wrong.