how to create a dynamic class at runtime in Java

会有一股神秘感。 提交于 2019-12-10 14:01:18

问题


Is it possible to create a new Java file from an existing Java file after changing some of its attributes at runtime?

Suppose I have a java file

public class Student {
    private int rollNo;
    private String name;
    // getters and setters
    // constructor
}

Is it possible to create something like this, provided that rollNo is a key element for the table?

public class Student {
    private StudentKey key;
    private String name;
    //getters and setters
    //constructor
}

public class StudentKey {
    private int rollNo;
    // getters and setters
    // construcotors
}

Please help. Thanks.


回答1:


Take a look at javassist.



来源:https://stackoverflow.com/questions/2980429/how-to-create-a-dynamic-class-at-runtime-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!