18
18
import java .util .Arrays ;
19
19
import java .util .List ;
20
20
21
- import org .aopalliance .intercept .MethodInterceptor ;
22
- import org .aopalliance .intercept .MethodInvocation ;
23
21
import org .apache .commons .logging .Log ;
24
22
import org .apache .commons .logging .LogFactory ;
25
23
import org .bson .Document ;
26
- import org .springframework .aop .framework .ProxyFactory ;
24
+
25
+ import org .springframework .data .mongodb .core .query .BasicQuery ;
27
26
import org .springframework .data .mongodb .core .query .Collation ;
28
27
import org .springframework .data .mongodb .core .query .Query ;
29
28
import org .springframework .data .repository .query .QueryMethodEvaluationContextProvider ;
30
29
import org .springframework .expression .ExpressionParser ;
31
- import org .springframework .lang .NonNull ;
32
30
import org .springframework .lang .Nullable ;
33
31
import org .springframework .util .ClassUtils ;
34
32
41
39
* @since 2.1
42
40
* @currentRead Assassin's Apprentice - Robin Hobb
43
41
*/
44
- public class QueryUtils {
42
+ class QueryUtils {
45
43
46
44
protected static final Log LOGGER = LogFactory .getLog (QueryUtils .class );
47
45
@@ -53,25 +51,19 @@ public class QueryUtils {
53
51
* @param defaultSort the default sort expression to apply to the query.
54
52
* @return the query having the given {@code sort} applied.
55
53
*/
56
- public static Query decorateSort (Query query , Document defaultSort ) {
54
+ static Query decorateSort (Query query , Document defaultSort ) {
57
55
58
56
if (defaultSort .isEmpty ()) {
59
57
return query ;
60
58
}
61
59
62
- ProxyFactory factory = prepareQueryProxy (query .getClass (), defaultSort );
63
- factory .setTarget (query );
64
- return (Query ) factory .getProxy (query .getClass ().getClassLoader ());
65
- }
60
+ BasicQuery defaultSortQuery = query instanceof BasicQuery bq ? bq : new BasicQuery (query );
66
61
67
- /**
68
- * Decorate {@link Query} and add a default sort expression to the given {@link Query}. Attributes of the given
69
- * {@code sort} may be overwritten by the sort explicitly defined by the {@link Query} itself.
70
- *
71
- * @param classLoader the {@link ClassLoader} to use for generating the proxy type with.
72
- */
73
- public static Class <?> queryProxyType (Class <? extends Query > baseType , ClassLoader classLoader ) {
74
- return prepareQueryProxy (baseType , new Document ()).getProxyClass (classLoader );
62
+ Document combinedSort = new Document (defaultSort );
63
+ combinedSort .putAll (defaultSortQuery .getSortObject ());
64
+ defaultSortQuery .setSortObject (combinedSort );
65
+
66
+ return defaultSortQuery ;
75
67
}
76
68
77
69
/**
@@ -116,48 +108,18 @@ static int indexOfAssignableParameter(Class<?> type, Class<?>[] parameters) {
116
108
*/
117
109
static int indexOfAssignableParameter (Class <?> type , List <Class <?>> parameters ) {
118
110
119
- if (parameters .isEmpty ()) {
111
+ if (parameters .isEmpty ()) {
120
112
return -1 ;
121
113
}
122
114
123
115
int i = 0 ;
124
- for (Class <?> parameterType : parameters ) {
125
- if (ClassUtils .isAssignable (type , parameterType )) {
116
+ for (Class <?> parameterType : parameters ) {
117
+ if (ClassUtils .isAssignable (type , parameterType )) {
126
118
return i ;
127
119
}
128
120
i ++;
129
121
}
130
122
return -1 ;
131
123
}
132
124
133
- private static ProxyFactory prepareQueryProxy (Class <? extends Query > query , Document defaultSort ) {
134
-
135
- ProxyFactory factory = new ProxyFactory ();
136
- factory .setTargetClass (query );
137
- factory .addAdvice (new DefaultSortingInterceptor (defaultSort ));
138
- factory .setInterfaces (new Class [0 ]);
139
- return factory ;
140
- }
141
-
142
- static class DefaultSortingInterceptor implements MethodInterceptor {
143
-
144
- private final Document defaultSort ;
145
-
146
- public DefaultSortingInterceptor (Document defaultSort ) {
147
- this .defaultSort = defaultSort ;
148
- }
149
-
150
- @ Nullable
151
- @ Override
152
- public Object invoke (@ NonNull MethodInvocation invocation ) throws Throwable {
153
-
154
- if (!invocation .getMethod ().getName ().equals ("getSortObject" )) {
155
- return invocation .proceed ();
156
- }
157
-
158
- Document combinedSort = new Document (defaultSort );
159
- combinedSort .putAll ((Document ) invocation .proceed ());
160
- return combinedSort ;
161
- }
162
- }
163
125
}
0 commit comments