Example #1
0
 private static void ListInvocationPoints(string type)
 {
     foreach (var h in JniRuntime.GetAvailableInvocationPointers())
     {
         Console.WriteLine("{1}: GetCreatedJavaVMs: {0}", h, type);
     }
 }
Example #2
0
        public static unsafe void Main(string[] args)
        {
            JreRuntime.Initialize("/usr/lib/jvm/java-11-openjdk-amd64/lib/server/libjvm.so");
            Console.WriteLine("Hello World!");
            bool       hosted = false;
            JniRuntime vm     = null;

            try
            {
                vm     = JniRuntime.CurrentRuntime;
                hosted = vm != null;
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e);
            }

            ListInvocationPoints("PRE");

            if (vm == null)
            {
                vm = new JreRuntimeOptions().CreateJreVM();
            }

            TryExplicitlyCallJni();
            ListInvocationPoints("WITHIN");
            TryImplicitlyInteroperate();

            if (!hosted)
            {
                vm.Dispose();
            }
            ListInvocationPoints("POST");
        }
Example #3
0
        static JavaVMFixture()
        {
            var c = new TestJVM(
                );

            JniRuntime.SetCurrent(c);
        }
Example #4
0
        static JavaVMFixture()
        {
            var c = new TestJVM(
                jars:           new[] { "export-test.jar" }
                );

            JniRuntime.SetCurrent(c);
        }
Example #5
0
        static JavaVMFixture()
        {
            var c = new TestJVM(
                jars:           new[] { "performance-test.jar" }
                );

            JniRuntime.SetCurrent(c);
        }
 static JniRuntime.CreationOptions CreateOptions(JniRuntime proxy)
 {
     return(new JniRuntime.CreationOptions {
         DestroyRuntimeOnDispose = false,
         InvocationPointer = proxy.InvocationPointer,
         MarshalMemberBuilder = new ProxyMarshalMemberBuilder(),
         ObjectReferenceManager = new ProxyObjectReferenceManager(),
         ValueManager = new ProxyValueManager(),
         TypeManager = new ProxyTypeManager(),
     });
 }
Example #7
0
        static partial void CreateJavaVM()
        {
            var c = new TestJVM(
                jars:           new[] { "interop-test.jar" },
                typeMappings:   new Dictionary <string, Type> ()
            {
                { TestType.JniTypeName, typeof(TestType) },
                { GenericHolder <int> .JniTypeName, typeof(GenericHolder <>) },
            }
                );

            JniRuntime.SetCurrent(c);
        }
Example #8
0
        public static void Main(string[] args)
        {
            string?jvmPath           = global::Java.InteropTests.TestJVM.GetJvmLibraryPath();
            bool   createMultipleVMs = false;
            bool   showHelp          = false;
            var    options           = new OptionSet()
            {
                "Using the JVM from C#!",
                "",
                "Options:",
                { "jvm=",
                  $"{{PATH}} to JVM to use.  Default is:\n  {jvmPath}",
                  v => jvmPath = v },
                { "m",
                  "Create multiple Java VMs.  This will likely creash.",
                  v => createMultipleVMs = v != null },
                { "h|help",
                  "Show this message and exit.",
                  v => showHelp = v != null },
            };

            options.Parse(args);
            if (showHelp)
            {
                options.WriteOptionDescriptions(Console.Out);
                return;
            }
            Console.WriteLine("Hello World!");
            var builder = new JreRuntimeOptions()
            {
                JniAddNativeMethodRegistrationAttributePresent = true,
                JvmLibraryPath = jvmPath,
            };

            builder.AddOption("-Xcheck:jni");
            var jvm = builder.CreateJreVM();

            Console.WriteLine($"JniRuntime.CurrentRuntime == jvm? {ReferenceEquals (JniRuntime.CurrentRuntime, jvm)}");
            foreach (var h in JniRuntime.GetAvailableInvocationPointers())
            {
                Console.WriteLine("PRE: GetCreatedJavaVMHandles: {0}", h);
            }

            CreateJLO();

            if (createMultipleVMs)
            {
                CreateAnotherJVM();
            }
        }
Example #9
0
        public static unsafe void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            try {
                var ignore = JniRuntime.CurrentRuntime;
            } catch (InvalidOperationException e) {
                Console.WriteLine(e);
            }
            foreach (var h in JniRuntime.GetAvailableInvocationPointers())
            {
                Console.WriteLine("PRE: GetCreatedJavaVMHandles: {0}", h);
            }
            Console.WriteLine("Part 2!");
            using (var vm = new JreRuntimeOptions().CreateJreVM()) {
                Console.WriteLine("# JniEnvironment.EnvironmentPointer={0}", JniEnvironment.EnvironmentPointer);
                Console.WriteLine("vm.SafeHandle={0}", vm.InvocationPointer);
                var t = new JniType("java/lang/Object");
                var c = t.GetConstructor("()V");
                var o = t.NewObject(c, null);
                var m = t.GetInstanceMethod("hashCode", "()I");
                int i = JniEnvironment.InstanceMethods.CallIntMethod(o, m);
                Console.WriteLine("java.lang.Object={0}", o);
                Console.WriteLine("hashcode={0}", i);
                JniObjectReference.Dispose(ref o);
                t.Dispose();
                // var o = JniTypes.FindClass ("java/lang/Object");

                /*
                 * var waitForCreation = new CountdownEvent (1);
                 * var exitThread = new CountdownEvent (1);
                 * var t = new Thread (() => {
                 *      var vm2 = new JavaVMBuilder ().CreateJavaVM ();
                 *      waitForCreation.Signal ();
                 *      exitThread.Wait ();
                 * });
                 * t.Start ();
                 * waitForCreation.Wait ();
                 */
                foreach (var h in JniRuntime.GetAvailableInvocationPointers())
                {
                    Console.WriteLine("WITHIN: GetCreatedJavaVMs: {0}", h);
                }
                // exitThread.Signal ();
            }
            foreach (var h in JniRuntime.GetAvailableInvocationPointers())
            {
                Console.WriteLine("POST: GetCreatedJavaVMs: {0}", h);
            }
        }
        static partial void CreateJavaVM()
        {
            var c = new TestJVM(
                jars:           new[] { "interop-test.jar" },
                typeMappings:   new Dictionary <string, Type> ()
            {
#if !NO_MARSHAL_MEMBER_BUILDER_SUPPORT
                { TestType.JniTypeName, typeof(TestType) },
#endif  // !NO_MARSHAL_MEMBER_BUILDER_SUPPORT
                { GenericHolder <int> .JniTypeName, typeof(GenericHolder <>) },
            }
                );

            JniRuntime.SetCurrent(c);
        }
        public void Dispose_ClearsJniEnvironment()
        {
            var        c = JniRuntime.CurrentRuntime;
            JniRuntime r = null;
            var        t = new Thread(() => {
                r = new JniProxyRuntime(c);
                JniRuntime.SetCurrent(r);
                Assert.AreEqual(r, JniEnvironment.Runtime);
                r.Dispose();
                Assert.Throws <NotSupportedException>(() => {
                    var env = JniEnvironment.Runtime;
                });
            });

            t.Start();
            t.Join();
            Assert.IsNotNull(r);
            JniRuntime.SetCurrent(c);
        }
 public JniProxyRuntime(JniRuntime proxy)
     : base(CreateOptions(proxy))
 {
     Proxy = proxy;
 }
 public void GetRegisteredJavaVM_ExistingInstance()
 {
     Assert.AreEqual(JniRuntime.CurrentRuntime, JniRuntime.GetRegisteredRuntime(JniRuntime.CurrentRuntime.InvocationPointer));
 }