广告宣传模板在线制作免费,网站流量优化,招聘网找工作,海外网站免费建设什么是ASM树API#xff1a; ASM树API是ASM的一部分#xff0c;可让您创建/修改内存中的类。 该类被视为信息树。 像整个类一样#xff0c;它是ClassNode的实例#xff0c;其中包含FieldNode对象列表#xff0c;MethodNode对象列表等。本文假设读者已经在这里阅读了第一部分… 什么是ASM树API ASM树API是ASM的一部分可让您创建/修改内存中的类。 该类被视为信息树。 像整个类一样它是ClassNode的实例其中包含FieldNode对象列表MethodNode对象列表等。本文假设读者已经在这里阅读了第一部分。 通过树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 classNodenew ClassNode(4);//4 is just the API version number//These properties of the classNode must be setclassNode.versionOpcodes.V1_6;//The generated class will only run on JRE 1.6 or aboveclassNode.accessOpcodes.ACC_PUBLIC;classNode.signatureLcom/geekyarticles/asm/Generated;;classNode.namecom/geekyarticles/asm/Generated;classNode.superNamejava/lang/Object;//Create a methodMethodNode mainMethodnew 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 cwnew ClassWriter(ClassWriter.COMPUTE_MAXS|ClassWriter.COMPUTE_FRAMES);classNode.accept(cw);//Dump the class in a fileFile outDirnew File(out/com/geekyarticles/asm);outDir.mkdirs();DataOutputStream doutnew 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 innew FileInputStream(out/com/geekyarticles/asm/Generated.class);ClassReader crnew ClassReader(in);ClassNode classNodenew ClassNode();//ClassNode is a ClassVisitorcr.accept(classNode, 0);//Lets 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 inLoggingInsertion.class.getResourceAsStream(/com/geekyarticles/asm/LoggingTest.class);ClassReader crnew ClassReader(in);ClassNode classNodenew ClassNode();cr.accept(classNode, 0);//Lets move through all the methodsfor(MethodNode methodNodeclassNode.methods){System.out.println(methodNode.name methodNode.desc);boolean hasAnnotationfalse;if(methodNode.visibleAnnotations!null){for(AnnotationNode annotationNodemethodNode.visibleAnnotations){if(annotationNode.desc.equals(Lcom/geekyarticles/asm/Loggable;)){hasAnnotationtrue;break;}}}if(hasAnnotation){//Lets insert the begin loggerInsnList beginListnew InsnList();beginList.add(new LdcInsnNode(methodNode.name));beginList.add(new MethodInsnNode(Opcodes.INVOKESTATIC, com/geekyarticles/asm/Logger, logMethodStart, (Ljava/lang/String;)V));IteratorAbstractInsnNode insnNodesmethodNode.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.insnNodesmethodNode.instructions.iterator();while(insnNodes.hasNext()){AbstractInsnNode insninsnNodes.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 endListnew 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 cwnew ClassWriter(ClassWriter.COMPUTE_MAXS|ClassWriter.COMPUTE_FRAMES);classNode.accept(cw);File outDirnew File(out/com/geekyarticles/asm);outDir.mkdirs();DataOutputStream doutnew 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