Commit b8f6f88
authored
[Java.Interop] address some "easy" trimmer warnings (#1184)
Context: #1157
We want to enable `$(IsAotCompatible)`=true, so we can identify and
fix trimmer warnings within `Java.Interop.dll`. (Then later work our
way "up the stack", fixing trimmer warnings within `Mono.Android.dll`
and `Microsoft.Maui.dll` and…)
On the path to enabling `$(IsAotCompatible)`=true, we can enable some
settings to get started:
<IsTrimmable>true</IsTrimmable>
<EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
<EnableAotAnalyzer>true</EnableAotAnalyzer>
This opts into the analyzers without declaring that the assembly is
fully AOT-compatible.
Starting out, I got 33 warnings: this is an attempt to address the
ones that don't require too much thinking. Unfortunately, solving
one warning likely will create dozens more -- as you have to update
all callers.
This results in 24 warnings remaining. Since `Release` builds have
`$(TreatWarningsAsErrors)`, I will wait to enable the analyzers until
all warnings are addressed.
~~ Example Warnings ~~
**`System.Linq.Expression` usage:**
`JniRuntime.JniValueManager.CreateParameterFromManagedExpression()`
/* 638 */ partial class JavaPeerableValueMarshaler {
/* 667 */ public override Expression CreateParameterFromManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize)
/* 668 */ {
/* 669 */ var r = CreateIntermediaryExpressionFromManagedExpression (context, sourceValue);
/* 670 */ var h = Expression.Variable (typeof (IntPtr), sourceValue.Name + "_handle");
/* 671 */ context.LocalVariables.Add (h);
/* 672 */ context.CreationStatements.Add (Expression.Assign (h, Expression.Property (r, "Handle")));
/* 674 */ return h;
/* 675 */ }
/* 710 */ }
emits an IL2026:
src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(672,58):
warning IL2026: Using member 'System.Linq.Expressions.Expression.Property(Expression, String)'
which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code.
Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed.
I updated this with:
partial class JniValueMarshaler {
internal const string ExpressionRequiresUnreferencedCode = "System.Linq.Expression usage may trim away required code.";
[RequiresUnreferencedCode (ExpressionRequiresUnreferencedCode)]
public virtual Expression CreateParameterFromManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize) => …
}
partial class JavaPeerableValueMarshaler /* indirectly inherits JniValueMarshaler */ {
[RequiresUnreferencedCode (ExpressionRequiresUnreferencedCode)]
public override Expression CreateParameterFromManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize) => …
}
**`Type.GetNestedType()` usage:**
`JniRuntime.JniTypeManager.TryLoadJniMarshalMethods()`:
/* 82 */ partial class JniRuntime.JniTypeManager {
/* 445 */ bool TryLoadJniMarshalMethods (JniType nativeClass, Type type, string? methods)
/* 446 */ {
/* 447 */ var marshalType = type?.GetNestedType ("__<$>_jni_marshal_methods", BindingFlags.NonPublic);
emits an IL2070 warning:
src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(447,28):
warning IL2070: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicNestedTypes' in call to
'System.Type.GetNestedType(String, BindingFlags)'.
The parameter 'type' of method 'Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String)'
does not have matching annotations.
The source value must declare at least the same requirements as those declared on the target location it is assigned to.
I updated this with:
partial class JniRuntime.JniTypeManager {
bool TryLoadJniMarshalMethods (
JniType nativeClass,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
Type type,
string? methods) => …
**`Activator.CreateInstance()` usage:**
`JniRuntime.JniValueManager.GetValueMarshaler()`:
/* 50 */ partial class JniRuntime.JniValueManager {
/* 530 */ public JniValueMarshaler GetValueMarshaler (Type type)
/* 531 */ {
/* 541 */ if (marshalerAttr != null)
/* 542 */ return (JniValueMarshaler) Activator.CreateInstance (marshalerAttr.MarshalerType)!;
/* 591 */ }
/* 612 */ }
emits an IL2072 warning:
src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(542,33): warning IL2072:
'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor'
in call to 'System.Activator.CreateInstance(Type)'.
The return value of method 'Java.Interop.JniValueMarshalerAttribute.MarshalerType.get' does not have matching annotations.
The source value must declare at least the same requirements as those declared on the target location it is assigned to.
I updated this with:
partial class JniRuntime.JniValueManager {
public JniValueMarshaler GetValueMarshaler (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.Interfaces)]
Type type)
=> …
}
partial class JniValueMarshalerAttribute {
public Type MarshalerType {
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
get;
}
}
~~ Code that Actually Changed ~~
As I added more attributes, these snowballed into more and more
warnings! I eventually had to make
`JniRuntime.JniValueManager.GetPeerType()` look like:
partial class JniRuntime.JniValueManager {
internal const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;
[return: DynamicallyAccessedMembers (Constructors)]
static Type GetPeerType ([DynamicallyAccessedMembers Constructors)] Type type) => …
}
The analyzer was not able to understand code like:
partial class JniRuntime.JniValueManager {
static readonly KeyValuePair<Type, Type>[] PeerTypeMappings = new []{
new KeyValuePair<Type, Type>(typeof (object), typeof (JavaObject)),
new KeyValuePair<Type, Type>(typeof (IJavaPeerable), typeof (JavaObject)),
new KeyValuePair<Type, Type>(typeof (Exception), typeof (JavaException)),
};
static Type GetPeerType (Type type)
{
foreach (var m in PeerTypeMappings) {
if (m.Key == type)
return m.Value;
}
}
}
Simply removing the `PeerTypeMappings` array and using `if` statements
solved the warnings. This may be the only real code change if any.1 parent dfcbd67 commit b8f6f88
File tree
16 files changed
+499
-106
lines changed- src/Java.Interop
- Java.Interop
16 files changed
+499
-106
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
17 | 21 | | |
18 | 22 | | |
19 | 23 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
362 | 362 | | |
363 | 363 | | |
364 | 364 | | |
365 | | - | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
366 | 371 | | |
367 | 372 | | |
368 | 373 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
11 | 15 | | |
12 | 16 | | |
13 | 17 | | |
| |||
164 | 168 | | |
165 | 169 | | |
166 | 170 | | |
167 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
168 | 176 | | |
169 | 177 | | |
170 | 178 | | |
| |||
194 | 202 | | |
195 | 203 | | |
196 | 204 | | |
197 | | - | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
198 | 210 | | |
199 | 211 | | |
200 | 212 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
244 | 244 | | |
245 | 245 | | |
246 | 246 | | |
247 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
248 | 251 | | |
249 | 252 | | |
250 | 253 | | |
251 | 254 | | |
252 | 255 | | |
253 | 256 | | |
254 | | - | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
255 | 262 | | |
256 | 263 | | |
257 | 264 | | |
| |||
276 | 283 | | |
277 | 284 | | |
278 | 285 | | |
| 286 | + | |
279 | 287 | | |
280 | 288 | | |
281 | 289 | | |
| |||
439 | 447 | | |
440 | 448 | | |
441 | 449 | | |
442 | | - | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
443 | 454 | | |
444 | 455 | | |
445 | 456 | | |
446 | 457 | | |
447 | 458 | | |
448 | 459 | | |
449 | | - | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
450 | 465 | | |
451 | 466 | | |
452 | 467 | | |
| |||
471 | 486 | | |
472 | 487 | | |
473 | 488 | | |
| 489 | + | |
474 | 490 | | |
475 | 491 | | |
476 | 492 | | |
| |||
634 | 650 | | |
635 | 651 | | |
636 | 652 | | |
637 | | - | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
638 | 657 | | |
639 | 658 | | |
640 | 659 | | |
641 | 660 | | |
642 | 661 | | |
643 | 662 | | |
644 | | - | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
645 | 668 | | |
646 | 669 | | |
647 | 670 | | |
| |||
666 | 689 | | |
667 | 690 | | |
668 | 691 | | |
| 692 | + | |
669 | 693 | | |
670 | 694 | | |
671 | 695 | | |
| |||
829 | 853 | | |
830 | 854 | | |
831 | 855 | | |
832 | | - | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
833 | 860 | | |
834 | 861 | | |
835 | 862 | | |
836 | 863 | | |
837 | 864 | | |
838 | 865 | | |
839 | | - | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
840 | 871 | | |
841 | 872 | | |
842 | 873 | | |
| |||
861 | 892 | | |
862 | 893 | | |
863 | 894 | | |
| 895 | + | |
864 | 896 | | |
865 | 897 | | |
866 | 898 | | |
| |||
1024 | 1056 | | |
1025 | 1057 | | |
1026 | 1058 | | |
1027 | | - | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
1028 | 1063 | | |
1029 | 1064 | | |
1030 | 1065 | | |
1031 | 1066 | | |
1032 | 1067 | | |
1033 | 1068 | | |
1034 | | - | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
1035 | 1074 | | |
1036 | 1075 | | |
1037 | 1076 | | |
| |||
1056 | 1095 | | |
1057 | 1096 | | |
1058 | 1097 | | |
| 1098 | + | |
1059 | 1099 | | |
1060 | 1100 | | |
1061 | 1101 | | |
| |||
1219 | 1259 | | |
1220 | 1260 | | |
1221 | 1261 | | |
1222 | | - | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
1223 | 1266 | | |
1224 | 1267 | | |
1225 | 1268 | | |
1226 | 1269 | | |
1227 | 1270 | | |
1228 | 1271 | | |
1229 | | - | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
1230 | 1277 | | |
1231 | 1278 | | |
1232 | 1279 | | |
| |||
1251 | 1298 | | |
1252 | 1299 | | |
1253 | 1300 | | |
| 1301 | + | |
1254 | 1302 | | |
1255 | 1303 | | |
1256 | 1304 | | |
| |||
1414 | 1462 | | |
1415 | 1463 | | |
1416 | 1464 | | |
1417 | | - | |
| 1465 | + | |
| 1466 | + | |
| 1467 | + | |
| 1468 | + | |
1418 | 1469 | | |
1419 | 1470 | | |
1420 | 1471 | | |
1421 | 1472 | | |
1422 | 1473 | | |
1423 | 1474 | | |
1424 | | - | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
| 1478 | + | |
| 1479 | + | |
1425 | 1480 | | |
1426 | 1481 | | |
1427 | 1482 | | |
| |||
1446 | 1501 | | |
1447 | 1502 | | |
1448 | 1503 | | |
| 1504 | + | |
1449 | 1505 | | |
1450 | 1506 | | |
1451 | 1507 | | |
| |||
1609 | 1665 | | |
1610 | 1666 | | |
1611 | 1667 | | |
1612 | | - | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
| 1671 | + | |
1613 | 1672 | | |
1614 | 1673 | | |
1615 | 1674 | | |
1616 | 1675 | | |
1617 | 1676 | | |
1618 | 1677 | | |
1619 | | - | |
| 1678 | + | |
| 1679 | + | |
| 1680 | + | |
| 1681 | + | |
| 1682 | + | |
1620 | 1683 | | |
1621 | 1684 | | |
1622 | 1685 | | |
| |||
1641 | 1704 | | |
1642 | 1705 | | |
1643 | 1706 | | |
| 1707 | + | |
1644 | 1708 | | |
1645 | 1709 | | |
1646 | 1710 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
241 | 241 | | |
242 | 242 | | |
243 | 243 | | |
244 | | - | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
245 | 248 | | |
246 | 249 | | |
247 | 250 | | |
248 | 251 | | |
249 | 252 | | |
250 | 253 | | |
251 | | - | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
252 | 259 | | |
253 | 260 | | |
254 | 261 | | |
| |||
273 | 280 | | |
274 | 281 | | |
275 | 282 | | |
| 283 | + | |
276 | 284 | | |
277 | 285 | | |
278 | 286 | | |
| |||
0 commit comments