Javassist操作方法总结

摘要:
//Classclazz=cc.toClass();这里可以看出,Javassist的加载是依靠ClassPool类,输出方式支持三种。这样做可以减少javassist的内存消耗。Javassist提供了四种动态加载classpath的方法。为了避免这个异常,javassist提供几种方法,一种是在上面提到的ClassPool.doPruning这个参数,还有一种方法是调用CtClass.detach()方法,可以把CtClassobject从ClassPool中移除。

CSDN参考
Javassist tutorial

1、读取和输出字节码

ClassPool pool =ClassPool.getDefault();

//会从classpath中查询该类
CtClass cc = pool.get("test.Rectangle");

//设置.Rectangle的父类
cc.setSuperclass(pool.get("test.Point"));

//输出.Rectangle.class文件到该目录中
cc.writeFile("c://");

//输出成二进制格式

//byte[] b=cc.toBytecode();

//输出并加载class 类,默认加载到当前线程的ClassLoader中,也可以选择输出的ClassLoader。

//Class clazz=cc.toClass();

这里可以看出,Javassist的加载是依靠ClassPool类,输出方式支持三种。

2、新增Class

ClassPool pool =ClassPool.getDefault();

CtClass cc = pool.makeClass("Point");

//新增方法
cc.addMethod(m);

//新增Field
cc.addField(f);

从上面可以看出,对Class的修改主要是依赖于CtClass 类。API也比较清楚和简单。

3、冻结Class

当CtClass 调用writeFile()、toClass()、toBytecode() 这些方法的时候,Javassist会冻结CtClass Object,对CtClass object的修改将不允许。这个主要是为了警告开发者该类已经被加载,而JVM是不允许重新加载该类的。如果要突破该限制,方法如下:

CtClasss cc =...;

    :

cc.writeFile();

cc.defrost();

cc.setSuperclass(...);    //OK since the class is not frozen.

当 ClassPool.doPruning=true的时 候,Javassist 在CtClass object被冻结时,会释放存储在ClassPool对应的数据。这样做可以减少javassist的内存消耗。默认情况 ClassPool.doPruning=false。例如

CtClasss cc =...;

cc.stopPruning(true);

    :

cc.writeFile();                             //convert to a class file.

//cc没有被释放

提示:当调试时,可以调用debugWriteFile(),该方法不会导致CtClass被释放。

4、Class 搜索路径

从上面可以看出Class 的载入是依靠ClassPool,而ClassPool.getDefault() 方法的搜索Classpath 只是搜索JVM的同路径下的class。当一个程序运行在JBoss或者Tomcat下,ClassPool Object 可能找到用户的classes。Javassist 提供了四种动态加载classpath的方法。如下

//默认加载方式如pool.insertClassPath(new ClassClassPath(this.getClass()));
ClassPool pool =ClassPool.getDefault();


//从file加载classpath
pool.insertClassPath("/usr/local/javalib")


//从URL中加载
ClassPath cp = new URLClassPath("www.javassist.org", 80, "/java/", "org.javassist.");

pool.insertClassPath(cp);

//从byte[] 中加载

byte[] b = a bytearray;

String name = classname;

cp.insertClassPath(newByteArrayClassPath(name, b));


//可以从输入流中加载class
InputStream ins = an input stream for reading a classfile;

CtClass cc = cp.makeClass(ins);

5、ClassPool

5.1 减少内存溢出

ClassPool是一个CtClass objects的装载容器。当加载了CtClass object后,是不会被ClassPool释放的(默认情况下)。这个是因为CtClass object 有可能在下个阶段会被用到。

当加载过多的CtClass object的时候,会造成OutOfMemory的异常。为了避免这个异常,javassist提供几种方法,一种是在上面提到 的 ClassPool.doPruning这个参数,还有一种方法是调用 CtClass.detach()方法,可以把CtClass object 从ClassPool中移除。例如:

CtClass cc =... ;

cc.writeFile();

cc.detach();

另外一中方法是不用默认的ClassPool即不用 ClassPool.getDefault()这个方式来生成。这样当ClassPool 没被引用的时候,JVM的垃圾收集会收集该类。例如

