|
40 | 40 | public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentResolver {
|
41 | 41 |
|
42 | 42 | private static final ResolvableType PREDICATE = ResolvableType.forClass(Predicate.class);
|
43 |
| - private static final ResolvableType OPTIONAL_OF_PREDICATE = ResolvableType.forClassWithGenerics(Optional.class, |
44 |
| - PREDICATE); |
45 |
| - |
46 |
| - private final QuerydslBindingsFactory bindingsFactory; |
47 |
| - private final QuerydslPredicateBuilder predicateBuilder; |
48 |
| - |
49 |
| - /** |
50 |
| - * Creates a new {@link QuerydslPredicateArgumentResolver} using the given |
51 |
| - * {@link ConversionService}. |
52 |
| - * |
53 |
| - * @param factory |
54 |
| - * @param conversionService defaults to {@link DefaultConversionService} if |
55 |
| - * {@literal null}. |
56 |
| - */ |
57 |
| - public QuerydslPredicateArgumentResolver(QuerydslBindingsFactory factory, |
58 |
| - Optional<ConversionService> conversionService) { |
59 |
| - |
60 |
| - this.bindingsFactory = factory; |
61 |
| - this.predicateBuilder = new QuerydslPredicateBuilder(conversionService.orElseGet(DefaultConversionService::new), |
62 |
| - factory.getEntityPathResolver()); |
63 |
| - } |
| 43 | + private static final ResolvableType OPTIONAL_OF_PREDICATE = ResolvableType.forClassWithGenerics(Optional.class, |
| 44 | + PREDICATE); |
| 45 | + |
| 46 | + private final QuerydslBindingsFactory bindingsFactory; |
| 47 | + private final QuerydslPredicateBuilder predicateBuilder; |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a new {@link QuerydslPredicateArgumentResolver} using the given {@link ConversionService}. |
| 51 | + * |
| 52 | + * @param factory |
| 53 | + * @param conversionService defaults to {@link DefaultConversionService} if {@literal null}. |
| 54 | + */ |
| 55 | + public QuerydslPredicateArgumentResolver(QuerydslBindingsFactory factory, |
| 56 | + Optional<ConversionService> conversionService) { |
| 57 | + |
| 58 | + this.bindingsFactory = factory; |
| 59 | + this.predicateBuilder = new QuerydslPredicateBuilder(conversionService.orElseGet(DefaultConversionService::new), |
| 60 | + factory.getEntityPathResolver()); |
| 61 | + } |
| 62 | + |
| 63 | + /* |
| 64 | + * (non-Javadoc) |
| 65 | + * @see org.springframework.web.method.support.HandlerMethodArgumentResolver#supportsParameter(org.springframework.core.MethodParameter) |
| 66 | + */ |
| 67 | + @Override |
| 68 | + public boolean supportsParameter(MethodParameter parameter) { |
| 69 | + |
| 70 | + ResolvableType type = ResolvableType.forMethodParameter(parameter); |
| 71 | + |
| 72 | + if (PREDICATE.isAssignableFrom(type) || OPTIONAL_OF_PREDICATE.isAssignableFrom(type)) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + if (parameter.hasParameterAnnotation(QuerydslPredicate.class)) { |
| 77 | + throw new IllegalArgumentException(String.format("Parameter at position %s must be of type Predicate but was %s.", |
| 78 | + parameter.getParameterIndex(), parameter.getParameterType())); |
| 79 | + } |
| 80 | + |
| 81 | + return false; |
| 82 | + } |
64 | 83 |
|
65 | 84 | /*
|
66 | 85 | * (non-Javadoc)
|
67 | 86 | *
|
68 |
| - * @see org.springframework.web.method.support.HandlerMethodArgumentResolver# |
69 |
| - * supportsParameter(org.springframework.core.MethodParameter) |
70 |
| - */ |
71 |
| - @Override |
72 |
| - public boolean supportsParameter(MethodParameter parameter) { |
73 |
| - |
74 |
| - ResolvableType type = ResolvableType.forMethodParameter(parameter); |
75 |
| - |
76 |
| - if (PREDICATE.isAssignableFrom(type) || OPTIONAL_OF_PREDICATE.isAssignableFrom(type)) { |
77 |
| - return true; |
78 |
| - } |
79 |
| - |
80 |
| - if (parameter.hasParameterAnnotation(QuerydslPredicate.class)) { |
81 |
| - throw new IllegalArgumentException( |
82 |
| - String.format("Parameter at position %s must be of type Predicate but was %s.", |
83 |
| - parameter.getParameterIndex(), parameter.getParameterType())); |
84 |
| - } |
85 |
| - |
86 |
| - return false; |
87 |
| - } |
88 |
| - |
89 |
| - /* |
90 |
| - * (non-Javadoc) |
91 |
| - * |
92 |
| - * @see org.springframework.web.method.support.HandlerMethodArgumentResolver# |
93 |
| - * resolveArgument(org.springframework.core.MethodParameter, |
94 |
| - * org.springframework.web.method.support.ModelAndViewContainer, |
95 |
| - * org.springframework.web.context.request.NativeWebRequest, |
96 |
| - * org.springframework.web.bind.support.WebDataBinderFactory) |
| 87 | + * @seeorg.springframework.web.reactive.result.method.HandlerMethodArgumentResolver#resolveArgument(org.springframework.core.MethodParameter, org.springframework.web.reactive.BindingContext, org.springframework.web.server.ServerWebExchange) |
97 | 88 | */
|
98 | 89 | @Override
|
99 | 90 | public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext bindingContext, ServerWebExchange exchange) {
|
@@ -127,55 +118,53 @@ public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext bi
|
127 | 118 | }
|
128 | 119 |
|
129 | 120 | /**
|
130 |
| - * Obtains the domain type information from the given method parameter. Will |
131 |
| - * favor an explicitly registered on through {@link QuerydslPredicate#root()} |
132 |
| - * but use the actual type of the method's return type as fallback. |
133 |
| - * |
134 |
| - * @param parameter must not be {@literal null}. |
135 |
| - * @return |
136 |
| - */ |
137 |
| - static TypeInformation<?> extractTypeInfo(MethodParameter parameter) { |
| 121 | + * Obtains the domain type information from the given method parameter. Will favor an explicitly registered on through |
| 122 | + * {@link QuerydslPredicate#root()} but use the actual type of the method's return type as fallback. |
| 123 | + * |
| 124 | + * @param parameter must not be {@literal null}. |
| 125 | + * @return |
| 126 | + */ |
| 127 | + static TypeInformation<?> extractTypeInfo(MethodParameter parameter) { |
138 | 128 |
|
139 |
| - Optional<QuerydslPredicate> annotation = Optional |
140 |
| - .ofNullable(parameter.getParameterAnnotation(QuerydslPredicate.class)); |
| 129 | + Optional<QuerydslPredicate> annotation = Optional |
| 130 | + .ofNullable(parameter.getParameterAnnotation(QuerydslPredicate.class)); |
141 | 131 |
|
142 |
| - return annotation.filter(it -> !Object.class.equals(it.root()))// |
143 |
| - .<TypeInformation<?>>map(it -> ClassTypeInformation.from(it.root()))// |
144 |
| - .orElseGet(() -> detectDomainType(parameter)); |
145 |
| - } |
| 132 | + return annotation.filter(it -> !Object.class.equals(it.root()))// |
| 133 | + .<TypeInformation<?>> map(it -> ClassTypeInformation.from(it.root()))// |
| 134 | + .orElseGet(() -> detectDomainType(parameter)); |
| 135 | + } |
146 | 136 |
|
147 |
| - private static TypeInformation<?> detectDomainType(MethodParameter parameter) { |
| 137 | + private static TypeInformation<?> detectDomainType(MethodParameter parameter) { |
148 | 138 |
|
149 |
| - Method method = parameter.getMethod(); |
| 139 | + Method method = parameter.getMethod(); |
150 | 140 |
|
151 |
| - if (method == null) { |
152 |
| - throw new IllegalArgumentException("Method parameter is not backed by a method!"); |
153 |
| - } |
| 141 | + if (method == null) { |
| 142 | + throw new IllegalArgumentException("Method parameter is not backed by a method!"); |
| 143 | + } |
154 | 144 |
|
155 |
| - return detectDomainType(ClassTypeInformation.fromReturnTypeOf(method)); |
156 |
| - } |
| 145 | + return detectDomainType(ClassTypeInformation.fromReturnTypeOf(method)); |
| 146 | + } |
157 | 147 |
|
158 |
| - private static TypeInformation<?> detectDomainType(TypeInformation<?> source) { |
| 148 | + private static TypeInformation<?> detectDomainType(TypeInformation<?> source) { |
159 | 149 |
|
160 |
| - if (source.getTypeArguments().isEmpty()) { |
161 |
| - return source; |
162 |
| - } |
| 150 | + if (source.getTypeArguments().isEmpty()) { |
| 151 | + return source; |
| 152 | + } |
163 | 153 |
|
164 |
| - TypeInformation<?> actualType = source.getActualType(); |
| 154 | + TypeInformation<?> actualType = source.getActualType(); |
165 | 155 |
|
166 |
| - if (actualType == null) { |
167 |
| - throw new IllegalArgumentException(String.format("Could not determine domain type from %s!", source)); |
168 |
| - } |
| 156 | + if (actualType == null) { |
| 157 | + throw new IllegalArgumentException(String.format("Could not determine domain type from %s!", source)); |
| 158 | + } |
169 | 159 |
|
170 |
| - if (source != actualType) { |
171 |
| - return detectDomainType(actualType); |
172 |
| - } |
| 160 | + if (source != actualType) { |
| 161 | + return detectDomainType(actualType); |
| 162 | + } |
173 | 163 |
|
174 |
| - if (source instanceof Iterable) { |
175 |
| - return source; |
176 |
| - } |
177 |
| - |
178 |
| - return detectDomainType(source.getRequiredComponentType()); |
179 |
| - } |
| 164 | + if (source instanceof Iterable) { |
| 165 | + return source; |
| 166 | + } |
180 | 167 |
|
| 168 | + return detectDomainType(source.getRequiredComponentType()); |
| 169 | + } |
181 | 170 | }
|
0 commit comments