D
D
Dikiiforever2016-11-15 07:41:30
Java
Dikiiforever, 2016-11-15 07:41:30

Is it possible to add a new method to a class at runtime?

For example, there is a simple empty class without properties and methods. Is it possible to add methods and properties there, and then call them through reflection?
Update
And still so thoughts aloud. JRebel somehow replaces classes with modified (added remote) methods and properties right at runtime. How?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Kosarev, 2016-11-15
@jaxtr

Any manipulations (at the JVM level) with classes at runtime are possible using libraries that work with bytecode (the same cglib or asm) and custom javaagent. There are projects like JRebel, DCEVM or Spring Loaded that allow you to change the logic at runtime (again using the tools already mentioned).
Using such tools in production is contraindicated, and in development they are useless with TDD.
UPD: answer to the question: it is possible, but is it necessary?

A
aol-nnov, 2016-11-15
@aol-nnov

cglib for example.
but as stackoverflow says, it's not a simple because the class is already loaded by the classloader

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question