1 56 57 package org.objectstyle.cayenne.util; 58 59 import java.lang.ref.WeakReference ; 60 import java.lang.reflect.InvocationTargetException ; 61 import java.lang.reflect.Method ; 62 63 import org.apache.log4j.Logger; 64 import org.objectstyle.cayenne.CayenneRuntimeException; 65 66 74 public class Invocation extends Object { 75 private static final Logger log = Logger.getLogger(Invocation.class); 76 77 private WeakReference _target; 78 private Method _method; 79 private Class [] _parameterTypes; 80 81 84 private Invocation() { 85 } 86 87 92 public Invocation(Object target, String methodName) throws NoSuchMethodException { 93 this(target, methodName, (Class []) null); 94 } 95 96 102 public Invocation(Object target, String methodName, Class parameterType) 103 throws NoSuchMethodException { 104 this(target, methodName, new Class [] { parameterType }); 105 } 106 107 120 public Invocation(Object target, String methodName, Class [] parameterTypes) 121 throws NoSuchMethodException { 122 super(); 123 124 if (target == null) { 125 throw new IllegalArgumentException ("target argument must not be null"); 126 } 127 128 if (methodName == null) { 129 throw new IllegalArgumentException ("method name must not be null"); 130 } 131 132 if (parameterTypes != null) { 133 if (parameterTypes.length > 0) { 134 for (int i = 0; i < parameterTypes.length; i++) { 135 if (parameterTypes[i] == null) { 136 throw new IllegalArgumentException ( 137 "parameter type[" + i + "] must not be null"); 138 } 139 } 140 } 141 else { 142 throw new IllegalArgumentException ("parameter types must not be empty"); 143 } 144 } 145 146 Method method = null; 147 148 Class targetClass = target.getClass(); 151 Class [] interfaces = targetClass.getInterfaces(); 152 for (int i = 0; i < interfaces.length; i++) { 153 try { 154 method = interfaces[i].getMethod(methodName, parameterTypes); 155 break; 156 } 157 catch (NoSuchMethodException ex) { 158 } 160 } 161 162 _method = 163 (method != null) ? method : targetClass.getMethod(methodName, parameterTypes); 164 _parameterTypes = parameterTypes; 165 _target = new WeakReference (target); 166 } 167 168 173 public boolean fire() { 174 return this.fire(null); 175 } 176 177 183 public boolean fire(Object argument) { 184 return this.fire(new Object [] { argument }); 185 } 186 187 201 public boolean fire(Object [] arguments) { 202 203 if (_parameterTypes == null) { 204 if (arguments != null) { 205 throw new IllegalArgumentException ("arguments unexpectedly != null"); 206 } 207 } 208 else if (arguments == null) { 209 throw new IllegalArgumentException ("arguments must not be null"); 210 } 211 else if (_parameterTypes.length != arguments.length) { 212 throw new IllegalArgumentException ( 213 "inconsistent number of arguments: expected" 214 + _parameterTypes.length 215 + ", got " 216 + arguments.length); 217 } 218 219 Object currentTarget = _target.get(); 220 if (currentTarget == null) { 221 log.info("null target for: " + _method.getName()); 222 return false; 223 } 224 225 try { 226 _method.invoke(currentTarget, arguments); 227 return true; 228 } 229 catch (InvocationTargetException ite) { 230 234 Throwable cause = ite.getCause(); 235 if (cause instanceof RuntimeException ) { 236 throw (RuntimeException ) cause; 237 } 238 else { 239 throw new CayenneRuntimeException(cause); 240 } 241 } 242 catch (Exception ex) { 243 log.info("exception firing '" + _method.getName() + "'", ex); 246 return false; 247 } 248 } 249 250 253 public boolean equals(Object obj) { 254 if ((obj != null) && (obj.getClass().equals(this.getClass()))) { 255 Invocation otherInvocation = (Invocation) obj; 256 if (_method.equals(otherInvocation.getMethod())) { 257 Object otherTarget = otherInvocation.getTarget(); 258 Object target = _target.get(); 259 260 if ((target == null) && (otherTarget == null)) { 261 return true; 262 } 263 264 if ((target == null) && (otherTarget != null)) { 265 return false; 266 } 267 268 if (target != null) { 269 return target.equals(otherTarget); 270 } 271 } 272 273 return false; 274 } 275 else { 276 return super.equals(obj); 277 } 278 } 279 280 283 public int hashCode() { 284 290 int hash = 42, hashMultiplier = 59; 292 return hash * hashMultiplier + _method.hashCode(); 293 } 294 295 298 public Method getMethod() { 299 return _method; 300 } 301 302 305 public Object getTarget() { 306 return _target.get(); 307 } 308 309 312 public Class [] getParameterTypes() { 313 return _parameterTypes; 314 } 315 316 } 317 | Popular Tags |