samedi 30 novembre 2019

Java 8 Is there a simple possibility to insert code in a method using reflections ore something else?

Is there a simple possibility to insert code in a method from an other class which i cant edit?

this is my example class which i cant edit because im not the owner

package me;

public class Test {

    public Test() {

    }

    public void doSomething() {
        System.out.println("im doing something...");
    }

}

and i want to have an event method in my class which is

//should fire when Test.doSomething is called
    public static void onDoSomethingEvent() {

    }

i want to add onDoSomethingEvent(); to the doSomething method in Test.

Method method = Class.forName("me.Test").getDeclaredMethod("doSomething");
            method.setAccessible(true);
            //is there something in some lib or else like this?
            //method.insert("Main.onDoSomethingEvent()");
    ```






Aucun commentaire:

Enregistrer un commentaire