asm[dynamically produce a class and use it without writing it to disk]
来源:互联网 发布:网络的使用界限 编辑:程序博客网 时间:2024/05/01 13:29
public class Chocalate { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("helloworld"); }}
It is basically a two-step task.
First step: use the bytecode plugin to view what asm code should be produced for such java code.
Second step, package the asm code and run them using the classloader:
add these code to the bottom of the dump() method. Dump returns the bytecode sequence for a class, it can be directly written into a class file using the FileOutputStream.
Also, we can directly use the class without storing it to the disk as follows.
byte[] code =cw.toByteArray();;ChocalateDump loader = new ChocalateDump();// the class extends the Classloader, so that we can use the "protected" defineClass method to load the class just generated.Class<?> exampleClass = loader.defineClass("Chocalate", code, 0, code.length);// load the classexampleClass.getMethods()[0].invoke(null, new Object[] { null });// this uses the reflection to invoke the main method.
That is all, the console will print "helloworld" as what the original Chocalate class did.
From the above process, we know we can generate asm code even without understanding the very details inside. All we need to is write an equivalent java code and ask plugin for the proper asm invocations.
For completeness, I also attach the ChocalateDump, i.e., the class containing the ASM invocations.
import java.util.*;import org.objectweb.asm.*;import org.objectweb.asm.attrs.*;public class ChocalateDump extends ClassLoader implements Opcodes { public static void main(final String args[]) throws Exception { dump(); }public static byte[] dump () throws Exception {ClassWriter cw = new ClassWriter(0);FieldVisitor fv;MethodVisitor mv;AnnotationVisitor av0;cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "Chocalate", null, "java/lang/Object", null);cw.visitSource("Chocalate.java", null);{mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);mv.visitCode();Label l0 = new Label();mv.visitLabel(l0);mv.visitLineNumber(2, l0);mv.visitVarInsn(ALOAD, 0);mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");mv.visitInsn(RETURN);Label l1 = new Label();mv.visitLabel(l1);mv.visitLocalVariable("this", "LChocalate;", null, l0, l1, 0);mv.visitMaxs(1, 1);mv.visitEnd();}{mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);mv.visitCode();Label l0 = new Label();mv.visitLabel(l0);mv.visitLineNumber(9, l0);mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");mv.visitLdcInsn("helloworld");mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");Label l1 = new Label();mv.visitLabel(l1);mv.visitLineNumber(10, l1);mv.visitInsn(RETURN);Label l2 = new Label();mv.visitLabel(l2);mv.visitLocalVariable("args", "[Ljava/lang/String;", null, l0, l2, 0);mv.visitMaxs(2, 1);mv.visitEnd();}cw.visitEnd();byte[] code =cw.toByteArray();;ChocalateDump loader = new ChocalateDump();Class<?> exampleClass = loader.defineClass("Chocalate", code, 0, code.length);exampleClass.getMethods()[0].invoke(null, new Object[] { null });return cw.toByteArray();}}
- asm[dynamically produce a class and use it without writing it to disk]
- GhostWriting: Writing to another process without opening it nor actually writing to it
- GhostWriting: Writing to another process without opening it nor actually writing to it
- Chinese version QQ—how to install it and use it to make a audio call and video call
- WHAT IS ISO? A CAMERA’S SENSITIVITY TO LIGHT EXPLAINED, AND HOW TO USE IT
- how to save a c++ object in java object and use it
- How to create a DLL library in C and then use it with C#
- How to build & install GLFW 3 and use it in a Linux project
- Why to use memory pool and how to implement it
- What is JNI Graphics and how to use it?
- What is reflection and how to use it in CSharp ?
- how to make Completion Handlers and use it in Swift
- What is a cronjob and how do I use it?
- What is SynchronizationContext, and what does it mean to capture and use it?
- Add a disk to ASM diskgroup.
- Introduction to COM - What It Is and How to Use It.(ZT)
- Introduction to COM - What It Is and How to Use It.
- Introduction to COM - What It Is and How to Use It.
- 字符串与数组的转换
- 一个合格程序员每天每月每年该做的事
- qwtplot3d 中的坐标系统
- google-perftools 简介
- MYSQL中出现data too long for column的解决方案
- asm[dynamically produce a class and use it without writing it to disk]
- μC/OS-II 移植笔记 1(FreeScale 68HCS12 核单片机)
- SQL语句查漏总结
- Android网络编程之Http通信
- 301/302重定向,网址劫持
- 利用Jsoup解析HTML
- 微机基础知识
- C#命名规范
- Label 字体颜色