Wrapper class definition.

Power Java
Dynamic Proxy Classes:
Toward Metalevel Programming in Java

Mathias Richter and Takashi Suezawa
Listing 6. Wrapper class definition.


public class Wrapper
    implements InvocationHandler
{
		private ArrayList fAspects;
		private Object fPrimary;

		protected Wrapper (IAspect[] aspects, Object primary )
		{
		   fAspects = new ArrayList();
		   fPrimary = primary;
		   for ( int i = 0; i < aspects.length; i++)
		     fAspects.add( aspects[i] );
		}
        ...// methods for adding/removing
        ...// aspects and primary left out
		protected void invokeAspects( int phase,
		     Method method, Object[] arguments )
		{
		     for ( Iterator i = fAspects.iterator(); i.hasNext(); )
		     {
		     IAspect aspect = (IAspect)i.next();
		     aspect.setPrimary( fPrimary );
		     aspect.setPhase( phase );
		     method.invoke( aspect, arguments );
		     aspect.setPhase( IAspect.NONE );
		     }
		}
		public Object invoke( Object target, Method method, Object[] arguments )
                              throws IllegalAccessException, Throwable
		{
		 try
		 {
		     synchronized ( method )
			{
			  invokeAspects( IAspect.BEFORE, method, args );
			  Object result = method.invoke( fPrimary, arguments );
			  invokeAspects( IAspect.AFTER, method, arguments );
			  return result;
			}
		} catch ( InvocationTargetException x )
		{
			 throw x.getTargetException();
		    }
		 }
}