I had this class that I wanted to add a convenience method to. But I was getting this weird exception: VerifyError: Bad type on operand stack.

 

The class in question is a JPA entity, I'm using Hibernate, and mind you that Hibernate uses a lot of stuff under the belt, such as Javassist. This convenience method seemed nothing special, but at some point it created an inline Comparator class, such as:

@Entity
...
public class OpeningHours extends AbstractPersistedEntity {

    ...

    public String getSummary() {

        ...


        Collections.sort(days, new Comparator<OpeningHoursDay>() {
            @Override
            public int compare(OpeningHoursDay o1, OpeningHoursDay o2) {
                return ObjectUtils.compare(o1.ordinal(), o2.ordinal());
            }
        });

    ...

This code was punished by an exception as follows:

Caused by: java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    com/totaalsoftware/incidentmanager/entity/OpeningHours$1.<init>(Lcom/totaalsoftware/incidentmanager/entity/OpeningHours;)V @2: invokevirtual
  Reason:
    Type uninitializedThis (current frame, stack[0]) is not assignable to 'com/totaalsoftware/incidentmanager/entity/OpeningHours$1'
  Current Frame:
    bci: @2
    flags: { flagThisUninit }
    locals: { uninitializedThis, 'com/totaalsoftware/incidentmanager/entity/OpeningHours' }
    stack: { uninitializedThis, 'com/totaalsoftware/incidentmanager/entity/OpeningHours' }
  Bytecode:
    0000000: 2a2b b600 542a b700 0eb1

        at com.totaalsoftware.incidentmanager.entity.OpeningHours.sortedDays(OpeningHours.java:125)
        at com.totaalsoftware.incidentmanager.entity.OpeningHours.getSummary(OpeningHours.java:100)
        ... 99 more

This exception looks a bit daunting. I am not sure what is going on here, but it looks nothing like a normal exception, rather it looks like a dump from a JVM crash.

It turns out that Hibernate is modifying the inline Comparator class. The solution is simple: lift that Comparator class out of the @Entity class, and all is fixed.