package com.google.common.util.concurrent; import com.google.common.base.Preconditions; import com.google.common.collect.ObjectArrays; import com.google.common.collect.Sets; import com.google.common.util.concurrent.SimpleTimeLimiter; import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.HashSet; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import javax.annotation.CheckForNull; @ElementTypesAreNonnullByDefault /* loaded from: classes3.dex */ public final class SimpleTimeLimiter implements TimeLimiter { private final ExecutorService executor; private SimpleTimeLimiter(ExecutorService executorService) { this.executor = (ExecutorService) Preconditions.checkNotNull(executorService); } public static SimpleTimeLimiter create(ExecutorService executorService) { return new SimpleTimeLimiter(executorService); } @Override // com.google.common.util.concurrent.TimeLimiter public T newProxy(T t, Class cls, long j, TimeUnit timeUnit) { Preconditions.checkNotNull(t); Preconditions.checkNotNull(cls); Preconditions.checkNotNull(timeUnit); checkPositiveTimeout(j); Preconditions.checkArgument(cls.isInterface(), "interfaceType must be an interface type"); return (T) newProxy(cls, new AnonymousClass1(t, j, timeUnit, findInterruptibleMethods(cls))); } /* renamed from: com.google.common.util.concurrent.SimpleTimeLimiter$1, reason: invalid class name */ /* loaded from: classes3.dex */ class AnonymousClass1 implements InvocationHandler { final /* synthetic */ Set val$interruptibleMethods; final /* synthetic */ Object val$target; final /* synthetic */ long val$timeoutDuration; final /* synthetic */ TimeUnit val$timeoutUnit; AnonymousClass1(Object obj, long j, TimeUnit timeUnit, Set set) { this.val$target = obj; this.val$timeoutDuration = j; this.val$timeoutUnit = timeUnit; this.val$interruptibleMethods = set; } @Override // java.lang.reflect.InvocationHandler @CheckForNull public Object invoke(Object obj, final Method method, @CheckForNull final Object[] objArr) throws Throwable { final Object obj2 = this.val$target; return SimpleTimeLimiter.this.callWithTimeout(new Callable() { // from class: com.google.common.util.concurrent.SimpleTimeLimiter$1$$ExternalSyntheticLambda0 @Override // java.util.concurrent.Callable public final Object call() { return SimpleTimeLimiter.AnonymousClass1.lambda$invoke$0(method, obj2, objArr); } }, this.val$timeoutDuration, this.val$timeoutUnit, this.val$interruptibleMethods.contains(method)); } /* JADX INFO: Access modifiers changed from: package-private */ public static /* synthetic */ Object lambda$invoke$0(Method method, Object obj, Object[] objArr) throws Exception { try { return method.invoke(obj, objArr); } catch (InvocationTargetException e) { throw SimpleTimeLimiter.throwCause(e, false); } } } private static T newProxy(Class cls, InvocationHandler invocationHandler) { return cls.cast(Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, invocationHandler)); } /* JADX INFO: Access modifiers changed from: private */ public T callWithTimeout(Callable callable, long j, TimeUnit timeUnit, boolean z) throws Exception { Preconditions.checkNotNull(callable); Preconditions.checkNotNull(timeUnit); checkPositiveTimeout(j); Future submit = this.executor.submit(callable); try { if (z) { try { return submit.get(j, timeUnit); } catch (InterruptedException e) { submit.cancel(true); throw e; } } return (T) Uninterruptibles.getUninterruptibly(submit, j, timeUnit); } catch (ExecutionException e2) { throw throwCause(e2, true); } catch (TimeoutException e3) { submit.cancel(true); throw new UncheckedTimeoutException(e3); } } @Override // com.google.common.util.concurrent.TimeLimiter public T callWithTimeout(Callable callable, long j, TimeUnit timeUnit) throws TimeoutException, InterruptedException, ExecutionException { Preconditions.checkNotNull(callable); Preconditions.checkNotNull(timeUnit); checkPositiveTimeout(j); Future submit = this.executor.submit(callable); try { return submit.get(j, timeUnit); } catch (InterruptedException | TimeoutException e) { submit.cancel(true); throw e; } catch (ExecutionException e2) { this.wrapAndThrowExecutionExceptionOrError(e2.getCause()); throw new AssertionError(); } } @Override // com.google.common.util.concurrent.TimeLimiter public T callUninterruptiblyWithTimeout(Callable callable, long j, TimeUnit timeUnit) throws TimeoutException, ExecutionException { Preconditions.checkNotNull(callable); Preconditions.checkNotNull(timeUnit); checkPositiveTimeout(j); Future submit = this.executor.submit(callable); try { return (T) Uninterruptibles.getUninterruptibly(submit, j, timeUnit); } catch (ExecutionException e) { this.wrapAndThrowExecutionExceptionOrError(e.getCause()); throw new AssertionError(); } catch (TimeoutException e2) { submit.cancel(true); throw e2; } } @Override // com.google.common.util.concurrent.TimeLimiter public void runWithTimeout(Runnable runnable, long j, TimeUnit timeUnit) throws TimeoutException, InterruptedException { Preconditions.checkNotNull(runnable); Preconditions.checkNotNull(timeUnit); checkPositiveTimeout(j); Future submit = this.executor.submit(runnable); try { submit.get(j, timeUnit); } catch (InterruptedException | TimeoutException e) { submit.cancel(true); throw e; } catch (ExecutionException e2) { wrapAndThrowRuntimeExecutionExceptionOrError(e2.getCause()); throw new AssertionError(); } } @Override // com.google.common.util.concurrent.TimeLimiter public void runUninterruptiblyWithTimeout(Runnable runnable, long j, TimeUnit timeUnit) throws TimeoutException { Preconditions.checkNotNull(runnable); Preconditions.checkNotNull(timeUnit); checkPositiveTimeout(j); Future submit = this.executor.submit(runnable); try { Uninterruptibles.getUninterruptibly(submit, j, timeUnit); } catch (ExecutionException e) { wrapAndThrowRuntimeExecutionExceptionOrError(e.getCause()); throw new AssertionError(); } catch (TimeoutException e2) { submit.cancel(true); throw e2; } } /* JADX INFO: Access modifiers changed from: private */ public static Exception throwCause(Exception exc, boolean z) throws Exception { Throwable cause = exc.getCause(); if (cause == null) { throw exc; } if (z) { cause.setStackTrace((StackTraceElement[]) ObjectArrays.concat(cause.getStackTrace(), exc.getStackTrace(), StackTraceElement.class)); } if (cause instanceof Exception) { throw ((Exception) cause); } if (cause instanceof Error) { throw ((Error) cause); } throw exc; } private static Set findInterruptibleMethods(Class cls) { HashSet newHashSet = Sets.newHashSet(); for (Method method : cls.getMethods()) { if (declaresInterruptedEx(method)) { newHashSet.add(method); } } return newHashSet; } private static boolean declaresInterruptedEx(Method method) { for (Class cls : method.getExceptionTypes()) { if (cls == InterruptedException.class) { return true; } } return false; } private void wrapAndThrowExecutionExceptionOrError(Throwable th) throws ExecutionException { if (th instanceof Error) { throw new ExecutionError((Error) th); } if (th instanceof RuntimeException) { throw new UncheckedExecutionException(th); } throw new ExecutionException(th); } private void wrapAndThrowRuntimeExecutionExceptionOrError(Throwable th) { if (th instanceof Error) { throw new ExecutionError((Error) th); } throw new UncheckedExecutionException(th); } private static void checkPositiveTimeout(long j) { Preconditions.checkArgument(j > 0, "timeout must be positive: %s", j); } }