Java assert nasty side-effect - compiler bug?
- by Alex
This
public class test
{
public static void main(String[] args)
{
Object o = null;
assert o != null;
if(o != null)
System.out.println("o != null");
}
}
prints out "o != null"; both 1.5_22 and 1.6_18. Compiler bug? Commenting out the assert fixes it. The byte code appears to jump directly to the print statement when assertions are disabled:
public static main(String[]) : void
L0
LINENUMBER 5 L0
ACONST_NULL
ASTORE 1
L1
LINENUMBER 6 L1
GETSTATIC test.$assertionsDisabled : boolean
IFNE L2
ALOAD 1: o
IFNONNULL L2
NEW AssertionError
DUP
INVOKESPECIAL AssertionError.<init>() : void
ATHROW
L2
LINENUMBER 8 L2
GETSTATIC System.out : PrintStream
LDC "o != null"
INVOKEVIRTUAL PrintStream.println(String) : void
L3
LINENUMBER 9 L3
RETURN
L4