1 19 package org.apache.cayenne.reflect; 20 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Modifier ; 23 24 import org.apache.cayenne.CayenneRuntimeException; 25 import org.apache.cayenne.util.Util; 26 27 35 class CallbackOnEntity extends AbstractCallback { 36 37 private Method callbackMethod; 38 39 CallbackOnEntity(Class objectClass, String methodName) 40 throws IllegalArgumentException { 41 this.callbackMethod = findMethod(objectClass, methodName); 42 } 43 44 public void performCallback(Object entity) { 45 try { 46 callbackMethod.invoke(entity, null); 47 } 48 catch (Exception e) { 49 throw new CayenneRuntimeException("Error invoking entity callback method " 50 + callbackMethod.getName(), e); 51 } 52 } 53 54 private Method findMethod(Class objectClass, String methodName) 55 throws IllegalArgumentException { 56 Method [] methods = objectClass.getDeclaredMethods(); 57 for (int i = 0; i < methods.length; i++) { 58 if (methodName.equals(methods[i].getName())) { 59 60 int modifiers = methods[i].getModifiers(); 63 if (!Modifier.isStatic(modifiers) 64 && Void.TYPE.isAssignableFrom(methods[i].getReturnType()) 65 && methods[i].getParameterTypes().length == 0) { 66 67 if (!Util.isAccessible(methods[i])) { 68 methods[i].setAccessible(true); 69 } 70 71 return methods[i]; 72 } 73 } 74 } 75 76 throw new IllegalArgumentException ("Class " 77 + objectClass.getName() 78 + " has no valid callback method '" 79 + methodName 80 + "'"); 81 } 82 } 83 | Popular Tags |