Exemple #1
0
 public virtual void Setup()
 {
     var sp = new FileIOPermission(PermissionState.Unrestricted);
     sp.Assert();
     string prefix;
     if (Environment.CurrentDirectory.EndsWith("target"))
     {
         prefix = "../../";
     }
     else
     {
         prefix = "../../../";
     }
     sp.Deny();
     Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
     BridgeSetup setup=new BridgeSetup (false){Verbose = true, Debug = true};
     setup.IgnoreJavaHome = true;
     setup.AddJVMOption("-Xmx512m");
     setup.AddClassPath(prefix + "jni4net.j/target/classes");
     setup.AddClassPath(prefix + "jni4net.tested.j/target/classes");
     setup.AddClassPath(prefix + "jni4net.test.j/target/test-classes");
     //setup.JavaHome = @"c:\Program Files (x86)\Java\ibm_sdk60";
     env = Bridge.CreateJVM(setup);
     Bridge.RegisterAssembly(typeof(TestBase).Assembly);
     Bridge.RegisterAssembly(typeof(JavaInstanceFields).Assembly);
 }
Exemple #2
0
 public JNIResult AttachCurrentThreadAsDaemon(out JNIEnv penv, JavaVMInitArgs? args)
 {
     if (attachCurrentThreadAsDaemon == null)
     {
         MethodWrapper.GetDelegateForFunctionPointer(functions.AttachCurrentThreadAsDaemon,
                                                     ref attachCurrentThreadAsDaemon);
     }
     IntPtr env;
     JNIResult result;
     if (args.HasValue)
     {
         JavaVMInitArgs value = args.Value;
         result = attachCurrentThreadAsDaemon.Invoke(native, out env, &value);
     }
     else
     {
         result = attachCurrentThreadAsDaemon.Invoke(native, out env, null);
     }
     if (result == JNIResult.JNI_OK)
     {
         penv = new JNIEnv(env);
     }
     else
     {
         penv = null;
     }
     return result;
 }
 public static unsafe void Unregister(Class jvmProxy, JNIEnv env)
 {
     JNIResult res = env.UnregisterNatives(jvmProxy);
     if (res != JNIResult.JNI_OK)
     {
         throw new JNIException("Can't unbind native methods to class " + jvmProxy);
     }
 }
 internal static void Init(JNIEnv env)
 {
     if (Bridge.Setup.BindNative)
     {
         MethodInfo initializer = Registry.GetWrapperInitializer(typeof(__Bridge), "__Init2");
         RegistryRecord record = Registry.GetCLRRecord(typeof(Bridge));
         Registry.RegisterNative(initializer, env, record.JVMProxy, record.JVMInterface);
     }
 }
Exemple #5
0
 internal static object GetObject(JNIEnv env, JniHandle obj)
 {
     long handle = getClrHandle(env, obj);
     if (handle==0)
     {
         return null;
     }
     object real = IntHandle.ToObject(handle);
     return real;
 }
