Friday, 31 July 2015

Solution for: java.lang.VerifyError: Bad method call from inside of a branch

I was using  Powermockito to write my junit test cases. When i ran my test case it started throwing  Verify Error.

java.lang.VerifyError: Bad <init> method call from inside of a branch
Exception Details:
   y/z/a/ABC.<init>(Lorg/powermock/core/IndicateReloadClass;)V @42: invokespecial
    Error exists in the bytecode
    0000000: 2a2b 4e4d 1300 c6b8 0051 04bd 0031 5903
    0000010: 2d53 1303 f6b8 0043 b800 ca3a 0519 05b2
    0000020: 005e a500 0e2a 01c0 00cc b700 cfa7 000a
    0000030: 2c2d b700 cf01 57b1                   
  Stackmap Table:

    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(
    at javassist.runtime.Desc.getClassObject(
    at javassist.runtime.Desc.getClassType(
    at javassist.runtime.Desc.getType(
    at javassist.runtime.Desc.getType(

My application was running on java "1.7.0_67" and using power mockito "1.6.2". I was not knowing what is the wrong with my test cases.

Root Cause
The VM in JDK 7 uses the new verifier for code compiled in Java 7 mode, but falls back to the old one for code compiled in Java 6 mode. Thus, in theory there should be no problem.
They basically read the bytecode, tagged as Java 7 bytecode, and perform changes in Java 6 mode, saving the results still tagged in Java 7 mode. Thus, the VM in JDK 7 sees Java 7 bytecode and activates the new Java 7 verifier, which fails (or can fail) when it meet the bytecode manipulated in Java 6 mode.

 We can solve this problem by 2 ways:
  1. >By changing java version to older version (1.7.0_25)
  2. By adding this VM runtime option: -XX:-UseSplitVerifier.
After applying this fix, power mockito worked fine for me. Hope it will work for you.

1 comment:

  1. Wasn't Easy? It took me lot of research. Thanks.