29
29
import org .springframework .beans .BeanUtils ;
30
30
import org .springframework .util .Assert ;
31
31
32
- import com .fasterxml .jackson .annotation .JsonIgnore ;
33
-
34
32
/**
35
33
* A {@link ProjectionFactory} to create JDK proxies to back interfaces and handle method invocations on them. By
36
34
* default accessor methods are supported. In case the delegating lookups result in an object of different type that the
@@ -64,13 +62,13 @@ public <T> T createProjection(Class<T> projectionType, Object source) {
64
62
ProxyFactory factory = new ProxyFactory ();
65
63
factory .setTarget (source );
66
64
factory .setOpaque (true );
67
- factory .setInterfaces (projectionType , TargetClassAware .class );
65
+ factory .setInterfaces (projectionType , TargetAware .class );
68
66
69
67
if (IS_JAVA_8 ) {
70
68
factory .addAdvice (new DefaultMethodInvokingMethodInterceptor ());
71
69
}
72
70
73
- factory .addAdvice (new TargetClassAwareMethodInterceptor (source .getClass ()));
71
+ factory .addAdvice (new TargetAwareMethodInterceptor (source .getClass ()));
74
72
factory .addAdvice (getMethodInterceptor (source , projectionType ));
75
73
76
74
return (T ) factory .getProxy (getClass ().getClassLoader ());
@@ -152,45 +150,37 @@ protected boolean isInputProperty(PropertyDescriptor descriptor) {
152
150
return true ;
153
151
}
154
152
155
- /**
156
- * Extension of {@link org.springframework.aop.TargetClassAware} to be able to ignore the getter on JSON rendering.
157
- *
158
- * @author Oliver Gierke
159
- */
160
- public static interface TargetClassAware extends org .springframework .aop .TargetClassAware {
161
-
162
- @ JsonIgnore
163
- Class <?> getTargetClass ();
164
- }
165
-
166
153
/**
167
154
* Custom {@link MethodInterceptor} to expose the proxy target class even if we set
168
155
* {@link ProxyFactory#setOpaque(boolean)} to true to prevent properties on {@link Advised} to be rendered.
169
156
*
170
157
* @author Oliver Gierke
171
158
*/
172
- private static class TargetClassAwareMethodInterceptor implements MethodInterceptor {
159
+ private static class TargetAwareMethodInterceptor implements MethodInterceptor {
173
160
174
161
private static final Method GET_TARGET_CLASS_METHOD ;
175
- private final Class <?> targetClass ;
162
+ private static final Method GET_TARGET_METHOD ;
163
+
164
+ private final Class <?> targetType ;
176
165
177
166
static {
178
167
try {
179
- GET_TARGET_CLASS_METHOD = TargetClassAware .class .getMethod ("getTargetClass" );
168
+ GET_TARGET_CLASS_METHOD = TargetAware .class .getMethod ("getTargetClass" );
169
+ GET_TARGET_METHOD = TargetAware .class .getMethod ("getTarget" );
180
170
} catch (NoSuchMethodException e ) {
181
171
throw new IllegalStateException (e );
182
172
}
183
173
}
184
174
185
175
/**
186
- * Creates a new {@link TargetClassAwareMethodInterceptor } with the given target class.
176
+ * Creates a new {@link TargetAwareMethodInterceptor } with the given target class.
187
177
*
188
- * @param targetClass must not be {@literal null}.
178
+ * @param targetType must not be {@literal null}.
189
179
*/
190
- public TargetClassAwareMethodInterceptor (Class <?> targetClass ) {
180
+ public TargetAwareMethodInterceptor (Class <?> targetType ) {
191
181
192
- Assert .notNull (targetClass , "Target class must not be null!" );
193
- this .targetClass = targetClass ;
182
+ Assert .notNull (targetType , "Target type must not be null!" );
183
+ this .targetType = targetType ;
194
184
}
195
185
196
186
/*
@@ -201,7 +191,9 @@ public TargetClassAwareMethodInterceptor(Class<?> targetClass) {
201
191
public Object invoke (MethodInvocation invocation ) throws Throwable {
202
192
203
193
if (invocation .getMethod ().equals (GET_TARGET_CLASS_METHOD )) {
204
- return targetClass ;
194
+ return targetType ;
195
+ } else if (invocation .getMethod ().equals (GET_TARGET_METHOD )) {
196
+ return invocation .getThis ();
205
197
}
206
198
207
199
return invocation .proceed ();
0 commit comments