Posts

Showing posts from November, 2022

The "exposes" Keyword in Java

Don't worry. You have not been sleeping under a rock for the past few years. There is no such keyword in Java. It's just something I wish for Christmas. It's great that Java modules finally allow us to truly seal off our carefully crafted API surfaces, without clients being able to crack them open anyway using reflection . If a module does not " open" a package to the outside world, there is no way you can access its private and package-private m embers. No more reflectively setting a private field's accessible flag to true and force it to yield or change its value. Unfortunately, you can also no longer use reflection to dynamically read/write public fields or execute public methods. It does not matter whether the fields and methods hail from an exported or non-exported package. As long as the module does not also open the package, any reflection is off limits. In fact you no longer have any self-contained (purely programmatic) way of knowing what publi...

Type Maps

Every so often I run into a problem that would just really neatly be solved by something I have come to call a TypeMap . The problem may not be so common that you would expect the JDK itself to provide a ready-made class for it. To me, hoewever, it seemed common enough that I was suprised to be unable to google my way towards the functionality I was after. Actually, I did stumble upon a class named exactly that: TypeMap . It was buried deep inside the JDK code base itself. And with a name like that, how could it not have the functionality I was after? But the class was in one of those not-for-you-earthling packages, so I didn't check. So what does a TypeMap do? Obviously i t is a type of Map . And also rather obviously, it is a Map whose keys are Class objects. But its get() and containsKey() methods behave in a way that clearly takes it beyond a regular map. They rely on, and leverage the structure of the Java type hierarchy to retrieve a value for the requested key. ...