247 lines
9.4 KiB
Java
247 lines
9.4 KiB
Java
|
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 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;
|
||
|
|
||
|
/* loaded from: classes2.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 final <T> T newProxy(T t, Class<T> 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(this, t, j, timeUnit, findInterruptibleMethods(cls)));
|
||
|
}
|
||
|
|
||
|
/* renamed from: com.google.common.util.concurrent.SimpleTimeLimiter$1, reason: invalid class name */
|
||
|
/* loaded from: classes2.dex */
|
||
|
class AnonymousClass1 implements InvocationHandler {
|
||
|
final SimpleTimeLimiter this$0;
|
||
|
final Set val$interruptibleMethods;
|
||
|
final Object val$target;
|
||
|
final long val$timeoutDuration;
|
||
|
final TimeUnit val$timeoutUnit;
|
||
|
|
||
|
AnonymousClass1(SimpleTimeLimiter simpleTimeLimiter, Object obj, long j, TimeUnit timeUnit, Set set) {
|
||
|
this.this$0 = simpleTimeLimiter;
|
||
|
this.val$target = obj;
|
||
|
this.val$timeoutDuration = j;
|
||
|
this.val$timeoutUnit = timeUnit;
|
||
|
this.val$interruptibleMethods = set;
|
||
|
}
|
||
|
|
||
|
@Override // java.lang.reflect.InvocationHandler
|
||
|
public Object invoke(Object obj, Method method, Object[] objArr) throws Throwable {
|
||
|
return this.this$0.callWithTimeout(new Callable<Object>(this, method, objArr) { // from class: com.google.common.util.concurrent.SimpleTimeLimiter.1.1
|
||
|
final AnonymousClass1 this$1;
|
||
|
final Object[] val$args;
|
||
|
final Method val$method;
|
||
|
|
||
|
{
|
||
|
this.this$1 = this;
|
||
|
this.val$method = method;
|
||
|
this.val$args = objArr;
|
||
|
}
|
||
|
|
||
|
@Override // java.util.concurrent.Callable
|
||
|
public Object call() throws Exception {
|
||
|
try {
|
||
|
return this.val$method.invoke(this.this$1.val$target, this.val$args);
|
||
|
} catch (InvocationTargetException e) {
|
||
|
throw SimpleTimeLimiter.throwCause(e, false);
|
||
|
}
|
||
|
}
|
||
|
}, this.val$timeoutDuration, this.val$timeoutUnit, this.val$interruptibleMethods.contains(method));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static <T> T newProxy(Class<T> cls, InvocationHandler invocationHandler) {
|
||
|
return cls.cast(Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, invocationHandler));
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: private */
|
||
|
public <T> T callWithTimeout(Callable<T> callable, long j, TimeUnit timeUnit, boolean z) throws Exception {
|
||
|
Preconditions.checkNotNull(callable);
|
||
|
Preconditions.checkNotNull(timeUnit);
|
||
|
checkPositiveTimeout(j);
|
||
|
Future<T> 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 final <T> T callWithTimeout(Callable<T> callable, long j, TimeUnit timeUnit) throws TimeoutException, InterruptedException, ExecutionException {
|
||
|
Preconditions.checkNotNull(callable);
|
||
|
Preconditions.checkNotNull(timeUnit);
|
||
|
checkPositiveTimeout(j);
|
||
|
Future<T> submit = this.executor.submit(callable);
|
||
|
try {
|
||
|
return submit.get(j, timeUnit);
|
||
|
} catch (InterruptedException e) {
|
||
|
e = e;
|
||
|
submit.cancel(true);
|
||
|
throw e;
|
||
|
} catch (ExecutionException e2) {
|
||
|
wrapAndThrowExecutionExceptionOrError(e2.getCause());
|
||
|
throw new AssertionError();
|
||
|
} catch (TimeoutException e3) {
|
||
|
e = e3;
|
||
|
submit.cancel(true);
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.util.concurrent.TimeLimiter
|
||
|
public final <T> T callUninterruptiblyWithTimeout(Callable<T> callable, long j, TimeUnit timeUnit) throws TimeoutException, ExecutionException {
|
||
|
Preconditions.checkNotNull(callable);
|
||
|
Preconditions.checkNotNull(timeUnit);
|
||
|
checkPositiveTimeout(j);
|
||
|
Future<T> submit = this.executor.submit(callable);
|
||
|
try {
|
||
|
return (T) Uninterruptibles.getUninterruptibly(submit, j, timeUnit);
|
||
|
} catch (ExecutionException e) {
|
||
|
wrapAndThrowExecutionExceptionOrError(e.getCause());
|
||
|
throw new AssertionError();
|
||
|
} catch (TimeoutException e2) {
|
||
|
submit.cancel(true);
|
||
|
throw e2;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.util.concurrent.TimeLimiter
|
||
|
public final 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 e) {
|
||
|
e = e;
|
||
|
submit.cancel(true);
|
||
|
throw e;
|
||
|
} catch (ExecutionException e2) {
|
||
|
wrapAndThrowRuntimeExecutionExceptionOrError(e2.getCause());
|
||
|
throw new AssertionError();
|
||
|
} catch (TimeoutException e3) {
|
||
|
e = e3;
|
||
|
submit.cancel(true);
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.util.concurrent.TimeLimiter
|
||
|
public final 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<Method> 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);
|
||
|
}
|
||
|
}
|