Skip to content

Commit cb161d2

Browse files
committed
Reduce Android.* dependencies from this not-only-about-Android repo.
1 parent e1ad506 commit cb161d2

File tree

16 files changed

+71
-101
lines changed

16 files changed

+71
-101
lines changed

src/Java.Interop.NamingCustomAttributes/Android.App/InstrumentationAttribute.cs

-8
This file was deleted.

src/Java.Interop.NamingCustomAttributes/Android.Content/BroadcastReceiverAttribute.cs

-8
This file was deleted.

src/Java.Interop.NamingCustomAttributes/Android.Content/ContentProviderAttribute.cs

-8
This file was deleted.

src/Java.Interop.NamingCustomAttributes/Android.Runtime/RegisterAttribute.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Android.Runtime {
66
#if !JCW_ONLY_TYPE_NAMES
77
public
88
#endif // !JCW_ONLY_TYPE_NAMES
9-
sealed class RegisterAttribute : Attribute {
9+
sealed class RegisterAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
1010

1111
string connector;
1212
string name;

src/Java.Interop.NamingCustomAttributes/Java.Interop.NamingCustomAttributes.projitems

+5-7
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99
<Import_RootNamespace>Java.Interop.NamingCustomAttributes</Import_RootNamespace>
1010
</PropertyGroup>
1111
<ItemGroup>
12-
<Compile Include="$(MSBuildThisFileDirectory)Android.Content\BroadcastReceiverAttribute.cs" />
13-
<Compile Include="$(MSBuildThisFileDirectory)Android.Content\ContentProviderAttribute.cs" />
14-
<Compile Include="$(MSBuildThisFileDirectory)Android.Runtime\RegisterAttribute.cs" />
1512
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\ExportAttribute.cs" />
1613
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\ExportFieldAttribute.cs" />
1714
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\ExportParameterAttribute.cs" />
1815
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\ExportParameterKind.cs" />
19-
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ActivityAttribute.cs" />
20-
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ApplicationAttribute.cs" />
21-
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ServiceAttribute.cs" />
22-
<Compile Include="$(MSBuildThisFileDirectory)Android.App\InstrumentationAttribute.cs" />
16+
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\IJniNameProviderAttribute.cs" />
17+
<Compile Include="$(MSBuildThisFileDirectory)Android.Runtime\RegisterAttribute.cs" />
18+
</ItemGroup>
19+
<ItemGroup>
20+
<Folder Include="$(MSBuildThisFileDirectory)Android.Runtime\" />
2321
</ItemGroup>
2422
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
namespace Java.Interop
3+
{
4+
public interface IJniNameProviderAttribute
5+
{
6+
string Name { get; }
7+
}
8+
}

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGenerator.cs

-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
using Mono.Cecil;
1010
using Mono.Cecil.Cil;
1111

12-
using Android.App;
1312
using Android.Runtime;
14-
using Java.Interop;
1513

1614
using Java.Interop.Tools.Cecil;
1715
using Java.Interop.Tools.Diagnostics;

src/Java.Interop.NamingCustomAttributes/Android.App/ActivityAttribute.cs renamed to src/Java.Interop.Tools.JavaCallableWrappers/Test/Android.App/ActivityAttribute.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Android.App
44
{
5-
sealed partial class ActivityAttribute : Attribute {
5+
sealed partial class ActivityAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
66
public string Name {get; set;}
77
}
88
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22

33
namespace Android.App {
4-
sealed partial class ApplicationAttribute : Attribute {
4+
sealed partial class ApplicationAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
55
public string Name {get; set;}
66
}
77
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
3+
namespace Android.App {
4+
sealed partial class InstrumentationAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
5+
public string Name { get; set; }
6+
}
7+
}
8+
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22

33
namespace Android.App {
4-
sealed partial class ServiceAttribute : Attribute {
4+
sealed partial class ServiceAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
55
public string Name {get; set;}
66
}
77
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
3+
namespace Android.Content {
4+
5+
partial class BroadcastReceiverAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
6+
public string Name { get; set; }
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
3+
namespace Android.Content {
4+
5+
partial class ContentProviderAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
6+
public string Name { get; set; }
7+
}
8+
}

src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers-Tests.csproj

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\JavaCallableWrapperGeneratorTests.cs" />
4040
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\TypeNameMapGeneratorTests.cs" />
4141
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\SupportDeclarations.cs" />
42+
<Compile Include="Android.App\ActivityAttribute.cs" />
43+
<Compile Include="Android.App\ApplicationAttribute.cs" />
44+
<Compile Include="Android.App\InstrumentationAttribute.cs" />
45+
<Compile Include="Android.App\ServiceAttribute.cs" />
46+
<Compile Include="Android.Content\BroadcastReceiverAttribute.cs" />
47+
<Compile Include="Android.Content\ContentProviderAttribute.cs" />
4248
</ItemGroup>
4349
<ItemGroup>
4450
<None Include="packages.config" />
@@ -63,6 +69,8 @@
6369
</ItemGroup>
6470
<ItemGroup>
6571
<Folder Include="Java.Interop.Tools.JavaCallableWrappers\" />
72+
<Folder Include="Android.App\" />
73+
<Folder Include="Android.Content\" />
6674
</ItemGroup>
6775
<Import Project="..\..\Java.Interop.NamingCustomAttributes\Java.Interop.NamingCustomAttributes.projitems" Label="Shared" Condition="Exists('..\..\Java.Interop.NamingCustomAttributes\Java.Interop.NamingCustomAttributes.projitems')" />
6876
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers/SupportDeclarations.cs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Android.Content;
1212
using Android.Runtime;
1313

14+
using RegisterAttribute = Android.Runtime.RegisterAttribute;
15+
1416
namespace Android.App {
1517

1618
[Register ("android/app/Application", DoNotGenerateAcw = true)]

src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings/JavaNativeTypeManager.cs

+20-64
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
using System.Reflection;
55
using System.Security.Cryptography;
66
using System.Text;
7-
using Java.Interop;
7+
using Android.Runtime;
88

99
#if HAVE_CECIL
1010
using Mono.Cecil;
1111
using Java.Interop.Tools.Cecil;
12-
using Android.Runtime;
1312
using Java.Interop.Tools.JavaCallableWrappers;
1413
#endif // HAVE_CECIL
1514

@@ -262,35 +261,8 @@ static string GetSpecialExportJniType (string typeName, ExportParameterKind expo
262261
// Keep in sync with ToJniNameFromAttributes(TypeDefinition)
263262
public static string ToJniNameFromAttributes (Type type)
264263
{
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 ('.', '/');
294266
}
295267

296268
/*
@@ -478,41 +450,25 @@ static string ToJniName (TypeDefinition type, ExportParameterKind exportKind)
478450

479451
static string ToJniNameFromAttributes (TypeDefinition type)
480452
{
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;
487465
if (!string.IsNullOrEmpty (name))
488466
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
516472
}
517473

518474
public static int GetArrayInfo (Mono.Cecil.TypeReference type, out Mono.Cecil.TypeReference elementType)

0 commit comments

Comments
 (0)