//ClassPool(true) 会默认加载Jvm的ClassPath
ClassPool cp = new ClassPool(true);

//if needed, append an extra search path by appendClassPath()

5.2 级联ClassPools

javassist支持级联的ClassPool,即类似于继承。例如: 

ClassPool parent =ClassPool.getDefault();

ClassPool child = newClassPool(parent);

child.insertClassPath("./classes");

5.3 修改已有Class的name以创建一个新的Class

当调用setName方法时,会直接修改已有的Class的类名,如果再次使用旧的类名,则会重新在classpath路径下加载。例如:

ClassPool pool =ClassPool.getDefault();

CtClass cc = pool.get("Point");

cc.setName("Pair");

//重新在classpath加载
CtClass cc1 = pool.get("Point");

对于一个被冻结(Frozen)的CtClass object ,是不可以修改class name的,如果需要修改,则可以重新加载,例如:

ClassPool pool =ClassPool.getDefault();

CtClass cc = pool.get("Point");

cc.writeFile();

//cc.setName("Pair");    wrong since writeFile() has been called.
CtClass cc2 = pool.getAndRename("Point", "Pair");

6、Class loader

上面也提到,javassist同个Class是不能在同个ClassLoader中加载两次的。所以在输出CtClass的时候需要注意下,例如:

//当Hello未加载的时候,下面是可以运行的。
ClassPool cp =ClassPool.getDefault();

CtClass cc = cp.get("Hello");

Class c =cc.toClass();

 

//下面这种情况,由于Hello2已加载,所以会出错
Hello2 h=newHello2();

CtClass cc2 = cp.get("Hello2");

Class c2 = cc.toClass();//这里会抛出java.lang.LinkageError 异常

 

//解决加载问题,可以指定一个未加载的ClassLoader
Class c3 = cc.toClass(new MyClassLoader());

6.1 使用javassist.Loader

从上面可以看到,如果在同一个ClassLoader加载两次Class抛出异常,为了方便javassist也提供一个Classloader供使用,例如

 ClassPool pool =ClassPool.getDefault();

 Loader cl = newLoader(pool);

 CtClass ct = pool.get("test.Rectangle");

ct.setSuperclass(pool.get("test.Point"));

Class c = cl.loadClass("test.Rectangle");

Object rect =c.newInstance();

         :

为了方便监听Javassist自带的ClassLoader的生命周期,javassist也提供了一个listener,可以监听ClassLoader的生命周期,例如:

//Translator 为监听器

public class MyTranslator implementsTranslator {

    voidstart(ClassPool pool)

        throwsNotFoundException, CannotCompileException {}

    voidonLoad(ClassPool pool, String classname)

        throwsNotFoundException, CannotCompileException

    {

        CtClass cc =pool.get(classname);

        cc.setModifiers(Modifier.PUBLIC);

    }

}

//示例

public classMain2 {

  public static void main(String[] args) throwsThrowable {

     Translator t = newMyTranslator();

     ClassPool pool =ClassPool.getDefault();

     Loader cl = newLoader();

     cl.addTranslator(pool, t);

     cl.run("MyApp", args);

  }

}

//输出

% java Main2 arg1 arg2...

6.2 修改系统Class

由JVM规范可知,system classloader 是比其他classloader 是优先加载的,而system classloader 主要是加载系统Class,所以要修改系统Class,如果默认参数运行程序是不可能修改的。如果需要修改也有一些办法,即在运 行时加入-Xbootclasspath/p: 参数的意义可以参考其他文件。下面修改String的例子如下:

ClassPool pool =ClassPool.getDefault();

CtClass cc = pool.get("java.lang.String");

CtField f = new CtField(CtClass.intType, "hiddenValue", cc);

f.setModifiers(Modifier.PUBLIC);

cc.addField(f);

cc.writeFile(".");

//运行脚本

% java -Xbootclasspath/p:. MyApp arg1 arg2...

6.3 动态重载Class

如果JVM运行时开启JPDA(Java Platform Debugger Architecture),则Class是运行被动态重新载入的。具体方式可以参考java.lang.Instrument。javassist也提 供了一个运行期重载Class的方法,具体可以看API 中的javassist.tools.HotSwapper。

