Using Compile Time Annotations for Modularizing URLStreamHandlerFactory

Paul Sandoz paul.sandoz at oracle.com
Mon Jul 2 11:02:15 PDT 2012


Hi,

A bit late replying...

On Jun 18, 2012, at 12:43 PM, Jaroslav Tulach wrote:

> Dne Pá 15. června 2012 17:17:32, Jesse Glick napsal(a):
>> Something to consider - in a big application it is not uncommon to have
>> multiple URL handlers implemented in various places. Yet the JRE only lets
>> you set a single URLStreamHandlerFactory for the whole JVM. 
> 
> What a coincidence! On Friday I gave a talk at GeekOut, Tallinn, Estonia about 
> annotation processors and my main example was creation of effective, modular, 
> scalable, robust, condense, no-boiler plate and lazy support for registration 
> of URLStreamHandlers.
> 
>> So for the
>> application to be modular, you need to write a proxy factory which accepts
>> modular registrations, 
> 
> Indeed I had to start with Proxy factory. Then I demonstrated classical 
> ServiceLoader approach (which you probably have in mind when thinking about 
> modularity & URLStreamHandlers). I continued showing, that it is not scalable. 
> To fix that I provided annotation and its processor. Still the code was too 
> verbose. I fixed that by allowing direct registration of URLConnection - and 
> generating the rest on a fly. At the end I eliminated almost all boiler plate 
> code in my final example[1] by generating wrapper URLConnection registration 
> by the annotation processor.

This reminds me of types of thing one can do with dependency injection frameworks e.g. register a CDI-based or Guice-based proxy to do the look up.

CDI implementations will scan, at runtime, the Java class files on the classpath for annotations and creates bindings from those, where as Guice requires a more explicit binding using Module (unless one layers on top). My preference is Guice's explicit binding rather than CDI's approach where everything is spread out.

IMHO this is an area worth exploring further.


> 
> Source code: https://github.com/jtulach/Annotations
> Presentation: http://wiki.apidesign.org/images/0/08/HackIntoYourCompiler.pdf
> 
> I know many of the ideas of using annotations go beyond the scope of Jigsaw 
> and you have your own problems, but (as I think this is the way effective 
> modularity should be built) I'd like to be able to build this on top of 
> Jigsaw. Won't Jigsaw try to prevent us doing this?

At the moment there no mechanism in javac to augment a module declaration with "provides service S with P;", plus the module declaration would need "requires M;" for the module M that exports the type S. And in general how annotation processors hook up in the module model still needs to be worked out since they might have a different dependency graph to the application itself.

I am still not sure in general about the annotation processor approach. It is rather clever and neat but, i wonder if it might be possible to explore an approach of a richer DI binding model some part declarative for the resolver to chew on and some part runtime for registration of the binding implementations.

Paul.


More information about the jigsaw-dev mailing list