Javassist & Java 8 – invalid constant type: 15

2014-03-21
1 min read

Here’s a fun one, we have some code generated using javassist that looks a bit like:

public void doStuff(byte actionId) {
  switch (actionId) {
    case 0: doOneThing(); break;
    case 1: doSomethingElse(); break;
    default: 
      throw new IllegalArgumentException("Unknown action: " + actionId);
  }
}

This works perfectly fine on Java 6 and Java 7 but fails on Java 8.  It turns out the problematic part is "Unknown action: " + actionId. When run on Java 8 that throws “java.io.IOException: invalid constant type: 15″ in javassist.bytecode.ConstPool.readOne.

The work around is simple, just do the conversion from byte to String explicitly:

throw new IllegalArgumentException("Unknown action: " + 
                                   String.valueOf(actionId));

There may be a more recent version of Javassist which fixes this, I haven’t looked as the work around is so trivial (and we only hit this problem in the error handling code which should never be triggered anyway).