If I calling toBytecode() method in my context it throws
java.lang.RuntimeException: remaper.by.moofMonkey.Main class is frozen at javassist.CtClassType.checkModify(CtClassType.java:515) at javassist.CtClass.getClassFile(CtClass.java:524) at com.moofMonkey.Main.writeFile(Main.java:340) at com.moofMonkey.Main.saveClasses(Main.java:324) at com.moofMonkey.Main.main(Main.java:309)
My context:
.....
for (CtClass cl : modClasses) {
cl.stopPruning(true);
writeFile(cl, "./ModifiedClasses"); //cl.writeFile("./ModifiedClasses");
cl.stopPruning(false);
}
.....
public static void writeFile(CtClass cl, String directoryName) throws Throwable {
System.out.println(">> " + cl.getName());
byte[] bc = cl.toBytecode();
String s = cl.getClassFile().getSourceFile();
int index = new String(bc).indexOf(s);
for(int i = 0; i < s.length(); i++) //KILL SOURCEFILE (c) moofMonkey
bc[index + i] = '-';
DataOutputStream out = cl.makeFileOutput(directoryName);
out.write(bc);
out.flush();
out.close();
}
BUT... But. If I calling analog of writeFile() - cl.writeFile() - all works!
I can do this:
1. Save File
2. Read bytes from him
3. Dp what I need
4. Save File
Having a look into the javadoc of CtClass reveals
Once this method is called, further modifications are not possible any more.
If you change the call order to
String s = cl.getClassFile().getSourceFile();
byte[] bc = cl.toBytecode();
you can call toBytecode
.