private static void RegisterClass(RegistryRecord record, 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 (record.IsJVMClass) { record.JVMInterface = LoadClass(interfaceName, env, false); } }
private static void RegisterProxy(Type proxyType, Type interfaceType, ref RegistryRecord record) { if (record == null) { if (!knownCLRProxies.TryGetValue(proxyType, out record)) { if (!knownCLRInterfaces.TryGetValue(interfaceType, out record)) { record = new RegistryRecord { CLRAssembly = proxyType.Assembly, IsInterface = interfaceType.IsInterface }; } } } record.CLRInterface = interfaceType; record.CLRProxy = proxyType; record.CLRConstructor = GetConstructor(proxyType); record.CLRProxyInitMethod = GetProxyInitializer(proxyType); record.CLRName = interfaceType.FullName; knownCLRProxies[proxyType] = record; knownCLRInterfaces[interfaceType] = record; knownCLR[interfaceType] = record; knownCLR[proxyType] = record; }
private static void RegisterPrimitiveType(string clazzName, Type type, Type jvmBoxed) { var record = new RegistryRecord(); Class clazz = Class.getPrimitiveClass(clazzName); record.JVMInterface = clazz; record.IsJVMClass = true; record.CLRInterface = type; record.IsCLRType = true; record.JVMName = clazzName; record.CLRName = type.Name; if (jvmBoxed != null) { RegistryRecord jvmBoxedRec = knownCLR[jvmBoxed]; record.JVMConstructor = jvmBoxedRec.JVMConstructor; record.JVMProxy = jvmBoxedRec.JVMProxy; record.JVMStatic = jvmBoxedRec.JVMStatic; record.JVMBound = jvmBoxedRec.JVMBound; } knownJVMInterfaces[clazz] = record; knownJVM[clazz] = record; //knownCLRInterfaces[record.CLRInterface] = record; //knownCLR[record.CLRInterface] = record; }
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; } }
private static void RegisterStaticAndMethods(RegistryRecord record, JNIEnv env) { try { record.CLRProxyInitMethod.Invoke(null, new object[] { env, record.JVMInterface }); } catch (Exception ex) { throw new JNIException("Can't initialize proxy " + record.CLRName, ex); } }
public static TRes StrongJ2Cp <TRes>(JNIEnv env, JniLocalHandle obj) where TRes : IJvmProxy { if (JniHandle.IsNull(obj)) { return(default(TRes)); } Class clazz = env.GetObjectClass(obj); RegistryRecord record = Registry.GetJVMRecord(clazz); return((TRes)record.CreateCLRProxy(env, obj)); }
private static void RegisterClassToMap(RegistryRecord record) { if (record.JVMInterface != null) { knownJVMInterfaces[record.JVMInterface] = record; knownJVM[record.JVMInterface] = record; } if (record.JVMStatic != null) { knownJVM[record.JVMStatic] = record; } }
public static JniHandle StrongC2Jp <TBoth>(JNIEnv env, TBoth obj) { // ReSharper disable CompareNonConstrainedGenericWithNull if (obj == null) { return(JniLocalHandle.Zero); } // ReSharper restore CompareNonConstrainedGenericWithNull RegistryRecord record = Registry.GetCLRRecord(obj.GetType()); return(record.CreateJVMProxy(env, obj)); }
private static void RegisterClassProxy(Type proxyType, ref RegistryRecord record) { JavaClassAttribute javaClassAttribute = GetJavaClassAttribute(proxyType); if (javaClassAttribute == null) { return; } RegisterProxy(proxyType, proxyType, ref record); record.IsJVMClass = true; record.CLRStatic = proxyType; }
public static object StrongJ2CpUntyped(IntPtr ptr) { JNIEnv env = JNIEnv.ThreadEnv; JniLocalHandle obj = ptr; if (JniHandle.IsNull(obj)) { return(null); } Class clazz = env.GetObjectClass(obj); RegistryRecord record = Registry.GetJVMRecord(clazz); return(record.CreateCLRProxyNoDelete(env, obj)); }
private static void RegisterTypeOf(RegistryRecord record, JNIEnv env) { MethodId constructor = knownCLR[typeof(Type)].JVMConstructor; var h = new Value { _long = IntHandle.Alloc(record.CLRInterface) }; var clazz = Type_._class.jvmHandle; var typeInfo = new Value { _object = env.NewObjectPtr(clazz, constructor, Value.Null, h) }; env.CallStaticVoidMethod(record.JVMStatic, "InitJNI", "(Lnet/sf/jni4net/inj/INJEnv;Lsystem/Type;)V", new[] { Value.Null, typeInfo }); //record.JVMStatic.Invoke("InitJNI", "(Lnet/sf/jni4net/inj/INJEnv;Lsystem/Type;)V", null, record.CLRInterface); }
public static JniHandle StrongC2JDelegate(JNIEnv env, Delegate obj) { if (obj == null) { return(JniLocalHandle.Zero); } IJvmProxy proxy = obj.Target as IJvmProxy; if (proxy != null) { return(proxy.JvmHandle); } RegistryRecord record = Registry.GetCLRRecord(obj.GetType()); return(record.CreateJVMProxy(env, obj)); }
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; } } }
public static void RegisterType(Type type, bool bindJVM, 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, env); } } }
private static void RegisterWrapper(Type wrapperType, ref RegistryRecord record) { ClrWrapperAttribute wrapperAttribute = GetClrWrapperAttribute(wrapperType); if (wrapperAttribute == null) { return; } Type interfaceType = wrapperAttribute.InterfaceType; if (record == null) { if (!knownCLRWrappers.TryGetValue(wrapperType, out record)) { if (!knownCLRInterfaces.TryGetValue(interfaceType, out record)) { record = new RegistryRecord { CLRAssembly = wrapperType.Assembly, IsInterface = interfaceType.IsInterface, IsCLRType = !interfaceType.IsInterface, IsDelegate = typeof(Delegate).IsAssignableFrom(interfaceType) && typeof(Delegate) != interfaceType && typeof(MulticastDelegate) != interfaceType, }; } } } record.CLRInterface = interfaceType; record.CLRWrapper = wrapperType; record.CLRWrapperInitMethod = GetWrapperInitializer(wrapperType, "__Init"); record.CLRName = interfaceType.FullName; record.JVMName = GetDefaultJavaName(interfaceType); record.CLRStatic = wrapperAttribute.StaticType; knownCLRWrappers[wrapperType] = record; knownCLRInterfaces[interfaceType] = record; knownCLR[interfaceType] = record; knownCLR[wrapperType] = record; }
private static void RegisterInterfaceProxy(Type proxyType, ref RegistryRecord record) { JavaProxyAttribute javaProxyAttribute = GetJavaProxyAttribute(proxyType); if (javaProxyAttribute == null) { return; } Type interfaceType = javaProxyAttribute.InterfaceType; if (interfaceType == null) { throw new JNIException("Can't initialize " + proxyType); } RegisterProxy(proxyType, interfaceType, ref record); record.CLRStatic = javaProxyAttribute.StaticType; knownCLR[record.CLRStatic] = record; }
public static TRes FullJ2C <TRes>(JNIEnv env, JniLocalHandle obj) { if (JniLocalHandle.IsNull(obj)) { return(default(TRes)); } Type reqType = typeof(TRes); if (reqType.IsPrimitive) { return((TRes)PrimJ2C(obj, env, reqType)); } #if DEBUG if (reqType.IsArray) { throw new InvalidOperationException("Call ArrayFullJ2C<TRes, TElem> instead"); } #endif Class clazz = env.GetObjectClass(obj); if (reqType == typeof(string) && clazz == String._class) { return((TRes)(object)StrongJ2CString(env, obj)); } //now we deal only with JVM instances RegistryRecord record = Registry.GetJVMRecord(clazz); if (reqType.IsAssignableFrom(record.CLRInterface)) { return((TRes)record.CreateCLRProxy(env, obj)); } record = Registry.GetCLRRecord(reqType); if (Bridge.Setup.Debug) { if (!record.JVMInterface.isAssignableFrom(clazz)) { throw new InvalidCastException("Can't cast JVM instance" + clazz + " to " + reqType); } } return((TRes)record.CreateCLRProxy(env, obj)); }
public static TRes StrongJ2CpDelegate <TRes>(JNIEnv env, JniLocalHandle obj) where TRes : class //Delegate { #if DEBUG if (!typeof(Delegate).IsAssignableFrom(typeof(TRes))) { throw new ArgumentException("expected delegate"); } #endif if (JniHandle.IsNull(obj)) { return(default(TRes)); } object res = __IClrProxy.GetObject(env, obj); if (res == null) { //that's delegate implemented in Java RegistryRecord delRecord = Registry.GetCLRRecord(typeof(TRes)); IJvmProxy jvmProxy = delRecord.CreateCLRProxy(env, obj); return((TRes)(object)Delegate.CreateDelegate(typeof(TRes), jvmProxy, delRecord.JVMDelegateInvoke)); } return((TRes)res); }
public static TRes FullJ2C <TRes>(JNIEnv env, JniLocalHandle obj) { if (JniLocalHandle.IsNull(obj)) { return(default(TRes)); } Type reqType = typeof(TRes); if (reqType.IsPrimitive) { return((TRes)PrimJ2C(obj, env, reqType)); } #if DEBUG if (reqType.IsArray) { throw new InvalidOperationException("Call ArrayFullJ2C<TRes, TElem> instead"); } #endif Class clazz = env.GetObjectClass(obj); if (reqType == typeof(string) && clazz == String._class) { return((TRes)(object)StrongJp2CString(env, obj)); } if (IClrProxy_._class.isAssignableFrom(clazz)) { if (!reqType.IsInterface && typeof(IJvmProxy).IsAssignableFrom(reqType)) { //now we double wrap return((TRes)__IClrProxy.CreateProxy(env, obj)); } object res = __IClrProxy.GetObject(env, obj); if (res == null && Delegate_._class.isAssignableFrom(clazz)) { //that's delegate implemented in Java RegistryRecord delRecord = Registry.GetJVMRecord(clazz); IJvmProxy jvmProxy = delRecord.CreateCLRProxy(env, obj); Delegate del = Delegate.CreateDelegate(delRecord.CLRInterface, jvmProxy, delRecord.JVMDelegateInvoke); return((TRes)(object)del); } if (Bridge.Setup.Debug) { Type realType = res.GetType(); if (!reqType.IsAssignableFrom(realType)) { throw new InvalidCastException("Can't cast CLR instance" + realType + " to " + reqType); } } return((TRes)res); } //now we deal only with JVM instances RegistryRecord record = Registry.GetJVMRecord(clazz); if (reqType.IsAssignableFrom(record.CLRInterface)) { return((TRes)record.CreateCLRProxy(env, obj)); } record = Registry.GetCLRRecord(reqType); if (Bridge.Setup.Debug) { if (!record.JVMInterface.isAssignableFrom(clazz)) { throw new InvalidCastException("Can't cast JVM instance" + clazz + " to " + reqType); } } return((TRes)record.CreateCLRProxy(env, obj)); }
public static JniHandle FullC2J <TBoth>(JNIEnv env, TBoth obj) { // ReSharper disable CompareNonConstrainedGenericWithNull if (obj == null) { return(JniLocalHandle.Zero); } // ReSharper restore CompareNonConstrainedGenericWithNull Type reqType = typeof(TBoth); if (reqType.IsPrimitive) { return(PrimC2J(env, obj, reqType)); } #if DEBUG if (reqType.IsArray) { throw new InvalidOperationException("Call ArrayFullJ2C<TRes, TElem> instead"); } #endif IJvmProxy proxy; Delegate del = obj as Delegate; if (del != null) { proxy = del.Target as IJvmProxy; } else { proxy = obj as IJvmProxy; } if (proxy != null) { if (!reqType.IsInterface && !typeof(IJvmProxy).IsAssignableFrom(reqType)) { //now we do double wrap RegistryRecord recordW = Registry.GetCLRRecord(typeof(IJvmProxy)); return(recordW.CreateJVMProxy(env, obj)); } if (Bridge.Setup.Debug) { Class clazzT = env.GetObjectClass(proxy.JvmHandle); RegistryRecord recordT = Registry.GetCLRRecord(reqType); if (!recordT.JVMInterface.isAssignableFrom(clazzT)) { throw new InvalidCastException("Can't cast JVM instance " + clazzT + " to " + reqType); } } return(proxy.JvmHandle); } Type realType = obj.GetType(); if (reqType == typeof(String) && realType == typeof(string)) { return(StrongC2JString(env, (string)(object)obj)); } //Now we deal only with CLR instances RegistryRecord record = Registry.GetCLRRecord(realType); if (reqType.IsAssignableFrom(record.CLRInterface)) { return(record.CreateJVMProxy(env, obj)); } record = Registry.GetCLRRecord(reqType); if (Bridge.Setup.Debug) { if (!record.CLRInterface.IsAssignableFrom(realType)) { throw new InvalidCastException("Can't cast JVM instance" + realType + " to " + reqType); } } return(record.CreateJVMProxy(env, obj)); }
private static void RegisterStaticAndMethods(RegistryRecord record, JNIEnv env) { try { record.CLRProxyInitMethod.Invoke(null, new object[] {env, record.JVMInterface}); } catch (Exception ex) { throw new JNIException("Can't initialize proxy " + record.CLRName, ex); } }
private static void RegisterTypeOf(RegistryRecord record, JNIEnv env) { MethodId constructor = knownCLR[typeof (Type)].JVMConstructor; var h = new Value {_long = IntHandle.Alloc(record.CLRInterface)}; var clazz = Type_._class.jvmHandle; var typeInfo = new Value { _object = env.NewObjectPtr(clazz, constructor, Value.Null, h) }; env.CallStaticVoidMethod(record.JVMStatic, "InitJNI", "(Lnet/sf/jni4net/inj/INJEnv;Lsystem/Type;)V", new[] {Value.Null, typeInfo}); //record.JVMStatic.Invoke("InitJNI", "(Lnet/sf/jni4net/inj/INJEnv;Lsystem/Type;)V", null, record.CLRInterface); }
private static void BindJvm(RegistryRecord record, JNIEnv env) { BindJvm(record, null, env); }
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; } }
private static void RegisterWrapper(Type wrapperType, ref RegistryRecord record) { ClrWrapperAttribute wrapperAttribute = GetClrWrapperAttribute(wrapperType); if (wrapperAttribute == null) { return; } Type interfaceType = wrapperAttribute.InterfaceType; if (record == null) { if (!knownCLRWrappers.TryGetValue(wrapperType, out record)) { if (!knownCLRInterfaces.TryGetValue(interfaceType, out record)) { record = new RegistryRecord { CLRAssembly = wrapperType.Assembly, IsInterface = interfaceType.IsInterface, IsCLRType = !interfaceType.IsInterface, IsDelegate = typeof (Delegate).IsAssignableFrom(interfaceType) && typeof (Delegate) != interfaceType && typeof (MulticastDelegate) != interfaceType, }; } } } record.CLRInterface = interfaceType; record.CLRWrapper = wrapperType; record.CLRWrapperInitMethod = GetWrapperInitializer(wrapperType, "__Init"); record.CLRName = interfaceType.FullName; record.JVMName = interfaceType.Namespace.ToLowerInvariant() + "." + interfaceType.Name; record.CLRStatic = wrapperAttribute.StaticType; knownCLRWrappers[wrapperType] = record; knownCLRInterfaces[interfaceType] = record; knownCLR[interfaceType] = record; knownCLR[wrapperType] = record; }
private static void RegisterProxy(Type proxyType, Type interfaceType, ref RegistryRecord record) { if (record == null) { if (!knownCLRProxies.TryGetValue(proxyType, out record)) { if (!knownCLRInterfaces.TryGetValue(interfaceType, out record)) { record = new RegistryRecord { CLRAssembly = proxyType.Assembly, IsInterface = interfaceType.IsInterface }; } } } record.CLRInterface = interfaceType; record.CLRProxy = proxyType; record.CLRConstructor = GetConstructor(proxyType); record.CLRProxyInitMethod = GetProxyInitializer(proxyType); record.CLRName = interfaceType.FullName; record.JVMName = interfaceType.Namespace.ToLowerInvariant() + "." + interfaceType.Name; knownCLRProxies[proxyType] = record; knownCLRInterfaces[interfaceType] = record; knownCLR[interfaceType] = record; knownCLR[proxyType] = record; }
private static RegistryRecord ResolveNew(Class clazz) { var fill = new List <Class> { clazz }; Class current = clazz.getSuperclass(); RegistryRecord res; while (!knownJVM.TryGetValue(current, out res)) { fill.Add(current); current = current.getSuperclass(); } if (current == Object._class) { RegistryRecord resi = null; // any interface is better than system.Object current = clazz; while (current != Object._class) { foreach (Class ifc in current.getInterfaces()) { if (knownJVM.TryGetValue(ifc, out resi)) { res = resi; fill = new List <Class> { clazz }; if (current != clazz) { fill.Add(current); } break; } foreach (Class ifcin in ifc.getInterfaces()) { if (knownJVM.TryGetValue(ifcin, out resi)) { res = resi; fill = new List <Class> { clazz }; fill.Add(ifc); if (current != clazz) { fill.Add(current); } break; } } if (resi != null) { break; } } if (resi != null) { break; } current = current.getSuperclass(); } } foreach (Class newType in fill) { knownJVM.Add(newType, res); } return(res); }