View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.integration.beans;
21  
22  import java.beans.PropertyEditor;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Properties;
27  import java.util.Set;
28  
29  /**
30   * A factory that creates a new {@link PropertyEditor} which is appropriate for
31   * the specified object or class. 
32   * 
33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
34   */
35  public final class PropertyEditorFactory {
36      @SuppressWarnings("unchecked")
37      public static PropertyEditor getInstance(Object object) {
38          if (object == null) {
39              return new NullEditor();
40          }
41  
42          if (object instanceof Collection) {
43              Class<?> elementType = null;
44              for (Object e : (Collection) object) {
45                  if (e != null) {
46                      elementType = e.getClass();
47                      break;
48                  }
49              }
50  
51              if (elementType != null) {
52                  if (object instanceof Set) {
53                      return new SetEditor(elementType);
54                  }
55  
56                  if (object instanceof List) {
57                      return new ListEditor(elementType);
58                  }
59  
60                  return new CollectionEditor(elementType);
61              }
62          }
63  
64          if (object instanceof Map) {
65              Class<?> keyType = null;
66              Class<?> valueType = null;
67              for (Object entry : ((Map) object).entrySet()) {
68                  Map.Entry e = (Map.Entry) entry;
69                  if (e.getKey() != null && e.getValue() != null) {
70                      keyType = e.getKey().getClass();
71                      valueType = e.getValue().getClass();
72                      break;
73                  }
74              }
75  
76              if (keyType != null && valueType != null) {
77                  return new MapEditor(keyType, valueType);
78              }
79          }
80  
81          return getInstance(object.getClass());
82      }
83  
84      // parent type / property name / property type
85      public static PropertyEditor getInstance(Class<?> type) {
86          if (type == null) {
87              throw new IllegalArgumentException("type");
88          }
89  
90          if (type.isEnum()) {
91              return new EnumEditor(type);
92          }
93  
94          if (type.isArray()) {
95              return new ArrayEditor(type.getComponentType());
96          }
97  
98          if (Collection.class.isAssignableFrom(type)) {
99              if (Set.class.isAssignableFrom(type)) {
100                 return new SetEditor(String.class);
101             }
102 
103             if (List.class.isAssignableFrom(type)) {
104                 return new ListEditor(String.class);
105             }
106 
107             return new CollectionEditor(String.class);
108         }
109 
110         if (Map.class.isAssignableFrom(type)) {
111             return new MapEditor(String.class, String.class);
112         }
113 
114         if (Properties.class.isAssignableFrom(type)) {
115             return new PropertiesEditor();
116         }
117 
118         type = filterPrimitiveType(type);
119 
120         try {
121             return (PropertyEditor) PropertyEditorFactory.class
122                     .getClassLoader()
123                     .loadClass(
124                             PropertyEditorFactory.class.getPackage().getName() + '.' + type.getSimpleName() + "Editor")
125                     .newInstance();
126         } catch (Exception e) {
127             return null;
128         }
129     }
130 
131     private static Class<?> filterPrimitiveType(Class<?> type) {
132         if (type.isPrimitive()) {
133             if (type == boolean.class) {
134                 type = Boolean.class;
135             }
136             if (type == byte.class) {
137                 type = Byte.class;
138             }
139             if (type == char.class) {
140                 type = Character.class;
141             }
142             if (type == double.class) {
143                 type = Double.class;
144             }
145             if (type == float.class) {
146                 type = Float.class;
147             }
148             if (type == int.class) {
149                 type = Integer.class;
150             }
151             if (type == long.class) {
152                 type = Long.class;
153             }
154             if (type == short.class) {
155                 type = Short.class;
156             }
157         }
158         return type;
159     }
160 
161     private PropertyEditorFactory() {
162     }
163 }