Putting new methods and properties into classes at runtime

Is there any way to introduce new methods and properties into classes at run time.

http://nurkiewicz.blogspot.com/2009/09/injecting-methods-at-runtime-to-java.html states that we can do this using Groovy.

Is this possible using Java?

+4
source share
5 answers

Is it possible just using Java?

The simple answer is decisive: "You do not want to do this!".

This is technically possible, but without resorting to extremely complex, expensive and fragile tricks, such as modifying bytecode 1 . And even then, you must rely on dynamic loading to access the modified type and (possibly) reflection in order to use its new members. In short, you will create a lot of pain for yourself, for a little, if you want.

Java is a statically typed language, and adding / modifying class type signatures can terminate the contract for static typing.


1 - AspectJ and the like allow you to introduce additional behavior into the class, but it is probably not the "runtime injection" you are using. Of course, the methods introduced will not be available for statically compiled code to be called.

+9
source

So, if you are really crazy, you can do something like what they describe here here . What you can do is download the .java file, find the correct insertion point, add any methods you need, call the java compiler and reload the class. Good luck debugging that mess though :)

Edit This can really be useful ...

+1
source

Is this possible using Java?

It is true that you only need to identify the monitoring agent that supplies the appropriate ClassFileTransformer , and you will have to use reflection to invoke the added methods. Most likely, this is not what you want to do, but it is doable, and there is a clearly defined interface for this. If you want to modify existing methods, you might be interested in something like AspectJ .

+1
source

You can do some pretty funny things with AOP, although the true modification of classes at runtime is a rather hairy technique that requires a lot of magic and class maneuvering.

The easier it is to use AOP methods to subclass your target class and instead introduce new methods into it, what the AOP is called "mixin" or "introduction". See here to read how Spring AOP does it, although it can be quite lame compared to what you are actually trying to achieve.

+1
source

Although it is possible, it is not useful.

How can you access these new fields and methods?

You cannot use these methods and fields directly (like "regular" fields and methods), since they will not be compiled.

If you only want to add "properties" and "methods", you can use Map<String, Object> for "dynamic properties" and Map<String, SuitableInterface> for "dynamic methods" and look them up in the name.

If you need an extension language for Java, you can add a built-in dynamic language (for example, Javascript or Groovy); most of them can access arbitrary java objects and methods.

0
source

All Articles