java中的位元組碼

來源:互聯網
上載者:User
這段時間一直在看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是有很大區別的。位元組碼的修改還有很多方法和作用,大家可以一起研究一下。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.