Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.eventbus;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
 
Wraps a single-argument subscriber method on a specific object.

This class only verifies the suitability of the method and event type if something fails. Callers are expected to verify their uses of this class.

Two EventSubscribers are equivalent when they refer to the same method on the same object (not class). This property is used to ensure that no subscriber method is registered more than once.

Author(s):
Cliff Biffle
 
 class EventSubscriber {

  
Object sporting the subscriber method.
 
   private final Object target;
  
Subscriber method.
 
   private final Method method;

  
Creates a new EventSubscriber to wrap method on

Parameters:
target object to which the method applies.
method subscriber method.
:
code target}.
 
   EventSubscriber(Object targetMethod method) {
     Preconditions.checkNotNull(target,
         "EventSubscriber target cannot be null.");
     Preconditions.checkNotNull(method"EventSubscriber method cannot be null.");
 
     this. = target;
     this. = method;
     method.setAccessible(true);
   }

  
Invokes the wrapped subscriber method to handle event.

Parameters:
event event to handle
Throws:
java.lang.reflect.InvocationTargetException if the wrapped method throws any java.lang.Throwable that is not an java.lang.Error (Error instances are propagated as-is).
 
   public void handleEvent(Object eventthrows InvocationTargetException {
     checkNotNull(event);
     try {
       .invoke(new Object[] { event });
     } catch (IllegalArgumentException e) {
       throw new Error("Method rejected target/argument: " + evente);
     } catch (IllegalAccessException e) {
       throw new Error("Method became inaccessible: " + evente);
     } catch (InvocationTargetException e) {
       if (e.getCause() instanceof Error) {
         throw (Errore.getCause();
       }
       throw e;
     }
   }
 
   @Override public String toString() {
     return "[wrapper " +  + "]";
   }
 
   @Override public int hashCode() {
     final int PRIME = 31;
     return (PRIME + .hashCode()) * PRIME
         + System.identityHashCode();
   }
 
   @Override public boolean equals(@Nullable Object obj) {
     if (obj instanceof EventSubscriber) {
       EventSubscriber that = (EventSubscriberobj;
      // Use == so that different equal instances will still receive events.
      // We only guard against the case that the same object is registered
      // multiple times
      return  == that.target && .equals(that.method);
    }
    return false;
  }
  public Object getSubscriber() {
    return ;
  }
  public Method getMethod() {
    return ;
  }
New to GrepCode? Check out our FAQ X