这段时间一直在看java,看到讲 reflection,classloader,javassist 的地方,很兴奋,因为以前用.net的时候接触过一点点reflection,但我再看到classloader的时候,简直觉得非常神奇,可是神奇的东西一般都很难懂,看了很多资料,都不太明白,直到在ibm的网站上看到《java编程的动态性》,很详细的阐述了classloader的作用,接着我看了使用javassist修改字节码的地方,跃跃欲试,动手做了一个小小的例子,朋友说我误入歧途,刚刚接触java就看这个东西,呵呵,不多说,看看代码就知道了。
addmethod.java
/*
* created on 2004-9-24
*
* todo to change the template for this generated file go to
* window – preferences – java – code style – code templates
*/
package cn.cpx.parco.addmethod;
import java.lang.reflect.*;
import javassist.*;
/**
* @author cpx.parco
*
* todo to change the template for this generated type comment go to
* window – preferences – java – code style – code templates
*/
public class addmethod {
/**
*
*/
public addmethod() {
super();
// todo auto-generated constructor stub
}
private static void addmethod(ctclass clas)
{
try
{
string stroldmethod = "outputhello";
string strnewmethod = stroldmethod + "2";
/* //add new method (copy)
* stringbuffer newmethodbody = new stringbuffer();
* ctmethod oldmethod = clas.getdeclaredmethod(stroldmethod);
* ctmethod newmethod = ctnewmethod.copy(oldmethod, strnewmethod, clas, null);
* newmethodbody.append("{system.out.println(\"hello\");}");
* newmethod.setbody(newmethodbody.tostring());
* clas.addmethod(newmethod);
*/
//add new method (create)
ctclass []arrclas = new ctclass[0];
ctclass []arrexce = new ctclass[0];
stringbuffer newmethodbody = new stringbuffer();
newmethodbody.append("{system.out.println(\"hello\");}");
ctmethod newmethod = ctnewmethod.make(ctclass.voidtype ,strnewmethod, arrclas, arrexce, newmethodbody.tostring(), clas);
clas.addmethod(newmethod);
//invoke new method
class tmpclass = clas.toclass();
object obj = tmpclass.newinstance();
method method = tmpclass.getdeclaredmethod(strnewmethod, null);
method.invoke(obj, null);
}
catch(exception e)
{
e.printstacktrace();
}
}
public static void main(string[] args) {
try
{
ctclass clas = classpool.getdefault().get("cn.cpx.parco.addmethod.sayhello");
addmethod(clas);
}
catch(notfoundexception e)
{
e.printstacktrace();
}
}
}
sayhello.java
/*
* created on 2004-9-24
*
* todo to change the template for this generated file go to
* window – preferences – java – code style – code templates
*/
package cn.cpx.parco.addmethod;
/**
* @author cpx.parco
*
* todo to change the template for this generated type comment go to
* window – preferences – java – code style – code templates
*/
public class sayhello {
/**
*
*/
public sayhello() {
super();
// todo auto-generated constructor stub
}
public void outputhello()
{
}
}
一开始的时候因为不知道用那个make方法,所以在sayhello里面放了一个空的outputhello,目的就是生成新的ctmethod时候使用的,查过文档后使用了make方法,使用make方法,可以不用依靠以前的outputhello方法,写得过程中,因为c++的影响,我把新的method的参数列表用了一个包含一个void.class的数组代替,结果不能通过编译,还有异常列表,是一个ctclass的数组,不能直接把exception放进去,估计需要继承一下ctclass。这两个地方我曾经都用null做参数尝试过,可是都不能正常运行,可见java中的空数组和null是有很大区别的。字节码的修改还有很多方法和作用,大家可以一起研究一下。
