My knowledge so far only relies on syntax and so far has not gone deeply with everyone, but such a topic is of interest, and I would ask someone to comment and, if possible, reply:

How and how difficult it is to implement a code change mechanism while the program is running, and what are the disadvantages and advantages of such a mechanism (apart from the fact that the code will be dynamically changed there without the need to re-compile everything)

  • one
    The most malicious viruses use the code modification technique to complicate the task of the antivirus to catch it. Obviously in this case we have advantages. In the overwhelming majority of others - and the fig is necessary? - Sergey
  • @Sergey, the most common Java framework, uses code generation right and left. AOP too. Large bloody enterprise solutions generate entity classes on the fly from configuration. Profilers use code instrumentation via the Java Agent. - Nofate
  • @Nofate But the same thing happens before the code is loaded for execution, when the class is loaded with instrumentation, the code changes. Not at the same time? Enterprises generate the code of their proxies and do not change it later during the work and never change the code of the classes for which these proxies are generated. - Sergey
  • @Sergey, from the point of view of the application, classes appear in runtime. By wrapping the source class in a dynamically generated proxy, you can change the behavior of the same object instance. This is not exactly "fair" metaprogramming as in Groovy, but a pretty good "illusion." - Nofate

2 answers 2

There is such a term "Metaprogramming".

Wikipedia definition: "a type of programming associated with the creation of programs that generate other programs as a result of their work (in particular, at the stage of compiling their source code), or programs that change themselves during execution (self-modifying code)." The first allows you to get the program at a lower cost of time and effort on encoding than if the programmer wrote them entirely by hand, the second allows you to improve the properties of the code (size and speed).

https://ru.wikipedia.org/wiki/%D0%9C%D0%B5%D1%82%D0%B0%D0%BF%D1%80 %D0BB%D0%B3%D18080 % B0% D0% BC% D0% BC% D0% B8% D1% 80% D0% BE% D0% B2% D0% B0% D0% BD% D0% B8% D0% B5

But the article about metaprogramming specifically in Java: https://habrahabr.ru/post/91239/

Read and google more materials about metaprogramming, I think that you will find something interesting for yourself.

    There is also an interesting technique of changing the program logic on the fly called aspects, programming using aspects is called aspect-oriented programming.