12 Reflection in a modular world

 

This chapter covers:

  • why exports directives do not suffice for reflection
  • opening packages and modules to reflection
  • updating reflection-based code to work with modules
  • alternatives to the reflection API
  • analyzing and modifying module properties
  • creating module layers to dynamically load modules at run time

If you’re working on a Java application, chances are very good that you rely on Spring, Hibernate, JAXP, GSON, or the like. What are "the like"? Frameworks that use Java’s reflection API to inspect your code, search for annotations, instantiate objects, or call methods. Thanks to reflection, they can do all that without having to compile against your code.

Moreover, the reflection API allows frameworks to access non-public classes and non-public members. It has superpowers beyond what is possible with compiled code, that bounces off of package boundaries if classes or members are not public. The thing is, with modules, reflection doesn’t work out of the box, anymore.

Quite the opposite, reflection lost its superpowers and is bound to the exact same accessibility rules as compiled code: It can only access public members of public classes in exported packages. These frameworks, on the other hand, use reflection over fields and methods that very often aren’t public and on classes that you might not want to export because they are not part of a module’s API. What to do then? That’s what this chapter is all about!

12.1  Why exports directives are no good fit for reflection

12.1.1  Breaking into non-modular code

12.1.2  Forcing the publication of internal types

12.1.3  Qualified exports create coupling to specific modules

12.1.4  No support for deep reflection

12.2  Open packages and modules—designed for the reflection use case

12.2.1 Opening packages to run-time access

12.2.2  Opening packages for specific modules

12.2.3  Exporting versus opening packages

12.2.4  Opening modules—reflection closeout

12.3  Reflecting over modules

12.3.1  Updating reflecting code for modules (or not)

sitemap