Skip to content

Commit 756aab5

Browse files
committed
Emit warning messages for invalid usage of [Cache] attribute
1 parent d20b960 commit 756aab5

15 files changed

+164
-24
lines changed

src/SpatialFocus.MethodCache.Fody/CecilExtension.cs renamed to src/SpatialFocus.MethodCache.Fody/Extensions/CecilExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Copyright (c) Spatial Focus GmbH. All rights reserved.
33
// </copyright>
44

5-
namespace SpatialFocus.MethodCache.Fody
5+
namespace SpatialFocus.MethodCache.Fody.Extensions
66
{
77
using System;
88
using Mono.Cecil;

src/SpatialFocus.MethodCache.Fody/MethodDefinitionExtension.cs renamed to src/SpatialFocus.MethodCache.Fody/Extensions/MethodDefinitionExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Copyright (c) Spatial Focus GmbH. All rights reserved.
33
// </copyright>
44

5-
namespace SpatialFocus.MethodCache.Fody
5+
namespace SpatialFocus.MethodCache.Fody.Extensions
66
{
77
using System;
88
using System.Linq;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// <copyright file="ModuleDefinitionExtension.cs" company="Spatial Focus GmbH">
2+
// Copyright (c) Spatial Focus GmbH. All rights reserved.
3+
// </copyright>
4+
5+
namespace SpatialFocus.MethodCache.Fody.Extensions
6+
{
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using Mono.Cecil;
11+
12+
public static class ModuleDefinitionExtension
13+
{
14+
public static ICollection<WeavingCandidate> GetWeavingCandidates(this ModuleDefinition moduleDefinition, References references)
15+
{
16+
if (moduleDefinition == null)
17+
{
18+
throw new ArgumentNullException(nameof(moduleDefinition));
19+
}
20+
21+
if (references == null)
22+
{
23+
throw new ArgumentNullException(nameof(references));
24+
}
25+
26+
return moduleDefinition.Types.Select(type =>
27+
{
28+
if (type.HasCacheAttribute(references))
29+
{
30+
return new WeavingCandidate(type, type.Methods.ToList());
31+
}
32+
33+
WeavingCandidate weavingCandidate =
34+
new WeavingCandidate(type, type.Methods.Where(method => method.HasCacheAttribute(references)).ToList());
35+
36+
if (!weavingCandidate.MethodDefinitions.Any())
37+
{
38+
return null;
39+
}
40+
41+
return weavingCandidate;
42+
})
43+
.Where(candidate => candidate != null)
44+
.ToList();
45+
}
46+
}
47+
}

src/SpatialFocus.MethodCache.Fody/TypeDefinitionExtension.cs renamed to src/SpatialFocus.MethodCache.Fody/Extensions/TypeDefinitionExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Copyright (c) Spatial Focus GmbH. All rights reserved.
33
// </copyright>
44

5-
namespace SpatialFocus.MethodCache.Fody
5+
namespace SpatialFocus.MethodCache.Fody.Extensions
66
{
77
using System;
88
using System.Linq;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// <copyright file="WeavingCandidate.cs" company="Spatial Focus GmbH">
2+
// Copyright (c) Spatial Focus GmbH. All rights reserved.
3+
// </copyright>
4+
5+
namespace SpatialFocus.MethodCache.Fody.Extensions
6+
{
7+
using System.Collections.Generic;
8+
using Mono.Cecil;
9+
10+
public class WeavingCandidate
11+
{
12+
public WeavingCandidate(TypeDefinition classDefinition, ICollection<MethodDefinition> methodDefinitions)
13+
{
14+
ClassDefinition = classDefinition;
15+
MethodDefinitions = methodDefinitions;
16+
}
17+
18+
public TypeDefinition ClassDefinition { get; }
19+
20+
public ICollection<MethodDefinition> MethodDefinitions { get; }
21+
}
22+
}

src/SpatialFocus.MethodCache.Fody/MemoryCache.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace SpatialFocus.MethodCache.Fody
1111
using global::Fody;
1212
using Mono.Cecil;
1313
using Mono.Cecil.Cil;
14+
using SpatialFocus.MethodCache.Fody.Extensions;
1415

1516
public static class MemoryCache
1617
{

src/SpatialFocus.MethodCache.Fody/ModuleWeaver.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace SpatialFocus.MethodCache.Fody
88
using System.Linq;
99
using global::Fody;
1010
using Mono.Cecil;
11+
using SpatialFocus.MethodCache.Fody.Extensions;
1112

1213
public partial class ModuleWeaver : BaseModuleWeaver
1314
{
@@ -17,35 +18,34 @@ public override void Execute()
1718
{
1819
References references = Fody.References.Init(this);
1920

20-
// TODO: Extract to method
21-
var elementsToCache = ModuleDefinition.Types.Select(type =>
21+
foreach (WeavingCandidate weavingCandidate in ModuleDefinition.GetWeavingCandidates(references))
22+
{
23+
if (weavingCandidate.ClassDefinition.HasCacheAttribute(references) && !weavingCandidate.MethodDefinitions.Any(x => x.IsEligibleForWeaving(references)))
2224
{
23-
if (type.HasCacheAttribute(references))
24-
{
25-
return new { Type = type, Methods = type.Methods.ToList() };
26-
}
27-
28-
return new { Type = type, Methods = type.Methods.Where(method => method.HasCacheAttribute(references)).ToList(), };
29-
})
30-
.Where(x => x.Methods.Any())
31-
.ToList();
25+
WriteWarning($"Class {weavingCandidate.ClassDefinition.Resolve().FullName} contains [Cache] attribute but does not contain eligible methods for caching");
26+
continue;
27+
}
3228

33-
foreach (var elementToCache in elementsToCache)
34-
{
35-
if (!elementToCache.Type.IsEligibleForWeaving(references))
29+
if (!weavingCandidate.ClassDefinition.IsEligibleForWeaving(references))
3630
{
37-
// TODO: Create warning
31+
WriteWarning($"Class {weavingCandidate.ClassDefinition.Name} contains [Cache] attribute but does not contain a single non-inherited property implementing IMemoryCache interface");
3832
continue;
3933
}
4034

41-
ClassWeavingContext classWeavingContext = new ClassWeavingContext(elementToCache.Type, references);
35+
ClassWeavingContext classWeavingContext = new ClassWeavingContext(weavingCandidate.ClassDefinition, references);
4236
classWeavingContext.CacheGetterMethod = MemoryCache.GetCacheGetterMethod(classWeavingContext);
4337

44-
foreach (MethodDefinition methodDefinition in elementToCache.Methods)
38+
foreach (MethodDefinition methodDefinition in weavingCandidate.MethodDefinitions)
4539
{
4640
if (!methodDefinition.IsEligibleForWeaving(references))
4741
{
48-
// TODO: Create warning if marked explicit for weaving
42+
// Show warning if test was marked explicitly
43+
if (methodDefinition.HasCacheAttribute(references))
44+
{
45+
WriteWarning($"Method {methodDefinition.FullName} contains [Cache] attribute but is not eligible for weaving");
46+
break;
47+
}
48+
4949
continue;
5050
}
5151

src/SpatialFocus.MethodCache.Fody/References.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace SpatialFocus.MethodCache.Fody
1010
using System.Runtime.CompilerServices;
1111
using Mono.Cecil;
1212
using Mono.Cecil.Rocks;
13+
using SpatialFocus.MethodCache.Fody.Extensions;
1314

1415
public class References
1516
{

src/SpatialFocus.MethodCache.Sample.Library/BasicSampleWeaved.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public BasicSampleWeaved(IMemoryCache memoryCache)
1919
public int Add(int a, int b)
2020
{
2121
// Create a unique cache key, based on namespace, class name and method name as first parameter and corresponding
22-
// generic class parameters,generic method parameters and method parameters
22+
// generic class parameters, generic method parameters and method parameters
2323
Tuple<string, int, int> key = new Tuple<string, int, int>("SpatialFocus.MethodCache.Sample.Library.BasicSample.Add", a, b);
2424

2525
// Check and return if a cached value exists for key

src/SpatialFocus.MethodCache.Sample.Library/GenericSample.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace SpatialFocus.MethodCache.Sample.Library
77
using Microsoft.Extensions.Caching.Memory;
88

99
// MethodCache.Fody will look for classes and methods decorated with the Cache attribute
10-
[Cache]
1110
public class GenericSample<TClass1, TClass2>
1211
{
1312
public GenericSample(IMemoryCache memoryCache)

0 commit comments

Comments
 (0)