org.jboss.aop.reflection
Class ReflectionAspect

java.lang.Object
  extended by org.jboss.aop.reflection.ReflectionAspect

public class ReflectionAspect
extends Object

Author:
Kabir Khan

Aspect to intercept calls to the reflection API. Calls to Class.newInstance(), Field.setXXX(), Field.getXXX(), Constructor.newInstance() don't get intercepted even though the underlying constructor/field might have an advice associated with it. This class adds advices to help intercept these calls and attach to the original chains on the caller or target object. Method.invoke() works for chains on the target object "out of the box", but bypasses caller chains.



Also, Class.getMethods(), Class.getDeclaredMethods(), Class.getInterfaces(), and Class.getDeclaredFields() return extra methods/interfaces/fields added by the AOP framework. The aspects contained here cleans this information.


Constructor Summary
ReflectionAspect()
           
 
Method Summary
protected  Object interceptConstructor(Invocation invocation, Constructor<?> constructor, Object[] args)
          Overridable advice for a Constructor.newInstance() or Class.newInstance() call.
 Object interceptFieldGet(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Field.getXXX().
 Object interceptFieldGet(MethodCalledByMethodInvocation invocation)
          Advice for calls to Field.getXXX().
protected  Object interceptFieldRead(Invocation invocation, Field field, Object instance)
          Overridable advice for a Field.setXXX() call.
 Object interceptFieldSet(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Field.setXXX().
 Object interceptFieldSet(MethodCalledByMethodInvocation invocation)
          Advice for calls to Field.setXXX().
protected  Object interceptFieldWrite(Invocation invocation, Field field, Object instance, Object arg)
          Overridable advice for a Field.setXXX() call.
 Object interceptGetClasses(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getClasses().
 Object interceptGetClasses(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getClasses().
 Object interceptGetConstructors(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getConstructors().
 Object interceptGetConstructors(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getConstructors().
 Object interceptGetDeclaredClasses(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getDeclaredClasses().
 Object interceptGetDeclaredClasses(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getDeclaredClasses().
 Object interceptGetDeclaredConstructor(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getDeclaredConstructor().
 Object interceptGetDeclaredConstructor(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getDeclaredConstructor().
 Object interceptGetDeclaredConstructors(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getDeclaredConstructors().
 Object interceptGetDeclaredConstructors(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getDeclaredConstructors().
 Object interceptGetDeclaredField(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getDeclaredField().
 Object interceptGetDeclaredField(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getDeclaredField().
 Object interceptGetDeclaredFields(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getDeclaredFields().
 Object interceptGetDeclaredFields(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getDeclaredFields().
 Object interceptGetDeclaredMethod(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getDeclaredMethod().
 Object interceptGetDeclaredMethod(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getDeclaredMethod().
 Object interceptGetDeclaredMethods(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getDeclaredMethods().
 Object interceptGetDeclaredMethods(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getDeclaredMethods().
 Object interceptGetFields(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getFields().
 Object interceptGetFields(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getFields().
 Object interceptGetInterfaces(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getInterfaces().
 Object interceptGetInterfaces(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getInterfaces().
 Object interceptGetMethod(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getMethod().
 Object interceptGetMethod(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getMethod().
 Object interceptGetMethods(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.getDeclaredMethod().
 Object interceptGetMethods(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.getMethods().
protected  Object interceptMethod(Invocation invocation, Method method, Object instance, Object[] args)
          Overridable advice for a Method.invoke() call.
 Object interceptMethodInvoke(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Method.invoke().
 Object interceptMethodInvoke(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.newInstance() and Constructor.newInstance().
 Object interceptNewInstance(MethodCalledByConstructorInvocation invocation)
          Advice for calls to Class.newInstance() and Constructor.newInstance().
 Object interceptNewInstance(MethodCalledByMethodInvocation invocation)
          Advice for calls to Class.newInstance() and Constructor.newInstance().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionAspect

public ReflectionAspect()
Method Detail

interceptNewInstance

public Object interceptNewInstance(MethodCalledByConstructorInvocation invocation)
                            throws Throwable
Advice for calls to Class.newInstance() and Constructor.newInstance(). Intended use is for caller pointcuts. If you wish to handle the intercepted calls, override interceptConstructor.

Parameters:
invocation -
Returns:
result of invocation
Throws:
Throwable
See Also:
interceptConstructor(Invocation, Constructor, Object[])

interceptNewInstance

public Object interceptNewInstance(MethodCalledByMethodInvocation invocation)
                            throws Throwable
Advice for calls to Class.newInstance() and Constructor.newInstance(). Intended use is for caller pointcuts. If you wish to handle the intercepted calls, override interceptConstructor.

Parameters:
invocation -
Returns:
result of invocation
Throws:
Throwable
See Also:
interceptConstructor(Invocation, Constructor, Object[])

interceptMethodInvoke

public Object interceptMethodInvoke(MethodCalledByConstructorInvocation invocation)
                             throws Throwable
Advice for calls to Method.invoke(). Intended use is for caller pointcuts. If you wish to handle the intercepted calls, override interceptMethod.

Parameters:
invocation -
Returns:
result of invocation
Throws:
Throwable
See Also:
ReflectionAspect#interceptMethod(Invocation, Object, Method, Object[])

interceptMethodInvoke

public Object interceptMethodInvoke(MethodCalledByMethodInvocation invocation)
                             throws Throwable
Advice for calls to Class.newInstance() and Constructor.newInstance(). Intended use is for caller pointcuts. If you wish to handle the intercepted calls, override interceptMethod.

Parameters:
invocation -
Returns:
result of invocation
Throws:
Throwable
See Also:
ReflectionAspect#interceptMethod(Invocation, Object, Method, Object[])

interceptFieldSet

public Object interceptFieldSet(MethodCalledByConstructorInvocation invocation)
                         throws Throwable
Advice for calls to Field.setXXX(). Intended use is for caller pointcuts. If you wish to handle the intercepted calls, override interceptFieldWrite.

Parameters:
invocation -
Returns:
result of invocation
Throws:
Throwable
See Also:
interceptFieldWrite(Invocation, Field, Object, Object)

interceptFieldSet

public Object interceptFieldSet(MethodCalledByMethodInvocation invocation)
                         throws Throwable
Advice for calls to Field.setXXX(). Intended use is for caller pointcuts. If you wish to handle the intercepted calls, override interceptFieldWrite.

Parameters:
invocation -
Returns:
result of invocation
Throws:
Throwable
See Also:
interceptFieldWrite(Invocation, Field, Object, Object)

interceptFieldGet

public Object interceptFieldGet(MethodCalledByConstructorInvocation invocation)
                         throws Throwable
Advice for calls to Field.getXXX(). Intended use is for caller pointcuts. If you wish to handle the intercepted calls, override interceptFieldRead.

Parameters:
invocation -
Returns:
The value of the field (or whatever you choose)
Throws:
Throwable
See Also:
interceptFieldRead(Invocation, Field, Object)

interceptFieldGet

public Object interceptFieldGet(MethodCalledByMethodInvocation invocation)
                         throws Throwable
Advice for calls to Field.getXXX(). Intended use is for caller pointcuts. If you wish to handle the intercepted calls, override interceptFieldRead.

Parameters:
invocation -
Returns:
The value of the field (or whatever you choose)
Throws:
Throwable
See Also:
interceptFieldRead(Invocation, Field, Object)

interceptGetDeclaredMethods

public final Object interceptGetDeclaredMethods(MethodCalledByConstructorInvocation invocation)
                                         throws Throwable
Advice for calls to Class.getDeclaredMethods(). Cleans methods that get added to the class by the AOP framework (Methods introduced by introductions/mixin classes will still be returned). Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Method[] containing the declared methods of the class
Throws:
Throwable
See Also:
Class.getDeclaredMethods()()

interceptGetDeclaredMethods

public final Object interceptGetDeclaredMethods(MethodCalledByMethodInvocation invocation)
                                         throws Throwable
Advice for calls to Class.getDeclaredMethods(). Cleans methods that get added to the class by the AOP framework (Methods introduced by introductions/mixin classes will still be returned). Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Method[] containing the declared methods of the class
Throws:
Throwable
See Also:
Class.getDeclaredMethods()()

interceptGetDeclaredMethod

public final Object interceptGetDeclaredMethod(MethodCalledByConstructorInvocation invocation)
                                        throws Throwable
Advice for calls to Class.getDeclaredMethod(). Cleans methods that get added to the class by the AOP framework (Methods introduced by introductions/mixin classes will still be returned). Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Method the declared method
Throws:
Throwable
See Also:
Class#getDeclaredMethod()()

interceptGetDeclaredMethod

public final Object interceptGetDeclaredMethod(MethodCalledByMethodInvocation invocation)
                                        throws Throwable
Advice for calls to Class.getDeclaredMethod(). Cleans methods that get added to the class by the AOP framework (Methods introduced by introductions/mixin classes will still be returned). Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Method the declared method
Throws:
Throwable
See Also:
Class#getDeclaredMethod()()

interceptGetMethods

public final Object interceptGetMethods(MethodCalledByConstructorInvocation invocation)
                                 throws Throwable
Advice for calls to Class.getDeclaredMethod(). Cleans methods that get added to the class by the AOP framework (Methods introduced by introductions/mixin classes will still be returned). Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Method[] The methods of the class
Throws:
Throwable
See Also:
Class#getDeclaredMethod()()

interceptGetMethods

public final Object interceptGetMethods(MethodCalledByMethodInvocation invocation)
                                 throws Throwable
Advice for calls to Class.getMethods(). Cleans methods that get added to the class by the AOP framework (Methods introduced by introductions/mixin classes will still be returned). Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Method[] The methods of the class
Throws:
Throwable
See Also:
Class.getMethods()()

interceptGetMethod

public final Object interceptGetMethod(MethodCalledByConstructorInvocation invocation)
                                throws Throwable
Advice for calls to Class.getMethod(). Cleans methods that get added to the class by the AOP framework (Methods introduced by introductions/mixin classes will still be returned). Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Method The method
Throws:
Throwable
See Also:
Class#getDeclaredMethod()()

interceptGetMethod

public final Object interceptGetMethod(MethodCalledByMethodInvocation invocation)
                                throws Throwable
Advice for calls to Class.getMethod(). Cleans methods that get added to the class by the AOP framework (Methods introduced by introductions/mixin classes will still be returned). Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Method The method
Throws:
Throwable
See Also:
Class#getDeclaredMethod()()

interceptGetInterfaces

public final Object interceptGetInterfaces(MethodCalledByConstructorInvocation invocation)
                                    throws Throwable
Advice for calls to Class.getInterfaces(). Cleans interfaces that get added to the class by the AOP framework. (Interfaces introduced by introductions will still be returned). Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.Class[] containing the interfaces of the class
Throws:
Throwable
See Also:
Class.getInterfaces()

interceptGetInterfaces

public final Object interceptGetInterfaces(MethodCalledByMethodInvocation invocation)
                                    throws Throwable
Advice for calls to Class.getInterfaces(). Cleans interfaces that get added to the class by the AOP framework. (Interfaces introduced by introductions will still be returned). Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.Class[] containing the interfaces of the class
Throws:
Throwable
See Also:
Class.getInterfaces()

interceptGetDeclaredClasses

public final Object interceptGetDeclaredClasses(MethodCalledByConstructorInvocation invocation)
                                         throws Throwable
Advice for calls to Class.getDeclaredClasses(). Cleans inner classes that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.Class[] containing the Class objects representing inner classes of the class
Throws:
Throwable
See Also:
Class.getDeclaredClasses()

interceptGetDeclaredClasses

public final Object interceptGetDeclaredClasses(MethodCalledByMethodInvocation invocation)
                                         throws Throwable
Advice for calls to Class.getDeclaredClasses(). Cleans inner classes that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.Class[] containing the Class objects representing inner classes of the class
Throws:
Throwable
See Also:
Class.getDeclaredClasses()

interceptGetClasses

public final Object interceptGetClasses(MethodCalledByConstructorInvocation invocation)
                                 throws Throwable
Advice for calls to Class.getClasses(). Cleans inner classes that get added to the class by the AOP framework. Intended use is for caller pointcuts. Extra stuff only seems to get returned when using JRockit

Parameters:
invocation - The invocation
Returns:
java.lang.Class[] containing the Class objects representing inner classes of the class
Throws:
Throwable
See Also:
Class.getClasses()

interceptGetClasses

public final Object interceptGetClasses(MethodCalledByMethodInvocation invocation)
                                 throws Throwable
Advice for calls to Class.getClasses(). Cleans inner classes that get added to the class by the AOP framework. Intended use is for caller pointcuts. Extra stuff only seems to get returned when using JRockit

Parameters:
invocation - The invocation
Returns:
java.lang.Class[] containing the Class objects representing inner classes of the class
Throws:
Throwable
See Also:
Class.getDeclaredClasses()

interceptGetDeclaredFields

public final Object interceptGetDeclaredFields(MethodCalledByConstructorInvocation invocation)
                                        throws Throwable
Advice for calls to Class.getDeclaredFields(). Cleans fields that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Field[] containing the Fields of the class
Throws:
Throwable
See Also:
Class.getDeclaredClasses()

interceptGetDeclaredFields

public final Object interceptGetDeclaredFields(MethodCalledByMethodInvocation invocation)
                                        throws Throwable
Advice for calls to Class.getDeclaredFields(). Cleans fields that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Field[] containing the Fields of the class
Throws:
Throwable
See Also:
Class.getDeclaredFields()

interceptGetDeclaredField

public final Object interceptGetDeclaredField(MethodCalledByConstructorInvocation invocation)
                                       throws Throwable
Advice for calls to Class.getDeclaredField(). Cleans fields that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Field The Field
Throws:
Throwable
See Also:
Class#getDeclaredField()

interceptGetDeclaredField

public final Object interceptGetDeclaredField(MethodCalledByMethodInvocation invocation)
                                       throws Throwable
Advice for calls to Class.getDeclaredField(). Cleans fields that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Field[] The Field
Throws:
Throwable
See Also:
Class#getDeclaredField()

interceptGetFields

public final Object interceptGetFields(MethodCalledByConstructorInvocation invocation)
                                throws Throwable
Advice for calls to Class.getFields(). Cleans fields that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Field[] containing the Fields of the class
Throws:
Throwable
See Also:
Class.getFields()

interceptGetFields

public final Object interceptGetFields(MethodCalledByMethodInvocation invocation)
                                throws Throwable
Advice for calls to Class.getFields(). Cleans fields that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Field[] containing the Fields of the class
Throws:
Throwable
See Also:
Class.getFields()

interceptGetDeclaredConstructors

public final Object interceptGetDeclaredConstructors(MethodCalledByConstructorInvocation invocation)
                                              throws Throwable
Advice for calls to Class.getDeclaredConstructors(). Cleans constructors that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Constructor[] containing the Constructors of the class
Throws:
Throwable
See Also:
Class.getDeclaredConstructors()

interceptGetDeclaredConstructors

public final Object interceptGetDeclaredConstructors(MethodCalledByMethodInvocation invocation)
                                              throws Throwable
Advice for calls to Class.getDeclaredConstructors(). Cleans constructors that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Constructor[] containing the Constructors of the class
Throws:
Throwable
See Also:
Class.getDeclaredConstructors()

interceptGetDeclaredConstructor

public final Object interceptGetDeclaredConstructor(MethodCalledByConstructorInvocation invocation)
                                             throws Throwable
Advice for calls to Class.getDeclaredConstructor(). Cleans constructors that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Constructor[] The constructor
Throws:
Throwable
See Also:
Class.getFields()

interceptGetDeclaredConstructor

public final Object interceptGetDeclaredConstructor(MethodCalledByMethodInvocation invocation)
                                             throws Throwable
Advice for calls to Class.getDeclaredConstructor(). Cleans constructors that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.reflect.Constructor[] The constructor
Throws:
Throwable
See Also:
Class.getFields()

interceptGetConstructors

public final Object interceptGetConstructors(MethodCalledByConstructorInvocation invocation)
                                      throws Throwable
Advice for calls to Class.getConstructors(). Cleans fields that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.Constructor[] containing the Constructors of the class
Throws:
Throwable
See Also:
Class.getFields()

interceptGetConstructors

public final Object interceptGetConstructors(MethodCalledByMethodInvocation invocation)
                                      throws Throwable
Advice for calls to Class.getConstructors(). Cleans constructors that get added to the class by the AOP framework. Intended use is for caller pointcuts.

Parameters:
invocation - The invocation
Returns:
java.lang.Constructor[] containing the Constructors of the class
Throws:
Throwable
See Also:
Class.getFields()

interceptConstructor

protected Object interceptConstructor(Invocation invocation,
                                      Constructor<?> constructor,
                                      Object[] args)
                               throws Throwable
Overridable advice for a Constructor.newInstance() or Class.newInstance() call. Default behaviour is to first try to attach to any caller advice chain. If that does not exist, try to attach to the advice chains on the target object. And if that does not exist to invoke the reflected constructor.

Parameters:
invocation - The invocation
constructor - The field on which we are calling set
args - The arguments for the constructor
Returns:
The new instance
Throws:
Throwable

interceptFieldRead

protected Object interceptFieldRead(Invocation invocation,
                                    Field field,
                                    Object instance)
                             throws Throwable
Overridable advice for a Field.setXXX() call. Default behaviour is to first try to attach to the advice chains on the target object. And if that does not exist to invoke the reflected field read.

Parameters:
invocation - The invocation
field - The field on which we are calling set
instance - The instance on which we want to write a field
Returns:
The value of the field (or whatever you choose)
Throws:
Throwable

interceptFieldWrite

protected Object interceptFieldWrite(Invocation invocation,
                                     Field field,
                                     Object instance,
                                     Object arg)
                              throws Throwable
Overridable advice for a Field.setXXX() call. Default behaviour is to first try attach to the advice chains on the target object. And if that does not exist to invoke the reflected field write.

Parameters:
invocation - The invocation
field - The field on which we are calling set
instance - The instance on which we want to read a field
arg - The value we want to set the field to
Returns:
result of invocation
Throws:
Throwable

interceptMethod

protected Object interceptMethod(Invocation invocation,
                                 Method method,
                                 Object instance,
                                 Object[] args)
                          throws Throwable
Overridable advice for a Method.invoke() call. Default behaviour is to first try to attach to any caller advice chain. If that does not exist, try to attach to the advice chains on the target object. And if that does not exist to invoke the reflected method.

Parameters:
invocation - The invocation
method - The method on which we are calling set
instance - The instance on which we want to read a field
arg - The value we want to set the field to
Returns:
result of invocation
Throws:
Throwable


Copyright © 2008 JBoss, a division of Red Hat, Inc.. All Rights Reserved.