]]>re: 浣跨敤javassist鍔ㄦ佹敞鍏ヤ唬鐮乕鏈櫥褰昡http://www.tkk7.com/zyl/archive/2009/05/10/99171.html#269899鑿滈笩鑿滈笩Sun, 10 May 2009 07:16:00 GMThttp://www.tkk7.com/zyl/archive/2009/05/10/99171.html#269899 Note that the program above depends on the fact that the Hello class is never loaded before toClass() is invoked. If not, the JVM would load the original Hello class before toClass() requests to load the modified Hello class. Hence loading the modified Hello class would be failed (LinkageError is thrown). For example, if main() in Test is something like this:
public static void main(String[] args) throws Exception {
Hello orig = new Hello();
ClassPool cp = ClassPool.getDefault();
CtClass cc = cp.get("Hello");
:
}
then the original Hello class is loaded at the first line of main and the call to toClass() throws an exception since the class loader cannot load two different versions of the Hello class at the same time.
If the program is running on some application server such as JBoss and Tomcat, the context class loader used by toClass() might be inappropriate. In this case, you would see an unexpected ClassCastException. To avoid this exception, you must explicitly give an appropriate class loader to toClass(). For example, if bean is your session bean object, then the following code:
CtClass cc = ...;
Class c = cc.toClass(bean.getClass().getClassLoader());
would work. You should give toClass() the class loader that has loaded your program (in the above example, the class of the bean object).
toClass() is provided for convenience. If you need more complex functionality, you should write your own class loader.