Skip to content

Commit

Permalink
Merge pull request dotnet#222 from atsushieno/reduce-android-dependen…
Browse files Browse the repository at this point in the history
…cy-in-attributes-2

Reduce Android.* dependencies from this not-only-about-Android repo.

We want the Java.Interop and xamarin-android repos to be distinct and
not (overly) overlapping. Philosophically, the Java.Interop repo
shouldn't use Xamarin.Android-specific names in public APIs.

The `Java.Interop.NamingCustomAttributes` shared project doesn't
comply with this philosophy, as it's full of Android-specific names.

Better separate the Java.Interop & xamarin-android repo's by
introducing a new `Java.Interop.IJniNameProviderAttribute` interface
which is used by `JavaNativeTypeManager` to support determining
the Java-side type name from a managed `System.Type` or Cecil
`TypeDefinition` instance. This allows us to remove the Android-
specific custom attributes from the Java.Interop repo.
  • Loading branch information
jonpryor authored Dec 6, 2017
2 parents f76b741 + cb161d2 commit cc1bb5f
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 101 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Android.Runtime {
#if !JCW_ONLY_TYPE_NAMES
public
#endif // !JCW_ONLY_TYPE_NAMES
sealed class RegisterAttribute : Attribute {
sealed class RegisterAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {

string connector;
string name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
<Import_RootNamespace>Java.Interop.NamingCustomAttributes</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Android.Content\BroadcastReceiverAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Content\ContentProviderAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Runtime\RegisterAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\ExportAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\ExportFieldAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\ExportParameterAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\ExportParameterKind.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ActivityAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ApplicationAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ServiceAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.App\InstrumentationAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\IJniNameProviderAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Runtime\RegisterAttribute.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)Android.Runtime\" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;
namespace Java.Interop
{
public interface IJniNameProviderAttribute
{
string Name { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
using Mono.Cecil;
using Mono.Cecil.Cil;

using Android.App;
using Android.Runtime;
using Java.Interop;

using Java.Interop.Tools.Cecil;
using Java.Interop.Tools.Diagnostics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Android.App
{
sealed partial class ActivityAttribute : Attribute {
sealed partial class ActivityAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
public string Name {get; set;}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;

namespace Android.App {
sealed partial class ApplicationAttribute : Attribute {
sealed partial class ApplicationAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
public string Name {get; set;}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Android.App {
sealed partial class InstrumentationAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
public string Name { get; set; }
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;

namespace Android.App {
sealed partial class ServiceAttribute : Attribute {
sealed partial class ServiceAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
public string Name {get; set;}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Android.Content {

partial class BroadcastReceiverAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Android.Content {

partial class ContentProviderAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\JavaCallableWrapperGeneratorTests.cs" />
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\TypeNameMapGeneratorTests.cs" />
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\SupportDeclarations.cs" />
<Compile Include="Android.App\ActivityAttribute.cs" />
<Compile Include="Android.App\ApplicationAttribute.cs" />
<Compile Include="Android.App\InstrumentationAttribute.cs" />
<Compile Include="Android.App\ServiceAttribute.cs" />
<Compile Include="Android.Content\BroadcastReceiverAttribute.cs" />
<Compile Include="Android.Content\ContentProviderAttribute.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand All @@ -63,6 +69,8 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Java.Interop.Tools.JavaCallableWrappers\" />
<Folder Include="Android.App\" />
<Folder Include="Android.Content\" />
</ItemGroup>
<Import Project="..\..\Java.Interop.NamingCustomAttributes\Java.Interop.NamingCustomAttributes.projitems" Label="Shared" Condition="Exists('..\..\Java.Interop.NamingCustomAttributes\Java.Interop.NamingCustomAttributes.projitems')" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Android.Content;
using Android.Runtime;

using RegisterAttribute = Android.Runtime.RegisterAttribute;

namespace Android.App {

[Register ("android/app/Application", DoNotGenerateAcw = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using Java.Interop;
using Android.Runtime;

#if HAVE_CECIL
using Mono.Cecil;
using Java.Interop.Tools.Cecil;
using Android.Runtime;
using Java.Interop.Tools.JavaCallableWrappers;
#endif // HAVE_CECIL

Expand Down Expand Up @@ -262,35 +261,8 @@ static string GetSpecialExportJniType (string typeName, ExportParameterKind expo
// Keep in sync with ToJniNameFromAttributes(TypeDefinition)
public static string ToJniNameFromAttributes (Type type)
{
var ras = (Android.Runtime.RegisterAttribute[]) type.GetCustomAttributes (typeof (Android.Runtime.RegisterAttribute), false);
if (ras.Length > 0 && !string.IsNullOrEmpty (ras [0].Name))
return ras [0].Name.Replace ('.', '/');

var aas = (Android.App.ActivityAttribute[]) type.GetCustomAttributes (typeof (Android.App.ActivityAttribute), false);
if (aas.Length > 0 && !string.IsNullOrEmpty (aas [0].Name))
return aas [0].Name.Replace ('.', '/');

var apps = (Android.App.ApplicationAttribute[]) type.GetCustomAttributes (typeof (Android.App.ApplicationAttribute), false);
if (apps.Length > 0 && !string.IsNullOrEmpty (apps [0].Name))
return apps [0].Name.Replace ('.', '/');

var sas = (Android.App.ServiceAttribute[]) type.GetCustomAttributes (typeof (Android.App.ServiceAttribute), false);
if (sas.Length > 0 && !string.IsNullOrEmpty (sas [0].Name))
return sas [0].Name.Replace ('.', '/');

var bras = (Android.Content.BroadcastReceiverAttribute[]) type.GetCustomAttributes (typeof (Android.Content.BroadcastReceiverAttribute), false);
if (bras.Length > 0 && !string.IsNullOrEmpty (bras [0].Name))
return bras [0].Name.Replace ('.', '/');

var cpas = (Android.Content.ContentProviderAttribute[]) type.GetCustomAttributes (typeof (Android.Content.ContentProviderAttribute), false);
if (cpas.Length > 0 && !string.IsNullOrEmpty (cpas [0].Name))
return cpas [0].Name.Replace ('.', '/');

var ias = (Android.App.InstrumentationAttribute[])type.GetCustomAttributes (typeof (Android.App.InstrumentationAttribute), false);
if (ias.Length > 0 && !string.IsNullOrEmpty (ias [0].Name))
return ias [0].Name.Replace ('.', '/');

return null;
var a = type.GetCustomAttributes ().OfType<IJniNameProviderAttribute> ().FirstOrDefault (j => !string.IsNullOrEmpty (j.Name));
return a == null ? null : a.Name.Replace ('.', '/');
}

/*
Expand Down Expand Up @@ -478,41 +450,25 @@ static string ToJniName (TypeDefinition type, ExportParameterKind exportKind)

static string ToJniNameFromAttributes (TypeDefinition type)
{
#region CustomAttribute alternate name support
// ToJniName(Type) doesn't do this, as it's instead done in
// JNIEnv.FindClass(Type) for perf reasons.
var attr = type.GetCustomAttributes ("Android.Runtime.RegisterAttribute").SingleOrDefault ();
if (attr != null) {
string name = (string) attr.ConstructorArguments [0].Value;
#region CustomAttribute alternate name support
var attrs = type.CustomAttributes.Where (a => a.AttributeType.Resolve ().Interfaces.Any (it => it.InterfaceType.FullName == typeof (IJniNameProviderAttribute).FullName));
return attrs.Select (attr => {
var ap = attr.Properties.FirstOrDefault (p => p.Name == "Name");
string name = null;
if (ap.Name == null) {
var ca = attr.ConstructorArguments.FirstOrDefault ();
if (ca.Type == null || ca.Type.FullName != "System.String")
return null;
name = (string) ca.Value;
} else
name = (string) ap.Argument.Value;
if (!string.IsNullOrEmpty (name))
return name.Replace ('.', '/');
}
foreach (var attributeType in new []{
"Android.App.ActivityAttribute",
"Android.App.ApplicationAttribute",
"Android.App.InstrumentationAttribute",
"Android.App.ServiceAttribute",
"Android.Content.BroadcastReceiverAttribute",
"Android.Content.ContentProviderAttribute",
}) {
attr = type.GetCustomAttributes (attributeType).SingleOrDefault ();
if (attr != null) {
var ap = attr.Properties.FirstOrDefault (p => p.Name == "Name");
string name = null;
if (ap.Name == null) {
var ca = attr.ConstructorArguments.FirstOrDefault ();
if (ca.Type == null || ca.Type.FullName != "System.String")
continue;
name = (string) ca.Value;
}
else
name = (string) ap.Argument.Value;
if (!string.IsNullOrEmpty (name))
return name.Replace ('.', '/');
}
}
#endregion
return null;
else
return null;
})
.FirstOrDefault (s => s != null);
#endregion
}

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

0 comments on commit cc1bb5f

Please sign in to comment.