【例子介绍】java反射实战
【相关图片】
【源码结构】
contents
preface xiii
acknowledgments xv
about this book xvii
about the title xx
about the cover illustration xxi
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
viii CONTENTS
1.8 Navigating the inheritance hierarchy 20
Introspecting the inheritance hierarchy 22
■
Exposing some
surprises 23
■
Another reflective circularity 24
1.9 Summary 26
2
Accessing fields reflectively 27
2.1 Serializing objects 28
Serializing to XML 29
■
Choosing reflection 30
Designing serialization with reflection 30
2.2 Finding fields at runtime 31
2.3 Understanding field objects 33
2.4 Getting and setting field values 34
2.5 Examining modifiers 35
Introducing Member 36
■
Interface introspection pitfall 37
Introspecting for instance variables 37
2.6 Accessing nonpublic members 38
2.7 Working with arrays 40
2.8 Serialization: putting it all together 41
Serializing each component 43
■
Serializing instance variables 43
2.9 Using reflective serialization 45
2.10 Summary 48
3
Dynamic loading and reflective construction 49
3.1 George’s deployment problem 50
Designing with patterns 51
■
Programming a reflective solution 52
Enhancing the factory method with reflection 54
■
Combining
benefits of delegation and reflection 54
3.2 Loading classes dynamically 55
Basics of forName 55
■
Getting array classes 56
Primitives and forName 56
3.3 Constructing objects reflectively 57
Reflective construction basics 57
■
Using constructor objects 57
Constructing arrays reflectively 59
3.4 Designing for dynamic loading 60
Disadvantages of reflective construction with arguments 61
Initializing through an interface 62
CONTENTS ix
3.5 Implementing deserialization 63
Initiating deserialization 64
■
Constructing the instances 65
Restoring the object structure 66
3.6 George’s serialization: limitations 69
No interaction with readObject or writeObject 69
■
No handling of
final instance variables 70
■
Only no-argument constructors 70
No handling of illegal XML characters 70
■
Performance 71
3.7 Summary 71
4
Using Java’s dynamic proxy 73
4.1 Working with proxies 74
4.2 George’s tracing problem 76
4.3 Exploring Proxy 77
Understanding invocation handlers 79
Handling the methods of Object 80
4.4 Implementing a tracing proxy 81
4.5 A note on factories 84
4.6 Chaining proxies 86
Structuring invocation handlers for chaining 86
■
Implementing
a synchronized proxy 88
■ Chaining the two proxies
89
4.7 Stubbing interfaces for unit testing 90
Examining stubs 90
■
Design for stubbing with Proxy 91
Implementation of stubbing with Proxy 93
4.8 Generating SOAP remote proxies 99
4.9 Pitfalls of using Proxy 103
4.10 Summary 105
5
Call stack introspection 107
5.1 George’s logging problem 108
5.2 Performing call stack introspection 111
5.3 Logging with call stack introspection 112
5.4 Pitfalls 114
5.5 Class invariant checking 115
5.6 Summary 120
x CONTENTS
6
Using the class loader 121
6.1 George’s test problem 122
6.2 Essentials of ClassLoader 123
Understanding the delegation model 123
■
Programming a simple
class loader 127
■
Reinitializing static fields: a solution 128
6.3 Multiple namespaces 130
6.4 Dynamic class replacement 132
Designing for replacement 132
■
Implementing replacement 134
Simplifying assumptions 137
6.5 Additional considerations 138
Security 139
■
Don’t reinvent the wheel 139
■
Modifying bytecode in a
class loader 140
■
When not to invent a specialized class loader 140
Additional examples 141
■
Endorsed Standards Override 142
6.6 Summary 142
7
Reflective code generation 143
7.1 Reflective code generation 143
7.2 Generating HelloWorld.java 145
7.3 Class-to-class transformation framework 147
C2C 148
■
Args 152
■
C2CConstructor 154
C2CTransformation 157
7.4 Example: extent management 159
7.5 C2IdentitySubclassOfC and its subclasses 168
7.6 UQueue 170
7.7 Using the framework 173
7.8 Relation to Aspect-Oriented Programming 175
7.9 Summary 176
8
Design patterns 179
8.1 Singleton 181
8.2 Decorator class-to-class transformations 187
8.3 Proxy (again) 197
8.4 Another composition feature 201
CONTENTS xi
8.5 Problematic issues in writing
class-to-class transformations 201
8.6 Summary 204
9
Evaluating performance 207
9.1 Evaluating performance 207
9.2 Categorizing performance impact 209
9.3 Using microbenchmarks 210
9.4 Benchmarking two ways to use Proxy 214
9.5 Understanding Amdahl’s Law 218
9.6 Applying Amdahl’s Law 221
9.7 Summary 223
10
Reflecting on the future 225
10.1 Looking forward: Java 1.5 226
JSR 14—Generics 227
■ JSR 175—Annotation Facility
229
JSR 201—Language extensions 234
Impact of Java 1.5 on reflective code 235
10.2 Looking forward: competition for Java reflection 236
C# 236
■
Python 236
■
Smalltalk 236
■
CLOS 237
Ruby 237
■
Perl 237
10.3 Looking forward: Aspect-Oriented Programming 237
10.4 Looking forward: your career 238
appendix A Reflection and metaobject protocols 241
appendix B Handling compilation errors in the
“Hello world!” program 253
appendix C UML 256
glossary 258
references 260
index 267
xiii
preface
评论