1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
31
32
33
34
35
36 public final class PropertyEditorFactory {
37 @SuppressWarnings("unchecked")
38 public static PropertyEditor getInstance(Object object) {
39 if (object == null) {
40 return new NullEditor();
41 }
42
43 if (object instanceof Collection) {
44 Class<?> elementType = null;
45 for (Object e: (Collection) object) {
46 if (e != null) {
47 elementType = e.getClass();
48 break;
49 }
50 }
51
52 if (elementType != null) {
53 if (object instanceof Set) {
54 return new SetEditor(elementType);
55 }
56
57 if (object instanceof List) {
58 return new ListEditor(elementType);
59 }
60
61 return new CollectionEditor(elementType);
62 }
63 }
64
65 if (object instanceof Map) {
66 Class<?> keyType = null;
67 Class<?> valueType = null;
68 for (Object entry: ((Map) object).entrySet()) {
69 Map.Entry e = (Map.Entry) entry;
70 if (e.getKey() != null && e.getValue() != null) {
71 keyType = e.getKey().getClass();
72 valueType = e.getValue().getClass();
73 break;
74 }
75 }
76
77 if (keyType != null && valueType != null) {
78 return new MapEditor(keyType, valueType);
79 }
80 }
81
82 return getInstance(object.getClass());
83 }
84
85
86 public static PropertyEditor getInstance(Class<?> type) {
87 if (type == null) {
88 throw new NullPointerException("type");
89 }
90
91 if (type.isEnum()) {
92 return new EnumEditor(type);
93 }
94
95 if (type.isArray()) {
96 return new ArrayEditor(type.getComponentType());
97 }
98
99 if (Collection.class.isAssignableFrom(type)) {
100 if (Set.class.isAssignableFrom(type)) {
101 return new SetEditor(String.class);
102 }
103
104 if (List.class.isAssignableFrom(type)) {
105 return new ListEditor(String.class);
106 }
107
108 return new CollectionEditor(String.class);
109 }
110
111 if (Map.class.isAssignableFrom(type)) {
112 return new MapEditor(String.class, String.class);
113 }
114
115 if (Properties.class.isAssignableFrom(type)) {
116 return new PropertiesEditor();
117 }
118
119 type = filterPrimitiveType(type);
120
121 try {
122 return (PropertyEditor)
123 PropertyEditorFactory.class.getClassLoader().loadClass(
124 PropertyEditorFactory.class.getPackage().getName() +
125 '.' + type.getSimpleName() + "Editor").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 }