When ClassWriter copies constant pool of class reader, ClassWriter.COMPUTE_MAX does not consider changes in argument sizes
Consider the following code snippet given some class SampleClass { }
:
ClassReader reader = new ClassReader(SampleClass.class.getName());
ClassWriter writer = new ClassWriter(reader, ClassWriter.COMPUTE_MAXS);
reader.accept(new ClassVisitor(Opcodes.ASM6, writer) {
@Override
public MethodVisitor visitMethod(int access,
String name,
String descriptor,
String signature,
String[] exceptions) {
return super.visitMethod(access,
name,
"(Ljava/lang/Void;)V", // original: "()V"
signature,
exceptions);
}
}, 0);
ClassReader result = new ClassReader(writer.toByteArray());
result.accept(new TraceClassVisitor(new PrintWriter(System.out)), 0);
The resulting class defines MAXLOCALS = 1
despite the constructor being widened to define one argument such that two slots are required for both the this
argument and the added Void
are not available what results in a verification error.
The problem does not occur if the class writer does not copy the constant pool.
Edited by raphw