1 19 20 21 package org.apache.cayenne.util; 22 23 import java.lang.ref.WeakReference ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 27 import org.apache.cayenne.CayenneRuntimeException; 28 29 37 public class Invocation extends Object { 38 39 private WeakReference _target; 40 private Method _method; 41 private Class [] _parameterTypes; 42 43 46 private Invocation() { 47 } 48 49 54 public Invocation(Object target, String methodName) throws NoSuchMethodException { 55 this(target, methodName, (Class []) null); 56 } 57 58 63 public Invocation(Object target, String methodName, Class parameterType) 64 throws NoSuchMethodException { 65 this(target, methodName, new Class [] { 66 parameterType 67 }); 68 } 69 70 81 public Invocation(Object target, String methodName, Class [] parameterTypes) 82 throws NoSuchMethodException { 83 super(); 84 85 if (target == null) { 86 throw new IllegalArgumentException ("target argument must not be null"); 87 } 88 89 if (methodName == null) { 90 throw new IllegalArgumentException ("method name must not be null"); 91 } 92 93 if (parameterTypes != null) { 94 if (parameterTypes.length > 0) { 95 for (int i = 0; i < parameterTypes.length; i++) { 96 if (parameterTypes[i] == null) { 97 throw new IllegalArgumentException ("parameter type[" 98 + i 99 + "] must not be null"); 100 } 101 } 102 } 103 else { 104 throw new IllegalArgumentException ("parameter types must not be empty"); 105 } 106 } 107 108 111 _method = lookupMethodInHierarchy(target.getClass(), methodName, parameterTypes); 112 113 if (_method == null) { 114 throw new NoSuchMethodException ("No such method: " 115 + target.getClass().getName() 116 + "." 117 + methodName); 118 } 119 120 if (!Util.isAccessible(_method)) { 121 _method.setAccessible(true); 122 } 123 124 _parameterTypes = parameterTypes; 125 _target = new WeakReference (target); 126 } 127 128 Method lookupMethodInHierarchy( 129 Class objectClass, 130 String methodName, 131 Class [] parameterTypes) throws SecurityException , NoSuchMethodException { 132 133 try { 134 return objectClass.getDeclaredMethod(methodName, parameterTypes); 135 } 136 catch (NoSuchMethodException e) { 137 138 Class superClass = objectClass.getSuperclass(); 139 if (superClass == null || superClass.getName().equals(Object .class.getName())) { 140 throw e; 141 } 142 143 return lookupMethodInHierarchy(superClass, methodName, parameterTypes); 144 } 145 } 146 147 152 public boolean fire() { 153 return this.fire(null); 154 } 155 156 162 public boolean fire(Object argument) { 163 return this.fire(new Object [] { 164 argument 165 }); 166 } 167 168 180 public boolean fire(Object [] arguments) { 181 182 if (_parameterTypes == null) { 183 if (arguments != null) { 184 throw new IllegalArgumentException ("arguments unexpectedly != null"); 185 } 186 } 187 else if (arguments == null) { 188 throw new IllegalArgumentException ("arguments must not be null"); 189 } 190 else if (_parameterTypes.length != arguments.length) { 191 throw new IllegalArgumentException ( 192 "inconsistent number of arguments: expected" 193 + _parameterTypes.length 194 + ", got " 195 + arguments.length); 196 } 197 198 Object currentTarget = _target.get(); 199 if (currentTarget == null) { 200 return false; 201 } 202 203 try { 204 _method.invoke(currentTarget, arguments); 205 return true; 206 } 207 catch (InvocationTargetException ite) { 208 212 Throwable cause = ite.getCause(); 213 if (cause instanceof RuntimeException ) { 214 throw (RuntimeException ) cause; 215 } 216 else { 217 throw new CayenneRuntimeException(cause); 218 } 219 } 220 catch (Exception ex) { 221 return false; 224 } 225 } 226 227 230 public boolean equals(Object obj) { 231 if ((obj != null) && (obj.getClass().equals(this.getClass()))) { 232 Invocation otherInvocation = (Invocation) obj; 233 if (_method.equals(otherInvocation.getMethod())) { 234 Object otherTarget = otherInvocation.getTarget(); 235 Object target = _target.get(); 236 237 if ((target == null) && (otherTarget == null)) { 238 return true; 239 } 240 241 if ((target == null) && (otherTarget != null)) { 242 return false; 243 } 244 245 if (target != null) { 246 return target.equals(otherTarget); 247 } 248 } 249 250 return false; 251 } 252 else { 253 return super.equals(obj); 254 } 255 } 256 257 260 public int hashCode() { 261 267 int hash = 42, hashMultiplier = 59; 269 return hash * hashMultiplier + _method.hashCode(); 270 } 271 272 275 public Method getMethod() { 276 return _method; 277 } 278 279 282 public Object getTarget() { 283 return _target.get(); 284 } 285 286 289 public Class [] getParameterTypes() { 290 return _parameterTypes; 291 } 292 293 } 294 | Popular Tags |