Adding one line of code to a method on the fly

Discussion in 'Spigot Plugin Development' started by gyurix, May 27, 2015.

  1. How can I add one line of code to a method, using some high level reflections, or idk what.

    So, what am I exactly need to reach is, to save a methods local variable, that's why I need to do this.

    i.e.
    //Original method
    public void method(){
    int i=5+4;
    for (int j=1;j<i;j++){
    System.out.println("test");
    }

    //Method after hook
    public void goti(int i){
    System.out.println("Got i, yeah, success: "+i);
    }
    public void method(){
    int i=5+4;
    goti(i);
    for (int j=1;j<i;j++){
    System.out.println("test");
    }
    }

    Is there any way to do it? Of course these simple methods are only examples, I will do it in more complex method.
     
  2. What does reflection have to do with this?
     
  3. instrumentation - nuff said
     
  4. konsolas

    Supporter

    Erm, you could get a method from your current class using Class#getMethod, and you can get the current class with this.class.
     
  5. Only possible solutions would be extending the class, making use of super.method() (so your code is only executed at begin / end of method) and then replacing the original object with an instance of your subclass or some magical bytecode transformation (I have no knowledge with these, don't ask me about it or whether it's actually possible at run time).
     
  6. @gyurix the question isn't making much sense, really. Did you mean instance variables? (moving i to the class body)
     
  7. konsolas

    Supporter

    Oh, I didn't realize the OP wanted to add a line from another method. In that case, I don't think it's possible.
     
  8. It must be possible anyhow...

    Maybe this bytecode transformation would be the solution? Does anyone know, how to use it?
     
  9. Yeah, I know instrumentation quite well. Help you? other than suggesting libraries, I'm not gonna spoonfeed or teach you
     
  10. If you're at the point where you need to add code to methods you don't control, maybe you should rethink your design.

    Also, don't try to make an example, post your actual problem. The answer to your question currently is just "don't do that".
     
  11. It's enough for me, that you tell me these libraries, and the solution for the current problem.

    I don't need a class file downloader and compiler:O

    I know, I will do it only, when I feel, there is no better solution... But really, the current solution, what I do, when I need to do something like that, is to simply copy my modified class file to the jar, but it's not a good solution, when I want to share plugins, which requires this code change.
     
  12. there's javassist, bcel, asm, procyon, etc. bcel is outdated and you should use something newer. javassist is decent but weaker. i personally prefer asm, as the documentation is quite large and its updated. procyon is nice if you want a less of a visitor or node style framework and would rather do things in the form of expressions.
     
  13. Please do tell us why you need this. What is your end goal? Chances are there is a much better solution to your problem.