Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ internal void Generate (JavaPeerInfo type, TextWriter writer)
WriteClassDeclaration (type, writer);
WriteStaticInitializer (type, writer);
WriteConstructors (type, writer);
WriteFields (type, writer);
WriteMethods (type, writer);
WriteGCUserPeerMethods (writer);
WriteClassClose (writer);
Expand Down Expand Up @@ -181,6 +182,28 @@ static void WriteConstructors (JavaPeerInfo type, TextWriter writer)
}
}

static void WriteFields (JavaPeerInfo type, TextWriter writer)
{
foreach (var field in type.JavaFields) {
writer.Write ('\t');
writer.Write (field.Visibility);
writer.Write (' ');
if (field.IsStatic) {
writer.Write ("static ");
}
writer.Write (field.JavaTypeName);
writer.Write (' ');
writer.Write (field.FieldName);
writer.Write (" = ");
writer.Write (field.InitializerMethodName);
writer.WriteLine (" ();");
}

if (type.JavaFields.Count > 0) {
writer.WriteLine ();
}
}

static void WriteMethods (JavaPeerInfo type, TextWriter writer)
{
foreach (var method in type.MarshalMethods) {
Expand Down Expand Up @@ -214,13 +237,14 @@ static void WriteMethods (JavaPeerInfo type, TextWriter writer)

""");
} else {
string access = method.IsExport && method.JavaAccess != null ? method.JavaAccess : "public";
writer.Write ($$"""

public {{javaReturnType}} {{method.JniName}} ({{parameters}}){{throwsClause}}
{{access}} {{javaReturnType}} {{method.JniName}} ({{parameters}}){{throwsClause}}
{
{{returnPrefix}}{{method.NativeCallbackName}} ({{args}});
}
public native {{javaReturnType}} {{method.NativeCallbackName}} ({{parameters}});
{{access}} native {{javaReturnType}} {{method.NativeCallbackName}} ({{parameters}});

""");
}
Expand Down
51 changes: 51 additions & 0 deletions src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaPeerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ sealed record JavaPeerInfo
/// </summary>
public IReadOnlyList<JavaConstructorInfo> JavaConstructors { get; init; } = [];

/// <summary>
/// Java fields from [ExportField] attributes.
/// Each field is initialized by calling the annotated method.
/// </summary>
public IReadOnlyList<JavaFieldInfo> JavaFields { get; init; } = [];

/// <summary>
/// Information about the activation constructor for this type.
/// May reference a base type's constructor if the type doesn't define its own.
Expand Down Expand Up @@ -159,6 +165,19 @@ sealed record MarshalMethodInfo
/// </summary>
public bool IsConstructor { get; init; }

/// <summary>
/// True if this method comes from an [Export] attribute (rather than [Register]).
/// [Export] methods use the C# method's access modifier in the JCW Java file
/// instead of always being "public".
/// </summary>
public bool IsExport { get; init; }

/// <summary>
/// Java access modifier for [Export] methods ("public", "protected", "private").
/// Null for [Register] methods (always "public").
/// </summary>
public string? JavaAccess { get; init; }

/// <summary>
/// For [Export] methods: Java exception types that the method declares it can throw.
/// Null for [Register] methods.
Expand Down Expand Up @@ -210,6 +229,38 @@ sealed record JavaConstructorInfo
public string? SuperArgumentsString { get; init; }
}

/// <summary>
/// Describes a Java field from an [ExportField] attribute.
/// The field is initialized by calling the annotated method.
/// </summary>
sealed record JavaFieldInfo
{
/// <summary>
/// Java field name, e.g., "STATIC_INSTANCE".
/// </summary>
public required string FieldName { get; init; }

/// <summary>
/// Java type name for the field, e.g., "java.lang.String".
/// </summary>
public required string JavaTypeName { get; init; }

/// <summary>
/// Name of the method that initializes this field, e.g., "GetInstance".
/// </summary>
public required string InitializerMethodName { get; init; }

/// <summary>
/// Java access modifier ("public", "protected", "private").
/// </summary>
public required string Visibility { get; init; }

/// <summary>
/// Whether the field is static.
/// </summary>
public bool IsStatic { get; init; }
}

/// <summary>
/// Describes how to call the activation constructor for a Java peer type.
/// </summary>
Expand Down
Loading