1 34 package org.codehaus.groovy.runtime; 35 36 import groovy.lang.Closure; 37 38 import java.lang.reflect.InvocationHandler ; 39 import java.lang.reflect.Method ; 40 41 48 public class ClosureListener implements InvocationHandler { 49 50 private String listenerMethodName; 51 private Closure closure; 52 53 public ClosureListener(String listenerMethodName, Closure closure) { 54 this.listenerMethodName = listenerMethodName; 55 this.closure = closure; 56 } 57 58 public Object invoke(Object object, Method method, Object [] arguments) throws Throwable { 59 if (listenerMethodName.equals(method.getName())) { 60 61 closure.call(arguments[0]); 62 return null; 63 } 64 65 String name = method.getName(); 67 if (name.equals("equals")) { 68 return object == arguments[0] ? Boolean.TRUE : Boolean.FALSE; 69 } 70 else if (name.equals("hashCode")) { 71 return new Integer (hashCode()); 72 } 73 else if (name.equals("toString")) { 74 return super.toString() + "[listener:" + listenerMethodName + "]"; 75 } 76 77 88 return null; 89 } 90 91 public boolean equals(Object that) { 92 return this == that; 93 } 94 95 } | Popular Tags |