LOOM Frequently Asked Questions

I get a System.MethodAccessException when I'm trying to interweave an aspect

This indicates that either you're aspect or your target class is not marked as public.

Why Advice.AfterThrowing eats my exceptions?

If you use the Advice.AfterThrowing join point attribute, then keep in mind that the interwoven code will eat the exception. To avoid this, re-throw the exception from your aspect code:

[Call(Advice.AfterThrowing)]
public void f([JPException] Exception e)

   /// ... your code
   throw e; // rethrow to keep the exception alive

3. I am confused about the call order when I interweave more then one aspect

Loom interweaves the aspects in the following order

  1. all aspects defined as assembly attribute
  2. all aspects defined as module attribute
  3. all aspects defined as class attributes
  4. all aspects given in the aspects parameter of Weaver.CreateInstance in reverse order

This has the following implications when you call a interwoven method:

  1. Advice.Before: LICF
  2. Advice.Around: LICF
  3. Invoke.AfterXXX: FICF

LICF = Last Interwoven Called First: the aspect which is the first in the aspects parameter will be called first. The first assembly attribute will be the last one.

FICF = First Interwoven Called First: the first aspect defined as assembly attribute will be called first and the first parameter of the aspects parameter will be called last.

If my aspect has more than one matching aspect method, in which order they will be interwoven?

If both methods belong to the same join point category, then it is the same order as they declared in the aspect class. Otherwise it is the following order:

  1. all Advice.Before methods, if there exist one
  2. the Advice.Around method, if there is one
  3. all Advice.AfterReturning and Advice.AfterThrowing methods, if there exist one
  4. all Advice.After methods, if there exist one

I've defined an aspect method but loom won't interweave this

Be sure that the target class method is either virtual or defined via an interface. If it is a virtual method it has to be at least protected. Your aspect method on the other hand should be declared as public. If you interweave interface methods, you have to cast your interwoven object to the appropriate interface before you call the interwoven method.

How many aspect instances are created, during my interweaving process?

To take control over the creation of aspect instances, you have to use the InstanceCreation attribute. The attribute can be used to annotate aspect classes only. There are three different creation types, depending on the CreationType you are using:

This UML model shows the classes of an InstanceCreation example.

UML model showing the classes of an InstanceCreation example

using InstanceCreation(PerInstance) (default):

UML model showing the classes of an InstanceCreation PerInstance example

using InstanceCreation(PerClass):

UML model showing the classes of an InstanceCreation PerClass example

using InstanceCreation(PerAttribute)

UML model showing the classes of an InstanceCreation PerClass example

How to share data between aspects and a target classes (Upcoming feature)?

To share data between aspects and their target classes or between aspects of different aspect classes you can use join-point variables. These join-point variables are additional join-point parameters. Use the [JPVariable()] attribute to mark method parameters to become a join-point variable.

You can use different scopes for these variables, to control the accessibility of the variable. Using a Scope.Private for example, the variable is accessible only inside the aspect class. Using a Scope.Protected or Scope.Public, it's possible to use the join-point variables even outside the aspect class, it's declared in.

This code shows, how to use a protected scope to share data between different aspects classes and their target class:

public class Aspect1 : AspectAttribute

    [Call(Advice.Before)]
    public void Test([JPVariable(Scope.Protected)] ref int data)
     ...

public class Aspect2 : AspectAttribute

    [Call(Advice.Before)]
    public void Test([JPVariable(Scope.Protected)] ref int data)
     ...

[Aspect1]
[Aspect2]
public class TargetClass

    protected int data;

    public virtual void Test()
     ...

In this example, both aspects and the target class have access to one shared protected variable named data.