Skip to content

Commit

Permalink
Reduce scope of "unchecked" suppression
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Sep 26, 2024
1 parent daea6f1 commit d013085
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ static <T> T instantiate(Class<T> spiInterface, Class<? extends T> implementatio
.invoke(findConstructor(spiInterface, implementationClass));
}

@SuppressWarnings("unchecked")
private static <T> Constructor<? extends T> findConstructor(Class<T> spiInterface,
Class<? extends T> implementationClass) {

return (Constructor<? extends T>) findBestConstructor(spiInterface, implementationClass);
}

/**
* Find the "best" constructor for the supplied implementation class.
*
Expand All @@ -38,9 +45,8 @@ static <T> T instantiate(Class<T> spiInterface, Class<? extends T> implementatio
* Otherwise, this method throws an exception stating that it failed to
* find a suitable constructor.
*/
@SuppressWarnings("unchecked")
private static <T, V extends T> Constructor<? extends V> findConstructor(Class<T> spiInterface,
Class<V> implementationClass) {
private static <T> Constructor<?> findBestConstructor(Class<T> spiInterface,
Class<? extends T> implementationClass) {

Preconditions.condition(!ReflectionUtils.isInnerClass(implementationClass),
() -> String.format("The %s [%s] must be either a top-level class or a static nested class",
Expand All @@ -50,12 +56,12 @@ private static <T, V extends T> Constructor<? extends V> findConstructor(Class<T

// Single constructor?
if (constructors.length == 1) {
return (Constructor<V>) constructors[0];
return constructors[0];
}
// Find default constructor.
for (Constructor<?> constructor : constructors) {
if (constructor.getParameterCount() == 0) {
return (Constructor<V>) constructor;
return constructor;
}
}
// Otherwise...
Expand Down

0 comments on commit d013085

Please sign in to comment.