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

网站建设的描述长沙发布app

网站建设的描述,长沙发布app,网站服务器搭建与管理,怎么建立自己网站视频asm 4.0 版本 http://forge.ow2.org/plugins/scmsvn/index.php?group_id23 asm是java的字节码操作框架#xff0c;可以动态查看类的信息#xff0c;动态修改#xff0c;删除#xff0c;增加类的方法。 下面基于4.0版本的一个使用示例#xff0c;演示了对类Foo进行修改方法… asm 4.0 版本 http://forge.ow2.org/plugins/scmsvn/index.php?group_id23   asm是java的字节码操作框架可以动态查看类的信息动态修改删除增加类的方法。   下面基于4.0版本的一个使用示例演示了对类Foo进行修改方法名称增加方法修改方法内容等  import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes;public class AsmExample extends ClassLoader implements Opcodes{public static class Foo {public static void execute() {System.out.println(test changed method name);}public static void changeMethodContent() {System.out.println(test change method);}}public static void main(String[] args) throws IOException, IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException {ClassReader cr new ClassReader(Foo.class.getName());ClassWriter cw new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);ClassVisitor cv new MethodChangeClassAdapter(cw);cr.accept(cv, Opcodes.ASM4);//新增加一个方法MethodVisitor mw cw.visitMethod(ACC_PUBLIC ACC_STATIC,add,([Ljava/lang/String;)V,null,null);// pushes the out field (of type PrintStream) of the System classmw.visitFieldInsn(GETSTATIC,java/lang/System,out,Ljava/io/PrintStream;);// pushes the Hello World! String constantmw.visitLdcInsn(this is add method print!);// invokes the println method (defined in the PrintStream class)mw.visitMethodInsn(INVOKEVIRTUAL,java/io/PrintStream,println,(Ljava/lang/String;)V);mw.visitInsn(RETURN);// this code uses a maximum of two stack elements and two local// variablesmw.visitMaxs(0, 0);mw.visitEnd();// gets the bytecode of the Example class, and loads it dynamicallybyte[] code cw.toByteArray();AsmExample loader new AsmExample();Class? exampleClass loader.defineClass(Foo.class.getName(), code, 0, code.length);for(Method method: exampleClass.getMethods()){System.out.println(method);}System.out.println(*************);// uses the dynamically generated class to print HelloworldexampleClass.getMethods()[0].invoke(null, null); //調用changeMethodContent修改方法內容System.out.println(*************);exampleClass.getMethods()[1].invoke(null, null); //調用execute,修改方法名// gets the bytecode of the Example class, and loads it dynamicallyFileOutputStream fos new FileOutputStream(e:\\logs\\Example.class);fos.write(code);fos.close();}static class MethodChangeClassAdapter extends ClassVisitor implements Opcodes {public MethodChangeClassAdapter(final ClassVisitor cv) {super(Opcodes.ASM4, cv);}Overridepublic void visit(int version,int access,String name,String signature,String superName,String[] interfaces){if (cv ! null) {cv.visit(version, access, name, signature, superName, interfaces);}}Overridepublic MethodVisitor visitMethod(int access,String name,String desc,String signature,String[] exceptions){if (cv ! null execute.equals(name)) { //当方法名为execute时修改方法名为execute1return cv.visitMethod(access, name 1, desc, signature, exceptions);}if(changeMethodContent.equals(name)) //此处的changeMethodContent即为需要修改的方法 修改方法內容{ MethodVisitor mv cv.visitMethod(access, name, desc, signature, exceptions);//先得到原始的方法 MethodVisitor newMethod null; newMethod new AsmMethodVisit(mv); //访问需要修改的方法 return newMethod; } if (cv ! null) {return cv.visitMethod(access, name, desc, signature, exceptions);}return null;}}static class AsmMethodVisit extends MethodVisitor {public AsmMethodVisit(MethodVisitor mv) {super(Opcodes.ASM4, mv); }Overridepublic void visitMethodInsn(int opcode, String owner, String name, String desc) {super.visitMethodInsn(opcode, owner, name, desc);}Overridepublic void visitCode() { //此方法在访问方法的头部时被访问到仅被访问一次//此处可插入新的指令super.visitCode();}Overridepublic void visitInsn(int opcode) { //此方法可以获取方法中每一条指令的操作类型被访问多次//如应在方法结尾处添加新指令则应判断if(opcode Opcodes.RETURN){// pushes the out field (of type PrintStream) of the System classmv.visitFieldInsn(GETSTATIC,java/lang/System,out,Ljava/io/PrintStream;);// pushes the Hello World! String constantmv.visitLdcInsn(this is a modify method!);// invokes the println method (defined in the PrintStream class)mv.visitMethodInsn(INVOKEVIRTUAL,java/io/PrintStream,println,(Ljava/lang/String;)V); // mv.visitInsn(RETURN);}super.visitInsn(opcode);}}}输出   add方法是新增的execute方法名改为execute1changeMethodContent方法修改后增加了输出this is a modify method! public static void AsmExample$Foo.changeMethodContent() public static void AsmExample$Foo.execute1() public static void AsmExample$Foo.add(java.lang.String[]) public native int java.lang.Object.hashCode() public final native java.lang.Class java.lang.Object.getClass() public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException public final void java.lang.Object.wait() throws java.lang.InterruptedException public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException public boolean java.lang.Object.equals(java.lang.Object) public java.lang.String java.lang.Object.toString() public final native void java.lang.Object.notify() public final native void java.lang.Object.notifyAll() ************* test change method this is a modify method! ************* test changed method name我们把最终的字节码保存到文件中e:\\logs\\Example.class中再用反编译工具java decompiler 查看最终的生成的源码   最终的类如下 import java.io.PrintStream;public class AsmExample$Foo {public static void execute1(){System.out.println(test changed method name);}public static void changeMethodContent() {System.out.println(test change method);System.out.println(this is a modify method!);}public static void add(String[] paramArrayOfString){System.out.println(this is add method print!);} }接下来再慢慢研究asm里面对字节码的操作还有其他框架是如果使用asm的。转载于:https://www.cnblogs.com/zhwj184/archive/2012/08/13/3027473.html
http://www.pierceye.com/news/205746/

