package com.google.common.util.concurrent; import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.common.collect.Ordering; import java.lang.ref.WeakReference; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public final class FuturesGetChecked { private static final Ordering> WITH_STRING_PARAM_FIRST = Ordering.natural().onResultOf(new Function, Boolean>() { // from class: com.google.common.util.concurrent.FuturesGetChecked.1 @Override // com.google.common.base.Function public Boolean apply(Constructor constructor) { return Boolean.valueOf(Arrays.asList(constructor.getParameterTypes()).contains(String.class)); } }).reverse(); /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public interface GetCheckedTypeValidator { void validateClass(Class cls); } /* JADX INFO: Access modifiers changed from: package-private */ public static V getChecked(Future future, Class cls) throws Exception { return (V) getChecked(bestGetCheckedTypeValidator(), future, cls); } static V getChecked(GetCheckedTypeValidator getCheckedTypeValidator, Future future, Class cls) throws Exception { getCheckedTypeValidator.validateClass(cls); try { return future.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw newWithCause(cls, e); } catch (ExecutionException e2) { wrapAndThrowExceptionOrError(e2.getCause(), cls); throw new AssertionError(); } } /* JADX INFO: Access modifiers changed from: package-private */ public static V getChecked(Future future, Class cls, long j, TimeUnit timeUnit) throws Exception { bestGetCheckedTypeValidator().validateClass(cls); try { return future.get(j, timeUnit); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw newWithCause(cls, e); } catch (ExecutionException e2) { wrapAndThrowExceptionOrError(e2.getCause(), cls); throw new AssertionError(); } catch (TimeoutException e3) { throw newWithCause(cls, e3); } } private static GetCheckedTypeValidator bestGetCheckedTypeValidator() { return GetCheckedTypeValidatorHolder.BEST_VALIDATOR; } static GetCheckedTypeValidator weakSetValidator() { return GetCheckedTypeValidatorHolder.WeakSetValidator.INSTANCE; } static GetCheckedTypeValidator classValueValidator() { return GetCheckedTypeValidatorHolder.ClassValueValidator.INSTANCE; } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static class GetCheckedTypeValidatorHolder { static final String CLASS_VALUE_VALIDATOR_NAME = String.valueOf(GetCheckedTypeValidatorHolder.class.getName()).concat("$ClassValueValidator"); static final GetCheckedTypeValidator BEST_VALIDATOR = getBestValidator(); GetCheckedTypeValidatorHolder() { } /* loaded from: classes2.dex */ enum ClassValueValidator implements GetCheckedTypeValidator { INSTANCE; private static final ClassValue isValidClass = new ClassValue() { // from class: com.google.common.util.concurrent.FuturesGetChecked.GetCheckedTypeValidatorHolder.ClassValueValidator.1 @Override // java.lang.ClassValue protected /* bridge */ /* synthetic */ Boolean computeValue(Class cls) { return computeValue((Class) cls); } /* JADX WARN: Can't rename method to resolve collision */ @Override // java.lang.ClassValue protected Boolean computeValue(Class cls) { FuturesGetChecked.checkExceptionClassValidity(cls.asSubclass(Exception.class)); return Boolean.TRUE; } }; @Override // com.google.common.util.concurrent.FuturesGetChecked.GetCheckedTypeValidator public final void validateClass(Class cls) { isValidClass.get(cls); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public enum WeakSetValidator implements GetCheckedTypeValidator { INSTANCE; private static final Set>> validClasses = new CopyOnWriteArraySet(); @Override // com.google.common.util.concurrent.FuturesGetChecked.GetCheckedTypeValidator public final void validateClass(Class cls) { Iterator>> it = validClasses.iterator(); while (it.hasNext()) { if (cls.equals(it.next().get())) { return; } } FuturesGetChecked.checkExceptionClassValidity(cls); Set>> set = validClasses; if (set.size() > 1000) { set.clear(); } set.add(new WeakReference<>(cls)); } } static GetCheckedTypeValidator getBestValidator() { try { return (GetCheckedTypeValidator) Class.forName(CLASS_VALUE_VALIDATOR_NAME).getEnumConstants()[0]; } catch (Throwable unused) { return FuturesGetChecked.weakSetValidator(); } } } private static void wrapAndThrowExceptionOrError(Throwable th, Class cls) throws Exception { if (th instanceof Error) { throw new ExecutionError((Error) th); } if (th instanceof RuntimeException) { throw new UncheckedExecutionException(th); } throw newWithCause(cls, th); } private static boolean hasConstructorUsableByGetChecked(Class cls) { try { newWithCause(cls, new Exception()); return true; } catch (Exception unused) { return false; } } private static X newWithCause(Class cls, Throwable th) { Iterator it = preferringStrings(Arrays.asList(cls.getConstructors())).iterator(); while (it.hasNext()) { X x = (X) newFromConstructor((Constructor) it.next(), th); if (x != null) { if (x.getCause() == null) { x.initCause(th); } return x; } } String valueOf = String.valueOf(cls); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 82); sb.append("No appropriate constructor for exception of type "); sb.append(valueOf); sb.append(" in response to chained exception"); throw new IllegalArgumentException(sb.toString(), th); } private static List> preferringStrings(List> list) { return (List>) WITH_STRING_PARAM_FIRST.sortedCopy(list); } private static X newFromConstructor(Constructor constructor, Throwable th) { Class[] parameterTypes = constructor.getParameterTypes(); Object[] objArr = new Object[parameterTypes.length]; for (int i = 0; i < parameterTypes.length; i++) { Class cls = parameterTypes[i]; if (cls.equals(String.class)) { objArr[i] = th.toString(); } else { if (!cls.equals(Throwable.class)) { return null; } objArr[i] = th; } } try { return constructor.newInstance(objArr); } catch (IllegalAccessException | IllegalArgumentException | InstantiationException | InvocationTargetException unused) { return null; } } static boolean isCheckedException(Class cls) { return !RuntimeException.class.isAssignableFrom(cls); } static void checkExceptionClassValidity(Class cls) { Preconditions.checkArgument(isCheckedException(cls), "Futures.getChecked exception type (%s) must not be a RuntimeException", cls); Preconditions.checkArgument(hasConstructorUsableByGetChecked(cls), "Futures.getChecked exception type (%s) must be an accessible class with an accessible constructor whose parameters (if any) must be of type String and/or Throwable", cls); } private FuturesGetChecked() { } }