WTF? I already posted a much better version of this in the
supermegauber thread. no need for class file hacking. sun.misc.Unsafe
is an even worse idea (Security Manager issues, as well as a
dependency on running in sun VMs).
public class SneakyThrow {
public static RuntimeException sneakyThrow(Throwable t) {
if ( t == null ) throw new NullPointerException("t");
SneakyThrow.<RuntimeException>sneakyThrow0(t);
return null;
}
@SuppressWarnings("unchecked")
private static <T extends Throwable> void sneakyThrow0(Throwable t)
throws T {
throw (T)t;
}
}
Note also how this is much more thought through: Java does not know
that calling this method automatically triggers a throw statement, so
the compiler will whine that you need to return something, and the DA
rules are all messed up. Therefore, the suggested usage is:
public int myMethod() {
throw sneakyThrow(new IOException());
}
Note the 'throw' in front of 'sneakyThrow'. If you've read up on your
JLS and JVMS, you'll know that this code is perfectly valid java (and,
given sun's dogged adherence to backwards compatibility, should mean
it'll continue to work just fine), and that it'll work on every java-
compatible VM.
On Aug 26, 5:54 am, Christian Catchpole <
christ...@catchpole.net>
wrote: