This period of time has been looking at Java, to see the place to speak Reflection,classloader,javassist, very excited, Because before using. NET time contact a little bit of reflection, but I see ClassLoader time, simply feel very magical, but the magic things are generally difficult to understand, read a lot of information, do not understand, until on the IBM website to see the dynamic of Java programming, Elaborated in detail the role of ClassLoader, and then I saw the use of javassist to modify the bytecode of the place, eager to do a small example, a friend said I went astray, just contact Java to see this thing, oh, not to say, look at the code to know.
Addmethod.java
/*
* Created on 2004-9-24
*
* TODO to change the template of this generated file go
* 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 of this generated type comment go
* 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";
}
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 of this generated file go
* Window-preferences-java-code Style-code Templates
*/
Package Cn.cpX.Parco.AddMethod;
/**
* @author Cpx.parco
*
* TODO to change the template of this generated type comment go
* Window-preferences-java-code Style-code Templates
*/
public class SayHello {
/**
*
*/
Public SayHello () {
Super ();
TODO auto-generated Constructor stub
}
public void Outputhello ()
{
}
}
In the beginning, because I didn't know how to use the Make method, I put an empty outputhello inside the SayHello to generate a new ctmethod, use the Make method after checking the document, use the Make method, Can not rely on the previous Outputhello method, written in the process, because of the impact of C + +, I took the new method's argument list with a void.class array instead, the result is not compiled, and the exception list, is a ctclass array, can not directly put exception into, estimated need to inherit ctclass. I've tried this two places with null, but none of them work, and there's a big difference between empty arrays and Null in Java. Bytecode modification There are many ways and effects, we can study together.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.