BYTE code in Java

Source: Internet
Author: User
Tags array constructor empty net time stub tostring window stringbuffer
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";

/*//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 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.


Contact Us

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.

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.