|
| 1 | +/* |
| 2 | + * Copyright 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 | +package org.springframework.data.repository.core.support; |
| 17 | + |
| 18 | +import static org.springframework.core.GenericTypeResolver.*; |
| 19 | + |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.lang.reflect.Type; |
| 22 | +import java.util.function.BiPredicate; |
| 23 | + |
| 24 | +import org.springframework.core.MethodParameter; |
| 25 | +import org.springframework.core.convert.ConversionService; |
| 26 | +import org.springframework.data.repository.core.RepositoryInformation; |
| 27 | +import org.springframework.data.repository.core.RepositoryMetadata; |
| 28 | +import org.springframework.data.repository.util.QueryExecutionConverters; |
| 29 | +import org.springframework.util.Assert; |
| 30 | + |
| 31 | +/** |
| 32 | + * This {@link RepositoryInformation} uses a {@link ConversionService} to check whether method arguments can be |
| 33 | + * converted for invocation of implementation methods. |
| 34 | + * |
| 35 | + * @author Mark Paluch |
| 36 | + */ |
| 37 | +public class ReactiveRepositoryInformation extends DefaultRepositoryInformation { |
| 38 | + |
| 39 | + private final ConversionService conversionService; |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a new {@link ReactiveRepositoryInformation} for the given repository interface and repository base class |
| 43 | + * using a {@link ConversionService}. |
| 44 | + * |
| 45 | + * @param metadata must not be {@literal null}. |
| 46 | + * @param repositoryBaseClass must not be {@literal null}. |
| 47 | + * @param customImplementationClass |
| 48 | + * @param conversionService must not be {@literal null}. |
| 49 | + */ |
| 50 | + public ReactiveRepositoryInformation(RepositoryMetadata metadata, Class<?> repositoryBaseClass, |
| 51 | + Class<?> customImplementationClass, ConversionService conversionService) { |
| 52 | + |
| 53 | + super(metadata, repositoryBaseClass, customImplementationClass); |
| 54 | + |
| 55 | + Assert.notNull(conversionService, "Conversion service must not be null!"); |
| 56 | + |
| 57 | + this.conversionService = conversionService; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns the given target class' method if the given method (declared in the repository interface) was also declared |
| 62 | + * at the target class. Returns the given method if the given base class does not declare the method given. Takes |
| 63 | + * generics into account. |
| 64 | + * |
| 65 | + * @param method must not be {@literal null} |
| 66 | + * @param baseClass |
| 67 | + * @return |
| 68 | + */ |
| 69 | + Method getTargetClassMethod(Method method, Class<?> baseClass) { |
| 70 | + |
| 71 | + if (baseClass == null) { |
| 72 | + return method; |
| 73 | + } |
| 74 | + |
| 75 | + boolean wantsWrappers = wantsMethodUsingReactiveWrapperParameters(method); |
| 76 | + |
| 77 | + if (wantsWrappers) { |
| 78 | + Method candidate = getMethodCandidate(method, baseClass, new ExactWrapperMatch(method)); |
| 79 | + |
| 80 | + if (candidate != null) { |
| 81 | + return candidate; |
| 82 | + } |
| 83 | + |
| 84 | + candidate = getMethodCandidate(method, baseClass, new WrapperConversionMatch(method, conversionService)); |
| 85 | + |
| 86 | + if (candidate != null) { |
| 87 | + return candidate; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + Method candidate = getMethodCandidate(method, baseClass, |
| 92 | + new MatchParameterOrComponentType(method, getRepositoryInterface())); |
| 93 | + |
| 94 | + if (candidate != null) { |
| 95 | + return candidate; |
| 96 | + } |
| 97 | + |
| 98 | + return method; |
| 99 | + } |
| 100 | + |
| 101 | + private boolean wantsMethodUsingReactiveWrapperParameters(Method method) { |
| 102 | + |
| 103 | + boolean wantsWrappers = false; |
| 104 | + |
| 105 | + for (Class<?> parameterType : method.getParameterTypes()) { |
| 106 | + if (isNonunwrappingWrapper(parameterType)) { |
| 107 | + wantsWrappers = true; |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return wantsWrappers; |
| 113 | + } |
| 114 | + |
| 115 | + private Method getMethodCandidate(Method method, Class<?> baseClass, BiPredicate<Class<?>, Integer> predicate) { |
| 116 | + |
| 117 | + for (Method baseClassMethod : baseClass.getMethods()) { |
| 118 | + |
| 119 | + // Wrong name |
| 120 | + if (!method.getName().equals(baseClassMethod.getName())) { |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + // Wrong number of arguments |
| 125 | + if (!(method.getParameterTypes().length == baseClassMethod.getParameterTypes().length)) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + // Check whether all parameters match |
| 130 | + if (!parametersMatch(method, baseClassMethod, predicate)) { |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + return baseClassMethod; |
| 135 | + } |
| 136 | + |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Checks the given method's parameters to match the ones of the given base class method. Matches generic arguments |
| 142 | + * against the ones bound in the given repository interface. |
| 143 | + * |
| 144 | + * @param method |
| 145 | + * @param baseClassMethod |
| 146 | + * @param predicate |
| 147 | + * @return |
| 148 | + */ |
| 149 | + private boolean parametersMatch(Method method, Method baseClassMethod, BiPredicate<Class<?>, Integer> predicate) { |
| 150 | + |
| 151 | + Type[] genericTypes = baseClassMethod.getGenericParameterTypes(); |
| 152 | + Class<?>[] types = baseClassMethod.getParameterTypes(); |
| 153 | + |
| 154 | + for (int i = 0; i < genericTypes.length; i++) { |
| 155 | + if (!predicate.test(types[i], i)) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Checks whether the type is a wrapper without unwrapping support. Reactive wrappers don't like to be unwrapped. |
| 165 | + * |
| 166 | + * @param parameterType |
| 167 | + * @return |
| 168 | + */ |
| 169 | + static boolean isNonunwrappingWrapper(Class<?> parameterType) { |
| 170 | + return QueryExecutionConverters.supports(parameterType) |
| 171 | + && !QueryExecutionConverters.supportsUnwrapping(parameterType); |
| 172 | + } |
| 173 | + |
| 174 | + static class WrapperConversionMatch implements BiPredicate<Class<?>, Integer> { |
| 175 | + |
| 176 | + final Method declaredMethod; |
| 177 | + final Class<?>[] declaredParameterTypes; |
| 178 | + final ConversionService conversionService; |
| 179 | + |
| 180 | + public WrapperConversionMatch(Method declaredMethod, ConversionService conversionService) { |
| 181 | + |
| 182 | + this.declaredMethod = declaredMethod; |
| 183 | + this.declaredParameterTypes = declaredMethod.getParameterTypes(); |
| 184 | + this.conversionService = conversionService; |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public boolean test(Class<?> candidateParameterType, Integer index) { |
| 189 | + |
| 190 | + // TODO: should check for component type |
| 191 | + if (isNonunwrappingWrapper(candidateParameterType) && isNonunwrappingWrapper(declaredParameterTypes[index])) { |
| 192 | + |
| 193 | + if (conversionService.canConvert(declaredParameterTypes[index], candidateParameterType)) { |
| 194 | + return true; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + |
| 203 | + static class ExactWrapperMatch implements BiPredicate<Class<?>, Integer> { |
| 204 | + |
| 205 | + final Method declaredMethod; |
| 206 | + final Class<?>[] declaredParameterTypes; |
| 207 | + |
| 208 | + public ExactWrapperMatch(Method declaredMethod) { |
| 209 | + |
| 210 | + this.declaredMethod = declaredMethod; |
| 211 | + this.declaredParameterTypes = declaredMethod.getParameterTypes(); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public boolean test(Class<?> candidateParameterType, Integer index) { |
| 216 | + |
| 217 | + // TODO: should check for component type |
| 218 | + if (isNonunwrappingWrapper(candidateParameterType) && isNonunwrappingWrapper(declaredParameterTypes[index])) { |
| 219 | + |
| 220 | + if (declaredParameterTypes[index].isAssignableFrom(candidateParameterType)) { |
| 221 | + return true; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + return false; |
| 226 | + } |
| 227 | + |
| 228 | + } |
| 229 | + |
| 230 | + static class MatchParameterOrComponentType implements BiPredicate<Class<?>, Integer> { |
| 231 | + |
| 232 | + final Method declaredMethod; |
| 233 | + final Class<?>[] declaredParameterTypes; |
| 234 | + final Class<?> repositoryInterface; |
| 235 | + |
| 236 | + public MatchParameterOrComponentType(Method declaredMethod, Class<?> repositoryInterface) { |
| 237 | + |
| 238 | + this.declaredMethod = declaredMethod; |
| 239 | + this.declaredParameterTypes = declaredMethod.getParameterTypes(); |
| 240 | + this.repositoryInterface = repositoryInterface; |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public boolean test(Class<?> candidateParameterType, Integer index) { |
| 245 | + |
| 246 | + MethodParameter parameter = new MethodParameter(declaredMethod, index); |
| 247 | + Class<?> parameterType = resolveParameterType(parameter, repositoryInterface); |
| 248 | + |
| 249 | + if (!candidateParameterType.isAssignableFrom(parameterType) |
| 250 | + || !candidateParameterType.equals(declaredParameterTypes[index])) { |
| 251 | + return false; |
| 252 | + } |
| 253 | + |
| 254 | + return true; |
| 255 | + } |
| 256 | + |
| 257 | + } |
| 258 | + |
| 259 | +} |
0 commit comments