I am currently utilizing org.objectweb.asm.util.CheckClassAdapter for bytecode verification in my Java project. However, I've observed that this class prints errors to stderr instead of throwing exceptions, which makes it challenging to handle verification failures programmatically.
Here is an example of the existing code snippet:
final byte[] bytes = …; // class bytes are generated previously
CheckClassAdapter.verify(
new ClassReader(bytes),
false,
new PrintWriter(System.err)
);
It appears that a potential workaround could be:
final byte[] bytes = …; // class bytes are generated previously
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
CheckClassAdapter.verify(
new ClassReader(bytes),
false,
pw
);
if (sw.toString().contains("AnalyzerException")) {
System.err.println(sw.toString());
fail();
}
However, this solution feels like a "crutch" to me. Is there a way to modify or extend the CheckClassAdapter to make it throw an exception when bytecode verification fails, rather than printing errors to stderr? I aim to catch and handle verification errors within my code. Any guidance or examples would be greatly appreciated.
ASM version: org.ow2.asm:asm:9.6
Since I haven't found how I can use
CheckClassAdapterto verify bytecode and throw anAnalyzerExceptionexception, I decided to modify the source code of the verify method, as Holger suggested. Here's how you can verify the bytecode:Imports:
Also, it's worth mentioning that this method isn't perfect since you generate bytecode and then parse it again. Most probably, you can simplify this process by integrating bytecode verification directly into the generation process using
CheckClassAdapterandCheckMethodAdapterclasses