1 7 package org.hibernate.ejb.callback; 8 9 import java.lang.reflect.Method ; 10 11 15 public final class CallbackResolver { 16 private CallbackResolver() {} 17 18 public static Callback resolveCallback(Class beanClass, Class annotation, Class listener) throws Exception { 19 Callback callback = null; 20 Method [] methods = beanClass.getDeclaredMethods(); 21 22 for ( int i = 0 ; i < methods.length ; i++ ) { 23 if ( methods[i].getAnnotation( annotation ) != null ) { 24 if ( callback == null ) { 25 callback = new BeanCallback( methods[i] ); 26 Class returnType = methods[i].getReturnType(); 27 Class [] args = methods[i].getParameterTypes(); 28 if ( returnType != Void.TYPE && args.length != 0 ) { 29 throw new RuntimeException ( 30 "Callback methods annotated on the bean class must return void and take no arguments: " + annotation.getName() + " - " + methods[i] 31 ); 32 } 33 } 34 else { 35 throw new RuntimeException ( 36 "You can only annotate one callback method with " 37 + annotation.getName() + " in bean class: " + beanClass.getName() 38 ); 39 } 40 } 41 } 42 43 if ( listener != null ) { 44 try { 45 methods = listener.getDeclaredMethods(); 46 for ( int i = 0 ; i < methods.length ; i++ ) { 47 if ( methods[i].getAnnotation( annotation ) != null ) { 48 if ( callback == null ) { 49 callback = new ListenerCallback( methods[i], listener.newInstance() ); 50 Class returnType = methods[i].getReturnType(); 51 Class [] args = methods[i].getParameterTypes(); 52 if ( returnType != Void.TYPE && args.length != 0 ) { 53 throw new RuntimeException ( 54 "Callback methods annotated in a listener bean class must return void and take one argument: " + annotation.getName() + " - " + methods[i] 55 ); 56 } 57 } 58 else { 59 throw new RuntimeException ( 60 "You can only annotate one callback method with " 61 + annotation.getName() + " in bean class: " + beanClass.getName() + " and callback listener: " 62 + listener.getName() 63 ); 64 } 65 } 66 } 67 } 68 catch (Exception e) { 69 throw new RuntimeException ( e.getCause() ); 70 } 71 } 72 73 return callback; 74 } 75 76 } 77 | Popular Tags |