1818import java .util .Arrays ;
1919import java .util .List ;
2020
21- import org .aopalliance .intercept .MethodInterceptor ;
22- import org .aopalliance .intercept .MethodInvocation ;
2321import org .apache .commons .logging .Log ;
2422import org .apache .commons .logging .LogFactory ;
2523import org .bson .Document ;
26- import org .springframework .aop .framework .ProxyFactory ;
24+
25+ import org .springframework .data .mongodb .core .query .BasicQuery ;
2726import org .springframework .data .mongodb .core .query .Collation ;
2827import org .springframework .data .mongodb .core .query .Query ;
2928import org .springframework .data .repository .query .QueryMethodEvaluationContextProvider ;
3029import org .springframework .expression .ExpressionParser ;
31- import org .springframework .lang .NonNull ;
3230import org .springframework .lang .Nullable ;
3331import org .springframework .util .ClassUtils ;
3432
4139 * @since 2.1
4240 * @currentRead Assassin's Apprentice - Robin Hobb
4341 */
44- public class QueryUtils {
42+ class QueryUtils {
4543
4644 protected static final Log LOGGER = LogFactory .getLog (QueryUtils .class );
4745
@@ -53,25 +51,19 @@ public class QueryUtils {
5351 * @param defaultSort the default sort expression to apply to the query.
5452 * @return the query having the given {@code sort} applied.
5553 */
56- public static Query decorateSort (Query query , Document defaultSort ) {
54+ static Query decorateSort (Query query , Document defaultSort ) {
5755
5856 if (defaultSort .isEmpty ()) {
5957 return query ;
6058 }
6159
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 );
6661
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 ;
7567 }
7668
7769 /**
@@ -116,48 +108,18 @@ static int indexOfAssignableParameter(Class<?> type, Class<?>[] parameters) {
116108 */
117109 static int indexOfAssignableParameter (Class <?> type , List <Class <?>> parameters ) {
118110
119- if (parameters .isEmpty ()) {
111+ if (parameters .isEmpty ()) {
120112 return -1 ;
121113 }
122114
123115 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 )) {
126118 return i ;
127119 }
128120 i ++;
129121 }
130122 return -1 ;
131123 }
132124
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- }
163125}
0 commit comments