7、Introspection和定制

javassist封装了很多很方便的方法以供使用,大部分使用只需要用这些API即可,如果不能满足,Javassist也提供了一个低层的API(具体参考javassist.bytecode 包)来修改原始的Class。

7.1 插入source 文本在方法体前或者后

CtMethod 和CtConstructor 提供了 insertBefore()、insertAfter()和 addCatch()方法,它们可以插入一个souce文本到存在的方法的相应的位置。javassist 包含了一个简单的编译器解析这souce文本成二进制插入到相应的方法体里。

javassist 还支持插入一个代码段到指定的行数,前提是该行数需要在class 文件里含有。

插入的source 可以关联fields 和methods,也可以关联方法的参数。但是关联方法参数的时,需要在程序编译时加上 -g 选项(该选项可以把本地变量的声明保存在class 文件中,默认是不加这个参数的。)。因为默认一般不加这个参数,所以Javassist也提供了一些特殊的变量来代表方法参 数:$1,$2,$args...要注意的是,插入的source文本中不能引用方法本地变量的声明,但是可以允许声明一个新的方法本地变量,除非在程序 编译时加入-g选项。

方法的特殊变量说明:

$0, $1, $2, ...    thisand actual parameters

$args    An array of parameters. The type of $args is Object[].

$$    All actual parameters.For example, m($$) is equivalent to m($1,$2,...)

$cflow(...)    cflow variable

$r    The result type. It is used in a cast expression.

$w    The wrapper type. It is used in a cast expression.

$_    The resulting value

$sig    An array of java.lang.Class objects representing the formal parameter types

$type    A java.lang.Class object representing the formal result type.

$class    A java.lang.Class object representing the class currently edited.

7.1.1 $0, $1, $2, ...

$0代表的是this,$1代表方法参数的第一个参数、$2代表方法参数的第二个参数,以此类推,$N代表是方法参数的第N个。例如:

    //实际方法

    void move(int dx, intdy)

    //javassist
CtMethod m = cc.getDeclaredMethod("move");

    //打印dx,和dy
m.insertBefore("{ System.out.println($1); System.out.println($2); }");

注意:如果javassist改变了$1的值,那实际参数值也会改变。

7.1.2 $args

$args 指的是方法所有参数的数组,类似Object[],如果参数中含有基本类型,则会转成其包装类型。需要注意的时候,$args[0]对应的是$1,而不是$0,$0!=$args[0],$0=this。

7.1.3 $$

$$是所有方法参数的简写,主要用在方法调用上。例如:

//原方法move(String a,String b)

move($$) 相当于move($1,$2)

如果新增一个方法,方法含有move的所有参数,则可以这些写:

exMove($$, context) 相当于 exMove($1, $2, context)

7.1.4 $cflow

$cflow意思为控制流(control flow),是一个只读的变量,值为一个方法调用的深度。例如:

//原方法

int fact(intn) {

    if (n <= 1)

        returnn;

    else

        return n * fact(n - 1);

}

 

//javassist调用
CtMethod cm =...;

//这里代表使用了cflow
cm.useCflow("fact");

//这里用了cflow,说明当深度为0的时候,就是开始当第一次调用fact的方法的时候,打印方法的第一个参数
cm.insertBefore("if ($cflow(fact) == 0)" + "    System.out.println("fact " + $1);");

7.1.5 $r

指的是方法返回值的类型,主要用在类型的转型上。例如:

Object result =... ;

$_ = ($r)result;

如果返回值为基本类型的包装类型,则该值会自动转成基本类型,如返回值为Integer,则$r为int。如果返回值为void,则该值为null。

7.1.6 $w

$w代表一个包装类型。主要用在转型上。比如:Integer i = ($w)5; 如果该类型不是基本类型,则会忽略。

7.1.7 $_

$_代表的是方法的返回值。

7.1.8 $sig

$sig指的是方法参数的类型(Class)数组,数组的顺序为参数的顺序。

7.1.9 $class

$class 指的是this的类型(Class)。也就是$0的类型。

7.1.10 addCatch()

