Skip to content

Commit

Permalink
Make resolution algorithm of ConnectionDetailsFactory more explicit
Browse files Browse the repository at this point in the history
This commit moves the resolution check for ConnectionDetailsFactory
to a dedicated method to make it more clear that it is meant to verify
that the implementation is resolved and can be loaded from the
classpath.

The previous algorithm relied on a behavior of ResolvableType that only
resolves the first level generics. Further improvements in Spring
Framework 6.2 make this check invalid as some implementations use a
Container that can hold a nested generic.
  • Loading branch information
snicoll committed Feb 23, 2024
1 parent 118120c commit 7f2cc74
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -122,13 +122,29 @@ record Registration<S, D extends ConnectionDetails>(Class<S> sourceType, Class<D
@SuppressWarnings("unchecked")
private static <S, D extends ConnectionDetails> Registration<S, D> get(ConnectionDetailsFactory<S, D> factory) {
ResolvableType type = ResolvableType.forClass(ConnectionDetailsFactory.class, factory.getClass());
if (!type.hasUnresolvableGenerics()) {
if (isResolved(type)) {
Class<?>[] generics = type.resolveGenerics();
return new Registration<>((Class<S>) generics[0], (Class<D>) generics[1], factory);
}
return null;
}

/**
* Check if this {@link ConnectionDetailsFactory} type has its generics resolved
* to a concrete type. Return {@code true} if the type is resolved and can be
* loaded from the classpath, {@code false} otherwise.
* @param type the type to inspect
* @return {@code true} if the type has its generic resolved
*/
private static boolean isResolved(ResolvableType type) {
for (Class<?> genericRawType : type.resolveGenerics()) {
if (genericRawType == null) {
return false;
}
}
return true;
}

}

}

0 comments on commit 7f2cc74

Please sign in to comment.