Directly on the code:
Pom Increases dependency:
<Dependency> <groupId>Org.javassist</groupId> <Artifactid>Javassist</Artifactid> <version>3.18.2-ga</version></Dependency>
Write a Hello class:
Package javassisttest.javassisttest; Public class Hello { publicvoid SayHello () { System.out.println ("Hello World") ; }}
Main class:
Packagejavassisttest.javassisttest;Importjavassist. cannotcompileexception;Importjavassist. Classpool;Importjavassist. Ctclass;Importjavassist. Ctmethod;Importjavassist. Ctnewmethod;Importjavassist. notfoundexception;/*** Hello world! **/ Public classApp { Public Static voidMain (string[] args)throwsnotfoundexception, Cannotcompileexception, Instantiationexception, Illegalaccessexception, ClassNotFoundEx ception {//~ plus the error, and the class loading has a relationship, here new will load the Hello class, then load the words will be error//Hello hell=new hello (); //Hell.sayhello (); //~ Ctclass Ctclass= Classpool.getdefault (). Getctclass ("JavassistTest.JavassistTest.Hello"); Ctmethod Ctmethod= Ctclass.getdeclaredmethod ("SayHello"); Ctmethod.setbody ("System.out.println (\" Hi\ ");); Class CH=Ctclass.toclass (); Hello h=(Hello) ch.newinstance (); H.sayhello (); Hello Hel=NewHello (); Hel.sayhello ();
}}
Output Result:
Hi
Hi
As a result, the description is class modified.
Javassist First Contact