Ejemplo n.º 1
0
        private static unsafe void TryExplicitlyCallJni()
        {
            Console.WriteLine("Part2: Check JNI Calls...");

            var vm = JniRuntime.CurrentRuntime;

            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 ();
             */
        }
Ejemplo n.º 2
0
        public unsafe void Dispose_Exceptions()
        {
            var t = new JniType("java/lang/Object");

            t.Dispose();
            Assert.Throws <ObjectDisposedException> (() => t.AllocObject());
            Assert.Throws <ObjectDisposedException> (() => t.NewObject(null, null));
            Assert.Throws <ObjectDisposedException> (() => t.GetConstructor(null));
            Assert.Throws <ObjectDisposedException> (() => t.GetInstanceField(null, null));
            Assert.Throws <ObjectDisposedException> (() => t.GetInstanceMethod(null, null));
            Assert.Throws <ObjectDisposedException> (() => t.GetStaticField(null, null));
            Assert.Throws <ObjectDisposedException> (() => t.GetStaticMethod(null, null));
            Assert.Throws <ObjectDisposedException> (() => t.GetSuperclass());
            Assert.Throws <ObjectDisposedException> (() => t.IsAssignableFrom(null));
            Assert.Throws <ObjectDisposedException> (() => t.IsInstanceOfType(new JniObjectReference()));
            Assert.Throws <ObjectDisposedException> (() => t.RegisterWithRuntime());
            Assert.Throws <ObjectDisposedException> (() => t.RegisterNativeMethods(null));
            Assert.Throws <ObjectDisposedException> (() => t.UnregisterNativeMethods());

            JniFieldInfo jif = null;

            Assert.Throws <ObjectDisposedException> (() => t.GetCachedInstanceField(ref jif, null, null));
            JniMethodInfo jim = null;

            Assert.Throws <ObjectDisposedException> (() => t.GetCachedConstructor(ref jim, null));
            Assert.Throws <ObjectDisposedException> (() => t.GetCachedInstanceMethod(ref jim, null, null));
            JniFieldInfo jsf = null;

            Assert.Throws <ObjectDisposedException> (() => t.GetCachedStaticField(ref jsf, null, null));
            JniMethodInfo jsm = null;

            Assert.Throws <ObjectDisposedException> (() => t.GetCachedStaticMethod(ref jsm, null, null));
        }
Ejemplo n.º 3
0
        protected override void Dispose(bool disposing)
        {
            if (!disposing || ReturnType == null)
            {
                return;
            }

            ReturnType.Dispose();
            ReturnType = null;
        }
Ejemplo n.º 4
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);
            }
        }