[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to add a new data member at runtime?

Posted on 2010-08-31
15
Medium Priority
?
507 Views
Last Modified: 2013-12-29
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
Comment
Question by:prain
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 2

Expert Comment

by:arch-itect
ID: 33572127
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
 

Author Comment

by:prain
ID: 33573372
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 33574049
you can achieve this with haspmap as said earlier!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Expert Comment

by:aaver
ID: 33574436
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
 
LVL 4

Assisted Solution

by:aaver
aaver earned 164 total points
ID: 33574546
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
 
LVL 10

Expert Comment

by:Hegemon
ID: 33575057
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
 
LVL 16

Accepted Solution

by:
Valeri earned 172 total points
ID: 33575644
You can not do that.
In my opinion If you need this, it means that there is something wrong in your design.
0
 

Author Comment

by:prain
ID: 33576197
Hegemon,

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

prain
0
 
LVL 2

Expert Comment

by:arch-itect
ID: 33576198
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
 
LVL 10

Assisted Solution

by:Hegemon
Hegemon earned 164 total points
ID: 33576687
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
 

Author Comment

by:prain
ID: 33578213
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
 
LVL 10

Expert Comment

by:Hegemon
ID: 33578234
Do you use Hibernate ? cglib and javassist are shipped with it.
0
 

Author Comment

by:prain
ID: 33578548
Yes we do use Hybernate. Let me see, and will get back. Thanks. prain
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 37126756
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question