001/*
002 * Copyright (C) 2013 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.eventbus;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018
019import java.lang.reflect.Method;
020
021/**
022 * Context for an exception thrown by a subscriber.
023 *
024 * @since 16.0
025 */
026@ElementTypesAreNonnullByDefault
027public class SubscriberExceptionContext {
028  private final EventBus eventBus;
029  private final Object event;
030  private final Object subscriber;
031  private final Method subscriberMethod;
032
033  /**
034   * @param eventBus The {@link EventBus} that handled the event and the subscriber. Useful for
035   *     broadcasting a new event based on the error.
036   * @param event The event object that caused the subscriber to throw.
037   * @param subscriber The source subscriber context.
038   * @param subscriberMethod the subscribed method.
039   */
040  SubscriberExceptionContext(
041      EventBus eventBus, Object event, Object subscriber, Method subscriberMethod) {
042    this.eventBus = checkNotNull(eventBus);
043    this.event = checkNotNull(event);
044    this.subscriber = checkNotNull(subscriber);
045    this.subscriberMethod = checkNotNull(subscriberMethod);
046  }
047
048  /**
049   * @return The {@link EventBus} that handled the event and the subscriber. Useful for broadcasting
050   *     a new event based on the error.
051   */
052  public EventBus getEventBus() {
053    return eventBus;
054  }
055
056  /** @return The event object that caused the subscriber to throw. */
057  public Object getEvent() {
058    return event;
059  }
060
061  /** @return The object context that the subscriber was called on. */
062  public Object getSubscriber() {
063    return subscriber;
064  }
065
066  /** @return The subscribed method that threw the exception. */
067  public Method getSubscriberMethod() {
068    return subscriberMethod;
069  }
070}