Java asm修改class信息
[TOC]
1 asm中提供了ClassReader和ClassVisitor用来获取class信息,如果要修改class信息就要用到ClassWriter
1.1 ClassWriter的构造方法
public ClassWriter(final int flags)
public ClassVisitor(final int api, final ClassVisitor cv)
2 BusIndex.class反编译后的代码为
public final class BusIndex extends a {
public BusIndex() {
}
public d generateSubscribeMethodIndex(Object var1) {
d var2 = new d();
Class var3 = var1.getClass();
Class var4 = null;
Class var5 = null;
String var6 = null;
if(var3 == MainActivity.class) {
var4 = com.sample.app.MainActivity.a.class;
var5 = com.a.a.d.a.class;
var6 = "showMessage";
var2.a(var6, var4, var5, new com.eventbus.gen.a(this, var1, var6, var4, var5));
var4 = com.sample.app.MainActivity.a.class;
var5 = com.a.a.d.a.class;
var6 = "showMessagedd";
var2.a(var6, var4, var5, new b(this, var1, var6, var4, var5));
return var2;
} else if(var3 == Main2Activity.class) {
var4 = com.sample.app.MainActivity.a.class;
var5 = com.a.a.d.a.class;
var6 = "showMessage";
var2.a(var6, var4, var5, new c(this, var1, var6, var4, var5));
var4 = com.sample.app.MainActivity.a.class;
var5 = com.a.a.d.a.class;
var6 = "showMessagedd";
var2.a(var6, var4, var5, new com.eventbus.gen.d(this, var1, var6, var4, var5));
return var2;
} else {
return var2;
}
}
}
3 使用asm将generateSubscribeMethodIndex方法中String变量的值都加上前缀
3.1 首先加载BusIndex.class
ClassReader classReader = new ClassReader(BusIndex.class.getName());
3.2 使用ClassReader的accept方法查找generateSubscribeMethodIndex方法
如果有多个generateSubscribeMethodIndex重载方法,查找方法时必须验证方法的 desc signature等其他参数
ClassReader classReader = new ClassReader(BusIndex.class.getName());
classReader.accept(new ClassVisitorAdapter(null), 0);
public class ClassVisitorAdapter extends ClassVisitor {
public ClassVisitorAdapter(ClassWriter classWriter) {
super(Opcodes.ASM6, classWriter);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
MethodVisitor methodVisitor = super.visitMethod(access, name, desc, signature, exceptions);
if (name.equals("generateSubscribeMethodIndex")) {
//methodVisitor = new MethodVisitorAdapter(methodVisitor);
return methodVisitor;
}
return methodVisitor;
}
}
3.3 使用自定义MethodVisitor遍历generateSubscribeMethodIndex方法的具体信息
public class ClassVisitorAdapter extends ClassVisitor {
public ClassVisitorAdapter(ClassWriter classWriter) {
super(Opcodes.ASM6, classWriter);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
MethodVisitor methodVisitor = super.visitMethod(access, name, desc, signature, exceptions);
if (name.equals("generateSubscribeMethodIndex")) {
methodVisitor = new MethodVisitorAdapter(methodVisitor);
return methodVisitor;
}
return methodVisitor;
}
}
public class MethodVisitorAdapter extends MethodVisitor {
public MethodVisitorAdapter(MethodVisitor methodVisitor) {
super(Opcodes.ASM6, methodVisitor);
}
@Override
public void visitCode() {
super.visitCode();
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
}
@Override
public void visitEnd() {
super.visitEnd();
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.println(csts);
}
@Override
public void visitInsn(int opcode) {
super.visitInsn(opcode);
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName() + ", opcode:" + Optcodes.options.get(opcode));
}
@Override
public void visitJumpInsn(int opcode, Label label) {
super.visitJumpInsn(opcode, label);
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName() + ", opcode:" + Optcodes.options.get(opcode));
}
@Override
public void visitLdcInsn(Object cst) {
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName() + ", cst:" + cst);
}
@Override
public void visitMaxs(int maxStack, int maxLocals) {
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName() + " ,maxstack:" + maxStack);
super.visitMaxs(maxStack, maxLocals);
}
}
3.4 运行查看输出结果
visitCode
visitInsn, opcode:DUP
visitInsn, opcode:ACONST_NULL
visitInsn, opcode:ACONST_NULL
visitInsn, opcode:ACONST_NULL
visitLdcInsn, cst:Lcom/sample/app/MainActivity;
visitJumpInsn, opcode:IF_ACMPNE
visitLdcInsn, cst:Lcom/sample/app/MainActivity$a;
visitLdcInsn, cst:Lcom/a/a/d/a;
visitLdcInsn, cst:showMessage
visitInsn, opcode:DUP
visitLdcInsn, cst:Lcom/sample/app/MainActivity$a;
visitLdcInsn, cst:Lcom/a/a/d/a;
visitLdcInsn, cst:showMessagedd
visitInsn, opcode:DUP
visitInsn, opcode:ARETURN
visitLdcInsn, cst:Lcom/sample/app/Main2Activity;
visitJumpInsn, opcode:IF_ACMPNE
visitLdcInsn, cst:Lcom/sample/app/MainActivity$a;
visitLdcInsn, cst:Lcom/a/a/d/a;
visitLdcInsn, cst:showMessage
visitInsn, opcode:DUP
visitLdcInsn, cst:Lcom/sample/app/MainActivity$a;
visitLdcInsn, cst:Lcom/a/a/d/a;
visitLdcInsn, cst:showMessagedd
visitInsn, opcode:DUP
visitInsn, opcode:ARETURN
visitInsn, opcode:ARETURN
visitMaxs ,maxstack:11
visitEnd
3.5 输出的结果中可以看到visitLdcInsn 指令给变量的赋值操作
visitLdcInsn, cst:Lcom/sample/app/MainActivity$a;
visitLdcInsn, cst:Lcom/a/a/d/a;
visitLdcInsn, cst:showMessage
visitLdcInsn, cst:Lcom/sample/app/MainActivity$a;
visitLdcInsn, cst:Lcom/a/a/d/a;
visitLdcInsn, cst:showMessagedd
visitLdcInsn, cst:Lcom/sample/app/Main2Activity;
visitLdcInsn, cst:Lcom/sample/app/MainActivity$a;
visitLdcInsn, cst:Lcom/a/a/d/a;
visitLdcInsn, cst:showMessage
visitLdcInsn, cst:Lcom/sample/app/MainActivity$a;
visitLdcInsn, cst:Lcom/a/a/d/a;
visitLdcInsn, cst:showMessagedd
3.6 MethodVisitorAdapter的visitLdcInsn方法中获取cst的类型
if(cst instanceof Type){
System.out.println("cst value type:"+((Type)cst).getClassName());
}else {
System.out.println("cst value type:"+cst.getClass());
}
3.7 在MethodVisitorAdapter的visitLdcInsn方法中给showMessage开头并且为String类型的cst加上前缀modify,然后通过父类的visitLdcInsn写回class
if (cst instanceof String) {
if(cst != null && cst.toString().startsWith("showMessage")){
super.visitLdcInsn("modify"+cst);
}else{
super.visitLdcInsn(cst);
}
} else {
super.visitLdcInsn(cst);
}
3.8 ClassReader遍历class信息时传入ClassWriter,并输出修改后的class
ClassReader classReader = new ClassReader(BusIndex.class.getName());
ClassWriter classWriter = new ClassWriter(0);
classReader.accept(new ClassVisitorAdapter(classWriter), 0);
System.out.println("修改后的字节码:\n"+new String(classWriter.toByteArray()));
3.9 输出结果
修改后的字节码:
���� 3 6 com/eventbus/gen/BusIndex com/a/a/c/a
SourceFile com/eventbus/gen/d com/eventbus/gen/c com/eventbus/gen/b
com/eventbus/gen/a com/sample/app/MainActivity$a com/sample/app/MainActivity a <init> ()V
generateSubscribeMethodIndex !(Ljava/lang/Object;)Lcom/a/a/c/d; com/a/a/c/d
java/lang/Object getClass ()Ljava/lang/Class;
com/a/a/d/a " modifyshowMessage $ d(Lcom/eventbus/gen/BusIndex;Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/Class;)V &
' D(Ljava/lang/String;Ljava/lang/Class;Ljava/lang/Class;Lcom/a/a/b/a;)V )
* modifyshowMessagedd ,
' com/sample/app/Main2Activity/
'
3.10 将修改后的class写入文件
Files.write(Paths.get(System.getProperty("user.dir")+"/BusIndex.class"), classWriter.toByteArray());
System.out.println("修改后的字节码成功保存到:\n"+Paths.get(System.getProperty("user.dir")+"/BusIndex.class"));
3.11 反编译生成的BusIndex.class
public final class BusIndex extends a {
public BusIndex() {
}
public d generateSubscribeMethodIndex(Object var1) {
d var2 = new d();
Class var3 = var1.getClass();
Class var4 = null;
Class var5 = null;
String var6 = null;
if(var3 == MainActivity.class) {
var4 = com.sample.app.MainActivity.a.class;
var5 = com.a.a.d.a.class;
var6 = "modifyshowMessage";
var2.a(var6, var4, var5, new com.eventbus.gen.a(this, var1, var6, var4, var5));
var4 = com.sample.app.MainActivity.a.class;
var5 = com.a.a.d.a.class;
var6 = "showMessagedd";
var2.a(var6, var4, var5, new b(this, var1, var6, var4, var5));
return var2;
} else if(var3 == Main2Activity.class) {
var4 = com.sample.app.MainActivity.a.class;
var5 = com.a.a.d.a.class;
var6 = "modifyshowMessage";
var2.a(var6, var4, var5, new c(this, var1, var6, var4, var5));
var4 = com.sample.app.MainActivity.a.class;
var5 = com.a.a.d.a.class;
var6 = "showMessagedd";
var2.a(var6, var4, var5, new com.eventbus.gen.d(this, var1, var6, var4, var5));
return var2;
} else {
return var2;
}
}
}