ADBA Add type safe newFactory method

Mark Rotteveel mark at lawinegevaar.nl
Fri Jul 13 15:26:27 UTC 2018


The existing DataSourceFactory.newFactory(String name) has an unchecked 
cast using a (possibly inferred) type parameter.

Would it make sense to add an alternative with the signature:

public static <T extends DataSourceFactory> T newFactory(Class<T> clazz)

When you use this, you are tied to an ADBA implementation at compile 
time, but it will make this more type-safe.

And maybe the unchecked cast (and type parameter) should be removed from 
the variant taking a name only.

That is, change the method to

   public static DataSourceFactory newFactory(String name) {
     if (name == null) throw new 
IllegalArgumentException("DataSourceFactory name is null");
     return ServiceLoader
             .load(DataSourceFactory.class)
             .stream()
             .filter(p -> p.type().getName().equals(name))
             .findFirst()
             .map(Provider::get)
             .orElse(null);
   }

Any casts would then be the explicit responsibility of the developer, 
instead of accidental due to type inference.

Mark
-- 
Mark Rotteveel


More information about the jdbc-spec-discuss mailing list