相关文章:

  • 好的设计作品网站代理网站建设
  • 做网站的软件m开头网站建设公司问候语
  • 做网站需要工商证吗app软件开发价格
  • 做足球原创短视频网站网站建设永远在路上
  • 做seo为什么要了解网站苏州做网站公司
  • 这几年做哪些网站能致富网站开发账务处理
  • 网站的版权信息做阿里巴巴网站卖货咋样
  • 找项目去哪个网站成都哪里有做网站的公司
  • 网站推广的方法及特点国外专门做童装的网站
  • 企业网站开发模型图wordpress 侧边导航
  • 济南网站系统优化网站建设属于什么专业
  • 114啦建站程序页面效果好的网站
  • 龙华网站建设-信科网络电子商务网站建设和技术现状
  • 网站备案有效期wordpress 评论图片
  • 搭建网站需要哪些步骤wordpress 主题使用
  • 网站怎么发布做微商天眼官方网站
  • qq群网站制作异常网站服务器失去响应
  • aspnet网站开发模板紫光华宇拼音输入法官方下载
  • 东莞网站设计价格wordpress的配置dns
  • 韶关网站建设公司电子商务网站建设考试重点
  • 网站左侧 导航小红书广告投放平台
  • 资阳住房和城乡建设厅网站重庆建设网站建站
  • 网站制作厂家电话多少女生学网络工程难吗
  • 网站建设要经历哪些步骤?网站建设岗位周计划
  • 贵阳网站制作工具福步外贸论坛网首页
  • 网站大全app下载任务发布平台
  • 专业商城网站建设哪家便宜河南做外贸网站的公司
  • seo博客网站东莞网络推广运营企业
  • 定制网站建设公司哪家好嘉兴网站建设多少时间
  • 快三竞猜网站建设wordpress 整站打包