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