KickJava   Java API By Example, From Geeks To Geeks.

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


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.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.servicemix.beanflow.State;
22
23 import java.lang.reflect.Field JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Uses reflection to find all of the fields which implement {@link State}
30  *
31  * @version $Revision: $
32  */

33 public class FieldIntrospector implements Introspector {
34
35     private static final Log log = LogFactory.getLog(FieldIntrospector.class);
36
37     public Iterator JavaDoc<State> iterator(Object JavaDoc owner) {
38         List JavaDoc<State> list = new ArrayList JavaDoc<State>();
39         for (Class JavaDoc type = owner.getClass(); type != null && type != Object JavaDoc.class; type = type.getSuperclass()) {
40             Field JavaDoc[] fields = type.getDeclaredFields();
41             for (int i = 0; i < fields.length; i++) {
42                 Field JavaDoc field = fields[i];
43                 Object JavaDoc value = null;
44                 try {
45                     field.setAccessible(true);
46                     value = field.get(owner);
47                 }
48                 catch (Exception JavaDoc e) {
49                     if (log.isDebugEnabled()) {
50                         log.debug("Could not access field: " + field + " reason: " + e, e);
51                     }
52                 }
53                 State state = toState(value);
54                 if (state != null) {
55                     list.add(state);
56                 }
57             }
58         }
59         return list.iterator();
60     }
61
62     /**
63      * Converts the given value to a state object or returns null if it can not
64      * be converted
65      */

66     protected State toState(Object JavaDoc value) {
67         if (value instanceof State) {
68             return (State) value;
69         }
70         return null;
71     }
72 }
73
Popular Tags