Javassist & Java 8 – invalid constant type: 15
By Adrian Sutton
Here’s a fun one, we have some code generated using javassist that looks a bit like:
public void doStuff(byte actionId) {<br /> switch (actionId) {<br /> case 0: doOneThing(); break;<br /> case 1: doSomethingElse(); break;<br /> default: <br /> throw new IllegalArgumentException("Unknown action: " + actionId);<br /> }<br />}
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: " + <br /> 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).