Exemple #6
0
 public virtual void Setup()
 {
     Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
     Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
     var setup = new BridgeSetup(false) { Verbose = true, Debug = true };
     string prefix;
     if (Environment.CurrentDirectory.EndsWith("target"))
     {
         prefix = "../../";
     }
     else
     {
         prefix = "../../../";
     }
     string userHome = Environment.GetEnvironmentVariable("USERPROFILE");
     string version = typeof (BridgeSetup).Assembly.GetName().Version.ToString();
     setup.AddClassPath(userHome + @"/.m2/repository/org/picocontainer/picocontainer/2.6/picocontainer-2.6.jar");
     setup.AddClassPath(userHome + @"/.m2/repository/net/sf/jni4net/jni4net.j/" + version + "/jni4net.j-" +
                        version + ".jar");
     setup.AddClassPath(prefix + "../../robocode.api/target/Test-classes");
     setup.AddClassPath(prefix + "../../robocode.api/target/classes");
     setup.AddClassPath(prefix + "../../robocode.battle/target/Test-classes");
     setup.AddClassPath(prefix + "../../robocode.battle/target/classes");
     setup.AddClassPath(prefix + "../../robocode.core/target/Test-classes");
     setup.AddClassPath(prefix + "../../robocode.core/target/classes");
     setup.AddClassPath(prefix + "../../robocode.host/target/Test-classes");
     setup.AddClassPath(prefix + "../../robocode.host/target/classes");
     setup.AddClassPath(prefix + "../../robocode.repository/target/Test-classes");
     setup.AddClassPath(prefix + "../../robocode.repository/target/classes");
     setup.AddClassPath(prefix + "../../robocode.roborumble/target/Test-classes");
     setup.AddClassPath(prefix + "../../robocode.roborumble/target/classes");
     setup.AddClassPath(prefix + "../../robocode.samples/target/Test-classes");
     setup.AddClassPath(prefix + "../../robocode.samples/target/classes");
     setup.AddClassPath(prefix + "../../robocode.sound/target/Test-classes");
     setup.AddClassPath(prefix + "../../robocode.sound/target/classes");
     setup.AddClassPath(prefix + "../../robocode.tests/target/Test-classes");
     setup.AddClassPath(prefix + "../../robocode.tests/target/classes");
     setup.AddClassPath(prefix + "../../robocode.ui/target/Test-classes");
     setup.AddClassPath(prefix + "../../robocode.ui/target/classes");
     setup.AddClassPath(prefix + "../../robocode.ui.editor/target/Test-classes");
     setup.AddClassPath(prefix + "../../robocode.ui.editor/target/classes");
     setup.AddClassPath(prefix + "robocode.dotnet.api/target/Test-classes");
     setup.AddClassPath(prefix + "robocode.dotnet.api/target/classes");
     setup.AddClassPath(prefix + "robocode.dotnet.host/target/Test-classes");
     setup.AddClassPath(prefix + "robocode.dotnet.host/target/classes");
     setup.AddClassPath(prefix + "robocode.dotnet.nhost/target/Test-classes");
     setup.AddClassPath(prefix + "robocode.dotnet.nhost/target/classes");
     setup.AddClassPath(prefix + "robocode.dotnet.samples/target/Test-classes");
     setup.AddClassPath(prefix + "robocode.dotnet.samples/target/classes");
     setup.AddClassPath(prefix + "robocode.dotnet.tests/target/Test-classes");
     setup.AddClassPath(prefix + "robocode.dotnet.tests/target/classes");
     env = Bridge.CreateJVM(setup);
     Bridge.RegisterAssembly(typeof (TestBase).Assembly);
     Bridge.RegisterAssembly(typeof (DllRootHelper).Assembly);
 }
Exemple #7
0
 public static RegistryRecord GetRecord(JNIEnv env, JniHandle obj, Class iface)
 {
     lock (typeof (Registry))
     {
         RegistryRecord res;
         if (knownJVM.TryGetValue(iface, out res))
         {
             return res;
         }
         return ResolveNew(iface);
     }
 }
Exemple #8
0
 public static void DetachCurrentThread()
 {
     if (threadJNIEnv != null)
     {
         JNIResult result = defaultVM.DetachCurrentThread();
         if (result != JNIResult.JNI_OK)
         {
             throw new JNIException("DetachCurrentThread failed: " + result);
         }
         threadJNIEnv = null;
     }
 }
 public static unsafe void Register(List<JNINativeMethod> registrations, Class jvmProxy, JNIEnv env)
 {
     JNINativeMethod[] methods = registrations.ToArray();
     fixed (JNINativeMethod* m = &(methods[0]))
     {
         JNIResult res = env.RegisterNatives(jvmProxy, m, methods.Length);
         if (res!=JNIResult.JNI_OK)
         {
             throw new JNIException("Can't bind native methods to class " + jvmProxy +" is it in system classloader ?");
         }
     }
 }
