通過樹API的簡單類:讓我們使用樹API創建我們的第一類。 同樣,我將直接進入一個代碼示例,因為沒有什么比代碼示例更好。 生成的類具有打印“ Hello World!”的主要方法。
TreeAPIDemo.java
package com.geekyarticles.asm;import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;public class TreeAPIDemo {public static void main(String [] args) throws Exception{ClassNode classNode=new ClassNode(4);//4 is just the API version number//These properties of the classNode must be setclassNode.version=Opcodes.V1_6;//The generated class will only run on JRE 1.6 or aboveclassNode.access=Opcodes.ACC_PUBLIC;classNode.signature="Lcom/geekyarticles/asm/Generated;";classNode.name="com/geekyarticles/asm/Generated";classNode.superName="java/lang/Object";//Create a methodMethodNode mainMethod=new MethodNode(4,Opcodes.ACC_PUBLIC|Opcodes.ACC_STATIC,"main", "([Ljava/lang/String;)V",null, null);mainMethod.instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"));mainMethod.instructions.add(new LdcInsnNode("Hello World!"));mainMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"));mainMethod.instructions.add(new InsnNode(Opcodes.RETURN));//Add the method to the classNodeclassNode.methods.add(mainMethod);//Write the classClassWriter cw=new ClassWriter(ClassWriter.COMPUTE_MAXS|ClassWriter.COMPUTE_FRAMES);classNode.accept(cw);//Dump the class in a fileFile outDir=new File("out/com/geekyarticles/asm");outDir.mkdirs();DataOutputStream dout=new DataOutputStream(new FileOutputStream(new File(outDir,"Generated.class")));dout.write(cw.toByteArray());dout.flush();dout.close();}
}
如您所見,代碼非常簡單。 與BCEL相比,它的主要優點是與BCEL不同,ASM不需要您將每個常量顯式添加到常量池中。 相反,ASM會照顧常量池本身。
讀取類文件: ClassNode是ClassVisitor。 因此,讀取用于樹API的類就像創建ClassReader對象并使用它讀取類文件一樣簡單,同時將ClassNode對象作為其accept方法傳遞給參數。 完成此操作后,將通過類中存在的所有信息完全初始化傳遞的ClassNode。 在下面的示例中,我們將打印類中的所有方法。
TreeAPIClassReaderDemo.java
package com.geekyarticles.asm;import java.io.FileInputStream;
import java.io.InputStream;import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;public class TreeAPIClassReaderDemo {public static void main(String[] args) throws Exception{InputStream in=new FileInputStream("out/com/geekyarticles/asm/Generated.class");ClassReader cr=new ClassReader(in);ClassNode classNode=new ClassNode();//ClassNode is a ClassVisitorcr.accept(classNode, 0);//Let's move through all the methodsfor(MethodNode methodNodeclassNode.methods){System.out.println(methodNode.name+" "+methodNode.desc);}}}
修改類文件:修改類文件是上述兩個過程的組合。 我們首先以通常的方式讀取該類,對數據進行必要的更改,然后將其寫回到文件中。 以下程序實現了一些日志代碼的自動注入。 當前,我們的Logger類僅打印到標準輸出。 @Loggable注釋的每個方法在開始和返回時都將被記錄。 在此,我們不記錄throw-exception。 但是,也可以通過檢查操作碼ATHROW以相同的方式實現。
LoggingInsertion.java
package com.geekyarticles.asm;import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;public class LoggingInsertion {public static void main(String[] args) throws Exception{InputStream in=LoggingInsertion.class.getResourceAsStream("/com/geekyarticles/asm/LoggingTest.class");ClassReader cr=new ClassReader(in);ClassNode classNode=new ClassNode();cr.accept(classNode, 0);//Let's move through all the methodsfor(MethodNode methodNodeclassNode.methods){System.out.println(methodNode.name+" "+methodNode.desc);boolean hasAnnotation=false;if(methodNode.visibleAnnotations!=null){for(AnnotationNode annotationNodemethodNode.visibleAnnotations){if(annotationNode.desc.equals("Lcom/geekyarticles/asm/Loggable;")){hasAnnotation=true;break;}}}if(hasAnnotation){//Lets insert the begin loggerInsnList beginList=new InsnList();beginList.add(new LdcInsnNode(methodNode.name));beginList.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "com/geekyarticles/asm/Logger", "logMethodStart", "(Ljava/lang/String;)V"));Iterator<AbstractInsnNode> insnNodes=methodNode.instructions.iterator();while(insnNodes.hasNext()){System.out.println(insnNodes.next().getOpcode());}methodNode.instructions.insert(beginList);System.out.println(methodNode.instructions);//A method can have multiple places for return//All of them must be handled.insnNodes=methodNode.instructions.iterator();while(insnNodes.hasNext()){AbstractInsnNode insn=insnNodes.next();System.out.println(insn.getOpcode());if(insn.getOpcode()==Opcodes.IRETURN||insn.getOpcode()==Opcodes.RETURN||insn.getOpcode()==Opcodes.ARETURN||insn.getOpcode()==Opcodes.LRETURN||insn.getOpcode()==Opcodes.DRETURN){InsnList endList=new InsnList();endList.add(new LdcInsnNode(methodNode.name));endList.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "com/geekyarticles/asm/Logger", "logMethodReturn", "(Ljava/lang/String;)V"));methodNode.instructions.insertBefore(insn, endList);}}}}//We are done now. so dump the classClassWriter cw=new ClassWriter(ClassWriter.COMPUTE_MAXS|ClassWriter.COMPUTE_FRAMES);classNode.accept(cw);File outDir=new File("out/com/geekyarticles/asm");outDir.mkdirs();DataOutputStream dout=new DataOutputStream(new FileOutputStream(new File(outDir,"LoggingTest.class")));dout.write(cw.toByteArray());dout.flush();dout.close();}}
LoggingTest.java
package com.geekyarticles.asm;public class LoggingTest {public static void run1(){System.out.println("run 1");}@Loggablepublic static void run2(){System.out.println("run 2");}@Loggablepublic static void main(String [] args){run1();run2();}
}
記錄器
package com.geekyarticles.asm;public class Logger {public static void logMethodStart(String methodName){System.out.println("Starting method: "+methodName);}public static void logMethodReturn(String methodName){System.out.println("Ending method: "+methodName);}
}
Loggable.java
package com.geekyarticles.asm;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Loggable {}
如果運行此程序,則生成的文件將依賴于Logger類。 手動將Logger類復制到out目錄中的正確軟件包。 如果運行生成的類(它是LoggingTest類的修改版本),則將輸出以下內容。
bash-4.1$ java com.geekyarticles.asm.LoggingTest
Starting method: main
run 1
Starting method: run2
run 2
Ending method: run2
Ending method: main
請注意,與普通列表不同,在迭代InsnList對象時可以對其進行修改。 任何更改都會立即反映出來。 因此,如果在當前位置之后插入了一些指令,則也將對其進行迭代。
參考: 使用ASM 4處理Java類文件–第二部分: JCG合作伙伴提供的 Tree API ? 極客文章博客上的Debasish Ray Chawdhuri。
翻譯自: https://www.javacodegeeks.com/2012/02/manipulating-java-class-files-with-asm_22.html