Bytecode in Java

xiaoxiao2021-03-06  57

During this time, I have been watching Java. I saw the place to say reflection, ClassLoader, Javassist. I was very excited. Magical things are generally hard to understand, I have read a lot of information, I don't quite understand until I IBM's website sees "Java programming dynamics", I have explained the role of ClassLoader, then I read Javassist. The place to modify the bytecode, eager to try, do a small example, friend said that I am going to get into my discrimination, just in Java, I will see this thing, huh, don't say, look at the code.

AddMethod.java

/ *

* CREATED ON 2004-9-24

*

* Todo to change the Template for this generated file go to to THERATED 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 To To To To To To 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.toTostring (), 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 to THERATED 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 To To To To To To TO

* WINDOW - Preferences - Java - Code Style - Code Templates

* /

public

Class

Sayhello {

/ *

*

*

* /

public

Sayhello () {

Super ();

//

Todo Auto-Generated Constructor Stub

}

public

Void

Outputhello ()

{

}

}

At the beginning, I didn't know that the Make method, so I put an empty OutputHello in Sayhello. The purpose is to generate new CTMethod, and use the Make method after checking the document. Using the Make method, you can use it before OutputHello method, because of the influence of C , I use a list of new Method's parameters with an array instead of a void.class, and the result cannot be compiled, and there is an abnormal list, which is an array of CTCLASS. You can't put Exception directly, and it is estimated that you need to inherit CTCLASS. These two places I used to do parameters with NULL, but I can't run normally. It can be seen that the empty array and null in Java are very different. There are also many methods and functions in the modification of the bytecode. You can study together.

转载请注明原文地址:https://www.9cbs.com/read-113380.html

New Post(0)