IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
IKVM.Runtime.JNI.JNIEnv Struct Reference

Classes

struct  JNINativeMethod
 

Public Member Functions

void * PinObject (object obj)
 
void UnpinObject (object obj)
 

Static Public Member Functions

static RuntimeClassLoader FindNativeMethodClassLoader (JNIEnv *pEnv)
 
static nint AllocObjectImpl (JNIEnv *pEnv, RuntimeJavaType wrapper)
 
static object InvokeHelper (JNIEnv *pEnv, nint objHandle, nint methodID, JValue *pArgs, bool nonVirtual)
 Invokes the given method, by ID, on the given object handle. Accepts a pointer to a JValue array of arguments.
 
static object InvokeNonVirtual (JNIEnvContext env, RuntimeJavaMethod mw, object obj, object[] argarray)
 Invokes the given non-virtual method, by ID, on the given object handle. Accepts a pointer to a JValue array of arguments.
 
static nint NewObjectA (JNIEnv *pEnv, nint clazz, nint methodID, JValue *args)
 
static RuntimeJavaMethod GetMethodImpl (RuntimeJavaType tw, string name, string sig)
 
static void AppendInterfaces (List< RuntimeJavaType > list, IList< RuntimeJavaType > add)
 
static List< RuntimeJavaType > TransitiveInterfaces (RuntimeJavaType tw)
 
static RuntimeJavaMethod GetInterfaceMethodImpl (RuntimeJavaType tw, string name, string sig)
 
static nint FindMethodID (JNIEnv *pEnv, nint clazz, byte *name, byte *sig, bool isStatic)
 
static RuntimeJavaField GetFieldImpl (RuntimeJavaType tw, string name, string sig)
 
static nint FindFieldID (JNIEnv *pEnv, nint clazz, byte *name, byte *sig, bool isStatic)
 
static RuntimeJavaField GetFieldWrapper (nint cookie)
 
static bool IsLocalRef (nint o)
 
static bool IsGlobalRef (nint o)
 

Static Public Attributes

static readonly MUTF8Encoding MUTF8 = MUTF8Encoding.GetMUTF8(52)
 

Detailed Description

Definition at line 53 of file JNIEnv.cs.

Member Function Documentation

◆ AllocObjectImpl()

static nint IKVM.Runtime.JNI.JNIEnv.AllocObjectImpl ( JNIEnv * pEnv,
RuntimeJavaType wrapper )
static

Definition at line 622 of file JNIEnv.cs.

623 {
624 try
625 {
626 if (wrapper.IsAbstract)
627 throw new java.lang.InstantiationException(wrapper.Name);
628
629 wrapper.Finish();
630#if NETFRAMEWORK
631 return pEnv->MakeLocalRef(FormatterServices.GetUninitializedObject(wrapper.TypeAsBaseType));
632#else
633 return pEnv->MakeLocalRef(RuntimeHelpers.GetUninitializedObject(wrapper.TypeAsBaseType));
634#endif
635 }
636 catch (RetargetableJavaException e)
637 {
638 ExceptionDispatchInfo.Capture(e.ToJava()).Throw();
639 throw null;
640 }
641 }

◆ AppendInterfaces()

static void IKVM.Runtime.JNI.JNIEnv.AppendInterfaces ( List< RuntimeJavaType > list,
IList< RuntimeJavaType > add )
static

Definition at line 826 of file JNIEnv.cs.

827 {
828 foreach (var iface in add)
829 if (list.Contains(iface) == false)
830 list.Add(iface);
831 }

◆ FindFieldID()

static nint IKVM.Runtime.JNI.JNIEnv.FindFieldID ( JNIEnv * pEnv,
nint clazz,
byte * name,
byte * sig,
bool isStatic )
static

Definition at line 1249 of file JNIEnv.cs.

1250 {
1251 try
1252 {
1253 var n = DecodeMUTF8Argument(name, nameof(name));
1254 var s = DecodeMUTF8Argument(sig, nameof(sig));
1255
1256 var tw = RuntimeJavaType.FromClass((java.lang.Class)pEnv->UnwrapRef(clazz));
1257 tw.Finish();
1258
1259 // don't allow dotted names!
1260 if (s.IndexOf('.') < 0)
1261 {
1262 var fw = GetFieldImpl(tw, n, s.Replace('/', '.'));
1263 if (fw != null)
1264 if (fw.IsStatic == isStatic)
1265 return fw.Cookie;
1266 }
1267
1268 throw new java.lang.NoSuchFieldError($"{(isStatic ? "Static" : "Instance")} field '{n}' with signature '{s}' not found in class '{tw.Name}'");
1269 }
1270 catch (RetargetableJavaException x)
1271 {
1272 ExceptionDispatchInfo.Capture(x.ToJava()).Throw();
1273 throw null;
1274 }
1275 }
static RuntimeJavaField GetFieldImpl(RuntimeJavaType tw, string name, string sig)
Definition JNIEnv.cs:1235

◆ FindMethodID()

static nint IKVM.Runtime.JNI.JNIEnv.FindMethodID ( JNIEnv * pEnv,
nint clazz,
byte * name,
byte * sig,
bool isStatic )
static

Definition at line 863 of file JNIEnv.cs.

864 {
865 try
866 {
867 var tw = RuntimeJavaType.FromClass((java.lang.Class)pEnv->UnwrapRef(clazz));
868 tw.Finish();
869
870 // if name == NULL, the JDK returns the constructor
871 var methodname = (IntPtr)name == IntPtr.Zero ? "<init>" : DecodeMUTF8Argument(name, nameof(name));
872 var methodsig = DecodeMUTF8Argument(sig, nameof(sig));
873
874 RuntimeJavaMethod mw = null;
875
876 // don't allow dotted names!
877 if (methodsig.IndexOf('.') < 0)
878 {
879 methodsig = methodsig.Replace('/', '.');
880 if (methodname == "<init>" || methodname == "<clinit>")
881 mw = tw.GetMethod(methodname, methodsig, false);
882 else
883 mw = GetMethodImpl(tw, methodname, methodsig) ?? GetInterfaceMethodImpl(tw, methodname, methodsig);
884 }
885 if (mw != null && mw.IsStatic == isStatic)
886 {
887 mw.Link();
888 return mw.Cookie;
889 }
890
891 throw new java.lang.NoSuchMethodError($"{methodname}{methodsig}");
892 }
893 catch (RetargetableJavaException e)
894 {
895 ExceptionDispatchInfo.Capture(e.ToJava()).Throw();
896 throw null;
897 }
898 }
static RuntimeJavaMethod GetInterfaceMethodImpl(RuntimeJavaType tw, string name, string sig)
Definition JNIEnv.cs:851
static RuntimeJavaMethod GetMethodImpl(RuntimeJavaType tw, string name, string sig)
Definition JNIEnv.cs:812

◆ FindNativeMethodClassLoader()

static RuntimeClassLoader IKVM.Runtime.JNI.JNIEnv.FindNativeMethodClassLoader ( JNIEnv * pEnv)
static

Definition at line 196 of file JNIEnv.cs.

197 {
198 var env = pEnv->GetContext();
199 if (env.callerID != null)
200 return RuntimeClassLoader.FromCallerID(env.callerID);
201 else if (env.classLoader != null)
202 return env.classLoader;
203 else
204 return env.context.ClassLoaderFactory.GetClassLoaderWrapper(java.lang.ClassLoader.getSystemClassLoader());
205 }

◆ GetFieldImpl()

static RuntimeJavaField IKVM.Runtime.JNI.JNIEnv.GetFieldImpl ( RuntimeJavaType tw,
string name,
string sig )
static

Definition at line 1235 of file JNIEnv.cs.

1236 {
1237 for (; ; )
1238 {
1239 var fw = tw.GetFieldWrapper(name, sig);
1240 if (fw == null || !fw.IsHideFromReflection)
1241 return fw;
1242
1243 tw = fw.DeclaringType.BaseTypeWrapper;
1244 if (tw == null)
1245 return null;
1246 }
1247 }

◆ GetFieldWrapper()

static RuntimeJavaField IKVM.Runtime.JNI.JNIEnv.GetFieldWrapper ( nint cookie)
static

Definition at line 1290 of file JNIEnv.cs.

1291 {
1292 return RuntimeJavaField.FromCookie(cookie);
1293 }

◆ GetInterfaceMethodImpl()

static RuntimeJavaMethod IKVM.Runtime.JNI.JNIEnv.GetInterfaceMethodImpl ( RuntimeJavaType tw,
string name,
string sig )
static

Definition at line 851 of file JNIEnv.cs.

852 {
853 foreach (var iface in TransitiveInterfaces(tw))
854 {
855 var mw = iface.GetMethod(name, sig, false);
856 if (mw != null && !mw.IsHideFromReflection)
857 return mw;
858 }
859
860 return null;
861 }
static List< RuntimeJavaType > TransitiveInterfaces(RuntimeJavaType tw)
Definition JNIEnv.cs:833

◆ GetMethodImpl()

static RuntimeJavaMethod IKVM.Runtime.JNI.JNIEnv.GetMethodImpl ( RuntimeJavaType tw,
string name,
string sig )
static

Definition at line 812 of file JNIEnv.cs.

813 {
814 for (; ; )
815 {
816 var mw = tw.GetMethod(name, sig, true);
817 if (mw == null || !mw.IsHideFromReflection)
818 return mw;
819
820 tw = mw.DeclaringType.BaseTypeWrapper;
821 if (tw == null)
822 return null;
823 }
824 }

◆ InvokeHelper()

static object IKVM.Runtime.JNI.JNIEnv.InvokeHelper ( JNIEnv * pEnv,
nint objHandle,
nint methodID,
JValue * pArgs,
bool nonVirtual )
static

Invokes the given method, by ID, on the given object handle. Accepts a pointer to a JValue array of arguments.

Parameters
pEnv
objHandle
methodID
pArgs
nonVirtual
Returns

Definition at line 652 of file JNIEnv.cs.

653 {
654 // resolve object who's method is being invoked
655 var env = pEnv->GetContext();
656 var obj = UnwrapRef(env, objHandle);
657
658 // resolve the method being invoked on the object
659 var mw = RuntimeJavaMethod.FromCookie(methodID);
660 mw.Link();
661 mw.ResolveMethod();
662
663 // assemble arguments into array
664 var argTypes = mw.GetParameters();
665 var args = new object[argTypes.Length + (mw.HasCallerID ? 1 : 0)];
666 for (int i = 0; i < argTypes.Length; i++)
667 {
668 var type = argTypes[i];
669 if (type == env.context.PrimitiveJavaTypeFactory.BOOLEAN)
670 args[i] = pArgs[i].z != JNI_FALSE;
671 else if (type == env.context.PrimitiveJavaTypeFactory.BYTE)
672 args[i] = (byte)pArgs[i].b;
673 else if (type == env.context.PrimitiveJavaTypeFactory.CHAR)
674 args[i] = (char)pArgs[i].c;
675 else if (type == env.context.PrimitiveJavaTypeFactory.SHORT)
676 args[i] = pArgs[i].s;
677 else if (type == env.context.PrimitiveJavaTypeFactory.INT)
678 args[i] = pArgs[i].i;
679 else if (type == env.context.PrimitiveJavaTypeFactory.LONG)
680 args[i] = pArgs[i].j;
681 else if (type == env.context.PrimitiveJavaTypeFactory.FLOAT)
682 args[i] = pArgs[i].f;
683 else if (type == env.context.PrimitiveJavaTypeFactory.DOUBLE)
684 args[i] = pArgs[i].d;
685 else
686 args[i] = argTypes[i].GhostWrap(UnwrapRef(env, pArgs[i].l));
687 }
688
689 // method requires caller
690 if (mw.HasCallerID)
691 args[args.Length - 1] = env.callerID;
692
693 if (nonVirtual && mw.RequiresNonVirtualDispatcher)
694 return InvokeNonVirtual(env, mw, obj, args);
695
696 if (mw.IsConstructor)
697 {
698 if (obj == null)
699 {
700 return mw.CreateInstance(args);
701 }
702 else
703 {
704 var mb = mw.GetMethod();
705 if (mb.IsStatic)
706 {
707 // we're dealing with a constructor on a remapped type, if obj is supplied, it means
708 // that we should call the constructor on an already existing instance, but that isn't
709 // possible with remapped types
710 throw new NotSupportedException($"Remapped type {mw.DeclaringType.Name} doesn't support constructor invocation on an existing instance");
711 }
712 else if (!mb.DeclaringType.IsInstanceOfType(obj))
713 {
714 // we're trying to initialize an existing instance of a remapped type
715 throw new NotSupportedException($"Unable to partially construct object of type {obj.GetType().FullName} to type {mb.DeclaringType.FullName}");
716 }
717 }
718 }
719
720 return mw.Invoke(obj, args);
721 }
static object InvokeNonVirtual(JNIEnvContext env, RuntimeJavaMethod mw, object obj, object[] argarray)
Invokes the given non-virtual method, by ID, on the given object handle. Accepts a pointer to a JValu...
Definition JNIEnv.cs:732

