java asm tree_使用ASM 4处理Java类文件–第二部分:Tree API

java asm tree

什么是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 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

java asm tree

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/357699.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php 验证码文件,php实现的验证码文件类实例

本文实例讲述了php实现的验证码文件类。分享给大家供大家参考。具体如下&#xff1a;/*** file* version 1.0* author 网海浪子* brief 验证码文件类**/class ccheckcodefile{//验证码位数private $mcheckcodenum 4;//产生的验证码private $mcheckcode ;//验证码的图片privat…

【概率与期望】[UVA11021]Tribles

题目大意 k只麻球&#xff0c;每活一天就会死亡&#xff0c;但第二天可能会生一些麻球&#xff0c;具体是 生i个麻球的概率为pi ,求m天后所有麻球都死亡的概率。 LRJ such a dog&#xff0c;你给我个错的翻译。 分析 用f(i)表示一开始有1只麻球&#xff0c;i天后死亡的概率。 …

【APICloud系列|35】APICLloud开源官方模块

目前官方开源的模块有: 1,bMap(百度地图):https://github.com/apicloudcom/bMap 2,UIListView(可侧滑item的列表):https://github.com/apicloudcom/UIListView 3,aMap(高德地图):https://github.com/apicloudcom/aMap 4,UIPullRefresh(下拉刷新):https://gith…

Java无处不在:使用DukeScript在任何地方运行一次编写

在相当长一段时间内&#xff0c;Java都未能兑现“一次编写&#xff0c;随处运行”的承诺。 DukeScript希望通过在跨平台应用程序中实现视图和逻辑的清晰分离来改变这种状况。 在本文中&#xff0c;一个简单的场景用于介绍DukeScript的基础。 多年来&#xff0c;Java Swing使开…

数据仓库建设中的数据建模方法(转)

简介&#xff1a; 本文的主要内容不是介绍现有的比较流行的主要行业的一些数据模型&#xff0c;而是将笔者在数据仓库建设项目中的一些经验&#xff0c;在这里分享给大家。希望帮助大家在数据仓库项目建设中总结出一套能够合乎目前业界规范的&#xff0c;满足大部分行业数据仓库…

php实现标签云,php标签云的实现代码

数据库中&#xff0c;存放文章的表中有“Tag”字段&#xff0c;用来存放标签。标签之间用“,”分隔。比如“PHP,VB,随笔”。下面的实现代码&#xff0c;将标签从数据库中搜出来&#xff0c;并格式化处理&#xff0c;使其以出现的次数为依据显示出不同大小的文字连接。其中的细节…

【APICloud系列|36】 mobVerify免费短信验证码的实现

使用mobVerify之前,请到mob官网注册开发者账号,并申请api,大致流程如下: 1、网址:http://www.mob.com 2、注册登陆后鼠标放在右上角头像处即可看到"进入后台",点击进入 3、点击SecurityCodeSDK进入短信管理界面 4、点击顶部导航中的创建应用,填写信息即可获取…

PAT 1065 A+B and C (64bit) (20)

1065. AB and C (64bit) (20) 时间限制 100 ms内存限制 65536 kB代码长度限制 16000 B判题程序 Standard作者 HOU, QimingGiven three integers A, B and C in [-263, 263], you are supposed to tell whether AB > C. Input Specification: The first line of the input gi…

十大有用但又偏执的Java编程技术

经过一段时间的编码&#xff08;以我为例&#xff0c;大约20年左右&#xff0c;当您玩得开心时光飞逝&#xff09;&#xff0c;人们开始接受这些习惯。 因为&#xff0c;你知道... 任何可能出错的事情都会发生。 这就是为什么人们会采用“防御性编程”的原因&#xff0c;即偏执…

php 接入微信 验证,PHP实现微信公众平台企业号验证接口

这篇文章主要介绍了PHP编程之微信公众平台企业号验证接口,是通过回调操作实现的企业号验证功能接口,需要的朋友可以参考下本文实例讲述了PHP微信公众平台企业号验证接口。分享给大家供大家参考&#xff0c;具体如下&#xff1a;微信公众平台企业号验证接口、回调 PHP版&#xf…

