Java Reflection in Action (In Action series)

Java Reflection in Action (In Action series)
Authors
Ira R. Forman, Nate Forman
ISBN
1932394184
Published
01 Oct 2004
Purchase online
amazon.com

Explaining the Java Reflection API and providing techniques for using it successfully, this guide describes the capabilities that allow a program to examine and modify itself at runtime. The book examines the java.lang.reflect package, explains how you can benefit from its use, and includes a detailed discussion of Java?s dynamic proxy facility. The authors also address less obvious reflective capabilities, such as call stack introspection and the Java class loader. You?

Page 2 of 2
  1. Editorial Reviews
  2. Customer Reviews

Customer Reviews

calvinnme said
I first ran across the concept of reflection in Java in Cornell's Core Java book back when I was first learning the Java programming language in 1998. However, that book just treated reflection as a set of cute pet tricks of which Java is capable. Interested in a particular aspect of a class? Use this or that method and inquire. Only thing was, as a developer of code and not of tools, I found this aspect of Java interesting but not particularly useful. This book gave me a whole new respect for the tool of reflection. In a large multimedia application, my main program was going through a long series of if statements and looking for matching strings and then calling the matching sub-application. It worked, but it was large, clunky, and not very maintainable. I read this book and got the idea for a much more elegant solution. Rather than enumerate each class, I build the class name of the object I need from my list of options to construct and instantiate using reflection at runtime. Mind you, I didn't read or buy this book with that solution in mind, the author was just so clear in talking about the usefulness of reflection that the idea came to me. That's just one of the uses I've found for reflection in reading this book. Trust me, you don't have to be a software tool developer to get good mileage from it. I highly recommend it. I list the table of contents just because the product description does not have it listed:

1 A few basics 1
2 Accessing fields reflectively 27
3 Dynamic loading and reflective construction 49
4 Using Java's dynamic proxy 73
5 Call stack introspection 107
6 Using the class loader 121
7 Reflective code generation 143
8 Design patterns 179
9 Evaluating performance 207
10 Reflecting on the future 225
appendix A Reflection and metaobject protocols 241
appendix B Handling compilation errors in the "Hello world!" program 253
appendix C UML 256

I subtract one star because this book was written before Java 1.5 came out, and new features have been added. However, it is still a good place to start when you want to see just what reflection in Java can do for you.

Riccardo Audano said
This book starts great, brilliant and clear, and then crushes and burns to a cold, dark, inglorious end. The first six chapters are very well written and informative. Thanks to them some pieces that were still missing from my java puzzle clicked into place nicely. Any reasonably experienced java programmer will find interesting material on the basics of reflection, manipulating fields and methods, dynamic object construction, stack trace reflection and the uses of a dynamic proxy.
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.

Ganeshji Marwaha said
Java Reflection is an extremely powerful API that most modern frameworks like Spring, Hibernate, XFire, JUnit etc., use as its core enabling technology. We all use these frameworks for our applications, but never really bothered to see how the framework achieves certain things that it does.

This might be because we think reflection
1. is complex --> maybe it is, but do you think that it should stop you when it can cater more elegant/flexible/reusable code.
2. is an advanced topic --> maybe it is, that is why we are learning this after learning the basics first.
3. performs poorly --> Latest versions of java has considerably improved reflection API's performance. Also, there is an entire chapter dedicated to performance in this book.
4. for framework programmers only --> Not any more
5. is not necessary --> It is not necessary until you learn it. Once you learn it, you will wonder how you even lived without it.

Today, Reflection is being used even for application requirements such as
1. Flexible and re-usable code
2. Implementing cross-cutting concerns
3. Validation
4. Dynamic addition/modification of functionality... and much more...

In this book, the Foremans do an excellent job of preaching the advantages of using reflection. They are pragmatic in that, they keep you adviced that reflection is a magic-bullet only when used for the right purposes. The chapters are well organized and starts with the basics and goes all the way to the most advanced concepts.

The authors start with a simple scenario where reflection could be useful. Using this scenario they teach the basics of Class and Method objects, how to find the class of an object at runtime, how to invoke a method on that object etc. The next couple of chapters takes "Serialization of Java objects to XML" as the use-case and explains the concepts of accessing fields reflectively, dynamic class loading, dynamic object construction etc. Moving onto the advanced concepts, the author takes you through a tour of dynamic proxies, custom classloaders, call stack introspection, performance metrics etc.

At this point, the authors stop to point out the shortcomings of the java reflection API and show you how to use Code-Generation technique to work around those limitations.

Finally, the authors give a sneak-peek at java 5 and how it is going to change the reflection API.

I have a couple of suggestions for this book though
1. This book needs a java 5 update
2. Java beans API could have been discussed at a shallow level atleast. Though java beans does not belong to the reflection api, it is <-- well="" --="">> indirectly related.

Overall, I found this book easy to read, rich in content, pragmatic in advice and i highly recommend it to any "moderate to experienced" java developer who wants to construct code that speaks for itself.

Yakov Fain said
If, for any reason I had to keep only 10 Java books, this one would have made this list. It's consise, up to the point and covers not only Java reflection, but many other important subjects such as class loaders, design patterns, design with interfaces, proxies, et al.

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.

wiredweird said
This book presents an unusual but surprisingly important Java API, the one that underlies component technologies, dynamic loading, and more. Reflection isn't just for ubergeeks writing debuggers and similarly gutsy applications, it can help with everyday tasks of many kinds. The Formans show how, using many examples and a friendly but technically dense style.

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.

//wiredweird

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan