mercredi 24 juillet 2019

Generating code in existing classes in runtime (Java)

I have already created an empty class SimpleClass. How can I generate some code in this class in runtime and recompile the class to be able to use that code?

public class SimpleClass {}

I've found the solution with generating whole class with it's content using custom ClassLoader and javax.tools API. But is there any for existing class?

public class SimpleClass {

    //Generated code in runtime
    public void method() {
       System.out.println("Generated method");
    }

}





Aucun commentaire:

Enregistrer un commentaire