Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ExpressionCallString.cs « Reflection « Mono.Linker.Tests.Cases « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 699bb9d9d1ca64faa14225ce6d73e84a58b253b4 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.Reflection
{
	[SetupCSharpCompilerToUse ("csc")]
	[Reference ("System.Core.dll")]
	[ExpectedNoWarnings]
	public class ExpressionCallString
	{
		public static void Main ()
		{
			PublicMethods.Test ();
			ProtectedMethods.Test ();
			PrivateMethods.Test ();

			Expression.Call (typeof (Derived), "PublicOnBase", Type.EmptyTypes);
			Expression.Call (typeof (Derived), "ProtectedOnBase", Type.EmptyTypes);
			Expression.Call (typeof (Derived), "PrivateOnBase", Type.EmptyTypes);

			// Keep all methods on type UnknownNameMethodClass
			Expression.Call (typeof (UnknownNameMethodClass), GetUnknownString (), Type.EmptyTypes);

			TestUnknownType.Test ();

			TestGenericMethods.Test ();
		}

		[Kept]
		class PublicMethods
		{
			[Kept]
			public static void PublicStaticMethod () { }

			public void PublicInstanceMethod () { }

			protected static void ProtectedStaticMethod () { }

			protected void ProtectedInstanceMethod () { }

			private static void PrivateStaticMethod () { }

			private void PrivateInstanceMethod () { }

			[Kept]
			public static void Test ()
			{
				Expression.Call (typeof (PublicMethods), nameof (PublicStaticMethod), Type.EmptyTypes);

				// This should not mark anything, but it should also not warn (it should fail at runtime to find the method as well)
				Expression.Call (typeof (PublicMethods), nameof (PublicInstanceMethod), Type.EmptyTypes);
			}
		}

		[Kept]
		class ProtectedMethods
		{
			public static void PublicStaticMethod () { }

			public void PublicInstanceMethod () { }

			[Kept]
			protected static void ProtectedStaticMethod () { }

			protected void ProtectedInstanceMethod () { }

			private static void PrivateStaticMethod () { }

			private void PrivateInstanceMethod () { }

			[Kept]
			public static void Test ()
			{
				Expression.Call (typeof (ProtectedMethods), nameof (ProtectedStaticMethod), Type.EmptyTypes);

				// This should not mark anything, but it should also not warn (it should fail at runtime to find the method as well)
				Expression.Call (typeof (ProtectedMethods), nameof (ProtectedInstanceMethod), Type.EmptyTypes);
			}
		}

		[Kept]
		class PrivateMethods
		{
			public static void PublicStaticMethod () { }

			public void PublicInstanceMethod () { }

			protected static void ProtectedStaticMethod () { }

			protected void ProtectedInstanceMethod () { }

			[Kept]
			private static void PrivateStaticMethod () { }

			private void PrivateInstanceMethod () { }

			[Kept]
			public static void Test ()
			{
				Expression.Call (typeof (PrivateMethods), nameof (PrivateStaticMethod), Type.EmptyTypes);

				// This should not mark anything, but it should also not warn (it should fail at runtime to find the method as well)
				Expression.Call (typeof (PrivateMethods), nameof (PrivateInstanceMethod), Type.EmptyTypes);
			}
		}

		[Kept]
		static string GetUnknownString ()
		{
			return "unknownstring";
		}

		[Kept]
		class TestUnknownType
		{
			[Kept]
			[UnrecognizedReflectionAccessPattern (typeof (Expression), nameof (Expression.Call),
				new Type[] { typeof (Type), typeof (string), typeof (Type[]), typeof (Expression[]) }, messageCode: "IL2072")]
			public static void Test ()
			{
				// Keep all methods of the type that made the call
				Expression.Call (GetUnknownType (), "This string will not be reached", Type.EmptyTypes);
				// UnrecognizedReflectionAccessPattern
				Expression.Call (TriggerUnrecognizedPattern (), "This string will not be reached", Type.EmptyTypes);
			}

			[Kept]
			[return: KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
			[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
			static Type GetUnknownType ()
			{
				return typeof (TestType);
			}

			[Kept]
			static Type TriggerUnrecognizedPattern ()
			{
				return typeof (TestType);
			}
		}

		[Kept]
		class UnknownNameMethodClass
		{
			[Kept]
			public static void PublicStaticMethod ()
			{
			}

			[Kept]
			public void PublicNonStaticMethod ()
			{
			}

			[Kept]
			protected static void ProtectedStaticMethod ()
			{
			}

			[Kept]
			protected void ProtectedNonStaticMethod ()
			{
			}

			[Kept]
			private static void PrivateStaticMethod ()
			{
			}

			[Kept]
			private void PrivateNonStaticMethod ()
			{
			}
		}

		[Kept]
		class Base
		{
			[Kept]
			public static void PublicOnBase ()
			{
			}

			[Kept]
			protected static void ProtectedOnBase ()
			{
			}

			private static void PrivateOnBase ()
			{
			}
		}

		[Kept]
		[KeptBaseType (typeof (Base))]
		class Derived : Base
		{
		}

		[Kept]
		class TestGenericMethods
		{
			[Kept]
			public static void GenericMethodCalledAsNonGeneric<T> () { }

			[Kept]
			public static void GenericMethodWithNoRequirements<T> () { }

			[Kept]
			public static void GenericMethodWithRequirements<
				[KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
			[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)] T> ()
			{ }

			[Kept]
			public static void GenericMethodWithRequirementsNoArguments<
				[KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
			[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)] T> ()
			{ }

			[Kept]
			static void TestWithNoTypeParameters ()
			{
				// Linker should not warn even if the type parameters don't match since the target method has no requirements
				// the fact that the reflection API may fail in this case is not something linker should worry about.
				Expression.Call (typeof (TestGenericMethods), nameof (GenericMethodCalledAsNonGeneric), Type.EmptyTypes);
			}

			[Kept]
			static void TestMethodWithoutRequirements ()
			{
				// This may not warn - as it's safe
				Expression.Call (typeof (TestGenericMethods), nameof (GenericMethodWithNoRequirements), new Type[] { GetUnknownType () });
			}

			[Kept]
			static void TestMethodWithRequirements ()
			{
				// This may not warn - as it's safe
				Expression.Call (typeof (TestGenericMethods), nameof (GenericMethodWithRequirements), new Type[] { GetUnknownTypeWithRequrements () });
			}

			[Kept]
			[ExpectedWarning ("IL2060", "Expression.Call")]
			static void TestMethodWithRequirementsUnknownTypeArray (Type[] types)
			{
				// The passed in types array cannot be analyzed, so a warning is produced.
				Expression.Call (typeof (TestGenericMethods), nameof (GenericMethodWithRequirements), types);
			}

			[Kept]
			[ExpectedWarning ("IL2060", "Expression.Call")]
			static void TestMethodWithRequirementsButNoTypeArguments ()
			{
				Expression.Call (typeof (TestGenericMethods), nameof (GenericMethodWithRequirementsNoArguments), Type.EmptyTypes);
			}

			[Kept]
			[KeptMember (".cctor()")]
			class UnknownMethodWithRequirements
			{
				[Kept]
				public static void GenericMethodWithRequirements<
					[KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
				[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)] T> ()
				{ }

				[Kept]
				static string _unknownMethodName = "NoMethod";

				[Kept]
				[ExpectedWarning ("IL2060")]
				public static void TestWithTypeParameters ()
				{
					// Linker has no idea which method to mark - so it should warn if there are type parameters
					Expression.Call (typeof (UnknownMethodWithRequirements), _unknownMethodName, new Type[] { GetUnknownType () });
				}

				[Kept]
				public static void TestWithoutTypeParameters ()
				{
					// Linker has no idea which method to mark - so it should warn if there are type parameters
					Expression.Call (typeof (UnknownMethodWithRequirements), _unknownMethodName, Type.EmptyTypes);
				}
			}

			[Kept]
			[KeptMember (".cctor()")]
			class UnknownTypeWithRequirements
			{
				public static void GenericMethodWithRequirements<
					[KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
				[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)] T> ()
				{ }

				[Kept]
				[KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
				[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
				static Type _unknownType = null;

				[Kept]
				[ExpectedWarning ("IL2060")]
				public static void TestWithTypeParameters ()
				{
					// Linker has no idea which method to mark - so it should warn if there are type parameters
					Expression.Call (_unknownType, "NoMethod", new Type[] { GetUnknownType () });
				}

				[Kept]
				public static void TestWithoutTypeParameters ()
				{
					// Linker has no idea which method to mark - so it should warn if there are type parameters
					Expression.Call (_unknownType, "NoMethod", Type.EmptyTypes);
				}
			}

			[Kept]
			public static void Test ()
			{
				TestWithNoTypeParameters ();
				TestMethodWithoutRequirements ();
				TestMethodWithRequirements ();
				TestMethodWithRequirementsUnknownTypeArray (null);
				TestMethodWithRequirementsButNoTypeArguments ();
				UnknownMethodWithRequirements.TestWithTypeParameters ();
				UnknownMethodWithRequirements.TestWithoutTypeParameters ();
				UnknownTypeWithRequirements.TestWithTypeParameters ();
				UnknownTypeWithRequirements.TestWithoutTypeParameters ();
			}

			[Kept]
			static Type GetUnknownType () { return null; }

			[Kept]
			[return: KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
			[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)]
			static Type GetUnknownTypeWithRequrements () { return null; }
		}

		[Kept]
		class TestType { }
	}
}