Java Reflection in Action (英語) ペーパーバック – 2004/10/30
Kindle 端末は必要ありません。無料 Kindle アプリのいずれかをダウンロードすると、スマートフォン、タブレットPCで Kindle 本をお読みいただけます。
Explaining the Java Reflection API and providing techniques for using it effectively, this guide describes the capabilities that allow a program to examine and modify itself at runtime. The java.lang.reflect package and its uses are covered, including a detailed discussion of Java's dynamic proxy facility. Less obvious reflective capabilities, such as call stack introspection and the Java class loader, are addressed. In recognition of the limitations of Java Reflection, the various ways to use Reflection to generate code and surpass these limitations are detailed. A discussion of performance analysis techniques and a look ahead at what is new in JDK 1.5 is included.
Dr. Ira Forman is a senior software engineer at IBM. He started working on reflection in the early 1990s when he developped IBM's SOM Metaclass Framework. Nate Forman works for Ticom Geomatics where he uses reflection in day-to-day problems. Ira and Nate are father and son. They live in Austin, Texas.
Overall: I would recommend buying after I've read the book.
Now, a less glorious side... This book is OLD. It is based on Java 1.4 and Java 5 was only in works when it was published, so annotations and generics are not really covered: they are mentioned as "well... let's wait until it gets finalized and then Reflection would be likely expanded to cover these topics"...
The second part of the book (starting from "code generation") is perceived by me as a collection on hacks which authors put into this book just to make up the volume...
Anyway: book is definitely worth reading :)
My only qualm about the book is their discussion of the use of microbenchmarks to evaluate the impact of reflection upon execution speed. My experience is that microbenchmarks such as these, give you a best case lower bound on execution speed.
Echoing the other reviews, every Java programmer should know this. It will make your code cleaner, better and more maintainable. The book also contains an excellent set of references to help the reader explore the general topic of reflection in greater depth.
The book covers all the basics. It starts with the whole idea of metaprogramming, writing programs about programs. Maybe it sounds involuted, but the first two chapters show how it works for handling the basic features of an application, the methods and fields. The next topics cover object creation: first, objects of classes that already exist, then classes created on the fly using Java's Proxy mechanism. A little later, they cover class loaders and custom loaders in the clearest, best-motivated discussion I've seen anywhere. The chapter on Design Patterns is, as in so many books, somewhat perfunctory. I've used reflection to analyze DPs in running programs, so I found that chapter disappointing. The last chapter begs to be rewritten. This book was in production when Java 5 was on the horizon, but issued after Java 5 hit the streets. Java 5 introduced many new features such as annotation, and new reflective APIs to support them. That last chapter looks forward to features that have since become real - not a fault of the authors', but enough reason for a second edition.
There are some real problems in this book, though. A minor one is that the reflection API isn't actually laid out entire anywhere in the discussion, but JavaDoc will take care of that for you. More importantly, serialization is a crucial part of the component technologies that reflection supports. This book largely disregards the standard APIs and SPIs in favor of an ad hoc, roll-your-own approach. Trust me, that's a bad idea. See Halloway's aging 'Component Development' book or old editions of 'Java in a Nutshell' for much more complete treatment of real serialization. Part of the problem in this treatment of serialization is its weak discussion of the inheritance hierarchy: when serializing a subclass, you have access to only half of an object. The superclass[es] is[are] the other half, and may have private data that the subclass can't serialize on its own. This weakness recurs in an otherwise interesting discussion on checking of invariants. The approach in this book seems to forget that the subclass invariants are only half of the object invariants, and the superclass is not handled. Also, as Szyperski notes, invariants interact subtly with callbacks, a discussion conspicuous by its absence.
This is an advanced book that Java newbies might not benefit from, and I don't mean that as a criticism. It's for experienced programmers with big, complex problems. It's for dynamic, extensible systems, the kind that we all want to work on. Even though it's Java-based, it's for anyone programming in any reflective language, at least until your language gets its own version of this book. Despite some significant problems, I recommend this book highly.
I liked the choice of introducing new material with a very simple and self contained example before attempting a more complex "put it all together" example. Also, yes, the examples are quite contrieved, but that can be justified because while it's true that little bits and pieces of the reflection API can be useful in most programs, it's definitely hard to come up with an example program that gains real advantage by using ALL of the reflection API.
Starting with chapter 7 on code generation, this book takes the very sorry road of an overly academical, obscure style that is only about introducing unnecessary complications. My bet is the real reason for introducing these last chapters was to fatten up a book that even like that is quite slim. In fact it's certainly not easy to produce a 200+ pages book on reflection alone, but still the author's would have been better off introducing more examples, or even treating another subject besides reflection, than introducing these bogus chapters. Still a worthy buy though, for a java programmer that has digested the basics and is curious to find out what is possible with the apparently arcane features of reflection.
I'm a technical writer myself and follow the simple rule: "the book/article is ready when you can't remove anything from it". The authors have managed to cover complicated topics in a 250-page book that has nothing to remove.