what-the-bank/sources/com/google/common/eventbus/Subscriber.java

112 lines
4.1 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.common.eventbus;
import com.google.common.base.Preconditions;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public class Subscriber {
private EventBus bus;
private final Executor executor;
private final Method method;
final Object target;
/* JADX INFO: Access modifiers changed from: package-private */
public static Subscriber create(EventBus eventBus, Object obj, Method method) {
if (isDeclaredThreadSafe(method)) {
return new Subscriber(eventBus, obj, method);
}
return new SynchronizedSubscriber(eventBus, obj, method);
}
private Subscriber(EventBus eventBus, Object obj, Method method) {
this.bus = eventBus;
this.target = Preconditions.checkNotNull(obj);
this.method = method;
method.setAccessible(true);
this.executor = eventBus.executor();
}
/* JADX INFO: Access modifiers changed from: package-private */
public final void dispatchEvent(Object obj) {
this.executor.execute(new Runnable(this, obj) { // from class: com.google.common.eventbus.Subscriber.1
final Subscriber this$0;
final Object val$event;
{
this.this$0 = this;
this.val$event = obj;
}
@Override // java.lang.Runnable
public void run() {
try {
this.this$0.invokeSubscriberMethod(this.val$event);
} catch (InvocationTargetException e) {
this.this$0.bus.handleSubscriberException(e.getCause(), this.this$0.context(this.val$event));
}
}
});
}
void invokeSubscriberMethod(Object obj) throws InvocationTargetException {
try {
this.method.invoke(this.target, Preconditions.checkNotNull(obj));
} catch (IllegalAccessException e) {
String valueOf = String.valueOf(obj);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 28);
sb.append("Method became inaccessible: ");
sb.append(valueOf);
throw new Error(sb.toString(), e);
} catch (IllegalArgumentException e2) {
String valueOf2 = String.valueOf(obj);
StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf2).length() + 33);
sb2.append("Method rejected target/argument: ");
sb2.append(valueOf2);
throw new Error(sb2.toString(), e2);
} catch (InvocationTargetException e3) {
if (e3.getCause() instanceof Error) {
throw ((Error) e3.getCause());
}
throw e3;
}
}
/* JADX INFO: Access modifiers changed from: private */
public SubscriberExceptionContext context(Object obj) {
return new SubscriberExceptionContext(this.bus, obj, this.target, this.method);
}
public final int hashCode() {
return ((this.method.hashCode() + 31) * 31) + System.identityHashCode(this.target);
}
public final boolean equals(Object obj) {
if (!(obj instanceof Subscriber)) {
return false;
}
Subscriber subscriber = (Subscriber) obj;
return this.target == subscriber.target && this.method.equals(subscriber.method);
}
private static boolean isDeclaredThreadSafe(Method method) {
return method.getAnnotation(AllowConcurrentEvents.class) != null;
}
/* loaded from: classes2.dex */
static final class SynchronizedSubscriber extends Subscriber {
private SynchronizedSubscriber(EventBus eventBus, Object obj, Method method) {
super(eventBus, obj, method);
}
@Override // com.google.common.eventbus.Subscriber
final void invokeSubscriberMethod(Object obj) throws InvocationTargetException {
synchronized (this) {
super.invokeSubscriberMethod(obj);
}
}
}
}