addCatch() 指的是在方法中加入try catch 块,需要注意的是,必须在插入的代码中,加入return 值$e代表 异常值。比如:

CtMethod m =...;

CtClass etype = ClassPool.getDefault().get("java.io.IOException");

m.addCatch("{ System.out.println($e); throw $e; }", etype);

实际代码如下:

try{

    the original method body

}

catch(java.io.IOException e) {

    System.out.println(e);

    throwe;

}

8、修改方法体

CtMethod 和CtConstructor 提供了 setBody() 的方法,可以替换方法或者构造函数里的所有内容。

支持的变量有:

$0, $1, $2, ...    thisand actual parameters

$args    An array of parameters. The type of $args is Object[].

$$    All actual parameters.For example, m($$) is equivalent to m($1,$2,...)

$cflow(...)    cflow variable

$r    The result type. It is used in a cast expression.

$w    The wrapper type. It is used in a cast expression.

$sig    An array of java.lang.Class objects representing the formal parameter types

$type    A java.lang.Class object representing the formal result type.

$class    A java.lang.Class object representing the class currently edited.

注意 $_变量不支持。

8.1 替换方法中存在的source

javassist 允许修改方法里的其中一个表达式。 javassist.expr.ExprEditor 这个class 可以替换该表达式。例如:

CtMethod cm =... ;

cm.instrument(

    newExprEditor() {

        public voidedit(MethodCall m)

                      throwsCannotCompileException

        {

            if (m.getClassName().equals("Point")

                          && m.getMethodName().equals("move"))

                m.replace("{ $1 = 0; $_ = $proceed($$); }");

        }

    });

注意: that the substituted code is not an expression but a statement or a block. It cannot be or contain a try-catch statement.

方法instrument() 可以用来搜索方法体里的内容。比如调用一个方法,field访问,对象创建等。如果你想在某个表达式前后插入方法,则修改的souce如下:

{

   before-statements;

    $_ =$proceed($$);

    after-statements;

}

8.2 javassist.expr.MethodCall

MethodCall代表的是一个方法的调用。用replace()方法可以对调用的方法进行替换。

$0The target object of the method call.

This is not equivalent to this, which represents the caller-side thisobject.

$0 is null if the method is static.

$1, $2, ...    The parameters of the method call.

$_    The resulting value of the method call.

$r    The result type of the method call.

$class    A java.lang.Class object representing the classdeclaring the method.

$sig    An array of java.lang.Class objects representing the formal parameter types

$type    A java.lang.Class object representing the formal result type.

$proceed    The name of the method originally called in the expression.

注意:$w, $args 和 $$也是允许的。$0不是this,是只调用方法的Object。$proceed指的是一个特殊的语法,而不是一个String。

8.3 javassist.expr.ConstructorCall

ConstructorCall 指的是一个构造函数,比如:this()、super()的调用。ConstructorCall.replace()是用来用替换一个块当调用构造方法的时候。

$0    The target object of the constructor call. This is equivalent to this.

$1, $2, ...    The parameters of the constructor call.

$class    A java.lang.Class object representing the classdeclaring the constructor.

$sig    An array of java.lang.Class objects representing the formal parameter types.

$proceed    The name of the constructor originally called in the expression.

$w, $args 和 $$ 也是允许的。

8.4 javassist.expr.FieldAccess

FieldAccess代表的是Field的访问类。

$0    The object containing the field accessed by the expression. This is not equivalent to this.

thisrepresents the object that the method including the expression is invoked on.

$0 is null if the field is static.

$1    The value that would be stored in the field ifthe expression is write access.

Otherwise, $1is not available.

$_    The resulting value of the field access ifthe expression is read access.

Otherwise, the value stored in $_ is discarded.

$r    The type of the field ifthe expression is read access.

Otherwise, $r is void.

$class    A java.lang.Class object representing the classdeclaring the field.

$type    A java.lang.Class object representing the field type.

$proceed    The name of a virtual method executing the original field access. .

$w, $args 和 $$ 也是允许的。

8.5 javassist.expr.NewExpr

NewExpr代表的是一个Object 的操作(但不包括数组的创建)。

$0    null
$1, $2, ...    The parameters to the constructor.

$_    The resulting value of the object creation.

