Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.00 KB | None | 0 0
  1. import com.sun.org.apache.bcel.internal.Constants;
  2. import com.sun.org.apache.bcel.internal.classfile.ClassParser;
  3. import com.sun.org.apache.bcel.internal.classfile.JavaClass;
  4. import com.sun.org.apache.bcel.internal.classfile.Method;
  5. import com.sun.org.apache.bcel.internal.generic.*;
  6.  
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9.  
  10. public class Transform {
  11.     public static void main(String[] args) {
  12.  
  13.         try {
  14.             JavaClass javaClass = new ClassParser(args[0]).parse();
  15.             ClassGen classGen = new ClassGen(javaClass);
  16.             Method[] methods = javaClass.getMethods();
  17.  
  18.             for(Method method: methods){
  19.                 addWrapper(classGen, method);
  20.                 FileOutputStream fileOutputStream = new FileOutputStream(args[0]);
  21.                 classGen.getJavaClass().dump(fileOutputStream);
  22.                 fileOutputStream.close();
  23.             }
  24.         } catch (IOException e) {
  25.             e.printStackTrace();
  26.         }
  27.  
  28.     }
  29.  
  30.     private static void addWrapper(ClassGen classGen, final Method method){
  31.  
  32.         final InstructionFactory factory = new InstructionFactory(classGen);
  33.         final ConstantPoolGen constantPoolGen = classGen.getConstantPool();
  34.         String className = classGen.getClassName();
  35.  
  36.         final MethodGen wrapMethod = new MethodGen(method, className, constantPoolGen);
  37.  
  38.         final InstructionList instructions = new InstructionList();
  39.  
  40.         Visitor visitor = new EmptyVisitor() {
  41.             @Override
  42.             public void visitInvokeInstruction(InvokeInstruction obj) {
  43.  
  44.                 StringBuilder builder = new StringBuilder();
  45.                 for(Type type: obj.getArgumentTypes(constantPoolGen)){
  46.                     builder.append(type.getSignature());
  47.                 }
  48.  
  49.                 String text = "Method to be called: " +
  50.                         obj.getMethodName(constantPoolGen) +
  51.                         "(" + builder.toString() + ")" +
  52.                         obj.getReturnType(constantPoolGen).getSignature();
  53.  
  54.                 instructions.append(factory.createPrintln(text));
  55.             }
  56.         };
  57.  
  58.  
  59.         for(Instruction i : wrapMethod.getInstructionList().getInstructions()){
  60.             i.accept(visitor);
  61.             instructions.append(i);
  62.             if(i instanceof InvokeInstruction &&  !(i instanceof INVOKESPECIAL)){
  63.                 InvokeInstruction tmp = (InvokeInstruction) i;
  64.                 if(!tmp.getReturnType(constantPoolGen).equals(Type.VOID)) {
  65.  
  66.                     instructions.append(factory.createFieldAccess(
  67.                             "java.lang.System",
  68.                             "out",
  69.                             new ObjectType("java.io.PrintStream"),
  70.                             Constants.GETSTATIC
  71.                     ));
  72.  
  73.                     instructions.append(new PUSH(constantPoolGen, "Got result: "));
  74.                     instructions.append(factory.createInvoke(
  75.                             "java.io.PrintStream",
  76.                             "print",
  77.                             Type.VOID,
  78.                             new Type[]{Type.STRING},
  79.                             Constants.INVOKEVIRTUAL
  80.                     ));
  81.  
  82.                     instructions.append(new DUP());
  83.                     if(!tmp.getReturnType(constantPoolGen).equals(Type.STRING)){
  84.  
  85.                         Type type = tmp.getReturnType(constantPoolGen);
  86.                         if(type instanceof ObjectType){
  87.                             type = Type.OBJECT;
  88.                         }
  89.  
  90.                         instructions.append(factory.createInvoke(
  91.                                 "java.lang.String",
  92.                                 "valueOf",
  93.                                 Type.STRING,
  94.                                 new Type[]{type},
  95.                                 Constants.INVOKESTATIC
  96.                         ));
  97.                     }
  98.  
  99.                     instructions.append(factory.createFieldAccess(
  100.                             "java.lang.System",
  101.                             "out",
  102.                             new ObjectType("java.io.PrintStream"),
  103.                             Constants.GETSTATIC
  104.                     ));
  105.  
  106.                     instructions.append(new SWAP());
  107.                     instructions.append(factory.createInvoke(
  108.                             "java.io.PrintStream",
  109.                             "println",
  110.                             Type.VOID,
  111.                             new Type[]{Type.STRING},
  112.                             Constants.INVOKEVIRTUAL
  113.                     ));
  114.  
  115.                 }
  116.             }
  117.         }
  118.  
  119.         wrapMethod.setInstructionList(instructions);
  120.         wrapMethod.stripAttributes(true);
  121.         wrapMethod.setMaxStack();
  122.         wrapMethod.setMaxLocals();
  123.         classGen.replaceMethod(method, wrapMethod.getMethod());
  124.         instructions.dispose();
  125.  
  126.         //Method to be called: someMethod(Ljava/lang/String;)Ljava/lang/String;
  127.     }
  128. }
  129.  
  130. //Todo podmienić na końcu skrypty na te co były
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement