@@ -326,12 +326,8 @@ private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
326
326
// TODO: small memory optimization here (can skip creation for methods with no advice)
327
327
for (int x = 0 ; x < methods .length ; x ++) {
328
328
List <Object > chain = this .advised .getInterceptorsAndDynamicInterceptionAdvice (methods [x ], rootClass );
329
- Object target = this .advised .getTargetSource ().getTarget ();
330
- Class <?> targetClass = this .advised .getTargetClass ();
331
- if (targetClass == null ) {
332
- targetClass = target .getClass ();
333
- }
334
- fixedCallbacks [x ] = new FixedChainStaticTargetInterceptor (chain , target , targetClass );
329
+ fixedCallbacks [x ] = new FixedChainStaticTargetInterceptor (
330
+ chain , this .advised .getTargetSource ().getTarget (), this .advised .getTargetClass ());
335
331
this .fixedInterceptorMap .put (methods [x ].toString (), x );
336
332
}
337
333
@@ -378,20 +374,22 @@ private static boolean implementsInterface(Method method, Set<Class<?>> ifcs) {
378
374
* {@code proxy} and also verifies that {@code null} is not returned as a primitive.
379
375
*/
380
376
@ Nullable
381
- private static Object processReturnType (Object proxy , Object target , Method method , @ Nullable Object retVal ) {
377
+ private static Object processReturnType (
378
+ Object proxy , @ Nullable Object target , Method method , @ Nullable Object returnValue ) {
379
+
382
380
// Massage return value if necessary
383
- if (retVal != null && retVal == target &&
381
+ if (returnValue != null && returnValue == target &&
384
382
!RawTargetAccess .class .isAssignableFrom (method .getDeclaringClass ())) {
385
383
// Special case: it returned "this". Note that we can't help
386
384
// if the target sets a reference to itself in another returned object.
387
- retVal = proxy ;
385
+ returnValue = proxy ;
388
386
}
389
387
Class <?> returnType = method .getReturnType ();
390
- if (retVal == null && returnType != Void .TYPE && returnType .isPrimitive ()) {
388
+ if (returnValue == null && returnType != Void .TYPE && returnType .isPrimitive ()) {
391
389
throw new AopInvocationException (
392
390
"Null return value from advice does not match primitive return type for: " + method );
393
391
}
394
- return retVal ;
392
+ return returnValue ;
395
393
}
396
394
397
395
@@ -413,7 +411,7 @@ private static class StaticUnadvisedInterceptor implements MethodInterceptor, Se
413
411
414
412
private final Object target ;
415
413
416
- public StaticUnadvisedInterceptor (Object target ) {
414
+ public StaticUnadvisedInterceptor (@ Nullable Object target ) {
417
415
this .target = target ;
418
416
}
419
417
@@ -434,7 +432,7 @@ private static class StaticUnadvisedExposedInterceptor implements MethodIntercep
434
432
435
433
private final Object target ;
436
434
437
- public StaticUnadvisedExposedInterceptor (Object target ) {
435
+ public StaticUnadvisedExposedInterceptor (@ Nullable Object target ) {
438
436
this .target = target ;
439
437
}
440
438
@@ -476,7 +474,9 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
476
474
return processReturnType (proxy , target , method , retVal );
477
475
}
478
476
finally {
479
- this .targetSource .releaseTarget (target );
477
+ if (target != null ) {
478
+ this .targetSource .releaseTarget (target );
479
+ }
480
480
}
481
481
}
482
482
}
@@ -505,7 +505,9 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
505
505
}
506
506
finally {
507
507
AopContext .setCurrentProxy (oldProxy );
508
- this .targetSource .releaseTarget (target );
508
+ if (target != null ) {
509
+ this .targetSource .releaseTarget (target );
510
+ }
509
511
}
510
512
}
511
513
}
@@ -612,7 +614,9 @@ private static class FixedChainStaticTargetInterceptor implements MethodIntercep
612
614
613
615
private final Class <?> targetClass ;
614
616
615
- public FixedChainStaticTargetInterceptor (List <Object > adviceChain , Object target , Class <?> targetClass ) {
617
+ public FixedChainStaticTargetInterceptor (
618
+ List <Object > adviceChain , @ Nullable Object target , @ Nullable Class <?> targetClass ) {
619
+
616
620
this .adviceChain = adviceChain ;
617
621
this .target = target ;
618
622
this .targetClass = targetClass ;
@@ -649,15 +653,16 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
649
653
Object oldProxy = null ;
650
654
boolean setProxyContext = false ;
651
655
Object target = null ;
656
+ TargetSource targetSource = this .advised .getTargetSource ();
652
657
try {
653
658
if (this .advised .exposeProxy ) {
654
659
// Make invocation available if necessary.
655
660
oldProxy = AopContext .setCurrentProxy (proxy );
656
661
setProxyContext = true ;
657
662
}
658
663
// Get as late as possible to minimize the time we "own" the target, in case it comes from a pool...
659
- target = getTarget ();
660
- Class <?> targetClass = target .getClass ();
664
+ target = targetSource . getTarget ();
665
+ Class <?> targetClass = ( target != null ? target .getClass () : null );
661
666
List <Object > chain = this .advised .getInterceptorsAndDynamicInterceptionAdvice (method , targetClass );
662
667
Object retVal ;
663
668
// Check whether we only have one InvokerInterceptor: that is,
@@ -678,8 +683,8 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
678
683
return retVal ;
679
684
}
680
685
finally {
681
- if (target != null ) {
682
- releaseTarget (target );
686
+ if (target != null && ! targetSource . isStatic () ) {
687
+ targetSource . releaseTarget (target );
683
688
}
684
689
if (setProxyContext ) {
685
690
// Restore old proxy.
@@ -702,14 +707,6 @@ public boolean equals(Object other) {
702
707
public int hashCode () {
703
708
return this .advised .hashCode ();
704
709
}
705
-
706
- protected Object getTarget () throws Exception {
707
- return this .advised .getTargetSource ().getTarget ();
708
- }
709
-
710
- protected void releaseTarget (Object target ) throws Exception {
711
- this .advised .getTargetSource ().releaseTarget (target );
712
- }
713
710
}
714
711
715
712
@@ -722,8 +719,9 @@ private static class CglibMethodInvocation extends ReflectiveMethodInvocation {
722
719
723
720
private final boolean publicMethod ;
724
721
725
- public CglibMethodInvocation (Object proxy , Object target , Method method , Object [] arguments ,
726
- Class <?> targetClass , List <Object > interceptorsAndDynamicMethodMatchers , MethodProxy methodProxy ) {
722
+ public CglibMethodInvocation (Object proxy , @ Nullable Object target , Method method ,
723
+ Object [] arguments , @ Nullable Class <?> targetClass ,
724
+ List <Object > interceptorsAndDynamicMethodMatchers , MethodProxy methodProxy ) {
727
725
728
726
super (proxy , target , method , arguments , targetClass , interceptorsAndDynamicMethodMatchers );
729
727
this .methodProxy = methodProxy ;
0 commit comments