I never thought that will be faced with the need to write code that analyzes itself, but recently it happened. It should be noted that many developers do not use this method under any circumstances ... But it turned out that this code is incredibly flexible. Why?
Writing such a code has led me to release the following features:
- Self-analysis enables you to create multiple configurations at the level of code that makes the system flexible and modular.
- Having defined the interface (I do not mean Java interfaces) of the analyzed classes, you do not need to make any (!) changes in the code. Maybe you just you want to use annotations, which still remains a very flexible solution. ;)
- We get the independence of code from the existence and correctness of CDI. Of course, if you have correctly written the analyzing code...
- The code's logic cuts a lot of extra code that validates the use of dependency.
- And the most attractive, that it becomes possible to create a common interface to exchange parameters! You no longer need to think and worry about the number of parameters in the called method, you simply create a special object, the properties of which method will be determined.
The advantages are very attractive to me, but maybe they are insignificant for other developers because of the obvious problems:
- It is very difficult to determine the rules of the code analysis in the project.
- It is difficult to unify these rules for the allocation of the universal functionality.
- Exchanging interface parameters is not always possible to unify even in any one module in the project without unnecessary complications in the architecture.
Solving these problems may leads to a very convenient framework, some of the makings of a functional already exist in specialized libraries (Spring Forms, Hibernate).
With all the charms that I found for myself in such an approach, you should be extremely careful with the performance ... Self-examination greatly slows down the system, so the use of reflection is necessary only when the gains from such an analysis (eg, cutting off unnecessary computations) will be significantly greater than the losses from the introspection.