Over a million developers have joined DZone.

About Reflection…

DZone's Guide to

About Reflection…

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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:

  1. Self-analysis enables you to create multiple configurations at the level of code that makes the system flexible and modular.
  2. 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. ;)
  3. We get the independence of code from the existence and correctness of CDI. Of course, if you have correctly written the analyzing code...
  4. The code's logic cuts a lot of extra code that validates the use of dependency.
  5. 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:

  1. It is very difficult to determine the rules of the code analysis in the project. 
  2. It is difficult to unify these rules for the allocation of the universal functionality.
  3. 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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}