Java字节码操作

字节码操作

  • JAVA动态性的两种常见实现方式:
    1. 字节码操作
    2. 反射
  • 运行时操作字节码可以实现如下功能
    • 动态生成新的类
    • 动态改变某个类的结构(添加/删除/修改 新的属性/方法)
  • 优势:
    • 比反射开销小
    • JAVAasist性能高于反射,低于ASM

常见的字节码操作类库

  • BCEL
    • Byte Code Engineering Library(BCEL),这是Apache Softeare Foundation 的Jakarta项目的一部分。BCEL是Java classworking广泛使用的一种框架,他可以让您深入JVM汇编语言进行类操作的细节。BCEL与Javassist有不同的处理字节码的方法,BCEL在实际的JVM指令层次上进行操作(BCEL拥有丰富的JVM指令级支持)而Javassist所强调的是源代码级别的工作
  • ASM
    • 是一个轻量级java字节码操作框架,直接涉及到JVM底层的操作和指令
  • CGLIB(Code Generation Library)
    • 是一个强大的,高性能,高质量的Code生成类库,给予ASM实现
  • Javassist

    • 是一个开源的分析、编辑和创建Java字节码的类库。性能较ASM差,和cglib差不多,但是使用简单。很多开源框架都在使用他
    • 主页:http://jboss-javassist.github.io/javassist/
  • javassist的局限性

    • JDK5.0的新语法不支持(包括泛型、枚举),不支持注解修改,但是可以通过底层的javassist类解决,具体参考:javassist.bytecode.annotation
    • 不支持数组的初始化。如:String[]{"1","2"},除非数组只有一个元素
    • 不支持内部类和匿名类
    • 不支持continue和break
    • 对于复杂的继承关系不支持

Javassist的简单使用:(注意要提前导入jar包)

demo01.java

package com.coderbean.test;

import java.io.IOException;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtField;
import javassist.CtMethod;
import javassist.NotFoundException;

/**
 * 测试使用javassist生成一个新的类
 * @author chang
 *
 */
public class Demo01 {

    public static void main(String[] args) throws CannotCompileException, NotFoundException, IOException {
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.makeClass("com.coderbean.bean.Emp");

    //创建属性
    CtField f1 = CtField.make("private int empno;", cc);
    CtField f2 = CtField.make("private String ename;", cc);
    cc.addField(f1);
    cc.addField(f2);

    //创建方法
    CtMethod m1 = CtMethod.make("public int getEmpno() {return empno;}", cc);
    CtMethod m2 = CtMethod.make(" public void setEmpno(int empno) { this.empno = empno;}", cc);
    cc.addMethod(m1);
    cc.addMethod(m2);

    //添加构造器
    CtConstructor constructor = new CtConstructor(new CtClass[]{CtClass.intType,pool.get("java.lang.String")},cc);
    constructor.setBody("{this.empno = empno;this.ename = ename;}");
    cc.addConstructor(constructor);
    cc.writeFile("/home/chang/Documents/test");//将上面构造好的类写入到文件
    System.out.println("成功生成类!");
    }

}

demo02.java

package com.coderbean.test;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.NotFoundException;

/**
 * 测试javassist的API
 * @author chang
 *
 */
public class Demo02 {
    /**
     * 处理类的基本用法
     * @throws NotFoundException 
     * @throws CannotCompileException 
     * @throws IOException 
     */
    public static void test01() throws NotFoundException, IOException, CannotCompileException{
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.get("com.coderbean.test.Emp");

    byte[] bytes = cc.toBytecode();
    System.out.println(Arrays.toString(bytes));

    System.out.println(cc.getName()); //获得类名
    System.out.println(cc.getSimpleName()); //获取简要类名
    System.out.println(cc.getSuperclass()); //获得父类
    System.out.println(cc.getInterfaces()); //获得接口
    }
    /**
     * 测试产生新的方法
     * @throws NotFoundException 
     * @throws CannotCompileException 
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     * @throws SecurityException 
     * @throws NoSuchMethodException 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     */
    public static void test02() throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.get("com.coderbean.test.Emp");

//  CtMethod m = CtNewMethod.make("public int add(int a,int b){return a+b}", cc);

    CtMethod m = new CtMethod(CtClass.intType,"add",
        new CtClass[]{CtClass.intType,CtClass.intType},cc);
    m.setModifiers(Modifier.PUBLIC);
    m.setBody("{System.out.println(\"HDU\");return $1+$2;}");

    cc.addMethod(m);

    //通过反射调用新生成的方法
    Class clazz = cc.toClass();//把CtClass转化为Class
    Object obj = clazz.newInstance();//通过调用Emp无参构造器,创建新的Emp对象
    Method method =  clazz.getDeclaredMethod("add", int.class,int.class);
    Object result = method.invoke(obj, 200,300);
    System.out.println(result);
    }
    /**
     * 修改方法的内容
     * @throws NotFoundException
     * @throws CannotCompileException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws InstantiationException
     */
    public static void test03() throws NotFoundException, CannotCompileException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, InstantiationException{
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.get("com.coderbean.test.Emp");

    CtMethod cm =  cc.getDeclaredMethod("sayHello",new CtClass[]{CtClass.intType});
    cm.insertBefore("System.out.println($1);System.out.println(\"start!!\");");
    cm.insertAt(28, "int a=3; System.out.println(a);");
    cm.insertAfter("System.out.println(\"end!!\");");
    //通过反射调用新生成的方法
    Class clazz = cc.toClass();//把CtClass转化为Class
    Object obj = clazz.newInstance();//通过调用Emp无参构造器,创建新的Emp对象
    Method method =  clazz.getDeclaredMethod("sayHello", int.class);
    method.invoke(obj,300);

    }
    /**
     * 属性的操作
     * @throws NotFoundException
     * @throws CannotCompileException
     */
    public static void test04() throws NotFoundException, CannotCompileException{
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.get("com.coderbean.test.Emp");

//  CtField f1 = CtField.make("private int empno;", cc);
    CtField f1 = new CtField(CtClass.intType, "salary", cc);
    f1.setModifiers(Modifier.PRIVATE);
    cc.addField(f1, "1000");

//  cc.getDeclaredField("ename"); //获取制定的属性

    cc.addMethod(CtNewMethod.getter("getsalary", f1));
    cc.addMethod(   CtNewMethod.setter("setSalary", f1));
    }
    /**
     * 构造器方法的操作
     * @throws NotFoundException
     */
    public static void test05() throws NotFoundException{
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.get("com.coderbean.test.Emp");

    CtConstructor[] cs = cc.getConstructors();
    for(CtConstructor c:cs){
        System.out.println(c.getLongName());
    }
    }
    /**
     * 注解的操作
     * @throws NotFoundException
     * @throws ClassNotFoundException
     */
    public static void test06() throws NotFoundException, ClassNotFoundException{
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.get("com.coderbean.test.Emp");

    Object[] all = cc.getAnnotations();
    Author author = (Author) all[0];
    String name = author.name();
    int year = author.year();
    System.out.println("name:"+name+"\tyear:"+year);
    }
    public static void main(String[] args) throws NotFoundException, IOException, CannotCompileException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException {
    test06();
    }

}

你可能感兴趣的:(Java)