KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > beanflow > support > FindCallableMethods


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.beanflow.support;
18
19 import org.apache.servicemix.beanflow.annotations.Parallel;
20
21 import java.lang.annotation.Annotation JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.concurrent.Callable JavaDoc;
27
28 /**
29  * A helper class to create callables from an object using methods with a
30  * matching annotation.
31  *
32  * @version $Revision: $
33  */

34 public class FindCallableMethods<T> implements CallablesFactory<T> {
35
36     private final Object JavaDoc source;
37     private List JavaDoc<Class JavaDoc> annotations;
38
39     public FindCallableMethods(Object JavaDoc source) {
40         this(source, Parallel.class);
41     }
42
43     public FindCallableMethods(Object JavaDoc source, Class JavaDoc annotation) {
44         this(source, Collections.singletonList(annotation));
45     }
46
47     public FindCallableMethods(Object JavaDoc source, List JavaDoc<Class JavaDoc> annotations) {
48         this.annotations = annotations;
49         this.source = source;
50     }
51
52     public List JavaDoc<Callable JavaDoc<T>> createCallables() {
53         List JavaDoc<Callable JavaDoc<T>> answer = new ArrayList JavaDoc<Callable JavaDoc<T>>();
54         if (source != null) {
55             for (Class JavaDoc type = source.getClass(); type != Object JavaDoc.class && type != null; type = type.getSuperclass()) {
56                 Method JavaDoc[] methods = type.getDeclaredMethods();
57                 for (Method JavaDoc 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     /**
69      * Returns the annotations used to detect callable methods
70      */

71     public List JavaDoc<Class JavaDoc> getAnnotations() {
72         return annotations;
73     }
74
75     @SuppressWarnings JavaDoc("unchecked")
76     protected boolean isValidMethod(Object JavaDoc source, Method JavaDoc method) {
77         if (method.getParameterTypes().length == 0) {
78             for (Class JavaDoc annotationType : annotations) {
79                 Annotation JavaDoc annotation = method.getAnnotation(annotationType);
80                 if (annotation != null) {
81                     return true;
82                 }
83             }
84         }
85         return false;
86     }
87 }
88
Popular Tags