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 */
014package com.google.common.eventbus;
015
016import static com.google.common.base.Preconditions.checkNotNull;
017
018import java.lang.reflect.Method;
019
020/**
021 * Context for an exception thrown by a subscriber.
022 *
023 * @since 16.0
024 */
025public class SubscriberExceptionContext {
026  private final EventBus eventBus;
027  private final Object event;
028  private final Object subscriber;
029  private final Method subscriberMethod;
030
031  /**
032   * @param eventBus The {@link EventBus} that handled the event and the subscriber. Useful for
033   *     broadcasting a a new event based on the error.
034   * @param event The event object that caused the subscriber to throw.
035   * @param subscriber The source subscriber context.
036   * @param subscriberMethod the subscribed method.
037   */
038  SubscriberExceptionContext(
039      EventBus eventBus, Object event, Object subscriber, Method subscriberMethod) {
040    this.eventBus = checkNotNull(eventBus);
041    this.event = checkNotNull(event);
042    this.subscriber = checkNotNull(subscriber);
043    this.subscriberMethod = checkNotNull(subscriberMethod);
044  }
045
046  /**
047   * @return The {@link EventBus} that handled the event and the subscriber. Useful for broadcasting
048   *     a a new event based on the error.
049   */
050  public EventBus getEventBus() {
051    return eventBus;
052  }
053
054  /**
055   * @return The event object that caused the subscriber to throw.
056   */
057  public Object getEvent() {
058    return event;
059  }
060
061  /**
062   * @return The object context that the subscriber was called on.
063   */
064  public Object getSubscriber() {
065    return subscriber;
066  }
067
068  /**
069   * @return The subscribed method that threw the exception.
070   */
071  public Method getSubscriberMethod() {
072    return subscriberMethod;
073  }
074}