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. Java Reflection in Action......Page 1 contents......Page 8 preface......Page 14 acknowledgments......Page 16 about this book......Page 18 about the title......Page 21 about the cover illustration......Page 22 A few basics......Page 24 1.1 Reflection’s value proposition......Page 26 1.2 Enter George the programmer......Page 27 1.2.1 Choosing reflection......Page 28 1.2.2 Programming a reflective solution......Page 29 1.3 Examining running programs......Page 31 1.4 Finding a method at runtime......Page 33 1.5 Representing types with class objects......Page 35 1.5.2 Representing interfaces......Page 36 1.6 Understanding method objects......Page 37 1.6.1 Using dynamic invocation......Page 38 1.6.2 Using primitives with dynamic invocation......Page 39 1.6.3 Avoiding invocation pitfalls......Page 40 1.7 Diagramming for reflection......Page 42 1.8 Navigating the inheritance hierarchy......Page 43 1.8.1 Introspecting the inheritance hierarchy......Page 45 1.8.2 Exposing some surprises......Page 46 1.8.3 Another reflective circularity......Page 47 1.9 Summary......Page 49 Accessing fields reflectively......Page 50 2.1 Serializing objects......Page 51 2.1.1 Serializing to XML......Page 52 2.1.3 Designing serialization with reflection......Page 53 2.2 Finding fields at runtime......Page 54 2.3 Understanding field objects......Page 56 2.4 Getting and setting field values......Page 57 2.5 Examining modifiers......Page 58 2.5.1 Introducing Member......Page 59 2.5.3 Introspecting for instance variables......Page 60 2.6 Accessing nonpublic members......Page 61 2.7 Working with arrays......Page 63 2.8 Serialization: putting it all together......Page 64 2.8.2 Serializing instance variables......Page 66 2.9 Using reflective serialization......Page 68 2.10 Summary......Page 71 Dynamic loading and reflective construction......Page 72 3.1 George’s deployment problem......Page 73 3.1.1 Designing with patterns......Page 74 3.1.2 Programming a reflective solution......Page 75 3.1.4 Combining benefits of delegation and reflection......Page 77 3.2.1 Basics of forName......Page 78 3.2.3 Primitives and forName......Page 79 3.3.2 Using constructor objects......Page 80 3.3.3 Constructing arrays reflectively......Page 82 3.4 Designing for dynamic loading......Page 83 3.4.1 Disadvantages of reflective construction with arguments......Page 84 3.4.2 Initializing through an interface......Page 85 3.5 Implementing deserialization......Page 86 3.5.1 Initiating deserialization......Page 87 3.5.2 Constructing the instances......Page 88 3.5.3 Restoring the object structure......Page 89 3.6.1 No interaction with readObject or writeObject......Page 92 3.6.4 No handling of illegal XML characters......Page 93 3.7 Summary......Page 94 Using Java’s dynamic proxy......Page 96 4.1 Working with proxies......Page 97 4.2 George’s tracing problem......Page 99 4.3 Exploring Proxy......Page 100 4.3.1 Understanding invocation handlers......Page 102 4.3.2 Handling the methods of Object......Page 103 4.4 Implementing a tracing proxy......Page 104 4.5 A note on factories......Page 107 4.6.1 Structuring invocation handlers for chaining......Page 109 4.6.2 Implementing a synchronized proxy......Page 111 4.6.3 Chaining the two proxies......Page 112 4.7.1 Examining stubs......Page 113 4.7.2 Design for stubbing with Proxy......Page 114 4.7.3 Implementation of stubbing with Proxy......Page 116 4.8 Generating SOAP remote proxies......Page 122 4.9 Pitfalls of using Proxy......Page 126 4.10 Summary......Page 128 Call stack introspection......Page 130 5.1 George’s logging problem......Page 131 5.2 Performing call stack introspection......Page 134 5.3 Logging with call stack introspection......Page 135 5.4 Pitfalls......Page 137 5.5 Class invariant checking......Page 138 5.6 Summary......Page 143 Using the class loader......Page 144 6.1 George’s test problem......Page 145 6.2.1 Understanding the delegation model......Page 146 6.2.2 Programming a simple class loader......Page 150 6.2.3 Reinitializing static fields: a solution......Page 151 6.3 Multiple namespaces......Page 153 6.4.1 Designing for replacement......Page 155 6.4.2 Implementing replacement......Page 157 6.4.3 Simplifying assumptions......Page 160 6.5 Additional considerations......Page 161 6.5.2 Don’t reinvent the wheel......Page 162 6.5.4 When not to invent a specialized class loader......Page 163 6.5.5 Additional examples......Page 164 6.6 Summary......Page 165 Reflective code generation......Page 166 7.1 Generating HelloWorld.java......Page 168 7.2 Class-to-class transformation framework......Page 170 7.2.1 C2C......Page 171 7.2.2 Args......Page 175 7.2.3 C2CConstructor......Page 177 7.2.4 C2CTransformation......Page 180 7.3 Example: extent management......Page 182 7.4 C2IdentitySubclassOfC and its subclasses......Page 191 7.5 UQueue......Page 193 7.6 Using the framework......Page 196 7.7 Relation to Aspect-Oriented Programming......Page 198 7.8 Summary......Page 199 Design patterns......Page 202 8.1 Singleton......Page 204 8.2 Decorator class-to-class transformations......Page 210 8.3 Proxy (again)......Page 220 8.5 Problematic issues in writing class-to-class transformations......Page 224 8.6 Summary......Page 227 Evaluating performance......Page 230 9.1 Categorizing performance impact......Page 232 9.2 Using microbenchmarks......Page 233 9.3 Benchmarking two ways to use Proxy......Page 237 9.4 Understanding Amdahl’s Law......Page 241 9.5 Applying Amdahl’s Law......Page 244 9.6 Summary......Page 246 Reflecting on the future......Page 248 10.1 Looking forward: Java 1.5......Page 249 10.1.1 JSR 14-Generics......Page 250 10.1.2 JSR 175-Annotation Facility......Page 252 10.1.3 JSR 201-Language extensions......Page 257 10.1.4 Impact of Java 1.5 on reflective code......Page 258 10.2.3 Smalltalk......Page 259 10.3 Looking forward: Aspect-Oriented Programming......Page 260 10.4 Looking forward: your career......Page 261 Reflection and metaobject protocols......Page 264 A.1 Reflection......Page 265 A.2 Reflective object-oriented programming......Page 267 A.3 Inheritance......Page 269 A.4 Metaobject protocols......Page 271 A.5 Metaclasses......Page 272 A.6 Class-to-class transformations......Page 274 Handling compilation errors in the “Hello world!” program......Page 276 UML......Page 279 glossary......Page 281 references......Page 283 index......Page 290
You are a Java developer. You are asked to add a simple feature to your application. But "simple" can be deceiving: you have to make many changes, in locations which can be difficult to find.
If this sounds familiar, you want to know about Java reflection. With reflection, you can work smarter by designing flexible applications to which you can easily add likely new requirements. Then, with a few code changes in easy-to-find places, you've got the job done. Reflection adds a new dimension to your programming skills. It will boost your effectiveness.
Java Reflection in Action starts from the basics. It gradually builds a complete understanding, adding as it goes reflective concepts illustrated with many small examples that are useful in real applications.
In a subplot, the book follows a programmer, George, as he tackles common but difficult tasks. In each case, George finds flexible, reflective solutions that replace the usual hard-coded ones. The power of reflection becomes clear through his story.
Publisher description: 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