当前位置: 首页 > news >正文

开源门户网站cms购物网站主页模板

开源门户网站cms,购物网站主页模板,微墨小程序制作平台,企业网站的建立步骤什么是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
http://www.zqtcl.cn/news/771299/

相关文章:

  • 网站建设傲鸿网站链轮内有死链
  • 哪些网站可以做微商品牌宣传网站怎么不花钱做排名 知乎
  • 上传了网站源码怎么做wordpress加百度广告代码出问题
  • 哪些网站做推广vi设计说明模板
  • 杭州市建设工程造价管理协会网站攀枝花建设工程质量监督站投诉网站
  • 做网站推广送什么深圳的网站建设公司流程
  • 中国网站开发的前景制作公司主页网站
  • 在线画流程图的网站购物网站的设计与实现论文
  • 淘宝客cms网站建设K12网站怎么建设
  • 专业门户网站开发浙江省湖州艺术与设计学校官网
  • 企业网站搭建价格搭建平台的另一种说法
  • 网站开发框架桂林人论坛风姿摄影
  • 吉林省建设安全信息网站网站服务器和空间有什么区别
  • 百度制作网站怎么去掉2345网址导航
  • 深圳网站建设有限公司 2019哪些建材网站可以做宣传
  • 西安阿里云网站建设一建报名资格条件
  • 聊城网站优化wordpress循环该分类子分类
  • 帮网站做关键词排名优化创造网站需要多少钱
  • 广西网站建设推荐wordpress 宣布停止
  • 专注网站制作青岛景观设计公司排名
  • 安庆做网站网站代理建设网站观澜
  • 网站开发需求收集 模板cms做门户网站
  • dw网站首页的导航怎么做有大佬给个网址吗
  • 一个网站怎么做聚合洛可可设计公司贾伟
  • 什么是优化型网站网页设计作业在线网站首页
  • 关于网站建设广告词英文案例网站
  • 有哪些可以做策划方案的网站国家域名
  • vk网站做婚介做seo排名好的网站
  • 广州企业网站建设公司苏州建网站提
  • html如何做购物网站天元建设集团有限公司法人代表