Skip to content

Commit

Permalink
dedup/simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Feb 23, 2024
1 parent 1fa0066 commit bbd8616
Showing 1 changed file with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,12 @@ private static boolean passesStaticAvailabilityCheck(ServiceLoader.Provider<?> p

@VisibleForTesting
static boolean passesStaticAvailabilityCheck(Class<?> type) {
try {
return passesAvailabilityCheck(type, null);
} catch (ExceptionInInitializerError | NoClassDefFoundError | RuntimeException t) {
LOG.warn("Unable to load service provider {}.", type.getName(), t);
return false;
}
return silentlyPassesAvailabilityCheck(type, null);
}

@VisibleForTesting
static boolean passesInstanceAvailabilityCheck(Object instance) {
try {
return passesAvailabilityCheck(instance.getClass(), instance);
} catch (RuntimeException rte) {
LOG.warn("Unable to load service provider {}.", instance.getClass().getName(), rte);
return false;
}
return silentlyPassesAvailabilityCheck(instance.getClass(), instance);
}

private static void logServiceIsAvailable(Class<?> apiType, Class<?> implType) {
Expand All @@ -111,6 +101,15 @@ private static void logServiceIsAvailable(Class<?> apiType, Class<?> implType) {
}
}

private static <T> boolean silentlyPassesAvailabilityCheck(Class<? extends T> type, @Nullable T instance) {
try {
return passesAvailabilityCheck(type, instance);
} catch (ExceptionInInitializerError | NoClassDefFoundError | RuntimeException e) {
LOG.warn("Unable to load service provider {}.", type.getName(), e);
return false;
}
}

private static <T> boolean passesAvailabilityCheck(Class<? extends T> type, @Nullable T instance) {
if (!type.isAnnotationPresent(CheckAvailability.class)) {
return true; // if type is not annotated, skip tests
Expand Down

0 comments on commit bbd8616

Please sign in to comment.