15
15
*/
16
16
package org .springframework .data .mongodb .core .convert ;
17
17
18
+ import static org .springframework .data .mongodb .core .convert .ReferenceLookupDelegate .*;
18
19
import static org .springframework .util .ReflectionUtils .*;
19
20
20
21
import java .io .Serializable ;
21
22
import java .lang .reflect .Method ;
22
23
23
- import javax .annotation .Nonnull ;
24
- import javax .annotation .Nullable ;
25
-
26
24
import org .aopalliance .intercept .MethodInterceptor ;
27
25
import org .aopalliance .intercept .MethodInvocation ;
26
+
28
27
import org .springframework .aop .framework .ProxyFactory ;
29
28
import org .springframework .cglib .proxy .Callback ;
30
29
import org .springframework .cglib .proxy .Enhancer ;
31
30
import org .springframework .cglib .proxy .Factory ;
32
31
import org .springframework .cglib .proxy .MethodProxy ;
33
- import org .springframework .data .mongodb .core .convert .ReferenceResolver .LookupFunction ;
34
- import org .springframework .data .mongodb .core .convert .ReferenceResolver .ResultConversionFunction ;
32
+ import org .springframework .data .mongodb .core .convert .ReferenceResolver .MongoEntityReader ;
35
33
import org .springframework .data .mongodb .core .mapping .MongoPersistentProperty ;
34
+ import org .springframework .lang .Nullable ;
36
35
import org .springframework .objenesis .ObjenesisStd ;
37
36
import org .springframework .util .ReflectionUtils ;
38
37
39
38
/**
40
39
* @author Christoph Strobl
41
40
*/
42
- class LazyLoadingProxyGenerator {
41
+ class LazyLoadingProxyFactory {
43
42
44
43
private final ObjenesisStd objenesis ;
45
- private final ReferenceReader referenceReader ;
44
+ private final ReferenceLookupDelegate lookupDelegate ;
46
45
47
- public LazyLoadingProxyGenerator ( ReferenceReader referenceReader ) {
46
+ public LazyLoadingProxyFactory ( ReferenceLookupDelegate lookupDelegate ) {
48
47
49
- this .referenceReader = referenceReader ;
48
+ this .lookupDelegate = lookupDelegate ;
50
49
this .objenesis = new ObjenesisStd (true );
51
50
}
52
51
53
52
public Object createLazyLoadingProxy (MongoPersistentProperty property , Object source , LookupFunction lookupFunction ,
54
- ResultConversionFunction resultConversionFunction ) {
53
+ MongoEntityReader entityReader ) {
55
54
56
55
Class <?> propertyType = property .getType ();
57
- LazyLoadingInterceptor interceptor = new LazyLoadingInterceptor (property , source , referenceReader , lookupFunction ,
58
- resultConversionFunction );
56
+ LazyLoadingInterceptor interceptor = new LazyLoadingInterceptor (property , source , lookupDelegate , lookupFunction ,
57
+ entityReader );
59
58
60
59
if (!propertyType .isInterface ()) {
61
60
@@ -97,13 +96,13 @@ private Class<?> getEnhancedTypeFor(Class<?> type) {
97
96
public static class LazyLoadingInterceptor
98
97
implements MethodInterceptor , org .springframework .cglib .proxy .MethodInterceptor , Serializable {
99
98
100
- private final ReferenceReader referenceReader ;
101
- MongoPersistentProperty property ;
99
+ private final ReferenceLookupDelegate referenceLookupDelegate ;
100
+ private final MongoPersistentProperty property ;
102
101
private volatile boolean resolved ;
103
- private @ org . springframework . lang . Nullable Object result ;
104
- private Object source ;
105
- private LookupFunction lookupFunction ;
106
- private ResultConversionFunction resultConversionFunction ;
102
+ private @ Nullable Object result ;
103
+ private final Object source ;
104
+ private final LookupFunction lookupFunction ;
105
+ private final MongoEntityReader entityReader ;
107
106
108
107
private final Method INITIALIZE_METHOD , TO_DBREF_METHOD , FINALIZE_METHOD , GET_SOURCE_METHOD ;
109
108
@@ -118,22 +117,23 @@ public static class LazyLoadingInterceptor
118
117
}
119
118
}
120
119
121
- public LazyLoadingInterceptor (MongoPersistentProperty property , Object source , ReferenceReader reader ,
122
- LookupFunction lookupFunction , ResultConversionFunction resultConversionFunction ) {
120
+ public LazyLoadingInterceptor (MongoPersistentProperty property , Object source , ReferenceLookupDelegate reader ,
121
+ LookupFunction lookupFunction , MongoEntityReader entityReader ) {
123
122
124
123
this .property = property ;
125
124
this .source = source ;
126
- this .referenceReader = reader ;
125
+ this .referenceLookupDelegate = reader ;
127
126
this .lookupFunction = lookupFunction ;
128
- this .resultConversionFunction = resultConversionFunction ;
127
+ this .entityReader = entityReader ;
129
128
}
130
129
131
130
@ Nullable
132
131
@ Override
133
- public Object invoke (@ Nonnull MethodInvocation invocation ) throws Throwable {
132
+ public Object invoke (MethodInvocation invocation ) throws Throwable {
134
133
return intercept (invocation .getThis (), invocation .getMethod (), invocation .getArguments (), null );
135
134
}
136
135
136
+ @ Nullable
137
137
@ Override
138
138
public Object intercept (Object o , Method method , Object [] args , MethodProxy proxy ) throws Throwable {
139
139
@@ -180,6 +180,7 @@ public Object intercept(Object o, Method method, Object[] args, MethodProxy prox
180
180
return method .invoke (target , args );
181
181
}
182
182
183
+ @ Nullable
183
184
private Object ensureResolved () {
184
185
185
186
if (!resolved ) {
@@ -190,7 +191,7 @@ private Object ensureResolved() {
190
191
return this .result ;
191
192
}
192
193
193
- private String proxyToString (Object source ) {
194
+ private String proxyToString (@ Nullable Object source ) {
194
195
195
196
StringBuilder description = new StringBuilder ();
196
197
if (source != null ) {
@@ -203,7 +204,7 @@ private String proxyToString(Object source) {
203
204
return description .toString ();
204
205
}
205
206
206
- private boolean proxyEquals (@ org . springframework . lang . Nullable Object proxy , Object that ) {
207
+ private boolean proxyEquals (@ Nullable Object proxy , Object that ) {
207
208
208
209
if (!(that instanceof LazyLoadingProxy )) {
209
210
return false ;
@@ -216,11 +217,11 @@ private boolean proxyEquals(@org.springframework.lang.Nullable Object proxy, Obj
216
217
return proxyToString (proxy ).equals (that .toString ());
217
218
}
218
219
219
- private int proxyHashCode (@ org . springframework . lang . Nullable Object proxy ) {
220
+ private int proxyHashCode (@ Nullable Object proxy ) {
220
221
return proxyToString (proxy ).hashCode ();
221
222
}
222
223
223
- @ org . springframework . lang . Nullable
224
+ @ Nullable
224
225
private synchronized Object resolve () {
225
226
226
227
if (resolved ) {
@@ -238,7 +239,7 @@ private synchronized Object resolve() {
238
239
// property.getOwner() != null ? property.getOwner().getName() : "unknown", property.getName());
239
240
// }
240
241
241
- return referenceReader .readReference (property , source , lookupFunction , resultConversionFunction );
242
+ return referenceLookupDelegate .readReference (property , source , lookupFunction , entityReader );
242
243
243
244
} catch (RuntimeException ex ) {
244
245
throw ex ;
@@ -254,4 +255,5 @@ private synchronized Object resolve() {
254
255
}
255
256
}
256
257
}
258
+
257
259
}
0 commit comments