9

Having Student Class.

Class Student{
    String _name;
    ....
    ....

    public Student(){
    }
}

is there any possibility to add dynamic attributes to Student Object? without extending the student class.

2

3 Answers 3

20

In short, yes it is possible to modify bytecode at runtime, but it can be extremely messy and it (most likely) isn't the approach you want. However, if you decide to take this approach, I recommend a byte code manipulation library such as ASM.

The better approach would be to use a Map<String, String> for "dynamic" getters and setters, and a Map<String, Callable<Object>> for anything that isn't a getter or setter. Yet, the best approach may be to reconsider why you need dynamic classes altogether.

public class Student {

    private Map<String, String> properties = new HashMap<String, String>();
    private Map<String, Callable<Object>> callables = new HashMap<String, Callable<Object>>();
    ....
    ....
    public String getProperty(String key) {
        return properties.get(key);
    }

    public void setProperty(String key, String value) {
        properties.put(key, value);
    }

    public Object call(String key) {
        Callable<Object> callable = callables.get(key);
        if (callable != null) {
            return callable.call();
        }
        return null;
    }

    public void define(String key, Callable<Object> callable) {
        callables.put(key, callable);
    }
}

As a note, you can define void methods with this concept by using Callable and returning null in it.

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

3 Comments

@Vulcan: Thanks for the update,i want to access the dynamic object property using the same i.e.Student Object.
Best solution ever crossed my mind in the Computer science era.
Good approach, I would also say it maybe even more flexible/dynamic to use a small class instead of the Map, say: PropertyFiled: public class Student2 { private List<PropertyField> propertyFields; ... } Whereas: public class PropertyField { private String propertyAccessModifier; private String propertyDataType; private String propertyName; private String propertyValue; // getters and setters . . . . }
8

You could get into bytecode manipulation but that way madness lies (especially for the programmer who has to maintain the code).

Store attributes in a Map<String,String> instead.

1 Comment

Thanks for the Quick update and is there any other way to implement instead of Map?
0

Although you can do that with some tricky, and complex way that others have suggested..

But you can sure have your attributes in some data structure(An appropriate one will be a Map).. Since you can modify your existing attributes, so can be done with you Data Structure. You can add more attributes to them.. This will be a better approach..

7 Comments

@Vulcan.. Can you post a solution.. How? May be I don't know.
Refer to Jim Garrison's answer. You will want to use a bytecode manipulation library such as BCEL or ASM, preferably the latter. However, the better approach here would most likely be to use Maps.
@Vulcan.. Thanks. I have heard of BCEL earlier, but haven't used that. Probably because of requirement.. Will sure have a look into it.. Meanwhile I have modified my post to remove my first 2 paragraphs..
@Vulcan Technically, there not wrong, from a certain point of view. The Java Specification does not allow for this, although there are tools and hacks which you can use to achieve it. It would be like asking if man can fly? Obviously we can't, but we do ;) - IMHO
@RohitJain Actually, I thought about it a bit more, and bytecode injection isn't even necessary for what OP wishes to achieve (I think). Refer to my answer for details. Also, BCEL hasn't been updated in 6 years and appears abandoned, so I recommend associating with ASM instead. (Though BCEL does work with Java 7 except for invokedynamic instructions.)
|

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.