Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.org.apache.bcel.internal.Constants;
- import com.sun.org.apache.bcel.internal.classfile.ClassParser;
- import com.sun.org.apache.bcel.internal.classfile.JavaClass;
- import com.sun.org.apache.bcel.internal.classfile.Method;
- import com.sun.org.apache.bcel.internal.generic.*;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class Transform {
- public static void main(String[] args) {
- try {
- JavaClass javaClass = new ClassParser(args[0]).parse();
- ClassGen classGen = new ClassGen(javaClass);
- Method[] methods = javaClass.getMethods();
- for(Method method: methods){
- addWrapper(classGen, method);
- FileOutputStream fileOutputStream = new FileOutputStream(args[0]);
- classGen.getJavaClass().dump(fileOutputStream);
- fileOutputStream.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void addWrapper(ClassGen classGen, final Method method){
- final InstructionFactory factory = new InstructionFactory(classGen);
- final ConstantPoolGen constantPoolGen = classGen.getConstantPool();
- String className = classGen.getClassName();
- final MethodGen wrapMethod = new MethodGen(method, className, constantPoolGen);
- final InstructionList instructions = new InstructionList();
- Visitor visitor = new EmptyVisitor() {
- @Override
- public void visitInvokeInstruction(InvokeInstruction obj) {
- StringBuilder builder = new StringBuilder();
- for(Type type: obj.getArgumentTypes(constantPoolGen)){
- builder.append(type.getSignature());
- }
- String text = "Method to be called: " +
- obj.getMethodName(constantPoolGen) +
- "(" + builder.toString() + ")" +
- obj.getReturnType(constantPoolGen).getSignature();
- instructions.append(factory.createPrintln(text));
- }
- };
- for(Instruction i : wrapMethod.getInstructionList().getInstructions()){
- i.accept(visitor);
- instructions.append(i);
- if(i instanceof InvokeInstruction && !(i instanceof INVOKESPECIAL)){
- InvokeInstruction tmp = (InvokeInstruction) i;
- if(!tmp.getReturnType(constantPoolGen).equals(Type.VOID)) {
- instructions.append(factory.createFieldAccess(
- "java.lang.System",
- "out",
- new ObjectType("java.io.PrintStream"),
- Constants.GETSTATIC
- ));
- instructions.append(new PUSH(constantPoolGen, "Got result: "));
- instructions.append(factory.createInvoke(
- "java.io.PrintStream",
- "print",
- Type.VOID,
- new Type[]{Type.STRING},
- Constants.INVOKEVIRTUAL
- ));
- instructions.append(new DUP());
- if(!tmp.getReturnType(constantPoolGen).equals(Type.STRING)){
- Type type = tmp.getReturnType(constantPoolGen);
- if(type instanceof ObjectType){
- type = Type.OBJECT;
- }
- instructions.append(factory.createInvoke(
- "java.lang.String",
- "valueOf",
- Type.STRING,
- new Type[]{type},
- Constants.INVOKESTATIC
- ));
- }
- instructions.append(factory.createFieldAccess(
- "java.lang.System",
- "out",
- new ObjectType("java.io.PrintStream"),
- Constants.GETSTATIC
- ));
- instructions.append(new SWAP());
- instructions.append(factory.createInvoke(
- "java.io.PrintStream",
- "println",
- Type.VOID,
- new Type[]{Type.STRING},
- Constants.INVOKEVIRTUAL
- ));
- }
- }
- }
- wrapMethod.setInstructionList(instructions);
- wrapMethod.stripAttributes(true);
- wrapMethod.setMaxStack();
- wrapMethod.setMaxLocals();
- classGen.replaceMethod(method, wrapMethod.getMethod());
- instructions.dispose();
- //Method to be called: someMethod(Ljava/lang/String;)Ljava/lang/String;
- }
- }
- //Todo podmienić na końcu skrypty na te co były
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement