Skip to content
Merged
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
3 changes: 3 additions & 0 deletions build-tools/Java.Interop.Sdk/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

<Target Name="_BuildJavaCompileForManagedBinding"
DependsOnTargets="_CollectJavaCompileForManagedBindingInputs;_JavaCollectJavacRefs"
Condition=" '$(_JavaCompileForBindingInputs->Count())' != '0' "
Inputs="@(_JavaCompileForBindingInputs)"
Outputs="$(_JavaManagedBindingInput)">
<PropertyGroup>
Expand Down Expand Up @@ -105,6 +106,7 @@

<Target Name="_GenerateApiDescription"
DependsOnTargets="_CollectClassParseInputs"
Condition=" '$(_ClassParseInputs->Count())' != '0' "
Inputs="@(_ClassParseInputs)"
Outputs="$(_JavaManagedBindingDir)api.xml">
<MakeDir Directories="$(_JavaManagedBindingDir)" />
Expand All @@ -120,6 +122,7 @@

<Target Name="_GenerateManagedBinding"
DependsOnTargets="_GenerateApiDescription"
Condition=" Exists('$(_JavaManagedBindingDir)api.xml') "
Inputs="$(_JavaManagedBindingDir)api.xml"
Outputs="$(_JavaManagedBindingDir)$(AssemblyName).projitems">
<MakeDir Directories="$(_JavaManagedBindingDir)" />
Expand Down
10 changes: 10 additions & 0 deletions samples/Hello-Java.Base/Hello-Java.Base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<Nullable>enable</Nullable>
<StartupObject>Hello.App</StartupObject>
</PropertyGroup>

<Import Project="..\..\build-tools\Java.Interop.Sdk\Sdk\Sdk.props" />
<!-- Currently needs to be included so that Sdk.targets can find things -->
<Import Project="..\..\TargetFrameworkDependentValues.props" />

<PropertyGroup>
<OutputPath>$(MSBuildThisFileDirectory)bin\$(Configuration)\</OutputPath>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Java.Interop\Java.Interop.csproj" />
Expand All @@ -15,4 +23,6 @@
<ProjectReference Include="..\..\tests\TestJVM\TestJVM.csproj" />
</ItemGroup>

<Import Project="..\..\build-tools\Java.Interop.Sdk\Sdk\Sdk.targets" />

</Project>
18 changes: 16 additions & 2 deletions samples/Hello-Java.Base/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.Threading;

using Mono.Options;
Expand All @@ -26,7 +29,7 @@ public static void Main (string[] args)
$"{{PATH}} to JVM to use. Default is:\n {jvmPath}",
v => jvmPath = v },
{ "m",
"Create multiple Java VMs. This will likely creash.",
"Create multiple Java VMs. This will likely crash.",
v => createMultipleVMs = v != null },
{ "t",
$"Timing; invoke Object.hashCode() {N} times, print average.",
Expand All @@ -43,6 +46,12 @@ public static void Main (string[] args)
var builder = new JreRuntimeOptions () {
JniAddNativeMethodRegistrationAttributePresent = true,
JvmLibraryPath = jvmPath,
ClassPath = {
Path.Combine (Path.GetDirectoryName (typeof (App).Assembly.Location)!, "Hello-Java.Base.jar"),
},
TypeMappings = {
[MyJLO.JniTypeName] = typeof (MyJLO),
},
};
builder.AddOption ("-Xcheck:jni");

Expand All @@ -63,7 +72,7 @@ public static void Main (string[] args)

static void CreateJLO ()
{
var jlo = new Java.Lang.Object ();
var jlo = new MyJLO ();
Console.WriteLine ($"binding? {jlo.ToString ()}");
}

Expand Down Expand Up @@ -115,4 +124,9 @@ static unsafe void CreateAnotherJVM ()
}
}
}

[JniTypeSignature (JniTypeName)]
class MyJLO : Java.Lang.Object {
internal const string JniTypeName = "net/dot/jni/sample/MyJLO";
}
}
5 changes: 4 additions & 1 deletion src/Java.Runtime.Environment/Java.Interop/JreRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class JreRuntimeOptions : JniRuntime.CreationOptions {
public TextWriter? JniGlobalReferenceLogWriter {get; set;}
public TextWriter? JniLocalReferenceLogWriter {get; set;}

internal Dictionary<string, Type>? typeMappings;
public IDictionary<string, Type> TypeMappings => typeMappings ??= new ();

internal JvmLibraryHandler? LibraryHandler {get; set;}

public JreRuntimeOptions ()
Expand Down Expand Up @@ -85,7 +88,7 @@ static unsafe JreRuntimeOptions CreateJreVM (JreRuntimeOptions builder)
builder.LibraryHandler = JvmLibraryHandler.Create ();

#if NET
builder.TypeManager ??= new JreTypeManager ();
builder.TypeManager ??= new JreTypeManager (builder.typeMappings);
#endif // NET

bool onMono = Type.GetType ("Mono.Runtime", throwOnError: false) != null;
Expand Down
38 changes: 38 additions & 0 deletions src/Java.Runtime.Environment/Java.Interop/JreTypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,44 @@ namespace Java.Interop {

public class JreTypeManager : JniRuntime.JniTypeManager {

IDictionary<string, Type>? typeMappings;

public JreTypeManager ()
: this (null)
{
}

public JreTypeManager (IDictionary<string, Type>? typeMappings)
{
this.typeMappings = typeMappings;
}

protected override IEnumerable<Type> GetTypesForSimpleReference (string jniSimpleReference)
{
foreach (var t in base.GetTypesForSimpleReference (jniSimpleReference))
yield return t;
if (typeMappings == null)
yield break;
if (typeMappings.TryGetValue (jniSimpleReference, out var target))
yield return target;
}

protected override IEnumerable<string> GetSimpleReferences (Type type)
{
return base.GetSimpleReferences (type)
.Concat (CreateSimpleReferencesEnumerator (type));
}

IEnumerable<string> CreateSimpleReferencesEnumerator (Type type)
{
if (typeMappings == null)
yield break;
foreach (var e in typeMappings) {
if (e.Value == type)
yield return e.Key;
}
}

const string NotUsedInAndroid = "This code path is not used in Android projects.";

public override void RegisterNativeMembers (
Expand Down
49 changes: 8 additions & 41 deletions tests/TestJVM/TestJVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public TestJVMOptions (Assembly? callingAssembly = null)

public ICollection<string> JarFilePaths {get;} = new List<string> ();
public Assembly CallingAssembly {get; set;}
public Dictionary<string, Type>? TypeMappings {get; set;}

internal JdkInfo? JdkInfo {get; set;}
}
Expand Down Expand Up @@ -141,9 +140,13 @@ public TestJVM (string[]? jars = null, Dictionary<string, Type>? typeMappings =
static TestJVMOptions CreateOptions (string[]? jarFiles, Assembly callingAssembly, Dictionary<string, Type>? typeMappings)
{
var o = new TestJVMOptions {
TypeMappings = typeMappings,
CallingAssembly = callingAssembly,
};
if (typeMappings != null) {
foreach (var e in typeMappings) {
o.TypeMappings.Add (e.Key, e.Value);
}
}
if (jarFiles != null) {
foreach (var jar in jarFiles) {
o.JarFilePaths.Add (jar);
Expand All @@ -153,48 +156,12 @@ static TestJVMOptions CreateOptions (string[]? jarFiles, Assembly callingAssembl
}
}

public class TestJvmTypeManager :
#if NET
JreTypeManager
#else // !NET
JniRuntime.JniTypeManager
#endif // !NET
public class TestJvmTypeManager : JreTypeManager
{

Dictionary<string, Type>? typeMappings;

public TestJvmTypeManager (Dictionary<string, Type>? typeMappings)
public TestJvmTypeManager (IDictionary<string, Type>? typeMappings)
: base (typeMappings)
{
this.typeMappings = typeMappings;
}

protected override IEnumerable<Type> GetTypesForSimpleReference (string jniSimpleReference)
{
foreach (var t in base.GetTypesForSimpleReference (jniSimpleReference))
yield return t;
if (typeMappings == null)
yield break;
Type target;
#pragma warning disable CS8600 // huh?
if (typeMappings.TryGetValue (jniSimpleReference, out target))
yield return target;
#pragma warning restore CS8600
}

protected override IEnumerable<string> GetSimpleReferences (Type type)
{
return base.GetSimpleReferences (type)
.Concat (CreateSimpleReferencesEnumerator (type));
}

IEnumerable<string> CreateSimpleReferencesEnumerator (Type type)
{
if (typeMappings == null)
yield break;
foreach (var e in typeMappings) {
if (e.Value == type)
yield return e.Key;
}
}
}
}
Expand Down