~ubuntu-branches/ubuntu/dapper/ikvm/dapper

« back to all changes in this revision

Viewing changes to runtime/JniInterface.cs

  • Committer: Bazaar Package Importer
  • Author(s): John Goerzen
  • Date: 2004-08-26 10:18:19 UTC
  • Revision ID: james.westby@ubuntu.com-20040826101819-plz8au2mx4uk1cvc
Tags: upstream-0.8.0.0
ImportĀ upstreamĀ versionĀ 0.8.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2002 Jeroen Frijters
 
3
 
 
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.
 
7
 
 
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:
 
11
 
 
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.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
using System;
 
25
using System.Text;
 
26
using System.Reflection;
 
27
using System.Runtime.InteropServices;
 
28
 
 
29
public sealed class JniHelper
 
30
{
 
31
        // NOTE sig contains slashed class names
 
32
        public static IntPtr GetMethodCookie(object clazz, string name, string sig, bool isStatic)
 
33
        {
 
34
                TypeWrapper wrapper = NativeCode.java.lang.VMClass.getWrapperFromClass(clazz);
 
35
                wrapper.Finish();
 
36
                MethodWrapper mw = wrapper.GetMethodWrapper(MethodDescriptor.FromNameSig(wrapper.GetClassLoader(), name, sig.Replace('/', '.')), true);
 
37
                if(mw != null)
 
38
                {
 
39
                        if(mw.IsStatic == isStatic)
 
40
                        {
 
41
                                return mw.Cookie;
 
42
                        }
 
43
                }
 
44
                return (IntPtr)0;
 
45
        }
 
46
 
 
47
        // this method returns a simplified method argument descriptor.
 
48
        // some examples:
 
49
        // "()V" -> ""
 
50
        // "(ILjava.lang.String;)I" -> "IL"
 
51
        // "([Ljava.lang.String;)V" -> "L"
 
52
        public static string GetMethodArgList(IntPtr cookie)
 
53
        {
 
54
                StringBuilder sb = new StringBuilder();
 
55
                string s = MethodWrapper.FromCookie(cookie).Descriptor.Signature;
 
56
                for(int i = 1;; i++)
 
57
                {
 
58
                        switch(s[i])
 
59
                        {
 
60
                                case '[':
 
61
                                        while(s[i] == '[') i++;
 
62
                                        if(s[i] == 'L')
 
63
                                        {
 
64
                                                while(s[i] != ';') i++;
 
65
                                        }
 
66
                                        sb.Append('L');
 
67
                                        break;
 
68
                                case 'L':
 
69
                                        while(s[i] != ';') i++;
 
70
                                        sb.Append('L');
 
71
                                        break;
 
72
                                case ')':
 
73
                                        return sb.ToString();
 
74
                                default:
 
75
                                        sb.Append(s[i]);
 
76
                                        break;
 
77
                        }
 
78
                }
 
79
        }
 
80
 
 
81
        public static object InvokeMethod(IntPtr cookie, object obj, object[] args, bool nonVirtual)
 
82
        {
 
83
                return MethodWrapper.FromCookie(cookie).Invoke(obj, args, nonVirtual);
 
84
        }
 
85
 
 
86
        // NOTE sig contains slashed class names
 
87
        public static IntPtr GetFieldCookie(object clazz, string name, string sig, bool isStatic)
 
88
        {
 
89
                TypeWrapper wrapper = NativeCode.java.lang.VMClass.getWrapperFromClass(clazz);
 
90
                wrapper.Finish();
 
91
                // TODO what about searching the base classes?
 
92
                FieldWrapper fw = wrapper.GetFieldWrapper(name, wrapper.GetClassLoader().ExpressionTypeWrapper(sig.Replace('/', '.')));
 
93
                if(fw != null)
 
94
                {
 
95
                        if(fw.IsStatic == isStatic)
 
96
                        {
 
97
                                return fw.Cookie;
 
98
                        }
 
99
                }
 
100
                return (IntPtr)0;
 
101
        }
 
102
 
 
103
        public static void SetFieldValue(IntPtr cookie, object obj, object val)
 
104
        {
 
105
                FieldWrapper.FromCookie(cookie).SetValue(obj, val);
 
106
        }
 
107
 
 
108
        public static object GetFieldValue(IntPtr cookie, object obj)
 
109
        {
 
110
                return FieldWrapper.FromCookie(cookie).GetValue(obj);
 
111
        }
 
112
 
 
113
        public static object FindClass(string javaName)
 
114
        {
 
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('/', '.'));
 
117
                wrapper.Finish();
 
118
                return NativeCode.java.lang.VMClass.getClassFromWrapper(wrapper);
 
119
        }
 
120
 
 
121
        public static Exception UnsatisfiedLinkError(string msg)
 
122
        {
 
123
                return JavaException.UnsatisfiedLinkError(msg);
 
124
        }
 
125
 
 
126
        public static object GetClassFromType(Type type)
 
127
        {
 
128
                return NativeCode.java.lang.VMClass.getClassFromType(type);
 
129
        }
 
130
 
 
131
        public static object AllocObject(object clazz)
 
132
        {
 
133
                TypeWrapper wrapper = NativeCode.java.lang.VMClass.getWrapperFromClass(clazz);
 
134
                wrapper.Finish();
 
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);
 
137
        }
 
138
}
 
139
 
 
140
public interface IJniProvider
 
141
{
 
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();
 
148
}