| Severity | Error |
| Reported by | Metalama.Framework |
| Message | The aspect '<AspectName>' makes an extension contribution, such as an interceptor registration, but it executes after a low-level aspect weaver, and contributions are processed only before the first low-level aspect weaver. Order the aspect before the aspects that require a low-level weaver. |
Cause
Aspects normally transform code with the high-level advising API. Some aspects need a low-level weaver, written with the Metalama SDK, and bind to it with [RequireAspectWeaver]. A low-level weaver runs after the high-level aspects that precede it and rewrites the compilation directly.
Some aspects also make extension contributions: they register data that a Metalama extension, such as Metalama.Extensions.Interceptors, processes once for the whole pipeline. For instance, an aspect registers interceptors with the InterceptMethods method. Extensions process only the contributions made before the first low-level aspect weaver, because after this weaver the compilation has been rewritten.
Metalama reports LAMA0091 when an aspect makes an extension contribution but is ordered to execute after an aspect that requires a low-level weaver. In the message, <AspectName> is the aspect that made the contribution. The error is reported on the target of that aspect.
In the following example, InterceptWrite registers an interceptor, but the aspect order makes it execute after Audit, which requires a low-level weaver. InterceptPrint, which executes before Audit, is processed normally.
1using Metalama.Extensions.Interceptors;
2using Metalama.Framework.Aspects;
3using Metalama.Framework.Code;
4using Metalama.Framework.Engine;
5using Metalama.Framework.Engine.AspectWeavers;
6using System;
7using System.Linq;
8using System.Threading.Tasks;
9
10// The [InterceptWrite] aspect executes after the weaver of the [Audit] aspect.
11[assembly: AspectOrder(
12 AspectOrderDirection.RunTime,
13 typeof(Doc.LAMA0091.Error.InterceptWriteAttribute),
14 typeof(Doc.LAMA0091.Error.AuditAttribute),
15 typeof(Doc.LAMA0091.Error.InterceptPrintAttribute) )]
16
17namespace Doc.LAMA0091.Error;
18
19// A low-level aspect, which requires a low-level aspect weaver.
20[RequireAspectWeaver( "Doc.LAMA0091.Error.AuditWeaver" )]
21internal class AuditAttribute : TypeAspect { }
22
23[MetalamaPlugIn]
24internal class AuditWeaver : IAspectWeaver
25{
26 public Task TransformAsync( AspectWeaverContext context ) => Task.CompletedTask;
27}
28
29// This aspect registers an interceptor for the Print method.
30internal class InterceptPrintAttribute : TypeAspect
31{
32 public override void BuildAspect( IAspectBuilder<INamedType> builder )
33 => builder.InterceptMethods(
34 b => b
35 .Type( typeof(Target) ).Methods( "Print" )
36 .RedirectToExistingMethod( typeof(Redirections).AsINamedType().Methods.OfName( "Print" ).Single() ) );
37}
38
39// This aspect registers an interceptor for the Write method.
40internal class InterceptWriteAttribute : TypeAspect
41{
42 public override void BuildAspect( IAspectBuilder<INamedType> builder )
43 => builder.InterceptMethods(
44 b => b
45 .Type( typeof(Target) ).Methods( "Write" )
46 .RedirectToExistingMethod( typeof(Redirections).AsINamedType().Methods.OfName( "Write" ).Single() ) );
47}
48
49internal static class Target
50{
51 public static void Print( string value ) => Console.WriteLine( "target: " + value );
52
53 public static void Write( string value ) => Console.WriteLine( "target: " + value );
54}
55
56internal static class Redirections
57{
58 public static void Print( string value ) => Console.WriteLine( "intercepted: " + value );
59
60 public static void Write( string value ) => Console.WriteLine( "intercepted: " + value );
61}
62
1namespace Doc.LAMA0091.Error;
2
3// Error: [InterceptWrite] executes after the weaver of [Audit], so its interceptor cannot be registered.
4[InterceptWrite]
5[Audit]
6[InterceptPrint]
Error LAMA0091: The aspect 'InterceptWrite' makes an extension contribution, such as an interceptor registration, but it executes after a low-level aspect weaver, and contributions are processed only before the first low-level aspect weaver. Order the aspect before the aspects that require a low-level weaver.
7internal static class Program
8{
9 public static void Run()
10 {
11 Target.Print( "a" );
12 Target.Write( "b" );
13 }
14}
15
Resolution
Order the aspect that makes the extension contribution before every aspect that requires a low-level weaver, with the [AspectOrder] attribute. In the following example, Audit executes after both interceptor aspects.
1using Metalama.Extensions.Interceptors;
2using Metalama.Framework.Aspects;
3using Metalama.Framework.Code;
4using Metalama.Framework.Engine;
5using Metalama.Framework.Engine.AspectWeavers;
6using System;
7using System.Linq;
8using System.Threading.Tasks;
9
10// Both interceptor aspects execute before the weaver of the [Audit] aspect.
11[assembly: AspectOrder(
12 AspectOrderDirection.RunTime,
13 typeof(Doc.LAMA0091.Fixed.AuditAttribute),
14 typeof(Doc.LAMA0091.Fixed.InterceptWriteAttribute),
15 typeof(Doc.LAMA0091.Fixed.InterceptPrintAttribute) )]
16
17namespace Doc.LAMA0091.Fixed;
18
19// A low-level aspect, which requires a low-level aspect weaver.
20[RequireAspectWeaver( "Doc.LAMA0091.Fixed.AuditWeaver" )]
21internal class AuditAttribute : TypeAspect { }
22
23[MetalamaPlugIn]
24internal class AuditWeaver : IAspectWeaver
25{
26 public Task TransformAsync( AspectWeaverContext context ) => Task.CompletedTask;
27}
28
29// This aspect registers an interceptor for the Print method.
30internal class InterceptPrintAttribute : TypeAspect
31{
32 public override void BuildAspect( IAspectBuilder<INamedType> builder )
33 => builder.InterceptMethods(
34 b => b
35 .Type( typeof(Target) ).Methods( "Print" )
36 .RedirectToExistingMethod( typeof(Redirections).AsINamedType().Methods.OfName( "Print" ).Single() ) );
37}
38
39// This aspect registers an interceptor for the Write method.
40internal class InterceptWriteAttribute : TypeAspect
41{
42 public override void BuildAspect( IAspectBuilder<INamedType> builder )
43 => builder.InterceptMethods(
44 b => b
45 .Type( typeof(Target) ).Methods( "Write" )
46 .RedirectToExistingMethod( typeof(Redirections).AsINamedType().Methods.OfName( "Write" ).Single() ) );
47}
48
49internal static class Target
50{
51 public static void Print( string value ) => Console.WriteLine( "target: " + value );
52
53 public static void Write( string value ) => Console.WriteLine( "target: " + value );
54}
55
56internal static class Redirections
57{
58 public static void Print( string value ) => Console.WriteLine( "intercepted: " + value );
59
60 public static void Write( string value ) => Console.WriteLine( "intercepted: " + value );
61}
62
1namespace Doc.LAMA0091.Fixed;
2
3// Fixed: both interceptor aspects are ordered before [Audit].
4[InterceptWrite]
5[Audit]
6[InterceptPrint]
7internal static class Program
8{
9 public static void Run()
10 {
11 Target.Print( "a" );
12 Target.Write( "b" );
13 }
14}
15
1namespace Doc.LAMA0091.Fixed;
2
3// Fixed: both interceptor aspects are ordered before [Audit].
4[InterceptWrite]
5[Audit]
6[InterceptPrint]
7internal static class Program
8{
9 public static void Run()
10 {
11 Redirections.Print("a");
12 Redirections.Write("b");
13 }
14}
15