A newly created object must be stored in thisvariable.

$r    The type of the created object.

$sig    An array of java.lang.Class objects representing the formal parameter types

$type    A java.lang.Class object representing the classof the created object.

$proceed    The name of a virtual method executing the original object creation. 

$w, $args 和 $$ 也是允许的。

8.6 javassist.expr.NewArray

NewArray 代表的是数组的创建。

$0    null
$1, $2, ...    The size of each dimension.

$_    The resulting value of the object creation.

A newly created array must be stored in thisvariable.

$r    The type of the created object.

$type    A java.lang.Class object representing the classof the created array .

$proceed    The name of a virtual method executing the original array creation. .

$w, $args 和 $$ 也是允许的。

例如:

String[][] s = new String[3][4];

$1 和 $2 的值为 3 和 4, $3 得不到的.

String[][] s = new String[3][];

$1 的值是 3 ,但 $2 得不到的.

8.7 javassist.expr.Instanceof

Instanceof 代表的是Instanceof 表达式。

$0    null
$1    The value on the left hand side of the original instanceofoperator.

$_    The resulting value of the expression. The type of $_ is boolean.

$r    The type on the right hand side of the instanceofoperator.

$type    A java.lang.Class object representing the type on the right hand side of the instanceofoperator.

$proceed    The name of a virtual method executing the original instanceofexpression.

It takes one parameter (the type is java.lang.Object) and returns true

ifthe parameter value is an instance of the type on the right hand side of

the original instanceof operator. Otherwise, it returns false.

$w, $args 和 $$ 也是允许的。

8.8 javassist.expr.Cast

Cast 代表的是一个转型表达式。

$0    null
$1The value the type of which is explicitly cast.

$_    The resulting value of the expression. The type of $_ is the same as the type

after the explicit casting, that is, the type surrounded by ( ).

$r    the type after the explicit casting, or the type surrounded by ( ).

$type    A java.lang.Class object representing the same type as $r.

$proceed    The name of a virtual method executing the original type casting.

It takes one parameter of the type java.lang.Object and returns it after

the explicit type casting specified by the original expression.

$w, $args 和 $$ 也是允许的。

8.9 javassist.expr.Handler

Handler 代表的是一个try catch 声明。

$1    The exception object caught by the catchclause.

$r    the type of the exception caught by the catchclause. It is used in a cast expression.

$w    The wrapper type. It is used in a cast expression.

$type    A java.lang.Class object representing the type of the exception caught by the catch clause.

9 新增一个方法或者field

Javassist 允许开发者创建一个新的方法或者构造方法。新增一个方法,例如:

CtClass point = ClassPool.getDefault().get("Point");

CtMethod m = CtNewMethod.make("public int xmove(int dx) { x +=dx; }",point);

point.addMethod(m);

在方法中调用其他方法,例如:

CtClass point = ClassPool.getDefault().get("Point");

CtMethod m = CtNewMethod.make("public int ymove(int dy) { $proceed(0, dy); }", point, "this", "move");

其效果如下:

publicint ymove(int dy) { this.move(0, dy); }

下面是javassist提供另一种新增一个方法(未看明白):

Javassist provides another way to add a new method. You can first create an abstract method and later give it a method body:

CtClass cc =... ;

CtMethod m = new CtMethod(CtClass.intType, "move",newCtClass[] { CtClass.intType }, cc);

cc.addMethod(m);

m.setBody("{ x += $1; }");

cc.setModifiers(cc.getModifiers() & ~Modifier.ABSTRACT);

Since Javassist makes a class abstract if an abstract method is added to the class, you have to explicitly change the class back to a non-abstract one after calling setBody().

9.1 递归方法

CtClass cc =... ;

CtMethod m = CtNewMethod.make("public abstract int m(int i);", cc);

CtMethod n = CtNewMethod.make("public abstract int n(int i);", cc);

cc.addMethod(m);

cc.addMethod(n);

m.setBody("{ return ($1 <= 0) ? 1 : (n($1 - 1) * $1); }");

n.setBody("{ return m($1); }");

cc.setModifiers(cc.getModifiers() & ~Modifier.ABSTRACT);

