2
Copyright (C) 2002 Jeroen Frijters
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
10
freely, subject to the following restrictions:
12
1. The origin of this software must not be misrepresented; you must not
13
claim that you wrote the original software. If you use this software
14
in a product, an acknowledgment in the product documentation would be
15
appreciated but is not required.
16
2. Altered source versions must be plainly marked as such, and must not be
17
misrepresented as being the original software.
18
3. This notice may not be removed or altered from any source distribution.
26
using System.Reflection;
27
using System.Runtime.InteropServices;
29
public sealed class JniHelper
31
// NOTE sig contains slashed class names
32
public static IntPtr GetMethodCookie(object clazz, string name, string sig, bool isStatic)
34
TypeWrapper wrapper = NativeCode.java.lang.VMClass.getWrapperFromClass(clazz);
36
MethodWrapper mw = wrapper.GetMethodWrapper(MethodDescriptor.FromNameSig(wrapper.GetClassLoader(), name, sig.Replace('/', '.')), true);
39
if(mw.IsStatic == isStatic)
47
// this method returns a simplified method argument descriptor.
50
// "(ILjava.lang.String;)I" -> "IL"
51
// "([Ljava.lang.String;)V" -> "L"
52
public static string GetMethodArgList(IntPtr cookie)
54
StringBuilder sb = new StringBuilder();
55
string s = MethodWrapper.FromCookie(cookie).Descriptor.Signature;
61
while(s[i] == '[') i++;
64
while(s[i] != ';') i++;
69
while(s[i] != ';') i++;
81
public static object InvokeMethod(IntPtr cookie, object obj, object[] args, bool nonVirtual)
83
return MethodWrapper.FromCookie(cookie).Invoke(obj, args, nonVirtual);
86
// NOTE sig contains slashed class names
87
public static IntPtr GetFieldCookie(object clazz, string name, string sig, bool isStatic)
89
TypeWrapper wrapper = NativeCode.java.lang.VMClass.getWrapperFromClass(clazz);
91
// TODO what about searching the base classes?
92
FieldWrapper fw = wrapper.GetFieldWrapper(name, wrapper.GetClassLoader().ExpressionTypeWrapper(sig.Replace('/', '.')));
95
if(fw.IsStatic == isStatic)
103
public static void SetFieldValue(IntPtr cookie, object obj, object val)
105
FieldWrapper.FromCookie(cookie).SetValue(obj, val);
108
public static object GetFieldValue(IntPtr cookie, object obj)
110
return FieldWrapper.FromCookie(cookie).GetValue(obj);
113
public static object FindClass(string javaName)
115
// TODO instead of using the bootstrap class loader, we need to use the system (aka application) class loader
116
TypeWrapper wrapper = ClassLoaderWrapper.GetBootstrapClassLoader().LoadClassByDottedName(javaName.Replace('/', '.'));
118
return NativeCode.java.lang.VMClass.getClassFromWrapper(wrapper);
121
public static Exception UnsatisfiedLinkError(string msg)
123
return JavaException.UnsatisfiedLinkError(msg);
126
public static object GetClassFromType(Type type)
128
return NativeCode.java.lang.VMClass.getClassFromType(type);
131
public static object AllocObject(object clazz)
133
TypeWrapper wrapper = NativeCode.java.lang.VMClass.getWrapperFromClass(clazz);
135
// TODO add error handling (e.g. when trying to instantiate an interface or abstract class)
136
return System.Runtime.Serialization.FormatterServices.GetUninitializedObject(wrapper.TypeAsBaseType);
140
public interface IJniProvider
142
int LoadNativeLibrary(string filename);
143
Type GetLocalRefStructType();
144
// NOTE the signature of the GetJniFuncPtr method is:
145
// IntPtr GetJniFuncPtr(String method, String sig, String clazz)
146
// sig & clazz are contain slashed class names
147
MethodInfo GetJniFuncPtrMethod();