java - Parse classes from jar using javassist -



java - Parse classes from jar using javassist -

i'm noob in javassist. can give sample how load classes jar , save them using javassist?

jar = new jarfile(filename); enumeration<jarentry> entries = jar.entries(); while (entries.hasmoreelements()) { jarentry jarentry = (jarentry) entries.nextelement(); if(jarentry == null) break; if(jarentry.getname().endswith(".class")) { // ?????? } else { resources.add(new rresource(jarentry.getname(), jar.getinputstream(jarentry))); }

you can load bytes respective class within jar file via code below:

jarfile jarfile = new jarfile(file); // lets reference .class-file contained in jar zipentry zipentry = jarfile.getentry(classname.replace(".", "/")+".class"); if (zipentry == null) { jarfile.close(); homecoming null; } // our valid reference, able bytes out of jar-archive inputstream fis = jarfile.getinputstream(zipentry); byte[] classbytes = new byte[fis.available()]; fis.read(classbytes);

to load bytes in javassist can next stuff:

classpool cp = classpool.getdefault(); cp.insertclasspath(new classclasspath(this.getclass())); classpath cp1 = null; classpath cp2 = null; // add together jar file classpath seek { cp1 = cp.insertclasspath(jarfile.getabsolutepath()); } grab (notfoundexception e1) { e1.printstacktrace(); homecoming null; } // add together class file going modify classpath cp2 = cp.appendclasspath(new bytearrayclasspath(classname, classbytes)); byte[] modifiedbytes; seek { ctclass cc = cp.get(classname); // skip instrumentation if class frozen , hence // can't modified if (!cc.isfrozen()) { // javassist stuff here } modifiedbytes = cc.tobytecode(); } grab (notfoundexception | ioexception | cannotcompileexception | classnotfoundexception e) { handleexception(e); } { // free locked resource files cp.removeclasspath(cp1); cp.removeclasspath(cp2); } // write modified bytes somewhere if (modifiedbytes.length > 0) { try(fileoutputstream fos = new fileoutputstream("pathname")) { fos.write(modifiedbytes); } }

maybe of code can reduced, how load bytes jar file , load them javassist. jar file loaded javassist classpath due eventual dependencies. class instrument javassist needed added classpath reason.

you might have @ how utilize them in plugin-use-case:

loading class-bytes jar file javassist instumentation

hth

java javassist

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -