• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

How to add a new data member at runtime?

Hello,
Is it possible to add a field (a data member) into a class at runtime? I could not find anything in reflection to do this in Java.

Thanks
prain
0
prain
Asked:
prain
  • 4
  • 3
  • 2
  • +4
3 Solutions
 
arch-itectCommented:
You do get java class file editors (see Google)

you also get bytecode libraries like

http://asm.ow2.org/

but I think what you are looking for is a Hashtable or HashMap where you can add name value pairs at runtime.

Hashtable<String, Object>() vehicles = new Hashtable<String, Object>();
myValues.put("car", new Car());

0
 
prainAuthor Commented:
OK. Let me give an example to explain what I am loking for. if I have a class

public class Person
{
   private String name;
   private int  age;
}

Is it possible for me to add a member

  private String Address;

info the class during runtime. So at runtime, the 'class' would like


public class Person
{
   private String name;
   private int  age;
   
   private String Address;
}

Thanks
prain
0
 
ksivananthCommented:
you can achieve this with haspmap as said earlier!
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
aaverCommented:
As already said, this is probably possible to do in a much simpler way.
Could you explain why you cannot use a hashmap to hold different data?
0
 
aaverCommented:
If you really need a truly dynamic approach, you might consider javax.tools.JavaCompiler.

This is a good example of doing that:
http://www.ibm.com/developerworks/java/library/j-jcomp/index.html
0
 
HegemonCommented:
If you need to add properties only once and need to getters/setters for it, you can generate a subclass with the necessary properties using any of available "code enhancements" libraries. For example, using the BeanGenerator library you can generate a runtime class extending your Person class and containing the "extra" properties. Something like:

        BeanGenerator bg = new BeanGenerator();
        bg.setClassLoader(cl);
        bg.setSuperClass (Person.class);
        bg.addProperty ("address", String.class);
        ...
        Class generatedPersonExt = bg.createClass();
0
 
ValeriCommented:
You can not do that.
In my opinion If you need this, it means that there is something wrong in your design.
0
 
prainAuthor Commented:
Hegemon,

Is the BeanGenerator part of J2SE? and what is 'cl' in line 2 of your code sample?

prain
0
 
arch-itectCommented:
You either have to generate and compile the code at runtime (which is possible)
or you can do that with javascript inside java (although not in the way you suggest)

http://www.mozilla.org/rhino/

It is like you are trying to write a language, or interpreter in java.  I can explain to you how javascript does it, enable you to create "classes" at runtime, and it involves HashMaps.
0
 
HegemonCommented:
BeanGenerator is a part of the cglib library: http://cglib.sourceforge.net/
cl is a ClassLoader object that you can obtain using Person.class.getClassLoader();

BTW, apart from cglib, there is another well known code instrumentation library - javassist: http://en.wikipedia.org/wiki/Javassist

In my example I used BeanGenerator as in my view it is the simplest way to achive the goal.
0
 
prainAuthor Commented:
I have a problem getting these non core-jave libraries. This is the problem when you work for the government. I cannot just download someone else's libraries without prior permission - which might take several weeks to approve. :-)

Your solution looks a viable solution to my issue. Unfortunately I cannot test it out because of the library issue.

-prain
0
 
HegemonCommented:
Do you use Hibernate ? cglib and javassist are shipped with it.
0
 
prainAuthor Commented:
Yes we do use Hybernate. Let me see, and will get back. Thanks. prain
0
 
mrcoffee365Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now