9.2 新增field

如下:

CtClass point = ClassPool.getDefault().get("Point");

CtField f = new CtField(CtClass.intType, "z", point);

point.addField(f);

//point.addField(f, "0");    //initial value is 0.

或者:

CtClass point = ClassPool.getDefault().get("Point");

CtField f = CtField.make("public int z = 0;", point);

point.addField(f);

9.3 移除方法或者field

调用removeField()或者removeMethod()。

10 注解

获取注解信息:

//注解

public @interfaceAuthor {

    String name();

    intyear();

}

//javassist代码
CtClass cc = ClassPool.getDefault().get("Point");

Object[] all =cc.getAnnotations();

Author a = (Author)all[0];

String name =a.name();

int year =a.year();

System.out.println("name: " + name + ", year: " + year);

11 javassist.runtime

12 import

引用包:

ClassPool pool =ClassPool.getDefault();

pool.importPackage("java.awt");

CtClass cc = pool.makeClass("Test");

CtField f = CtField.make("public Point p;", cc);

cc.addField(f);

13 限制

(1)不支持java5.0的新增语法。不支持注解修改,但可以通过底层的javassist类来解决,具体参考:javassist.bytecode.annotation

(2)不支持数组的初始化,如String[]{"1","2"},除非只有数组的容量为1

(3)不支持内部类和匿名类

(4)不支持continue和btreak 表达式。

(5)对于继承关系,有些不支持。例如

classA {}

class B extendsA {}

class C extendsB {}

 

classX {

    voidfoo(A a) { .. }

    voidfoo(B b) { .. }

}

如果调用 x.foo(new C()),可能会调用foo(A) 。

(6)推荐开发者用#分隔一个class name和static method或者 static field。例如:

javassist.CtClass.intType.getName()推荐用javassist.CtClass#intType.getName()

13 底层API

14 debug

可以设置一个文件夹,javassist生成的class会保存在该文件夹下面。例如:CtClass.debugDump = "./dump"; 默认debugDump=null.

免责声明:文章转载自《Javassist操作方法总结》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Android studio初见及结构分析移动端三合一瀑布流插件(原生JS)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

c#下怎么判断一个字符串是否可以转换为double类型

using System.Text.RegularExpressions; //引入Regex命名空间bool a = Regex.IsMatch(string, @"^[+-]?d*[.]?d*$"); //这个方法会返回一个布尔值,如果string字符串可以转换为double,则返回True,反之为False。bool b = Regex.IsMatc...

使用RemObjects Pascal Script (转)

http://www.cnblogs.com/MaxWoods/p/3304954.html 摘自RemObjects Wiki 本文提供RemObjects Pascal Script的整体概要并演示如何创建一些简单的脚本. Pascal Script包括两个不同部分: 编译器 (uPSCompiler.pas) 运行时 (uPSRuntime.pas...

java获取本机ip,mac,

1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 5 /** 6 * 获取MAC地址 7 * @author sunlightcs 8...

在.net中读写config文件的各种方法

阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CDATA config文件 - Collection config文件 - 读与写 读写 .net framework中已经定义的节点 xml配置文件 xml配置文件 - CDATA xml文件读写...

Java 使用stringTemplate导出大批量数据excel(百万级)

目前java框架中能够生成excel文件的的确不少,但是,能够生成大数据量的excel框架,我倒是没发现,一般数据量大了都会出现内存溢出,所以,生成大数据量的excel文件要返璞归真,用java的基础技术,IO流来实现。 如果想用IO流来生成excel文件,必须要知道excel的文件格式内容,相当于生成html文件一样,用字符串拼接html标签保存到文本文...

关于Android中的三级缓存

三级缓存的提出就是为了提升用户体验。当我们第一次打开应用获取图片时,先到网络去下载图片,然后依次存入内存缓存,磁盘缓存,当我们再一次需要用到刚才下载的这张图片时,就不需要再重复的到网络上去下载,直接可以从内存缓存和磁盘缓存中找,由于内存缓存速度较快,我们优先到内存缓存中寻找该图片,如果找到则运用,如果没有找到(内存缓存大小有限),那么我们再到磁盘缓存中去找...