Exemple #10
0
        public static void CreateJavaVM(out JavaVM jvm, out JNIEnv env, bool attachIfExists, params string[] options)
        {
            Init();
            IntPtr njvm;
            IntPtr nenv;
            var args = new JavaVMInitArgs();
            args.version = JNI_VERSION_1_4;

            if (options.Length > 0)
            {
                args.nOptions = options.Length;
                var opt = new JavaVMOption[options.Length];
                for (int i = 0; i < options.Length; i++)
                {
                    opt[i].optionString = Marshal.StringToHGlobalAnsi(options[i]);
                }
                fixed (JavaVMOption* a = &opt[0])
                {
                    args.options = a;
                }
            }
            JNIResult result;
            if (attachIfExists)
            {
                IntPtr njvma;
                int count;
                result = Dll.JNI_GetCreatedJavaVMs(out njvma, 1, out count);
                if (result != JNIResult.JNI_OK)
                {
                    throw new JNIException("Can't enumerate current JVMs " + result);
                }
                if (count > 0)
                {
                    njvm = njvma;
                    jvm = new JavaVM(njvm);
                    result = jvm.AttachCurrentThread(out env, args);
                    if (result != JNIResult.JNI_OK)
                    {
                        throw new JNIException("Can't join current JVM " + result);
                    }
                    return;
                }
            }
            result = Dll.JNI_CreateJavaVM(out njvm, out nenv, &args);
            if (result != JNIResult.JNI_OK)
            {
                Console.Error.WriteLine("Can't load JVM (already have one ?)");
                throw new JNIException("Can't load JVM (already have one ?) " + result);
            }
            jvm = new JavaVM(njvm);
            env = new JNIEnv(nenv);
        }
Exemple #11
0
 public JNIResult AttachCurrentThread(out JNIEnv penv, JavaVMInitArgs? args)
 {
     if (attachCurrentThread == null)
     {
         MethodWrapper.GetDelegateForFunctionPointer(functions.AttachCurrentThread, ref attachCurrentThread);
     }
     IntPtr env;
     JNIResult result;
     if (args.HasValue)
     {
         JavaVMInitArgs initArgs = args.Value;
         result = attachCurrentThread.Invoke(native, out env, &initArgs);
     }
     else
     {
         result = attachCurrentThread.Invoke(native, out env, null);
     }
     penv = new JNIEnv(env);
     return result;
 }
Exemple #12
0
 private static bool CheckAlreadyLoaded(BridgeSetup newSetup, JNIEnv env)
 {
     if (clrLoaded)
     {
         return true;
     }
     if (newSetup.BindNative)
     {
         JniLocalHandle br = env.FindClassPtrNoThrow("net/sf/jni4net/Bridge");
         if (JniLocalHandle.IsNull(br))
         {
             throw new JNIException("Can't find class net.sf.jni4net.Bridge on classpath");
         }
         IntPtr ir = env.GetStaticFieldIDPtr(br, "isRegistered", "Z");
         bool isRegistered = env.GetStaticBooleanField(br, ir);
         if (isRegistered)
         {
             // rather neat. When called like Clr->Java->Clr this dll gets loaded twice.
             // which means two separate sets CLR of classes and static members
             if (newSetup.Verbose)
             {
                 Console.WriteLine("Already initialized jni4net core before");
             }
             return true;
         }
     }
     return false;
 }
Exemple #13
0
        internal static void BindCore(JNIEnv env, BridgeSetup newSetup)
        {
            if (CheckAlreadyLoaded(newSetup, env))
            {
                return;
            }

            if (newSetup.Verbose)
            {
                var homeDll = new Uri(typeof(Bridge).Assembly.GetName().CodeBase).AbsolutePath;
                Console.WriteLine("loading core from " + homeDll);
            }
            setup = newSetup;
            if (!setup.BindCoreOnly)
            {
                RegisterAssembly(typeof (Bridge).Assembly);
            }
            __Bridge.Init(env);

            if (Setup.Verbose)
            {
                Console.WriteLine("Initialized jni4net core");
            }
            if (Setup.BindNative)
            {
                JniLocalHandle br = env.FindClassPtr("net/sf/jni4net/Bridge");
                IntPtr ir = env.GetStaticFieldIDPtr(br, "isRegistered", "Z");
                env.SetStaticBooleanField(br, ir, true);
                if (!env.GetStaticBooleanField(br, ir))
                {
                    Console.Error.WriteLine("Can't mark bridge");
                }
                Ref.Init();
                Out.Init();
            }
            if (Setup.Verbose)
            {
                var homeDll = new Uri(typeof(Bridge).Assembly.GetName().CodeBase).AbsolutePath;
                Console.WriteLine("core loaded from " + homeDll);
            }

            AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload;

            clrLoaded = true;
        }
 private static List<JNINativeMethod> __Init2(JNIEnv @__env, Class @__class)
 {
     Type @__type = typeof (__Bridge);
     var methods = new List<JNINativeMethod>();
     methods.Add(JNINativeMethod.Create(@__type, "WrapJVM", "WrapJVM", "(Ljava/lang/Object;)J"));
     methods.Add(JNINativeMethod.Create(@__type, "Convert", "Convert", "(Ljava/lang/String;)J"));
     methods.Add(JNINativeMethod.Create(@__type, "UnwrapJVM", "UnwrapJVM", "(J)Ljava/lang/Object;"));
     methods.Add(JNINativeMethod.Create(@__type, "DisposeClrHandle", "DisposeClrHandle", "(J)V"));
     methods.Add(JNINativeMethod.Create(@__type, "Cast", "Cast", "(Ljava/lang/Object;Ljava/lang/Class;)Lnet/sf/jni4net/inj/IClrProxy;"));
     return methods;
 }
Exemple #15
0
 private static long getClrHandle(JNIEnv env, JniHandle obj)
 {
     return env.CallLongMethod(obj, _getClrHandle0);
 }
 private static void RegisterClass(RegistryRecord record, ClassLoader classLoader, JNIEnv env)
 {
     string package = record.CLRInterface.Namespace;
     string className = record.CLRInterface.Name;
     string interfaceName = record.JVMName;
     string proxyName;
     string staticName;
     if (record.IsJVMClass)
     {
         staticName = GetStaticName(package, className, true);
         proxyName = null;
     }
     else if (record.IsInterface)
     {
         proxyName = GetProxyName(package, className, true);
         staticName = GetStaticName(package, className, true);
     }
     else if (record.IsDelegate)
     {
         proxyName = GetProxyName(package, className, true);
         staticName = interfaceName;
     }
     else
     {
         proxyName = interfaceName;
         staticName = interfaceName;
     }
     if (Bridge.Setup.BindCLRTypes || record.IsJVMClass)
     {
         record.JVMInterface = LoadClass(interfaceName, classLoader, env);
     }
     if (Bridge.Setup.BindStatic)
     {
         record.JVMStatic = LoadClass(staticName, classLoader, env);
         if (proxyName != null)
         {
             record.JVMProxy = LoadClass(proxyName, classLoader, env);
             record.JVMConstructor = GetJVMConstructor(env, record.JVMProxy);
             knownJVMProxies[record.JVMProxy] = record;
             knownJVM[record.JVMProxy] = record;
         }
     }
 }
Exemple #17
0
 internal static IClrProxy CreateProxy(JNIEnv env, JniLocalHandle obj)
 {
     IClrProxy proxy = new __IClrProxy(env);
     proxy.Init(env, obj);
     return proxy;
 }
 private static void BindJvm(RegistryRecord record, ClassLoader classLoader, JNIEnv env)
 {
     RegisterClass(record, classLoader, env);
     if (record.CLRProxy != null)
     {
         if (record.IsJVMClass || Bridge.Setup.BindCLRTypes)
         {
             RegisterStaticAndMethods(record, env);
             if (record.IsDelegate)
             {
                 MethodInfo methodInfo = record.CLRInterface.GetMethod("Invoke");
                 ParameterInfo[] parameterInfo = methodInfo.GetParameters();
                 Type[] param=new Type[parameterInfo.Length];
                 for (int i = 0; i < param.Length; i++)
                 {
                     param[i] = parameterInfo[i].ParameterType;
                 }
                 record.JVMDelegateInvoke = record.CLRProxy.GetMethod("Invoke", param);
             }
         }
         if (initialized && Bridge.Setup.BindStatic)
         {
             RegisterTypeOf(record, env);
         }
     }
     if (initialized)
     {
         if (Bridge.Setup.BindNative && record.CLRWrapper != null)
         {
             RegisterNative(record.CLRWrapperInitMethod, env, record.JVMProxy, record.JVMInterface);
         }
         RegisterClassToMap(record);
         record.JVMBound = true;
     }
 }
Exemple #19
0
 public static void RegisterType(Type type, bool bindJVM, JNIEnv env)
 {
     RegisterType(type, bindJVM, env, null);
 }
Exemple #20
0
 public LocalFrame(JNIEnv env)
     : this(env, 10)
 {
 }
Exemple #21
0
 public static void CreateJavaVM(out JavaVM jvm, out JNIEnv env, params string[] options)
 {
     CreateJavaVM(out jvm, out env, false, options);
 }
 private static void BindJvm(RegistryRecord record, JNIEnv env)
 {
     BindJvm(record, null, env);
 }
Exemple #23
0
 public static void CreateJavaVM(out JavaVM jvm, out JNIEnv env, params string[] options)
 {
     CreateJavaVM(out jvm, out env, false, options);
 }
 private static void RegisterType(Type type, bool bindJVM, ClassLoader classLoader, JNIEnv env)
 {
     if (Bridge.Setup.VeryVerbose)
       {
     Console.WriteLine("Registration : " + type.FullName);
       }
       RegistryRecord record = null;
       RegisterWrapper(type, ref record);
       RegisterInterfaceProxy(type, ref record);
       RegisterClassProxy(type, ref record);
       if (record != null)
       {
     if (bindJVM && !record.JVMBound)
     {
       BindJvm(record, classLoader, env);
     }
       }
 }
Exemple #25
0
 public JNIResult GetEnv(out JNIEnv penv, int version)
 {
     if (getEnv == null)
     {
         MethodWrapper.GetDelegateForFunctionPointer(functions.GetEnv, ref getEnv);
     }
     IntPtr env;
     JNIResult result = getEnv.Invoke(native, out env, version);
     penv = new JNIEnv(env);
     return result;
 }
Exemple #26
0
 // This constructor is a requirement for all CLR classes that inherit from java.lang.Object
 protected MainActivity(net.sf.jni4net.jni.JNIEnv env) : base(env)
 {
 }
Exemple #27
0
        internal JNIResult AttachCurrentThread(out JNIEnv penv, JavaVMInitArgs? args)
        {
            if (attachCurrentThread == null)
            {
                MethodWrapper.GetDelegateForFunctionPointer(functions.AttachCurrentThread, ref attachCurrentThread);
            }
            IntPtr env;
            JNIResult result;

            //DC: using Patch_IgnoreArgsInAttach to handle the prob with attaching existing VMs
            if (BridgeSetup.Patch_IgnoreArgsInAttach == false && args.HasValue)
            {
                JavaVMInitArgs initArgs = args.Value;
                result = attachCurrentThread.Invoke(native, out env, &initArgs);
            }
            else
            {
                result = attachCurrentThread.Invoke(native, out env, null);
            }
            penv = new JNIEnv(env);
            return result;
        }
        private static Class LoadClass(string name, ClassLoader classLoader, JNIEnv env)
        {
            Class res = null;
            if (classLoader == null)
            {
                classLoader = systemClassLoader;
                string rn = name.Replace('.', '/');
                res = env.FindClassNoThrow(rn);
            }
            if (classLoader != null && res==null)
            {
                try
                {
                    res = classLoader.loadClass(name);
                }
                catch (Throwable th)
                {
                    throw new JNIException("Can't load java class for " + name + " from classLoader " + classLoader, th);
                }
            }

            return res;
        }
Exemple #29
0
 public LocalFrame(JNIEnv env, int size)
 {
     this.env = env;
     env.PushLocalFrame(size);
 }
Exemple #30
0
 private __Object(JNIEnv env)
     : base(env)
 {
 }
Exemple #31
0
 internal static String CreateProxy(JNIEnv env, JniLocalHandle obj)
 {
     var proxy = new String(env);
     ((IJvmProxy) proxy).Init(env, obj);
     return proxy;
 }
Exemple #32
0
 private static Type IsCLR(Class iface, JniHandle obj, JNIEnv env)
 {
     if (IClrProxy_._class.isAssignableFrom(iface))
     {
         object real = __IClrProxy.GetObject(env, obj);
         return real.GetType();
     }
     return null;
 }