My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads

Flex AOP library for use with Parsley

Flapper provides the ability to introduce Aspects in your code through simple MXML configuration.

Example

Here's an example of adding an aspect to time how long a method takes to complete. We'll use a custom metatag - [Timed] to indicate that we want a method timed.

Step 1:

Associate the Timed metatag with an Aspect in the Parsley config:

<fx:Declarations>
    <parsley:ContextBuilder>
        <!-- Add the rest of your standard Parsley config here -->

	<tag:AspectSupport>
		<tag:MetatagAspectDefinition metatag="Timed" aspect="{new TimerAspect()}" />
	</tag:AspectSupport>			
    </parsley:ContextBuilder>
</fx:Declarations>

Step 2:

Add the annotation to the method

[Timed]
public function add(a:int,b:int):int
{
return a + b;
}

That's it. Now, when the add method is called, it'll be timed to see how long it runs for, and the results get logged out.

Writing a custom Aspect

Writing an Aspect is pretty simple too.

Here's the TimerAspect used in the above example:

	public class TimerAspect implements Aspect
	{
		public function intercept(pointCut:ProceedingJoinPoint):void
		{
			var startTime:int = getTimer();
			pointCut.proceed();
			var endTime:int = getTimer();
			logMethodDuration(endTime - startTime,pointCut); 
		}

		private function logMethodDuration(duration:int, pointCut:ProceedingJoinPoint):void
		{
			var category:String = getQualifiedSuperclassName(pointCut.targetInstance);
			category = category.replace("::",".")
			Log.getLogger(category).info("{0} completed in {1}ms",pointCut.targetMember.toString(),duration.toString());
		}

The pointCut parameter gives us access to the details of the method that is about to be called. If / when we're ready for the method to be invoked, we simply call pointCut.proceed().

(Note, if you don't call this, then the original method that has been proxied will never be called. ... and that's perfectly legal)

Inside your Aspect you can perform any number of changes to alter the behaviour of what's about to happen.

For example, you could

  • Add or remove parameters from the arguments passed to the method
  • Prevent the actual method from getting invoked
  • Invoke the actual method multiple times
  • Invoke a completely different method entirely.

Powered by Google Project Hosting