◆ InvokeNonVirtual()

static object IKVM.Runtime.JNI.JNIEnv.InvokeNonVirtual ( JNIEnvContext env,
RuntimeJavaMethod mw,
object obj,
object[] argarray )
static

Invokes the given non-virtual method, by ID, on the given object handle. Accepts a pointer to a JValue array of arguments.

Parameters
env
mw
obj
argarray
Returns
Exceptions
NotSupportedException

Definition at line 732 of file JNIEnv.cs.

733 {
734 if (mw.HasCallerID || mw.IsDynamicOnly)
735 throw new NotSupportedException();
736
737 if (mw.DeclaringType.IsRemapped && !mw.DeclaringType.TypeAsBaseType.IsInstanceOfType(obj))
738 return mw.InvokeNonvirtualRemapped(obj, argarray);
739
740 var del = (Delegate)Activator.CreateInstance(mw.GetDelegateType(), new object[] { obj, mw.GetMethod().MethodHandle.GetFunctionPointer() });
741 try
742 {
743 return del.DynamicInvoke(argarray);
744 }
745 catch (TargetInvocationException e)
746 {
747 ExceptionDispatchInfo.Capture(ikvm.runtime.Util.mapException(e.InnerException)).Throw();
748 throw null;
749 }
750 }

◆ IsGlobalRef()

static bool IKVM.Runtime.JNI.JNIEnv.IsGlobalRef ( nint o)
static

Definition at line 3146 of file JNIEnv.cs.

3147 {
3148 return o < 0;
3149 }

◆ IsLocalRef()

static bool IKVM.Runtime.JNI.JNIEnv.IsLocalRef ( nint o)
static

Definition at line 3141 of file JNIEnv.cs.

3142 {
3143 return o > 0;
3144 }

◆ NewObjectA()

static nint IKVM.Runtime.JNI.JNIEnv.NewObjectA ( JNIEnv * pEnv,
nint clazz,
nint methodID,
JValue * args )
static

Definition at line 752 of file JNIEnv.cs.

