Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically add class fields at runtime in Java [duplicate]

Tags:

java

Possible Duplicate:
How to dynamically add properties to class

I would like to know is their any way to add fields to my entity class in Java, dynamically, or is their any alternative way of doing this? We are developing a framework and our existing products has this as a requirement.

Example:

class Entity1 {
    String name;
    String age;

    // Getters/setters;
}

Suppose I want to add another field to my Entity1 class (eg: designation), is their any way to add it dynamically/at runtime, so that if my client asks me to add any other properties I can do it with less alteration of code?

Waiting for your reply. Any suggestions are welcome.

like image 226
pradeepds Avatar asked Aug 19 '26 09:08

pradeepds


1 Answers

You can use javaassist to do this - hibernate use this for a lot of its cool features.

This should help you get started.

You could even use groovy to achieve this so this depends on your preference and any specifics that you may have omitted from the question.

like image 109
Scorpion Avatar answered Aug 21 '26 22:08

Scorpion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!