0

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");
    }

}
5
  • Why do you need this exactly? As depending on context other solutions might work, like you can't add methods to loaded classes at runtime. You can do that at compile time. And you can do it before class is loaded at runtime. You can also just generate new class with that method, or new class that will extend this class. A lot of possibilities, and maybe you don't even really need to generate a method, maybe it is xyproblem.info Please describe why you need this. Commented Jul 24, 2019 at 21:22
  • Using bytecode instrumentation API (like ASM , Javaassit, ByteBuddy, etc) you can add / remove methods, fileds, method body , etc. Commented Jul 25, 2019 at 5:07
  • @GotoFinal, I have a class with over one hundred fields and I need to call method for concrete one. Methods have the same body but use different fields, like: public class SimpleClass { private String f1; private String f2; private String f3; public void useF1() { // do some action with f1 } public void useF2() { // do some action with f2 } public void useF3() { // do some action with f3 } } Commented Jul 25, 2019 at 9:14
  • @GotoFinal, And I want this class to have only one generated method which will be recompiled every time I want to call it with concrete parameter, like: public void useF(String fieldName) { // do some action with field with name - fieldName } Commented Jul 25, 2019 at 9:20
  • Why do you have code like that, can't you use map for this? This does not sounds like good idea. Maybe just edit your question with REAL thing you are trying to do, and not how. Commented Jul 25, 2019 at 16:45

1 Answer 1

1

You can register a Java agent to manipulate a class before it is loaded to include a method as you want this. For doing so, you need to compile a different jar in which you register a ClassFileTransformer where you can change the class file. The Javadoc for the instrument package gives you an overview on how to do so.

Once a class is loaded, most JVM implementations do not allow you to add new methods.

Sign up to request clarification or add additional context in comments.

1 Comment

You can add agent at runtime too, but then you need to be sure to run transformer before given class is loaded. edit: ah, just realized who wrote the answer, you definitely know that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.