753 {
754 try
755 {
756 var wrapper = RuntimeJavaType.FromClass((java.lang.Class)pEnv->UnwrapRef(clazz));
757 if (wrapper.IsAbstract == false && wrapper.TypeAsBaseType.IsAbstract)
758 return pEnv->MakeLocalRef(InvokeHelper(pEnv, IntPtr.Zero, methodID, args, false)); // static newinstance helper method
759
760 var obj = AllocObjectImpl(pEnv, wrapper);
761 if (obj != IntPtr.Zero)
762 {
763 InvokeHelper(pEnv, obj, methodID, args, false);
764
765 if (ExceptionCheck(pEnv) == JNI_TRUE)
766 {
767 DeleteLocalRef(pEnv, obj);
768 obj = IntPtr.Zero;
769 }
770 }
771
772 return obj;
773 }
774 catch (Exception e)
775 {
776 JVM.SetPendingException(e);
777 return 0;
778 }
779 }
static nint AllocObjectImpl(JNIEnv *pEnv, RuntimeJavaType wrapper)
Definition JNIEnv.cs:622
static object InvokeHelper(JNIEnv *pEnv, nint objHandle, nint methodID, JValue *pArgs, bool nonVirtual)
Invokes the given method, by ID, on the given object handle. Accepts a pointer to a JValue array of a...
Definition JNIEnv.cs:652

◆ PinObject()

void * IKVM.Runtime.JNI.JNIEnv.PinObject ( object obj)

Definition at line 2939 of file JNIEnv.cs.

2940 {
2941 if (pinHandleInUseCount == pinHandleMaxCount)
2942 {
2943 var newCount = pinHandleMaxCount + 32;
2944 var pNew = (GCHandle*)JNIMemory.Alloc(sizeof(GCHandle) * newCount);
2945 for (int i = 0; i < pinHandleMaxCount; i++)
2946 pNew[i] = pinHandles[i];
2947
2948 for (int i = pinHandleMaxCount; i < newCount; i++)
2949 pNew[i] = new GCHandle();
2950
2951 JNIMemory.Free((nint)pinHandles);
2952 pinHandles = pNew;
2953 pinHandleMaxCount = newCount;
2954 }
2955
2956 var index = pinHandleInUseCount++;
2957 if (pinHandles[index].IsAllocated == false)
2958 pinHandles[index] = GCHandle.Alloc(null, GCHandleType.Pinned);
2959
2960 pinHandles[index].Target = obj;
2961 return (void*)pinHandles[index].AddrOfPinnedObject();
2962 }

◆ TransitiveInterfaces()

static List< RuntimeJavaType > IKVM.Runtime.JNI.JNIEnv.TransitiveInterfaces ( RuntimeJavaType tw)
static

Definition at line 833 of file JNIEnv.cs.

834 {
835 var list = new List<RuntimeJavaType>();
836
837 // append interfaces from base type
838 if (tw.BaseTypeWrapper != null)
839 AppendInterfaces(list, TransitiveInterfaces(tw.BaseTypeWrapper));
840
841 // append transitive interfaces of current type
842 foreach (RuntimeJavaType iface in tw.Interfaces)
844
845 // append interfaces of current type
846 AppendInterfaces(list, tw.Interfaces);
847
848 return list;
849 }
static void AppendInterfaces(List< RuntimeJavaType > list, IList< RuntimeJavaType > add)
Definition JNIEnv.cs:826

◆ UnpinObject()

void IKVM.Runtime.JNI.JNIEnv.UnpinObject ( object obj)

Definition at line 2964 of file JNIEnv.cs.

2965 {
2966 for (int i = 0; i < pinHandleInUseCount; i++)
2967 {
2968 if (pinHandles[i].Target == obj)
2969 {
2970 pinHandles[i].Target = pinHandles[--pinHandleInUseCount].Target;
2971 pinHandles[pinHandleInUseCount].Target = null;
2972 return;
2973 }
2974 }
2975 }

Member Data Documentation

◆ MUTF8

readonly MUTF8Encoding IKVM.Runtime.JNI.JNIEnv.MUTF8 = MUTF8Encoding.GetMUTF8(52)
static

Definition at line 76 of file JNIEnv.cs.


The documentation for this struct was generated from the following file: