|
| 1 | +/** |
| 2 | + * Copyright 2009-2016 the original author or authors. |
| 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, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.apache.ibatis.reflection; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.SortedMap; |
| 24 | +import java.util.TreeMap; |
| 25 | + |
| 26 | +import org.apache.ibatis.annotations.Param; |
| 27 | +import org.apache.ibatis.binding.MapperMethod.ParamMap; |
| 28 | +import org.apache.ibatis.session.ResultHandler; |
| 29 | +import org.apache.ibatis.session.RowBounds; |
| 30 | + |
| 31 | +public class ParamNameResolver { |
| 32 | + |
| 33 | + private static final String GENERIC_NAME_PREFIX = "param"; |
| 34 | + |
| 35 | + /** |
| 36 | + * <p> |
| 37 | + * The key is the index and the value is the name of the parameter.<br /> |
| 38 | + * The name is obtained from {@link Param} if specified. When {@link Param} is not specified, |
| 39 | + * the parameter index is used. Note that this index could be different from the actual index |
| 40 | + * when the method has special parameters (i.e. {@link RowBounds} or {@link ResultHandler}). |
| 41 | + * </p> |
| 42 | + * <ul> |
| 43 | + * <li>aMethod(@Param("M") int a, @Param("N") int b) -> {{0, "M"}, {1, "N"}}</li> |
| 44 | + * <li>aMethod(int a, int b) -> {{0, "0"}, {1, "1"}}</li> |
| 45 | + * <li>aMethod(int a, RowBounds rb, int b) -> {{0, "0"}, {2, "1"}}</li> |
| 46 | + * </ul> |
| 47 | + */ |
| 48 | + private final SortedMap<Integer, String> names; |
| 49 | + |
| 50 | + private boolean hasParamAnnotation; |
| 51 | + |
| 52 | + public ParamNameResolver(Method method) { |
| 53 | + final Class<?>[] paramTypes = method.getParameterTypes(); |
| 54 | + final Annotation[][] paramAnnotations = method.getParameterAnnotations(); |
| 55 | + final SortedMap<Integer, String> map = new TreeMap<Integer, String>(); |
| 56 | + int paramCount = paramAnnotations.length; |
| 57 | + // get names from @Param annotations |
| 58 | + for (int paramIndex = 0; paramIndex < paramCount; paramIndex++) { |
| 59 | + if (isSpecialParameter(paramTypes[paramIndex])) { |
| 60 | + // skip special parameters |
| 61 | + continue; |
| 62 | + } |
| 63 | + String name = null; |
| 64 | + for (Annotation annotation : paramAnnotations[paramIndex]) { |
| 65 | + if (annotation instanceof Param) { |
| 66 | + hasParamAnnotation = true; |
| 67 | + name = ((Param) annotation).value(); |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + // When @Param is not specified, use the parameter index as the name |
| 72 | + // ("0", "1", ...) gcode issue #71 |
| 73 | + map.put(paramIndex, name == null ? String.valueOf(map.size()) : name); |
| 74 | + } |
| 75 | + names = Collections.unmodifiableSortedMap(map); |
| 76 | + } |
| 77 | + |
| 78 | + private static boolean isSpecialParameter(Class<?> clazz) { |
| 79 | + return RowBounds.class.isAssignableFrom(clazz) || ResultHandler.class.isAssignableFrom(clazz); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns parameter names referenced by SQL providers. |
| 84 | + */ |
| 85 | + public String[] getNames() { |
| 86 | + return names.values().toArray(new String[0]); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * <p> |
| 91 | + * A single non-special parameter is returned without a name.<br /> |
| 92 | + * Multiple parameters are named using the naming rule.<br /> |
| 93 | + * In addition to the default names, this method also adds the generic names (param1, param2, |
| 94 | + * ...). |
| 95 | + * </p> |
| 96 | + */ |
| 97 | + public Object getNamedParams(Object[] args) { |
| 98 | + final int paramCount = names.size(); |
| 99 | + if (args == null || paramCount == 0) { |
| 100 | + return null; |
| 101 | + } else if (!hasParamAnnotation && paramCount == 1) { |
| 102 | + return args[names.firstKey()]; |
| 103 | + } else { |
| 104 | + final Map<String, Object> param = new ParamMap<Object>(); |
| 105 | + int i = 0; |
| 106 | + for (Map.Entry<Integer, String> entry : names.entrySet()) { |
| 107 | + param.put(entry.getValue(), args[entry.getKey()]); |
| 108 | + // add generic param names (param1, param2, ...) |
| 109 | + final String genericParamName = GENERIC_NAME_PREFIX + String.valueOf(i + 1); |
| 110 | + // ensure not to overwrite parameter named with @Param |
| 111 | + if (!names.containsValue(genericParamName)) { |
| 112 | + param.put(genericParamName, args[entry.getKey()]); |
| 113 | + } |
| 114 | + i++; |
| 115 | + } |
| 116 | + return param; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments