-
Notifications
You must be signed in to change notification settings - Fork 6
/
TrackChangesAttribute.cs
172 lines (142 loc) · 6.15 KB
/
TrackChangesAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using Metalama.Framework.Advising;
using Metalama.Framework.Aspects;
using Metalama.Framework.Code;
using Metalama.Framework.Diagnostics;
#pragma warning disable IDE0031, IDE1005
[Inheritable]
public class TrackChangesAttribute : TypeAspect
{
private static readonly DiagnosticDefinition<INamedType> _mustHaveOnChangeMethod = new(
"MY001",
Severity.Error,
$"The '{nameof(ISwitchableChangeTracking)}' interface is implemented manually on type '{{0}}', but the type does not have an '{nameof(OnChange)}()' method.");
private static readonly DiagnosticDefinition _onChangeMethodMustBeProtected = new(
"MY002",
Severity.Error,
$"The '{nameof(OnChange)}()' method must be have the 'protected' accessibility.");
private static readonly DiagnosticDefinition<IMethod> _onPropertyChangedMustBeVirtual = new(
"MY003",
Severity.Error,
"The '{0}' method must be virtual.");
public override void BuildAspect( IAspectBuilder<INamedType> builder )
{
// Implement the ISwitchableChangeTracking interface.
var implementInterfaceResult = builder.Advice.ImplementInterface( builder.Target,
typeof(ISwitchableChangeTracking), OverrideStrategy.Ignore );
if ( implementInterfaceResult.Outcome == AdviceOutcome.Ignore )
{
// If the type already implements ISwitchableChangeTracking, it must have a protected method called OnChanged, without parameters, otherwise
// this is a contract violation, so we report an error.
var onChangeMethod = builder.Target.AllMethods.OfName( nameof(this.OnChange) )
.SingleOrDefault( m => m.Parameters.Count == 0 );
if ( onChangeMethod == null )
{
builder.Diagnostics.Report( _mustHaveOnChangeMethod.WithArguments( builder.Target ) );
}
else if ( onChangeMethod.Accessibility != Accessibility.Protected )
{
builder.Diagnostics.Report( _onChangeMethodMustBeProtected );
}
}
else
{
builder.Advice.IntroduceField( builder.Target, "_isTrackingChanges", typeof(bool) );
}
var onPropertyChanged = this.GetOnPropertyChangedMethod( builder.Target );
if ( onPropertyChanged == null ) /*<NoOnPropertyChanged>*/
{
// If the type has an OnPropertyChanged method, we assume that all properties
// and fields already call it, and we hook into OnPropertyChanged instead of
// overriding each setter.
var fieldsOrProperties = builder.Target.FieldsAndProperties
.Where( f =>
!f.IsImplicitlyDeclared && f.Writeability == Writeability.All && f.IsAutoPropertyOrField == true );
foreach ( var fieldOrProperty in fieldsOrProperties )
{
builder.Advice.OverrideAccessors( fieldOrProperty, null, nameof(this.OverrideSetter) );
}
} /*</NoOnPropertyChanged>*/
else if ( onPropertyChanged.DeclaringType.Equals( builder.Target ) ) /*<OnPropertyChangedInCurrentType>*/
{
// If the OnPropertyChanged method was declared in the current type, override it.
builder.Advice.Override( onPropertyChanged, nameof(this.OnPropertyChanged) );
} /*</OnPropertyChangedInCurrentType>*/
else if ( implementInterfaceResult.Outcome == AdviceOutcome.Ignore ) /*<OnPropertyChangedInBaseType>*/
{
// If we have an OnPropertyChanged method but the type already implements ISwitchableChangeTracking,
// we assume that the type already hooked the OnPropertyChanged method, and
// there is nothing else to do.
}
else
{
// If the OnPropertyChanged method was defined in a base class, but not overridden
// in the current class, and if we implement ISwitchableChangeTracking ourselves,
// then we need to override OnPropertyChanged.
if ( !onPropertyChanged.IsVirtual )
{
builder.Diagnostics.Report( _onPropertyChangedMustBeVirtual.WithArguments( onPropertyChanged ) );
}
else
{
builder.Advice.IntroduceMethod( builder.Target, nameof(this.OnPropertyChanged),
whenExists: OverrideStrategy.Override );
}
} /*</OnPropertyChangedInBaseType>*/
}
private IMethod? GetOnPropertyChangedMethod( INamedType type )
=> type.AllMethods
.OfName( "OnPropertyChanged" )
.SingleOrDefault( m => m.Parameters.Count == 1 );
[InterfaceMember]
public bool IsChanged { get; private set; }
[InterfaceMember]
public bool IsTrackingChanges
{
get => meta.This._isTrackingChanges;
set
{
if ( meta.This._isTrackingChanges != value )
{
meta.This._isTrackingChanges = value;
var onPropertyChanged = this.GetOnPropertyChangedMethod( meta.Target.Type );
if ( onPropertyChanged != null )
{
onPropertyChanged.Invoke( nameof(this.IsTrackingChanges) );
}
}
}
}
[InterfaceMember]
public void AcceptChanges() => this.IsChanged = false;
[Introduce( WhenExists = OverrideStrategy.Ignore )]
protected void OnChange()
{
if ( this.IsChanged == false )
{
this.IsChanged = true;
var onPropertyChanged = this.GetOnPropertyChangedMethod( meta.Target.Type );
if ( onPropertyChanged != null )
{
onPropertyChanged.Invoke( nameof(this.IsChanged) );
}
}
}
[Template]
private void OverrideSetter( dynamic? value )
{
meta.Proceed();
if ( value != meta.Target.Property.Value )
{
this.OnChange();
}
}
[Template]
protected virtual void OnPropertyChanged( string name )
{
meta.Proceed();
if ( name is not (nameof(this.IsChanged) or nameof(this.IsTrackingChanges)) )
{
this.OnChange();
}
}
}