Service provider factory <was> Re: Dump services webrev

Paul Sandoz paul.sandoz at oracle.com
Tue May 29 02:05:57 PDT 2012


On May 25, 2012, at 6:02 PM, Jesse Glick wrote:
>> there was also the suggestion that the "provides service" should allow a static factory method to be specified
> 
> This is very handy, and a good idea if you are dropping the legacy META-INF/services/* format anyway. This avoids the need to create new subclasses for minor variants of a service.
> 
> You could also allow static final fields - typically the service provider is a singleton with no instance state so there is no real overhead associated with a field. This could be especially convenient in conjunction with lambdas:
> 
> provides service java.nio.file.spi.FileTypeDetector with my.Utils.DETECTOR;
> ...
> package my;
> public class Utils {
>  public static final FileTypeDetector DETECTOR = (f) -> f.endsWith(".myext") ? "text/mine" : null;
> }

If we cannot reference fields because there is no language syntax to do so we might be able to reuse the lambda method reference syntax:
 
  provides service java.nio.file.spi.FileTypeDetector with my.Utils::detector;
  ...
  package my;
  public class Utils {
    private static final FileTypeDetector DETECTOR = (f) -> f.endsWith(".myext") ? "text/mine" : null;
    public static final FileTypeDetector detector() = { return DETECTOR; }
  }

Paul.


More information about the jigsaw-dev mailing list