ch.elca.el4j.util.codingsupport
Class AopHelper

java.lang.Object
  extended by ch.elca.el4j.util.codingsupport.AopHelper

public class AopHelper
extends Object

Similar to Spring's AOPUtil class, with some more features:

Code sample:
 
   DefaultPerson p = new DefaultPerson();
   p = addAdvice(p, 0, new AMixin(),
                       mySharedCInterceptor,
                       new BMixin());

         // the next call now uses the 3 advices set up
   p.setAge(11);
   
Remark: We delegate sometimes to the AOPUtil of Spring (we duplicated the methods here for simplicity).

Author:
Philipp Oser (POS)
File-location:
AopHelper
Last check-in date:
2010-01-05 09:38:21 +0100 (Di, 05. Jan 2010) by jonasha for revision 4068

Constructor Summary
AopHelper()
           
 
Method Summary
static
<T> T
addAdvice(T object, org.aopalliance.aop.Advice... advices)
          Convenience method.
static
<T> T
addAdvice(T object, int position, org.aopalliance.aop.Advice... advices)
          Add the advice(s) to the object.
static String getClassNameOfCglibProxyClass(Class clazz)
          Return the class name (String) of a CGLIB-generated class.
static Class getClassOfCglibProxyClass(Class clazz)
          Return the class (Class) of a CGLIB-generated class.
static Class<?> getTargetClass(Object candidate)
          Determine the target class of the given bean instance, which might be an AOP proxy.
static boolean isAopProxy(Object object)
          Check whether the given object is a JDK dynamic proxy or a CGLIB proxy.
static boolean isCglibProxy(Object object)
          Check whether the given object is a CGLIB proxy.
static boolean isCglibProxyClass(Class<?> clazz)
          Check whether the specified class is a CGLIB-generated class.
static boolean isJdkDynamicProxy(Object object)
          Check whether the given object is a JDK dynamic proxy.
static boolean isProxied(Object object)
          Does the given object have Spring AOP support (i.e.
static boolean isProxiedBy(Object object, Class<? extends org.aopalliance.aop.Advice> cls)
          Is the given object advised by an instance of the given advice class?
static
<T> T
removeAllAdvice(T object)
          Remove all advice (and the proxy) that were added via Spring AOP.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AopHelper

public AopHelper()
Method Detail

isAopProxy

public static boolean isAopProxy(Object object)
Check whether the given object is a JDK dynamic proxy or a CGLIB proxy.

Parameters:
object - the object to check
See Also:
isJdkDynamicProxy(java.lang.Object),
delegates to the same method of Spring's AopUtil

isJdkDynamicProxy

public static boolean isJdkDynamicProxy(Object object)
Check whether the given object is a JDK dynamic proxy.

Parameters:
object - the object to check
See Also:

delegates to the same method of Spring's AopUtil

isCglibProxy

public static boolean isCglibProxy(Object object)
Check whether the given object is a CGLIB proxy.

Parameters:
object - the object to check
delegates to the same method of Spring's AopUtil

isCglibProxyClass

public static boolean isCglibProxyClass(Class<?> clazz)
Check whether the specified class is a CGLIB-generated class.

Parameters:
clazz - the class to check
delegates to the same method of Spring's AopUtil

getClassNameOfCglibProxyClass

public static String getClassNameOfCglibProxyClass(Class clazz)
Return the class name (String) of a CGLIB-generated class.

Parameters:
clazz - the class to work on
Returns:
null if it is not a proxy class

getClassOfCglibProxyClass

public static Class getClassOfCglibProxyClass(Class clazz)
Return the class (Class) of a CGLIB-generated class.

Parameters:
clazz -
Returns:
null if no class was found or if it's not a proxy class

getTargetClass

public static Class<?> getTargetClass(Object candidate)
Determine the target class of the given bean instance, which might be an AOP proxy.

Returns the target class for an AOP proxy and the plain class else.

Parameters:
candidate - the instance to check (might be an AOP proxy)
Returns:
the target class (or the plain class of the given object as fallback)
See Also:

delegates to the same method of Spring's AopUtil

addAdvice

public static <T> T addAdvice(T object,
                              int position,
                              org.aopalliance.aop.Advice... advices)
Add the advice(s) to the object. The method adds a Spring proxy if needed. If there is already a Spring proxy, it repackages the object in a new Spring proxy. The new advise is added in position 0 of the advice chain.

CAVEAT: In case "object" is already wrapped (=proxied), the type of the target source must be assignable to T.
Take care that Mixins should typically be instanciated as Spring prototypes (i.e. 1 mixin instance per underlying object), Interceptors can typically be shared among all the objects they apply to. When you add an advice that implement IntroductionInterceptor (i.e. are Mixins) this class automatically wraps them in a DefaultIntroductionAdvisor.

For sample code please refer to the javadoc of AopHelper.

The method allows to add n advice at once (for performance: it does not require n times a re-proxying (for mixins)).

Parameters:
object - the object to wrap
position - where in the advice chain this new advice should be placed. 0 means at the beginning (=called first).
advices - the advise(s) (=the interceptors and mixins) to add
Returns:
the same object wrapped with a spring proxy (if needed) that has in addition the given advice(s)


addAdvice

public static <T> T addAdvice(T object,
                              org.aopalliance.aop.Advice... advices)
Convenience method. See #addAdvice(Object, int, Advice) The position is always set to 0 (=first position)


removeAllAdvice

public static <T> T removeAllAdvice(T object)
Remove all advice (and the proxy) that were added via Spring AOP.

CAVEAT: this method does NOT remove hibernate proxies!

Parameters:
object -
Returns:
the object with all advice removed.

isProxied

public static boolean isProxied(Object object)
Does the given object have Spring AOP support (i.e. a proxy with AOP methods on them)?

Parameters:
object -
Returns:
whether the object is proxied.

isProxiedBy

public static boolean isProxiedBy(Object object,
                                  Class<? extends org.aopalliance.aop.Advice> cls)
Is the given object advised by an instance of the given advice class?

Parameters:
object - the adviced object
cls - the advice class
Returns:
true if object is adviced by given advice class


Copyright © 2005-2011 ELCA. All Rights Reserved.