【APICloud系列|37】 银联支付的实现

正式使用请与银联签约端调用方法&#xff1a;xlUnionPayapi.require(xlUnionPay); xlUnionPay .xlUnionPay({params},callback(ret,err))params: spId &#xff1a;银联保留参数&#xff0c;默认为null&#xff08;Android用&#xff09; sysProvider &#xff1a;银联保留参数…

NGUI 3.5教程(二)Label 标签 (Hello world)、多行文本

写在前面&#xff1a; 本文将创建NGUI的第一个样例。依照编程传统&#xff0c;第一个样例&#xff0c;就是做一个Hello world 显示出来。NGUI。我们用Label来实现 。欢迎大家纠错、拍砖&#xff01;原创非常辛苦&#xff0c;如有转载&#xff0c;请注明出处&#xff1a;htt…

【APICloud系列|38】 微信登录分享、QQ登录分享实现方法

对微信登录分享、QQ登录分享进行一个流程性的讲解。在微信分享经常是分享不成功或者图片不显示,主要问题是图片过大或者是没有本地化。 1、流程:使用auth进行授权--->getToken获取用户信息---->同步至服务端 注意:在安卓端可以提示用户没有安装微信端,但是ios端切…

PHP发送数据到指定方法,php通过header发送自定义数据方法_php技巧

下面小编就为大家分享一篇php通过header发送自定义数据方法&#xff0c;具有很好的参考价值&#xff0c;希望对大家有所帮助。一起跟随小编过来看看吧本文将介绍如何通过header发送自定义数据。发送请求时&#xff0c;除了可以使用$_GET/$_POST发送数据&#xff0c;也可以把数据…

JavaScript高级程序设计学习笔记第二十章--JSON

1.JSON&#xff1a;JavaScript Object Notation&#xff0c; JavaScript 对象表示法。 2.最重要的是要理解它是一种数据格式&#xff0c;不是一种编程语言。虽然具有相同的语法形式&#xff0c;但 JSON 并不从属于 JavaScript。而且&#xff0c;并不是只有 JavaScript 才使用 J…

jboss-5.1.0_JBoss AS 7.1.0.Final“ Thunder”发布-Java EE 6 Full Profile认证!

jboss-5.1.0在JBoss AS7上进行了大约一年的开发后&#xff0c;我们现已发布7.1.0.Final“ Thunder” &#xff01; 可从此处的通常位置下载该文件。 对于JBoss AS7团队来说&#xff0c;这是一个非常大的成就&#xff0c;我们为此版本感到非常自豪。 这个版本包含了7.1.0.CR1b的…

java编程一般类抽象类的定义,Java编程基础抽象类

本文主要是讲述Java编程基础-抽象类&#xff0c;更多Java技术知识&#xff0c;请登陆疯狂软件教育官网。  抽象类&#xff1a;  抽象&#xff1a;不具体&#xff0c;看不明白关键字abstract  抽象类的特点&#xff1a;  1.抽象方法只能定义在抽象类中&#xff0c;抽象类…

蓝牙手柄按键码

&#xfeff;&#xfeff;/*------------------------------------------------------------------------------ ZetaGC串口码与键码对应关系&#xff1a; 上 0x60 -> 0x26 \ 下 0x30 -> 0x28 | 此4键只能相邻按键组合按下 左 0x20 -> 0x25 | 右 …

【APICloud系列|39】实现人脸识别,人脸登录,人脸信息获取

场景:大部分的APP的注册、登录模式都是采用手机号和密码进行登录或者注册,当然这种模式并没有任何不妥,只是不能快速的实登录或者注册,减少手误或者慢的问题。 资源:现在我们可以通过人脸识别进行登录和注册,比如使用一登SDK集成,或者Facecore SDK集成,前后者都需要编程…

将PostgreSQL PL / Java安装为PostgreSQL扩展

在2011年&#xff0c;我在PostgreSQL PL / Java上写了一系列文章。 基本信息仍然可靠&#xff0c;但是现在有了一种从源代码安装PL / Java的简便方法。 这也消除了依赖第三方来创建软件包的需要。 这些注释将非常简短&#xff0c;因为我认为我的读者已经对git和maven熟悉了。 …