1 17 package org.apache.servicemix.beanflow.support; 18 19 import org.apache.servicemix.beanflow.annotations.Parallel; 20 21 import java.lang.annotation.Annotation ; 22 import java.lang.reflect.Method ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import java.util.concurrent.Callable ; 27 28 34 public class FindCallableMethods<T> implements CallablesFactory<T> { 35 36 private final Object source; 37 private List <Class > annotations; 38 39 public FindCallableMethods(Object source) { 40 this(source, Parallel.class); 41 } 42 43 public FindCallableMethods(Object source, Class annotation) { 44 this(source, Collections.singletonList(annotation)); 45 } 46 47 public FindCallableMethods(Object source, List <Class > annotations) { 48 this.annotations = annotations; 49 this.source = source; 50 } 51 52 public List <Callable <T>> createCallables() { 53 List <Callable <T>> answer = new ArrayList <Callable <T>>(); 54 if (source != null) { 55 for (Class type = source.getClass(); type != Object .class && type != null; type = type.getSuperclass()) { 56 Method [] methods = type.getDeclaredMethods(); 57 for (Method method : methods) { 58 if (isValidMethod(source, method)) { 59 MethodReflector<T> reflector = new MethodReflector<T>(source, method); 60 answer.add(reflector); 61 } 62 } 63 } 64 } 65 return answer; 66 } 67 68 71 public List <Class > getAnnotations() { 72 return annotations; 73 } 74 75 @SuppressWarnings ("unchecked") 76 protected boolean isValidMethod(Object source, Method method) { 77 if (method.getParameterTypes().length == 0) { 78 for (Class annotationType : annotations) { 79 Annotation annotation = method.getAnnotation(annotationType); 80 if (annotation != null) { 81 return true; 82 } 83 } 84 } 85 return false; 86 } 87 } 88 | Popular Tags |