Search code examples
javabyte-buddy

How can I use bytebuddy to generate pojo code


I'm new to bytebuddy, and I've written a junit test to generate a class file. Blow is my code:

@Test
public void testGener() throws IOException {
    DynamicType type = new ByteBuddy().subclass(Object.class).name("TestInterFace").defineField("test", String.class, Visibility.PUBLIC).make();
    type.saveIn(new File("C:/Users/zhouxiang/Desktop/develop"));
}

But, what I really need is to print a string like this:

public class TestInterFace
{
  public String test;
}

yes. Just the Java code, not the class file. So, does byte buddy can do This? Or, I will only to use code model lib.


Solution

  • Byte Buddy is a class for generating byte code, not Java source code. For generating source code, have a look at Java poet: https://github.com/square/javapoet