Explicit exception types for 'code size' related exceptions
Currently the ClassWriter and MethodWriter classes throw runtime exceptions if
the constant pool or code size is too large.
These exceptions are plain RuntimeExceptions with no explicit type.
This is a problem for those who actually want to catch these exceptions and do
something about them.
For example, we currently de-instrument the method if after instrumentation the
code size is > the 64k limit.
Currently the only way to identify these exceptions is to write a generic
RuntimeException handler and look for the exact message string of the
exception.
This is very type unsafe and frankly an ugly way of writing the handlers.
I propose adding explicitly typed Exceptions for both these cases. The
exceptions still subclass RuntimeException so they are not checked exceptions
and dont change existing callers of these methods.
The changes are described below:
MethodWriter.getSize()
line 1850: throw new RuntimeException("Method code too large!");
should be: throw new MethodSizeException("Method code too large!");
ClassWriter.toByteArray()
line 773: throw new RuntimeException("Class file too large!");
should be: throw new ConstantPoolSizeException("Method code too large!");
New exception classes:
public abstract class SizeException extends RuntimeException {
public SizeException(String message) {
super(message);
}
}
public class MethodSizeException extends SizeException {
public MethodSizeException(String message) {
super(message);
}
}
public class ConstantPoolSizeException extends SizeException {
public ConstantPoolSizeException(String message) {
super(message);
}
}