-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathInstructionExtensions.cs
More file actions
143 lines (120 loc) · 4.25 KB
/
InstructionExtensions.cs
File metadata and controls
143 lines (120 loc) · 4.25 KB
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
using System;
using JetBrains.Annotations;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace ArchUnitNET.Loader
{
public static class InstructionExtensions
{
public static bool IsOperationForBackedProperty(
[NotNull] this Instruction methodBodyInstruction
)
{
if (methodBodyInstruction == null)
{
throw new ArgumentNullException(nameof(methodBodyInstruction));
}
return IsMethodCallToSetBackingFieldDefinition(methodBodyInstruction)
|| IsNewObjectToSetBackingField(methodBodyInstruction);
}
public static bool IsMethodCallAssignment([NotNull] this Instruction instruction)
{
if (instruction == null)
{
throw new ArgumentNullException(nameof(instruction));
}
var nextOp = instruction.Next;
return IsReturnOp(nextOp) && IsMethodCallOp(instruction);
}
public static FieldDefinition GetAssigneeFieldDefinition(
[NotNull] this Instruction methodCallInstruction
)
{
if (methodCallInstruction == null)
{
throw new ArgumentNullException(nameof(methodCallInstruction));
}
var methodCallAssignment = methodCallInstruction.Next;
if (
methodCallAssignment != null
&& methodCallAssignment.Operand is FieldReference fieldReference
)
{
return fieldReference.Resolve();
}
return null;
}
public static bool IsMethodCallToSetBackingFieldDefinition(
[NotNull] this Instruction instruction
)
{
if (instruction == null)
{
throw new ArgumentNullException(nameof(instruction));
}
var nextOp = instruction.Next;
if (nextOp == null)
{
return false;
}
var assigneeIsBackingField = nextOp.IsOperandBackingField();
var isValidSetBakingFieldOp =
instruction.IsMethodCallOp() && nextOp.IsSetFieldOp() && assigneeIsBackingField;
return isValidSetBakingFieldOp;
}
public static bool IsNewObjectToSetBackingField([NotNull] this Instruction instruction)
{
if (instruction == null)
{
throw new ArgumentNullException(nameof(instruction));
}
var nextOp = instruction.Next;
return instruction.IsNewObjectOp() && nextOp.IsOperandBackingField();
}
public static bool IsNewObjectOp(this Instruction instruction)
{
if (instruction == null)
{
throw new ArgumentNullException(nameof(instruction));
}
return instruction.OpCode == OpCodes.Newobj;
}
public static bool IsOperandBackingField([NotNull] this Instruction instruction)
{
if (instruction == null)
{
throw new ArgumentNullException(nameof(instruction));
}
if (instruction.Operand is FieldReference fieldReference)
{
return fieldReference.IsBackingField();
}
return false;
}
public static bool IsMethodCallOp([NotNull] this Instruction methodBodyInstruction)
{
if (methodBodyInstruction == null)
{
throw new ArgumentNullException(nameof(methodBodyInstruction));
}
return methodBodyInstruction.OpCode == OpCodes.Call
|| methodBodyInstruction.OpCode == OpCodes.Callvirt;
}
public static bool IsSetFieldOp([NotNull] this Instruction nextOp)
{
if (nextOp == null)
{
throw new ArgumentNullException(nameof(nextOp));
}
return nextOp.OpCode == OpCodes.Stfld;
}
public static bool IsReturnOp([NotNull] this Instruction operation)
{
if (operation == null)
{
throw new ArgumentNullException(nameof(operation));
}
return operation.OpCode == OpCodes.Ret;
}
}
}