ADBA Add type safe newFactory method

Douglas Surber douglas.surber at
Fri Jul 13 16:46:03 UTC 2018

This is a good question. I defined it the way I did specifically to avoid the explicit cast. I have no strong opinion on which is better. I’ll ask for opinions internally and I hope other members of this list give their thoughts.


> On Jul 13, 2018, at 8:26 AM, Mark Rotteveel <mark at> wrote:
> 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