1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
import java.io.File;
import java.io.IOException;
import org.apache.bcel.classfile.ClassFormatException;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.InstructionFactory;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.MethodGen;
public class Sleeper {
/**
* @param args
* @throws IOException
* @throws ClassFormatException
*/
public static void main(String[] args) throws ClassFormatException, IOException {
String TargetclassFile = "bin\\Tosleep.class";
String SaveTargetclassFile = "Tosleep.class";
File f = new File(TargetclassFile);
if (f.exists() == true) {
JavaClass jclas = new ClassParser(TargetclassFile).parse();
ClassGen cg = new ClassGen(jclas);
ConstantPoolGen cp = new ConstantPoolGen(jclas.getConstantPool());
// Recupere les methode de la classe.
org.apache.bcel.classfile.Method[] methods = jclas.getMethods();
for (int i = 0; i < methods.length; i++) {
org.apache.bcel.classfile.Method m = methods[i];
// Ne touche pas au main.
if (m.getName().contains("main") == true) continue;
// Recupere la "methode" i de la classe
MethodGen mg = new MethodGen(methods[i], jclas.getClassName(),cp);
MethodGen mg2 = new MethodGen(methods[i], jclas.getClassName(),cp);
// Recupere la liste de la methode i
InstructionList il = mg.getInstructionList();
// Cree une liste d'instruction
InstructionList il2 = new InstructionList();// mg2.getInstructionList();
InstructionFactory fM = new InstructionFactory(cg);
// Recopie les instruction en ajoutant les instructions entre les instructions.
org.apache.bcel.generic.Instruction[] ins = il.getInstructions();
for (int j = 0; j < ins.length; j++) {
org.apache.bcel.generic.Instruction instruct = ins[j];
il2.append(instruct);
// ICI TU DOIS CHANGER POUR AVOIR TON SLEEP;
il2.append(fM.createPrintln("["+instruct.toString()+"]"));
}
mg2.setInstructionList(il2);
mg2.setMaxStack(cp.getSize()); //? setMaxStack(); -> nullpointer.
// Remplace la methode.
cg.removeMethod(mg.getMethod());
cg.addMethod(mg2.getMethod());
}
// Sauve
try {
cg.getJavaClass().dump(SaveTargetclassFile);
} catch (java.io.IOException e) {
System.err.println(e);
}
}
}
} |