View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.desc.impl;
17  
18  import java.lang.reflect.Field;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.seasar.framework.beans.BeanDesc;
24  import org.seasar.framework.beans.PropertyDesc;
25  import org.seasar.framework.beans.factory.BeanDescFactory;
26  import org.seasar.framework.exception.EmptyRuntimeException;
27  import org.seasar.uruma.annotation.ExportSelection;
28  import org.seasar.uruma.annotation.ExportValue;
29  import org.seasar.uruma.annotation.ImportExportValue;
30  import org.seasar.uruma.annotation.ImportSelection;
31  import org.seasar.uruma.annotation.ImportValue;
32  import org.seasar.uruma.desc.FormDesc;
33  
34  /**
35   * {@link FormDesc} の実装クラスです。<br />
36   * 
37   * @author y-komori
38   */
39  public class FormDescImpl implements FormDesc {
40      private Class<?> formClass;
41  
42      private BeanDesc beanDesc;
43  
44      private List<PropertyDesc> importValueProps = new ArrayList<PropertyDesc>(1);
45  
46      private List<PropertyDesc> exportValueProps = new ArrayList<PropertyDesc>(1);
47  
48      private List<PropertyDesc> importSelectionProps = new ArrayList<PropertyDesc>(
49              1);;
50  
51      private List<PropertyDesc> exportSelectionProps = new ArrayList<PropertyDesc>(
52              1);;
53  
54      /**
55       * {@link FormDescImpl} を構築します。<br />
56       * 
57       * @param formClass
58       *            フォームクラスの {@link Class} オブジェクト
59       * @throws EmptyRuntimeException
60       *             <code>formClass</code> が <code>null</code> であった場合
61       */
62      public FormDescImpl(final Class<?> formClass) throws EmptyRuntimeException {
63          if (formClass == null) {
64              throw new EmptyRuntimeException("formClass");
65          }
66  
67          this.formClass = formClass;
68          this.beanDesc = BeanDescFactory.getBeanDesc(formClass);
69  
70          setupFields();
71  
72          importValueProps = Collections.unmodifiableList(importValueProps);
73          exportValueProps = Collections.unmodifiableList(exportValueProps);
74          importSelectionProps = Collections
75                  .unmodifiableList(importSelectionProps);
76          exportSelectionProps = Collections
77                  .unmodifiableList(exportSelectionProps);
78      }
79  
80      protected void setupFields() {
81          setupFieldsByClass(formClass);
82          Class<?> superClass = formClass.getSuperclass();
83          while (superClass != Object.class && superClass != null) {
84              setupFieldsByClass(superClass);
85              superClass = superClass.getSuperclass();
86          }
87      }
88  
89      protected void setupFieldsByClass(final Class<?> targetClass) {
90          Field[] fields = targetClass.getDeclaredFields();
91          for (int i = 0; i < fields.length; i++) {
92              Field field = fields[i];
93  
94              setupImportValueField(field);
95              setupExportvalueField(field);
96              setupImportSelectionField(field);
97              setupExportSelectionField(field);
98          }
99      }
100 
101     protected void setupImportValueField(final Field field) {
102         if (field.isAnnotationPresent(ImportValue.class)
103                 || field.isAnnotationPresent(ImportExportValue.class)) {
104             PropertyDesc pd = beanDesc.getPropertyDesc(field.getName());
105             importValueProps.add(pd);
106         }
107     }
108 
109     protected void setupExportvalueField(final Field field) {
110         if (field.isAnnotationPresent(ExportValue.class)
111                 || field.isAnnotationPresent(ImportExportValue.class)) {
112             PropertyDesc pd = beanDesc.getPropertyDesc(field.getName());
113             exportValueProps.add(pd);
114         }
115     }
116 
117     protected void setupImportSelectionField(final Field field) {
118         if (field.isAnnotationPresent(ImportSelection.class)) {
119             PropertyDesc pd = beanDesc.getPropertyDesc(field.getName());
120             importSelectionProps.add(pd);
121         }
122     }
123 
124     protected void setupExportSelectionField(final Field field) {
125         if (field.isAnnotationPresent(ExportSelection.class)) {
126             PropertyDesc pd = beanDesc.getPropertyDesc(field.getName());
127             exportSelectionProps.add(pd);
128         }
129     }
130 
131     /*
132      * @see org.seasar.uruma.desc.FormDesc#getFormClass()
133      */
134     public Class<?> getFormClass() {
135         return this.formClass;
136     }
137 
138     /*
139      * @see org.seasar.uruma.desc.FormDesc#getExportSelectionProperties()
140      */
141     public List<PropertyDesc> getExportSelectionProperties() {
142         return exportSelectionProps;
143     }
144 
145     /*
146      * @see org.seasar.uruma.desc.FormDesc#getExportValueProperties()
147      */
148     public List<PropertyDesc> getExportValueProperties() {
149         return exportValueProps;
150     }
151 
152     /*
153      * @see org.seasar.uruma.desc.FormDesc#getImportSelectionProperties()
154      */
155     public List<PropertyDesc> getImportSelectionProperties() {
156         return importSelectionProps;
157     }
158 
159     /*
160      * @see org.seasar.uruma.desc.FormDesc#getImportValueProperties()
161      */
162     public List<PropertyDesc> getImportValueProperties() {
163         return importValueProps;
164     }
165 
166 }