• Post Reply Bookmark Topic Watch Topic
  • New Topic

can we modify class definition dynamicallly in JAVA?  RSS feed

 
ramesh ramineedi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IN actionscript 2.0 :
Class definitions can be modified with one or both of two attributes: dynamic and intrinsic.
when a class is defined with the dynamic attribute, new properties and methods can legally be added both to the class's instances and to the class itself.
 
Roberto Perillo
Bartender
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Ramesh!

So the question is: is it possible to alter the structure of a class? Well, there are ways to manipulate the generated bytecode. One way is to modify it at compile-time; another way is to modify it at load-time. There are frameworks that can do that, such as ASM, CGLib and Javassist. As far as I know, once a class is loaded, it is not possible to alter its structure. I have never used, but it looks like Javassist can also define new classes at runtime.
 
himanshu.harish agrawal
Ranch Hand
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Roberto,

First question:
What do you mean by changing the structure of class? Is it adding and removing fields/methods or is it altering the values of them?
If it is changing the values then at runtime also through code HotSwap we can change the values, right?
If it is adding and removing fields/methods then are both things possible or only adding is possible.

Second:
Can you please brief me how i can modify the class structure at compile time and load time? Also, if you can share some good online resources.

Regards.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Actionscript, while some parts of it looks very much like Java... is not. It is actually closer to Javascript than to Java (only opinion, of course). And one of the cool features of Javascript, which Actionscript supports, is the ability to add any property and method to any object.

While this topic can go down into detail discussions about using class loaders to dynamically modify classes, etc. etc., I think the correct solution is to just simulate the functionality -- if this is need in Java.


For properties, you can include a hashmap with the class, then adding and using properties is simply setting and getting values from the hashmap (where the property is a key in the hashmap). For methods, you can have the value in the hashmap be some sort of callable object, which you can provide a caller method for.

Henry
 
ramesh ramineedi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto Perillo //
sir please explain me the way, adding new members to a class or class instance at compile time or while loading the class.
 
Roberto Perillo
Bartender
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Ramesh!

Well champ, it is possible to add new members before the JVM loads the bytecodes. After compiling your classes, you can, for instance, create a program that uses one of the frameworks that manipulate bytecodes in order to do so. Then, when you run the program, what will be loaded is the modified version of the classes. The Using the ASM Tookit for Bytecode Manipulation tutorial shows how you can do some transformations at compile-time and load-time using ASM. For transformations at load-time, it envolves classloaders and is done when the bytecodes are being loaded. Runtime transformations are not possible; however, what you can do is create a proxy at runtime that "intercepts" the class that you would like to modify.

The Daileon Tool (a tool that I created to deal with domain annotations) manipulates the classes' bytecodes using ASM, in order to transform a domain annotation to other annotations, according to a given mapping.

But, as Henry already said, an easier way to do this is to simulate the functionality. You can also use the AnnotatedElement interface as value of your HashMap, since both java.lang.reflect.Field and java.lang.reflect.Method implement this interface.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!