1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.reflection;
17
18 import java.lang.annotation.Annotation;
19 import java.lang.reflect.GenericArrayType;
20 import java.lang.reflect.Method;
21 import java.lang.reflect.ParameterizedType;
22 import java.lang.reflect.Type;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.SortedMap;
30 import java.util.TreeMap;
31
32 import org.apache.ibatis.annotations.Param;
33 import org.apache.ibatis.binding.MapperMethod.ParamMap;
34 import org.apache.ibatis.reflection.property.PropertyTokenizer;
35 import org.apache.ibatis.session.Configuration;
36 import org.apache.ibatis.session.ResultHandler;
37 import org.apache.ibatis.session.RowBounds;
38
39 public class ParamNameResolver {
40
41 public static final String GENERIC_NAME_PREFIX = "param";
42
43 public static final String[] GENERIC_NAME_CACHE = new String[10];
44
45 static {
46 for (int i = 0; i < 10; i++) {
47 GENERIC_NAME_CACHE[i] = GENERIC_NAME_PREFIX + (i + 1);
48 }
49 }
50
51 private final boolean useActualParamName;
52
53
54
55
56
57
58
59
60
61
62
63
64 private final SortedMap<Integer, String> names;
65 private final Map<String, Type> typeMap = new HashMap<>();
66
67 private boolean hasParamAnnotation;
68 private boolean useParamMap;
69
70 public ParamNameResolver(Configuration config, Method method, Class<?> mapperClass) {
71 this.useActualParamName = config.isUseActualParamName();
72 final Class<?>[] paramTypes = method.getParameterTypes();
73 final Annotation[][] paramAnnotations = method.getParameterAnnotations();
74 final SortedMap<Integer, String> map = new TreeMap<>();
75 Type[] actualParamTypes = TypeParameterResolver.resolveParamTypes(method, mapperClass);
76 int paramCount = paramAnnotations.length;
77
78 for (int paramIndex = 0; paramIndex < paramCount; paramIndex++) {
79 if (isSpecialParameter(paramTypes[paramIndex])) {
80
81 continue;
82 }
83 String name = null;
84 for (Annotation annotation : paramAnnotations[paramIndex]) {
85 if (annotation instanceof Param) {
86 hasParamAnnotation = true;
87 useParamMap = true;
88 name = ((Param) annotation).value();
89 break;
90 }
91 }
92 if (name == null) {
93
94 if (useActualParamName) {
95 name = getActualParamName(method, paramIndex);
96 }
97 if (name == null) {
98
99
100 name = String.valueOf(map.size());
101 }
102 }
103 map.put(paramIndex, name);
104 typeMap.put(name, actualParamTypes[paramIndex]);
105 }
106 names = Collections.unmodifiableSortedMap(map);
107 if (names.size() > 1) {
108 useParamMap = true;
109 }
110 if (names.size() == 1) {
111 Type soleParamType = actualParamTypes[0];
112 if (soleParamType instanceof GenericArrayType) {
113 typeMap.put("array", soleParamType);
114 } else {
115 Class<?> soleParamClass = null;
116 if (soleParamType instanceof ParameterizedType) {
117 soleParamClass = (Class<?>) ((ParameterizedType) soleParamType).getRawType();
118 } else if (soleParamType instanceof Class) {
119 soleParamClass = (Class<?>) soleParamType;
120 }
121 if (Collection.class.isAssignableFrom(soleParamClass)) {
122 typeMap.put("collection", soleParamType);
123 if (List.class.isAssignableFrom(soleParamClass)) {
124 typeMap.put("list", soleParamType);
125 }
126 }
127 }
128 }
129 }
130
131 private String getActualParamName(Method method, int paramIndex) {
132 return ParamNameUtil.getParamNames(method).get(paramIndex);
133 }
134
135 private static boolean isSpecialParameter(Class<?> clazz) {
136 return RowBounds.class.isAssignableFrom(clazz) || ResultHandler.class.isAssignableFrom(clazz);
137 }
138
139
140
141
142
143
144 public String[] getNames() {
145 return names.values().toArray(new String[0]);
146 }
147
148
149
150
151
152
153
154
155
156
157 public Object getNamedParams(Object[] args) {
158 final int paramCount = names.size();
159 if (args == null || paramCount == 0) {
160 return null;
161 }
162 if (!hasParamAnnotation && paramCount == 1) {
163 Object value = args[names.firstKey()];
164 return wrapToMapIfCollection(value, useActualParamName ? names.get(names.firstKey()) : null);
165 } else {
166 final Map<String, Object> param = new ParamMap<>();
167 int i = 0;
168 for (Map.Entry<Integer, String> entry : names.entrySet()) {
169 param.put(entry.getValue(), args[entry.getKey()]);
170
171 final String genericParamName = i < 10 ? GENERIC_NAME_CACHE[i] : GENERIC_NAME_PREFIX + (i + 1);
172
173 if (!names.containsValue(genericParamName)) {
174 param.put(genericParamName, args[entry.getKey()]);
175 }
176 i++;
177 }
178 return param;
179 }
180 }
181
182 public Type getType(String name) {
183 PropertyTokenizer propertyTokenizer = new PropertyTokenizer(name);
184 String unindexed = propertyTokenizer.getName();
185 Type type = typeMap.get(unindexed);
186
187 if (type == null && unindexed.startsWith(GENERIC_NAME_PREFIX)) {
188 try {
189 Integer paramIndex = Integer.valueOf(unindexed.substring(GENERIC_NAME_PREFIX.length())) - 1;
190 unindexed = names.get(paramIndex);
191 if (unindexed != null) {
192 type = typeMap.get(unindexed);
193 }
194 } catch (NumberFormatException e) {
195
196 }
197 }
198
199 if (propertyTokenizer.getIndex() != null) {
200 if (type instanceof ParameterizedType) {
201 Type[] typeArgs = ((ParameterizedType) type).getActualTypeArguments();
202 return typeArgs[0];
203 } else if (type instanceof Class && ((Class<?>) type).isArray()) {
204 return ((Class<?>) type).getComponentType();
205 }
206 }
207 return type;
208 }
209
210
211
212
213
214
215
216
217
218
219
220
221
222 public static Object wrapToMapIfCollection(Object object, String actualParamName) {
223 if (object instanceof Collection) {
224 ParamMap<Object> map = new ParamMap<>();
225 map.put("collection", object);
226 if (object instanceof List) {
227 map.put("list", object);
228 }
229 Optional.ofNullable(actualParamName).ifPresent(name -> map.put(name, object));
230 return map;
231 }
232 if (object != null && object.getClass().isArray()) {
233 ParamMap<Object> map = new ParamMap<>();
234 map.put("array", object);
235 Optional.ofNullable(actualParamName).ifPresent(name -> map.put(name, object));
236 return map;
237 }
238 return object;
239 }
240
241 public boolean isUseParamMap() {
242 return useParamMap;
243 }
244 }