|
4 | 4 | using System.Reflection;
|
5 | 5 | using System.Security.Cryptography;
|
6 | 6 | using System.Text;
|
7 |
| -using Java.Interop; |
| 7 | +using Android.Runtime; |
8 | 8 |
|
9 | 9 | #if HAVE_CECIL
|
10 | 10 | using Mono.Cecil;
|
11 | 11 | using Java.Interop.Tools.Cecil;
|
12 |
| -using Android.Runtime; |
13 | 12 | using Java.Interop.Tools.JavaCallableWrappers;
|
14 | 13 | #endif // HAVE_CECIL
|
15 | 14 |
|
@@ -262,35 +261,8 @@ static string GetSpecialExportJniType (string typeName, ExportParameterKind expo
|
262 | 261 | // Keep in sync with ToJniNameFromAttributes(TypeDefinition)
|
263 | 262 | public static string ToJniNameFromAttributes (Type type)
|
264 | 263 | {
|
265 |
| - var ras = (Android.Runtime.RegisterAttribute[]) type.GetCustomAttributes (typeof (Android.Runtime.RegisterAttribute), false); |
266 |
| - if (ras.Length > 0 && !string.IsNullOrEmpty (ras [0].Name)) |
267 |
| - return ras [0].Name.Replace ('.', '/'); |
268 |
| - |
269 |
| - var aas = (Android.App.ActivityAttribute[]) type.GetCustomAttributes (typeof (Android.App.ActivityAttribute), false); |
270 |
| - if (aas.Length > 0 && !string.IsNullOrEmpty (aas [0].Name)) |
271 |
| - return aas [0].Name.Replace ('.', '/'); |
272 |
| - |
273 |
| - var apps = (Android.App.ApplicationAttribute[]) type.GetCustomAttributes (typeof (Android.App.ApplicationAttribute), false); |
274 |
| - if (apps.Length > 0 && !string.IsNullOrEmpty (apps [0].Name)) |
275 |
| - return apps [0].Name.Replace ('.', '/'); |
276 |
| - |
277 |
| - var sas = (Android.App.ServiceAttribute[]) type.GetCustomAttributes (typeof (Android.App.ServiceAttribute), false); |
278 |
| - if (sas.Length > 0 && !string.IsNullOrEmpty (sas [0].Name)) |
279 |
| - return sas [0].Name.Replace ('.', '/'); |
280 |
| - |
281 |
| - var bras = (Android.Content.BroadcastReceiverAttribute[]) type.GetCustomAttributes (typeof (Android.Content.BroadcastReceiverAttribute), false); |
282 |
| - if (bras.Length > 0 && !string.IsNullOrEmpty (bras [0].Name)) |
283 |
| - return bras [0].Name.Replace ('.', '/'); |
284 |
| - |
285 |
| - var cpas = (Android.Content.ContentProviderAttribute[]) type.GetCustomAttributes (typeof (Android.Content.ContentProviderAttribute), false); |
286 |
| - if (cpas.Length > 0 && !string.IsNullOrEmpty (cpas [0].Name)) |
287 |
| - return cpas [0].Name.Replace ('.', '/'); |
288 |
| - |
289 |
| - var ias = (Android.App.InstrumentationAttribute[])type.GetCustomAttributes (typeof (Android.App.InstrumentationAttribute), false); |
290 |
| - if (ias.Length > 0 && !string.IsNullOrEmpty (ias [0].Name)) |
291 |
| - return ias [0].Name.Replace ('.', '/'); |
292 |
| - |
293 |
| - return null; |
| 264 | + var a = type.GetCustomAttributes ().OfType<IJniNameProviderAttribute> ().FirstOrDefault (j => !string.IsNullOrEmpty (j.Name)); |
| 265 | + return a == null ? null : a.Name.Replace ('.', '/'); |
294 | 266 | }
|
295 | 267 |
|
296 | 268 | /*
|
@@ -478,41 +450,25 @@ static string ToJniName (TypeDefinition type, ExportParameterKind exportKind)
|
478 | 450 |
|
479 | 451 | static string ToJniNameFromAttributes (TypeDefinition type)
|
480 | 452 | {
|
481 |
| -#region CustomAttribute alternate name support |
482 |
| - // ToJniName(Type) doesn't do this, as it's instead done in |
483 |
| - // JNIEnv.FindClass(Type) for perf reasons. |
484 |
| - var attr = type.GetCustomAttributes ("Android.Runtime.RegisterAttribute").SingleOrDefault (); |
485 |
| - if (attr != null) { |
486 |
| - string name = (string) attr.ConstructorArguments [0].Value; |
| 453 | + #region CustomAttribute alternate name support |
| 454 | + var attrs = type.CustomAttributes.Where (a => a.AttributeType.Resolve ().Interfaces.Any (it => it.InterfaceType.FullName == typeof (IJniNameProviderAttribute).FullName)); |
| 455 | + return attrs.Select (attr => { |
| 456 | + var ap = attr.Properties.FirstOrDefault (p => p.Name == "Name"); |
| 457 | + string name = null; |
| 458 | + if (ap.Name == null) { |
| 459 | + var ca = attr.ConstructorArguments.FirstOrDefault (); |
| 460 | + if (ca.Type == null || ca.Type.FullName != "System.String") |
| 461 | + return null; |
| 462 | + name = (string) ca.Value; |
| 463 | + } else |
| 464 | + name = (string) ap.Argument.Value; |
487 | 465 | if (!string.IsNullOrEmpty (name))
|
488 | 466 | return name.Replace ('.', '/');
|
489 |
| - } |
490 |
| - foreach (var attributeType in new []{ |
491 |
| - "Android.App.ActivityAttribute", |
492 |
| - "Android.App.ApplicationAttribute", |
493 |
| - "Android.App.InstrumentationAttribute", |
494 |
| - "Android.App.ServiceAttribute", |
495 |
| - "Android.Content.BroadcastReceiverAttribute", |
496 |
| - "Android.Content.ContentProviderAttribute", |
497 |
| - }) { |
498 |
| - attr = type.GetCustomAttributes (attributeType).SingleOrDefault (); |
499 |
| - if (attr != null) { |
500 |
| - var ap = attr.Properties.FirstOrDefault (p => p.Name == "Name"); |
501 |
| - string name = null; |
502 |
| - if (ap.Name == null) { |
503 |
| - var ca = attr.ConstructorArguments.FirstOrDefault (); |
504 |
| - if (ca.Type == null || ca.Type.FullName != "System.String") |
505 |
| - continue; |
506 |
| - name = (string) ca.Value; |
507 |
| - } |
508 |
| - else |
509 |
| - name = (string) ap.Argument.Value; |
510 |
| - if (!string.IsNullOrEmpty (name)) |
511 |
| - return name.Replace ('.', '/'); |
512 |
| - } |
513 |
| - } |
514 |
| -#endregion |
515 |
| - return null; |
| 467 | + else |
| 468 | + return null; |
| 469 | + }) |
| 470 | + .FirstOrDefault (s => s != null); |
| 471 | + #endregion |
516 | 472 | }
|
517 | 473 |
|
518 | 474 | public static int GetArrayInfo (Mono.Cecil.TypeReference type, out Mono.Cecil.TypeReference elementType)
|
|
0 commit comments