I am struggling here with ASM to create a visitor that can remove unreachable code. For now, the code is the same as provided in ASM 4.0 Guide, that is:
public class RemoveDeadCodeAdapter extends MethodVisitor {
String owner;
MethodVisitor next;
public RemoveDeadCodeAdapter(String owner, int access, String name,
String desc, MethodVisitor mv) {
super(Opcodes.ASM4, new MethodNode(access, name, desc, null, null));
this.owner = owner;
next = mv;
}
@Override public void visitEnd() {
MethodNode mn = (MethodNode) mv;
Analyzer<BasicValue> a =
new Analyzer<BasicValue>(new BasicInterpreter());
try {
a.analyze(owner, mn);
Frame<BasicValue>[] frames = a.getFrames();
AbstractInsnNode[] insns = mn.instructions.toArray();
for (int i = 0; i < frames.length; ++i) {
if (frames[i] == null && !(insns[i] instanceof LabelNode)) {
mn.instructions.remove(insns[i]);
}
}
} catch (AnalyzerException ignored) {
}
mn.accept(next);
}
}
So, the question is: is there any way to achieve this with Bytebuddy? Because Bytebuddy seems to be pretty easy to work. If yes, could anybody tell me what would be the process?
Byte Buddy is no code analysis tool, it is intended for code generation based on a class's API, i.e. it operates based on fields and methods. For deleting dead code, you should find a static tool or a code coverage agent for doing so.