MethodHandle.java :  » Aspect-oriented » dynaop » dynaop » util » Java Open Source

Java Open Source » Aspect oriented » dynaop 
dynaop » dynaop » util » MethodHandle.java
package dynaop.util;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Serializable method handle.
 * 
 * @author Bob Lee (crazybob@crazybob.org)
 */
public class MethodHandle implements Serializable {
  
  static long serialVersionUID = 0;
  
  Class declaringClass;
  String name;
  Class[] parameterTypes;
  
  public MethodHandle(Method method) {
    this.declaringClass = method.getDeclaringClass();
    this.name = method.getName();
    this.parameterTypes = method.getParameterTypes();
  }
  
  public Method getMethod() throws NoSuchMethodException {
    return declaringClass.getDeclaredMethod(this.name, 
      this.parameterTypes);
  }

  public static Map handleMethodKeys(Map map) {
    Map handled = new HashMap();
    for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
      Map.Entry entry = (Map.Entry) i.next();
      handled.put(
        new MethodHandle((Method) entry.getKey()),
        entry.getValue()
      );
    }
    return handled;
  }

  public static Map unhandleMethodKeys(Map map) throws IOException {
    try {
      Map unhandled = new HashMap();
      for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
        Map.Entry entry = (Map.Entry) i.next();
        unhandled.put(
          ((MethodHandle) entry.getKey()).getMethod(),
          entry.getValue()
        );
      }
      return unhandled;
    }
    catch (NoSuchMethodException e) {
      throw NestedException.wrap(e);
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.