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
  * 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.
 import static;
 import  javax.annotation.Nullable;

Wraps a single-argument 'handler' 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 EventHandlers are equivalent when they refer to the same method on the same object (not class). This property is used to ensure that no handler method is registered more than once.

Cliff Biffle
 class EventHandler {

Object sporting the handler method.
   private final Object target;
Handler method.
   private final Method method;

Creates a new EventHandler to wrap method on

target object to which the method applies.
method handler method.
code target}.
   EventHandler(Object targetMethod method) {
         "EventHandler target cannot be null.");
     Preconditions.checkNotNull(method"EventHandler method cannot be null.");
     this. = target;
     this. = method;

Invokes the wrapped handler method to handle event.

event event to handle
InvocationTargetException if the wrapped method throws any Throwable that is not an Error (Error instances are propagated as-is).
   public void handleEvent(Object eventthrows InvocationTargetException {
     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 EventHandler) {
       EventHandler that = (EventHandlerobj;
      // 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  == && .equals(that.method);
    return false;
New to GrepCode? Check out our FAQ X