| 1 | /* |
| 2 | * Copyright 2007 brunella ltd |
| 3 | * |
| 4 | * Licensed under the LGPL Version 3 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * |
| 7 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 8 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 9 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 10 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 13 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 14 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 15 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 16 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 17 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 18 | */ |
| 19 | package sf.qof.mapping; |
| 20 | |
| 21 | import java.lang.reflect.Method; |
| 22 | import java.lang.reflect.Type; |
| 23 | |
| 24 | import net.sf.cglib.core.Signature; |
| 25 | import sf.qof.util.ReflectionUtils; |
| 26 | |
| 27 | public class MethodInfoFactory { |
| 28 | |
| 29 | public static MethodInfo createMethodInfo(Method method) { |
| 30 | Signature signature = ReflectionUtils.getMethodSignature(method); |
| 31 | int modifiers = method.getModifiers(); |
| 32 | MethodParameterInfo[] parameterInfos = createParameterInfos(method); |
| 33 | MethodParameterInfo[] collectionParameterInfos = createCollectionParameterInfos(parameterInfos); |
| 34 | MethodReturnInfo returnInfo = createReturnInfos(method); |
| 35 | |
| 36 | return new MethodInfoImpl(signature, modifiers, parameterInfos, collectionParameterInfos, returnInfo, method.toGenericString()); |
| 37 | } |
| 38 | |
| 39 | private static MethodParameterInfo[] createParameterInfos(Method method) { |
| 40 | Type[] genericTypes = method.getGenericParameterTypes(); |
| 41 | Class<?>[] types = method.getParameterTypes(); |
| 42 | MethodParameterInfo[] parameterInfos = new MethodParameterInfo[genericTypes.length]; |
| 43 | |
| 44 | for (int i = 0; i < genericTypes.length; i++) { |
| 45 | Class<?> type = types[i]; |
| 46 | Class<?> collectionType = ReflectionUtils.getCollectionType(genericTypes[i]); |
| 47 | Class<?> collectionElementType; |
| 48 | if (collectionType == null) { |
| 49 | collectionElementType = null; |
| 50 | } else { |
| 51 | collectionElementType = ReflectionUtils.getCollectionParameterizedType(genericTypes[i]); |
| 52 | } |
| 53 | Class<?> arrayElementType = ReflectionUtils.getArrayComponentType(genericTypes[i]); |
| 54 | parameterInfos[i] = new MethodParameterInfoImpl(i, type, collectionType, collectionElementType, arrayElementType); |
| 55 | } |
| 56 | |
| 57 | return parameterInfos; |
| 58 | } |
| 59 | |
| 60 | private static MethodParameterInfo[] createCollectionParameterInfos(MethodParameterInfo[] parameterInfos) { |
| 61 | int num = 0; |
| 62 | for (int i = 0; i < parameterInfos.length; i++) { |
| 63 | if (parameterInfos[i].getCollectionType() != null) { |
| 64 | num++; |
| 65 | } |
| 66 | } |
| 67 | MethodParameterInfo[] collectionParameterInfos = new MethodParameterInfo[num]; |
| 68 | int index = 0; |
| 69 | for (int i = 0; i < parameterInfos.length; i++) { |
| 70 | if (parameterInfos[i].getCollectionType() != null) { |
| 71 | collectionParameterInfos[index++] = parameterInfos[i]; |
| 72 | } |
| 73 | } |
| 74 | return collectionParameterInfos; |
| 75 | } |
| 76 | |
| 77 | private static MethodReturnInfo createReturnInfos(Method method) { |
| 78 | Class<?> type = method.getReturnType(); |
| 79 | Class<?> collectionType = ReflectionUtils.getCollectionType(method.getGenericReturnType()); |
| 80 | Class<?> collectionElementType; |
| 81 | if (collectionType == null) { |
| 82 | collectionElementType = null; |
| 83 | } else { |
| 84 | collectionElementType = ReflectionUtils.getCollectionParameterizedType(method.getGenericReturnType()); |
| 85 | } |
| 86 | Class<?> mapKeyType = ReflectionUtils.getCollectionParameterizedKeyType(method.getGenericReturnType()); |
| 87 | return new MethodReturnInfoImpl(type, collectionType, collectionElementType, mapKeyType); |
| 88 | } |
| 89 | |
| 90 | protected static class MethodInfoImpl implements MethodInfo { |
| 91 | |
| 92 | public MethodInfoImpl(Signature signature, int modifiers, MethodParameterInfo[] parameterInfos, |
| 93 | MethodParameterInfo[] collectionParameterInfos, MethodReturnInfo returnInfo, String description) { |
| 94 | super(); |
| 95 | this.signature = signature; |
| 96 | this.modifiers = modifiers; |
| 97 | this.parameterInfos = parameterInfos; |
| 98 | this.collectionParameterInfos = collectionParameterInfos; |
| 99 | this.returnInfo = returnInfo; |
| 100 | this.description = description; |
| 101 | } |
| 102 | |
| 103 | private Signature signature; |
| 104 | private int modifiers; |
| 105 | private MethodParameterInfo[] parameterInfos; |
| 106 | private MethodParameterInfo[] collectionParameterInfos; |
| 107 | private MethodReturnInfo returnInfo; |
| 108 | private String description; |
| 109 | |
| 110 | public Signature getSignature() { |
| 111 | return signature; |
| 112 | } |
| 113 | |
| 114 | public int getModifiers() { |
| 115 | return modifiers; |
| 116 | } |
| 117 | |
| 118 | public MethodParameterInfo[] getParameterInfos() { |
| 119 | return parameterInfos; |
| 120 | } |
| 121 | |
| 122 | public MethodReturnInfo getReturnInfo() { |
| 123 | return returnInfo; |
| 124 | } |
| 125 | |
| 126 | public MethodParameterInfo[] getCollectionParameterInfos() { |
| 127 | return collectionParameterInfos; |
| 128 | } |
| 129 | |
| 130 | public String toString() { |
| 131 | return description; |
| 132 | } |
| 133 | |
| 134 | public String getDescription() { |
| 135 | return description; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | protected static class MethodParameterInfoImpl implements MethodParameterInfo { |
| 140 | |
| 141 | private int index; |
| 142 | private Class<?> type; |
| 143 | private Class<?> collectionType; |
| 144 | private Class<?> collectionElementType; |
| 145 | private Class<?> arrayElementType; |
| 146 | |
| 147 | public MethodParameterInfoImpl(int index, Class<?> type, Class<?> collectionType, Class<?> collectionElementType, |
| 148 | Class<?> arrayElementType) { |
| 149 | super(); |
| 150 | this.index = index; |
| 151 | this.type = type; |
| 152 | this.collectionType = collectionType; |
| 153 | this.collectionElementType = collectionElementType; |
| 154 | this.arrayElementType = arrayElementType; |
| 155 | } |
| 156 | |
| 157 | public int getIndex() { |
| 158 | return index; |
| 159 | } |
| 160 | |
| 161 | public Class<?> getType() { |
| 162 | return type; |
| 163 | } |
| 164 | |
| 165 | public Class<?> getCollectionType() { |
| 166 | return collectionType; |
| 167 | } |
| 168 | |
| 169 | public Class<?> getCollectionElementType() { |
| 170 | return collectionElementType; |
| 171 | } |
| 172 | |
| 173 | public Class<?> getArrayElementType() { |
| 174 | return arrayElementType; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | protected static class MethodReturnInfoImpl implements MethodReturnInfo { |
| 179 | |
| 180 | private Class<?> type; |
| 181 | private Class<?> collectionType; |
| 182 | private Class<?> collectionElementType; |
| 183 | private Class<?> mapKeyType; |
| 184 | |
| 185 | public MethodReturnInfoImpl(Class<?> type, Class<?> collectionType, Class<?> collectionElementType, |
| 186 | Class<?> mapKeyType) { |
| 187 | super(); |
| 188 | this.type = type; |
| 189 | this.collectionType = collectionType; |
| 190 | this.collectionElementType = collectionElementType; |
| 191 | this.mapKeyType = mapKeyType; |
| 192 | } |
| 193 | |
| 194 | public Class<?> getType() { |
| 195 | return type; |
| 196 | } |
| 197 | |
| 198 | public Class<?> getCollectionType() { |
| 199 | return collectionType; |
| 200 | } |
| 201 | |
| 202 | public Class<?> getCollectionElementType() { |
| 203 | return collectionElementType; |
| 204 | } |
| 205 | |
| 206 | public Class<?> getMapKeyType() { |
| 207 | return mapKeyType; |
| 208 | } |
| 209 | |
| 210 | } |
| 211 | } |