logo资料库

Java+Reflection+in+Action(英文版).pdf

第1页 / 共297页
第2页 / 共297页
第3页 / 共297页
第4页 / 共297页
第5页 / 共297页
第6页 / 共297页
第7页 / 共297页
第8页 / 共297页
资料共297页,剩余部分请下载后查看
Cover
contents
preface
A few basics
1.1 Reflection’s value proposition
1.2 Enter George the programmer
1.2.1 Choosing reflection
1.2.2 Programming a reflective solution
1.3 Examining running programs
1.4 Finding a method at runtime
1.5 Representing types with class objects
1.5.1 Representing primitive types
1.5.2 Representing interfaces
1.5.3 Representing array types
1.6 Understanding method objects
1.6.1 Using dynamic invocation
1.6.2 Using primitives with dynamic invocation
1.6.3 Avoiding invocation pitfalls
1.7 Diagramming for reflection
1.8 Navigating the inheritance hierarchy
1.8.1 Introspecting the inheritance hierarchy
1.8.2 Exposing some surprises
1.8.3 Another reflective circularity
1.9 Summary
Accessing fields reflectively
2.1 Serializing objects
2.1.1 Serializing to XML
2.1.2 Choosing reflection
2.1.3 Designing serialization with reflection
2.2 Finding fields at runtime
2.3 Understanding field objects
2.4 Getting and setting field values
2.5 Examining modifiers
2.5.1 Introducing Member
2.5.2 Interface introspection pitfall
2.5.3 Introspecting for instance variables
2.6 Accessing nonpublic members
2.7 Working with arrays
2.8 Serialization: putting it all together
2.8.1 Serializing each component
2.8.2 Serializing instance variables
2.9 Using reflective serialization
2.10 Summary
Dynamic loading and reflective construction
3.1 George’s deployment problem
3.1.1 Designing with patterns
3.1.2 Programming a reflective solution
3.1.3 Enhancing the factory method with reflection
3.1.4 Combining benefits of delegation and reflection
3.2 Loading classes dynamically
3.2.1 Basics of forName
3.2.2 Getting array classes
3.2.3 Primitives and forName
3.3 Constructing objects reflectively
3.3.1 Reflective construction basics
3.3.2 Using constructor objects
3.3.3 Constructing arrays reflectively
3.4 Designing for dynamic loading
3.4.1 Disadvantages of reflective construction with arguments
3.4.2 Initializing through an interface
3.5 Implementing deserialization
3.5.1 Initiating deserialization
3.5.2 Constructing the instances
3.5.3 Restoring the object structure
3.6 George’s serialization: limitations
3.6.1 No interaction with readObject or writeObject
3.6.2 No handling of final instance variables
3.6.3 Only no-argument constructors
3.6.4 No handling of illegal XML characters
3.6.5 Performance
3.7 Summary
Using Java dynamic proxy
4.1 Working with proxies
4.2 George’s tracing problem
4.3 Exploring Proxy
4.3.1 Understanding invocation handlers
4.3.2 Handling the methods of Object
4.4 Implementing a tracing proxy
4.5 A note on factories
4.6 Chaining proxies
4.6.1 Structuring invocation handlers for chaining
4.6.2 Implementing a synchronized proxy
4.6.3 Chaining the two proxies
4.7 Stubbing interfaces for unit testing
4.7.1 Examining stubs
4.7.2 Design for stubbing with Proxy
4.7.3 Implementation of stubbing with Proxy
4.8 Generating SOAP remote proxies
4.9 Pitfalls of using Proxy
4.10 Summary
Call stack introspection
5.1 George’s logging problem
5.2 Performing call stack introspection
5.3 Logging with call stack introspection
5.4 Pitfalls
5.5 Class invariant checking
5.6 Summary
Using the class loader
6.1 George’s test problem
6.2 Essentials of ClassLoader
6.2.1 Understanding the delegation model
6.2.2 Programming a simple class loader
6.2.3 Reinitializing static fields: a solution
6.3 Multiple namespaces
6.4 Dynamic class replacement
6.4.1 Designing for replacement
6.4.2 Implementing replacement
6.4.3 Simplifying assumptions
6.5 Additional considerations
6.5.1 Security
6.5.2 Don’t reinvent the wheel
6.5.3 Modifying bytecode in a class loader
6.5.4 When not to invent a specialized class loader
6.5.5 Additional examples
6.5.6 Endorsed Standards Override
6.6 Summary
Reflective code generation
Reflective code generation
7.1 Generating HelloWorld.java
7.2 Class-to-class transformation framework
7.2.1 C2C
7.2.2 Args
7.2.3 C2CConstructor
7.2.4 C2CTransformation
7.3 Example: extent management
7.4 C2IdentitySubclassOfC and its subclasses
7.5 UQueue
7.6 Using the framework
7.7 Relation to Aspect-Oriented Programming
7.8 Summary
Design patterns
8.1 Singleton
8.2 Decorator class-to-class transformations
8.3 Proxy (again)
8.4 Another composition feature
8.5 Problematic issues in writing class-to-class transformations
8.6 Summary
Evaluating performance
Evaluating performance
9.1 Categorizing performance impact
9.2 Using microbenchmarks
9.3 Benchmarking two ways to use Proxy
9.4 Understanding Amdahl’s Law
9.5 Applying Amdahl’s Law
9.6 Summary
Reflecting on the future
10.1 Looking forward: Java 1.5
10.1.1 JSR 14-Generics
10.1.2 JSR 175-Annotation Facility
10.1.3 JSR 201-Language extensions
10.1.4 Impact of Java 1.5 on reflective code
10.2 Looking forward: competition for Java reflection
10.2.1 C#
10.2.2 Python
10.2.3 Smalltalk
10.2.4 CLOS
10.2.5 Ruby
10.2.6 Perl
10.3 Looking forward: Aspect-Oriented Programming
10.4 Looking forward: your career
Reflection and metaobject protocols
A.1 Reflection
A.2 Reflective object-oriented programming
A.3 Inheritance
A.4 Metaobject protocols
A.5 Metaclasses
A.6 Class-to-class transformations
Handling compilation errors in the “Hello world!” program
UML
glossary
references
index
Ira R. Forman Nate Forman M A N N I N G
PRAISE FOR ... Java Reflection in Action Java Reflection in Action is unique in presenting a clear account of all the cool things you can do with reflection, and at the same time pro- viding the sound conceptual basis that developers need to create advanced applications. The book includes careful explanations of sometimes perplexing programming techniques along with enough background to understand how to extend and vary them. This book overcomes reflection’s reputation as a mysterious and esoteric philo- sophical pursuit, or as a set of messy error-prone coding tricks. As reflection becomes increasingly common and useful in all sorts of applications, it is great to finally have a book that features disci- plined yet still creative and fun software engineering practices based on reflection. Even occasional users will immediately adopt the book’s patterns and idioms to solve common problems. Many of the examples can be directly adapted for customized solutions in diverse areas such as XML processing, automated software testing, and pro- gram analysis tools. Readers will also find underlying rationales for code performing introspection, proxies, class loading, and so on, that are often seen but not often explained well in everyday Java pro- grams. And even experts will find new ideas and well-thought out advice for using some of the more subtle aspects of reflection. —Prof. Doug Lea, SUNY Oswego, author of Concurrent Programming in Java Java has brought reflection to the programming masses, but they're still struggling with it. The Formans turn struggle into adventure as they guide you through one compelling example after another, each one illustrating reflection’s power while avoiding its pitfalls. —Dr. John Vlissides, IBM —coauthor of Design Patterns
Java Reflection in Action IRA R. FORMAN NATE FORMAN M A N N I N G Greenwich (74° w. long.)
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Greenwich, CT 06830 Fax: (203) 661-9018 email: manning@manning.com ©2005 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books they publish printed on acid-free paper, and we exert our best efforts to that end. Manning Publications Co. 209 Bruce Park Avenue Greenwich, CT 06830 Copyeditor: Linda Recktenwald Typesetter: Dottie Marsico Cover designer: Leslie Haimes ISBN 1-932394-18-4 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – VHG – 07 06 05 04
To Janet/Mom This project wouldn’t have happened without your love and support.
contents xiii preface acknowledgments about this book about the title about the cover illustration xxi xv xvii xx 1 A few basics 1 1.1 Reflection’s value proposition 3 1.2 Enter George the programmer 4 Choosing reflection 5 ■ Programming a reflective solution 6 1.3 Examining running programs 8 1.4 Finding a method at runtime 10 1.5 Representing types with class objects 12 Representing primitive types 13 ■ Representing interfaces 13 Representing array types 14 1.6 Understanding method objects 14 Using dynamic invocation 15 ■ Using primitives with dynamic invocation 16 ■ Avoiding invocation pitfalls 17 1.7 Diagramming for reflection 19 vii
分享到:
收藏