Hi Gunnar,
Thanks. Yes, I suspected you were using ServiceLoader::load to get a
dummy instance of each custom constraint validator class (in general,
there might be multiple modules providing a validator for @Min-on-int),
and then obtaining each dummy's Class object and doing your own
instantiations of validator classes thereafter.
It wouldn't help if ServiceLoader simply returned the Class objects for
service implementations, because you still wouldn't be able to
newInstance() them without the export. You would need ServiceLoader to
take one of those Class objects back (call it cImpl), along with the
Class object for a service interface (call it cIntf), and then
instantiate cImpl on your behalf if a) cIntf.isAssignableFrom(cImpl) and
b) <<some caller-sensitive conditions>>. No more export needed.
Alex
On 6/21/2016 11:55 PM, Gunnar Morling wrote:
Hi Alex,
Good question, I should have mentioned some more details on this.
Hibernate Validator is using the service loader here to discover
constraint validator *types*, but then it itself is going to create (and
configure) instances of these types based on the annotation properties
set for specific usages of a constraint. I.e. there will be one instance
of a validator per constraint usage (the instance obtained from the
service loader will be dropped once its type has been examined).
E.g. two instances of the constraint validator type for @Min on int
would be created here:
class Foo {
@Min(1)
int bar;
@Min(2)
int baz;
}
One might argue that this is a bit mis-using the service loader
mechanism and we should rather detect some sort of constraint validator
factory in charge of creating actual constraint validator instances (in
which case we could take the instance returned from the service loader
as is).
But then it's working good enough in its current form and is simpler on
implementors that way (who just need to provide/expose constraint
validators and nothing more). But it explains why that export is needed
under Jigsaw.
Alternatively, an API for obtaining just the types of services (not
instances) might help with this case.
--Gunnar