ParameterMapping.java
8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software;Designed and Developed mainly by many Chinese
* opensource volunteers. you can redistribute it and/or modify it under the
* terms of the GNU General Public License version 2 only, as published by the
* Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Any questions about this component can be directed to it's project Web address
* https://code.google.com/p/opencloudb/.
*
*/
package io.mycat.config.util;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.mycat.util.StringUtil;
/**
* @author mycat
*/
public class ParameterMapping {
private static final Logger LOGGER = LoggerFactory
.getLogger(ParameterMapping.class);
private static final Map<Class<?>, PropertyDescriptor[]> descriptors = new HashMap<Class<?>, PropertyDescriptor[]>();
/**
* 将property键值对赋值组装到object中
* @param object 目标反射对象
* @param parameter property的键值对
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void mapping(Object object, Map<String, ? extends Object> parameter) throws IllegalAccessException,
InvocationTargetException {
//获取用于导出clazz这个JavaBean的所有属性的PropertyDescriptor
PropertyDescriptor[] pds = getDescriptors(object.getClass());
for (int i = 0; i < pds.length; i++) {
PropertyDescriptor pd = pds[i];
Object obj = parameter.get(pd.getName());
Object value = obj;
Class<?> cls = pd.getPropertyType();
//类型转换
if (obj instanceof String) {
String string = (String) obj;
if (!StringUtil.isEmpty(string)) {
string = ConfigUtil.filter(string);
}
if (isPrimitiveType(cls)) {
value = convert(cls, string);
}
} else if (obj instanceof BeanConfig) {
value = createBean((BeanConfig) obj);
} else if (obj instanceof BeanConfig[]) {
List<Object> list = new ArrayList<Object>();
for (BeanConfig beanconfig : (BeanConfig[]) obj) {
list.add(createBean(beanconfig));
}
value = list.toArray();
}
//赋值
if (cls != null
&& value != null) {
Method method = pd.getWriteMethod();
if (method != null) {
method.invoke(object, new Object[] { value });
}
}
}
}
@SuppressWarnings("unchecked")
public static Object createBean(BeanConfig config) throws IllegalAccessException, InvocationTargetException {
Object bean = config.create(true);
if (bean instanceof Map) {
Map<String, Object> map = (Map<String, Object>) bean;
for (Map.Entry<String, Object> entry : config.getParams().entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof BeanConfig) {
BeanConfig mapBeanConfig = (BeanConfig) entry.getValue();
value = mapBeanConfig.create(true);
mapping(value, mapBeanConfig.getParams());
}
map.put(key, value);
}
} else if (bean instanceof List) {
} else {
mapping(bean, config.getParams());
}
return bean;
}
/**
* 用于导出clazz这个JavaBean的所有属性的PropertyDescriptor
* @param clazz
* @return
*/
private static PropertyDescriptor[] getDescriptors(Class<?> clazz) {
//PropertyDescriptor类表示JavaBean类通过存储器导出一个属性
PropertyDescriptor[] pds;
List<PropertyDescriptor> list;
PropertyDescriptor[] pds2 = descriptors.get(clazz);
//该clazz是否第一次加载
if (null == pds2) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
pds = beanInfo.getPropertyDescriptors();
list = new ArrayList<PropertyDescriptor>();
//加载每一个类型不为空的property
for (int i = 0; i < pds.length; i++) {
if (null != pds[i].getPropertyType()) {
list.add(pds[i]);
}
}
pds2 = new PropertyDescriptor[list.size()];
list.toArray(pds2);
} catch (IntrospectionException ie) {
LOGGER.error("ParameterMappingError", ie);
pds2 = new PropertyDescriptor[0];
}
}
descriptors.put(clazz, pds2);
return (pds2);
}
private static Object convert(Class<?> cls, String string) {
Method method = null;
Object value = null;
if (cls.equals(String.class)) {
value = string;
} else if (cls.equals(Boolean.TYPE)) {
value = Boolean.valueOf(string);
} else if (cls.equals(Byte.TYPE)) {
value = Byte.valueOf(string);
} else if (cls.equals(Short.TYPE)) {
value = Short.valueOf(string);
} else if (cls.equals(Integer.TYPE)) {
value = Integer.valueOf(string);
} else if (cls.equals(Long.TYPE)) {
value = Long.valueOf(string);
} else if (cls.equals(Double.TYPE)) {
value = Double.valueOf(string);
} else if (cls.equals(Float.TYPE)) {
value = Float.valueOf(string);
} else if ((cls.equals(Boolean.class)) || (cls.equals(Byte.class)) || (cls.equals(Short.class))
|| (cls.equals(Integer.class)) || (cls.equals(Long.class)) || (cls.equals(Float.class))
|| (cls.equals(Double.class))) {
try {
method = cls.getMethod("valueOf", new Class[] { String.class });
value = method.invoke(null, new Object[] { string });
} catch (Exception t) {
LOGGER.error("valueofError", t);
value = null;
}
} else if (cls.equals(Class.class)) {
try {
value = Class.forName(string);
} catch (ClassNotFoundException e) {
throw new ConfigException(e);
}
} else {
value = null;
}
return (value);
}
private static boolean isPrimitiveType(Class<?> cls) {
if (cls.equals(String.class) || cls.equals(Boolean.TYPE) || cls.equals(Byte.TYPE) || cls.equals(Short.TYPE)
|| cls.equals(Integer.TYPE) || cls.equals(Long.TYPE) || cls.equals(Double.TYPE)
|| cls.equals(Float.TYPE) || cls.equals(Boolean.class) || cls.equals(Byte.class)
|| cls.equals(Short.class) || cls.equals(Integer.class) || cls.equals(Long.class)
|| cls.equals(Float.class) || cls.equals(Double.class) || cls.equals(Class.class)) {
return true;
} else {
return false;
}
}
}