First, because some fields of a DTO may be private and thus accessible only through reflection. In order to let the serialization engine break encapsulation barriers, a programming language has to have reflection. It’s usually done by a special serialization engine, which takes a DTO, breaks all possible encapsulation barriers, reads the values of all of its fields, and packages them into, say, a piece of JSON. Then, in order to transfer a DTO over a network or save it to a file, they serialize or marshall them. When programmers don’t understand object-oriented paradigm, they make DTOs instead of proper objects. The problem will surface only later at runtime, when the expectations of other objects won’t be satisfied. #Reflection meaning code#An annotated object can change its interface or modify annotations and the code will compile just fine. The clients of annotated objects/classes are coupled with them, and this coupling is hidden. Life would be much better and Java/OOP much cleaner. Neither dependency injection containers, not auto-wiring, nor annotations would exist if there was no reflection. I’ve also said earlier that annotations are an anti-pattern. I’ve said it earlier that this very mechanism of discovering objects and automatically wiring them together is an anti-pattern. In modern frameworks like Spring this feature is frequently used in order to automate objects wiring: you just attach some annotations to your classes and the framework will find them, instantiate them, place them into a DI container, and assign to other objects’ attributes. The information from the annotation can then be read at runtime or compile time. In Java you can attach an annotation (an instance of a DTO-ish interface) to a class (or an element of it like a method or an argument). Without reflection it wouldn’t be possible to have static factories at all and the code would be better and more maintainable. There are a few reasons why people love static factories. Second, the logic of class finding is provided out-of-the-box by JVM: I don’t need to guess what happens when make("Plus") is called. First of all, because in any IDE that enables code navigation it would be possible to click on OpMinus or OpPlus and immediately jump to the body of the class. If you ask me, this code looks much more readable and maintainable. Of course, life would be even better if we also didn’t have private methods. If nobody had the ability to reach the private method in any way, the Inspector anti-pattern in unit tests would not be possible. This coupling between the unit test BookTest and the class Book would not happen if it was not possible to use reflection in the first place. How can I trust it if it lies in such a simple scenario? I modify the code and I know that I didn’t break anything. The tests are not a “safety net” for me anymore. However, changing the test and at the same time changing main code is, I believe, a dangerous practice: most probably I will introduce some new bugs. What would I do next? I would have to roll back my changes and then start refactoring the test and the class, in order to get rid of this assumption. It’s just an assumption the test made about the internals of Book and this assumption has no reasons aside from “We didn’t have time to refactor the class and make System.out injectable.”īy the way, this testing approach is known as the “Inspector” test anti-pattern. Otherwise, why would a test demand anything from a private implementation of a class? Very soon, after some investigation I would find out that there is no reason. Why? What’s wrong with returning StringBuilder? I would think that there is some hidden reason for this. If it’s not my test or I wrote it a long time ago, I would be frustrated to learn this fact: the test expects me to write my private methods only one specific way. However, the test BookTest will fail, because it expects my class Book to have method name which returns String.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |