Coerced Access via Reflection
Java’s reflection features distinguish it from most other strongly typed, compiled languages. Java reflection allows deep introspection and manipulation of types at runtime. Used judiciously, this can lead to highly capable generic code in production systems, although care must be taken to avoid the inherent performance penalty. You can also commit great evils with the power of reflection.5 However, let’s use it for good: to increase our ability to test.
5. For some great examples of the evils that can be committed, see http://stackoverflow.com/questions/2481862/how-to-limit-setaccessible-to-only-legitimate-uses. My favorite part is making Strings mutable.
Consider a simple class with only a single private method, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access