View Javadoc
1   /*
2    * Copyright (C) 2013 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package com.google.common.eventbus;
15  
16  import static com.google.common.base.Preconditions.checkNotNull;
17  
18  import java.lang.reflect.Method;
19  
20  /**
21   * Context for an exception thrown by a subscriber.
22   *
23   * @since 16.0
24   */
25  public class SubscriberExceptionContext {
26    private final EventBus eventBus;
27    private final Object event;
28    private final Object subscriber;
29    private final Method subscriberMethod;
30  
31    /**
32     * @param eventBus The {@link EventBus} that handled the event and the subscriber. Useful for
33     *     broadcasting a a new event based on the error.
34     * @param event The event object that caused the subscriber to throw.
35     * @param subscriber The source subscriber context.
36     * @param subscriberMethod the subscribed method.
37     */
38    SubscriberExceptionContext(
39        EventBus eventBus, Object event, Object subscriber, Method subscriberMethod) {
40      this.eventBus = checkNotNull(eventBus);
41      this.event = checkNotNull(event);
42      this.subscriber = checkNotNull(subscriber);
43      this.subscriberMethod = checkNotNull(subscriberMethod);
44    }
45  
46    /**
47     * @return The {@link EventBus} that handled the event and the subscriber. Useful for broadcasting
48     *     a a new event based on the error.
49     */
50    public EventBus getEventBus() {
51      return eventBus;
52    }
53  
54    /**
55     * @return The event object that caused the subscriber to throw.
56     */
57    public Object getEvent() {
58      return event;
59    }
60  
61    /**
62     * @return The object context that the subscriber was called on.
63     */
64    public Object getSubscriber() {
65      return subscriber;
66    }
67  
68    /**
69     * @return The subscribed method that threw the exception.
70     */
71    public Method getSubscriberMethod() {
72      return subscriberMethod;
73    }
74  }