11

I want to add new fields(variables) and encapsulating methods for a given class. For example: A class name Student has no any fields like below:

public class Student implements Serializable{

}

then in my application an instance is created;

Student s=new Student();

I want to add new methods which do not exist for student class at the run time.for example: I want to add a field called studentName, and getStudentName() and setStudentName() methods.

Then at the run time the student object will be like this;

public class Student implements Serializable{

    private String studentName;

    public void setStudentName(..){}
    public String getStudentName(){return ...;}
}

In my application objects are written to a text file and all objects of same type do not have all variables. Therefore, I want to add only the required fields to save memory.

Any way is there a way to do this? Any sample code or link?

EDIT: or else can we create a class either and create instances which does not exists ?

EDIT 2: Thanks all of you answered and got many info and ideas. And changed the way to a better path from your suggestions as well

8
  • 2
    Partial Duplicate: stackoverflow.com/questions/6680674/… Commented Aug 16, 2013 at 14:13
  • 4
    It sounds like you may be coming up with a solution to an unfounded concern, and one which could be better addressed with more upfront thought about the data model (and possibly more types). Commented Aug 16, 2013 at 14:15
  • Don't follow that road, for God's sake ! Commented Aug 16, 2013 at 14:15
  • You don't need that, seriously, just don't do it. Ask about how to implement the thing that you want to implement this way Commented Aug 16, 2013 at 14:17
  • "In my application objects are written to a text file and all objects of same type do not have all variables" .. what this means?? Commented Aug 16, 2013 at 14:17

7 Answers 7

8

Why not just create a HashMap of values? Much more efficient, and has all the flexibility you're looking for.

public class Student
{
      private HashMap<String, String> values;

      public Student()
      {
          this.values = new HashMap<String, String>();
      }

      public void addValue(String name, String value)
      {
          values.put(name, value);
      }

      public String getValue(String name)
      {
          return values.get(name);
      }
}

Why a HashMap?

You said that all objects may have differing values, and you'll be defining those new methods and attributes by a String. Well.. this will achieve that functionality without any horrible bytecode manipulation. For example:

String attrName = "name";
String attrValue = "jim";
Student stu = new Student();
stu.addValue(attrName, attrValue);

At the moment, you've only got the one value in your HashMap. The only overheard you have to face is the HashMap object itself, and two methods, which frankly is a fair trade off for a far tidier solution.

Sign up to request clarification or add additional context in comments.

4 Comments

I think the OP should rather rethink the data model as Jon Skeet suggested in his comments, rather than creating some class that can hold any arbitrary set of attributes.
Be worry of the memory consumption of the HashMap, perhaps you want to give it a initial capacity estimate at construction.
I think these details are more for OP to work out. I don't like handing them everything. And agreed Griffey, but if we can achieve their desired functionality, then it makes sense to post it. If anything, so future readers know what to do is this type of class is the correct choice.
You are assuming values are stored as String... anyways if they are needed to be retrieved as a specific type it would be necessary to create parsers with its probably consequent impact in performance.
3

You can use bytecode instrumentation libraries like Javassist or ASM for this purpose. Here is an example of adding a field or method by using Javassist.

2 Comments

True, but I don't think it is a good idea to follow that route specially for someone that is making such a basic question about Java in the first place.
Yes. Agree. This was just another option.
1

While it is possible with bytecode manipulation and such it wouldn't be wise, especially if you intend to do this to "save memory". It's unlikely that you would have so much data that it would make a difference, and if you did, you would store them in a database anyways.

Comments

1

Instead of writing your own HashMap based solution you can use DynaBean and DynaClass: support not only simple properties but also indexed (Array) and mapped (Map).
DynaBean can be introspected to get properties and values so you can dump to file BUT with this solution you are only "simulating" a bean, your Student class doesn't really contains fields and accessors (you you call Student.getClass().getDeclaredField() you will get an empty array).

If you need to compose a "real" java java.lang.Class Javassist (my preferred choice, I used to resolve a solution similar to your question) or ASM (or CGLIB) are the best choiches.

1 Comment

It's HashMap not HasMap a minor typo there
0

I dont believe if this is possible in java but I'm sure it will only add to the memory because if you add them dynamically they must be set up beforehand + the code to add them dynamically.

Comments

0

Practically speaking, not in Java. In other languages like Javascript, this is possible.

2 Comments

Reflection can't add fields. You need bytecode manipulation for that.
You can do that using bycode instrumentation libraries like ASM or Javassist.
0

Java is not a dynamic programming language and so I would not advice to follow that route even if some advance approaches may allow you to do so.

The Java idiom for that scenario would be to store the field values in a (hash) map instead. So you would have a couple of common accessors to set or get all attribute values and in the accessor you would need to indicate the name of the attribute you want to change.

However this solution won't save memory unless the maximum number of attributes is rather large and most object just have values for a small number of such attributes.

   public class Entity {
   
      // 5 is an estimate for the number attrs.
      private Map<String,Object> attrs = new HashMap<>(5); 

      public Object getAttribute(String name) { return attrs.get(name); }

      public void setAttribute(String name, Object obj) { attrs.put(name,obj); } 

   }

You could implement some runtime type-checking if you manage meta-data about possible attributes and their value types.

1 Comment

IDEA reports an error here: attrs.put(name, obj); , because ? is unknown type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.