2 * Copyright (C) 2003 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #ifndef _JNI_RUNTIME_H_
26 #define _JNI_RUNTIME_H_
28 #include <CoreFoundation/CoreFoundation.h>
30 #include <jni_utility.h>
31 #include <jni_instance.h>
47 void _commonInit (JNIEnv *e, jstring s)
49 int _size = e->GetStringLength (s);
50 const jchar *uc = getUCharactersFromJStringInEnv (e, s);
51 _ustring = UString((UChar *)uc,_size);
52 releaseUCharactersForJStringInEnv (e, s, uc);
55 JavaString (JNIEnv *e, jstring s) {
59 JavaString (jstring s) {
60 _commonInit (getJNIEnv(), s);
63 const char *UTF8String() const {
64 if (_utf8String.c_str() == 0)
65 _utf8String = _ustring.UTF8String();
66 return _utf8String.c_str();
68 const jchar *uchars() const { return (const jchar *)_ustring.data(); }
69 int length() const { return _ustring.size(); }
70 KJS::UString ustring() const { return _ustring; }
74 mutable CString _utf8String;
77 class JavaParameter : public Parameter
80 JavaParameter () : _JNIType(invalid_type) {};
82 JavaParameter (JNIEnv *env, jstring type);
87 JavaParameter(const JavaParameter &other) : Parameter() {
89 _JNIType = other._JNIType;
92 JavaParameter &operator=(const JavaParameter &other)
98 _JNIType = other._JNIType;
103 virtual RuntimeType type() const { return _type.UTF8String(); }
105 JNIType getJNIType() const { return _JNIType; }
113 class JavaConstructor : public Constructor
116 JavaConstructor() : _parameters (0), _numParameters(0) {};
118 JavaConstructor (JNIEnv *e, jobject aConstructor);
121 delete [] _parameters;
124 void _commonCopy(const JavaConstructor &other) {
125 _numParameters = other._numParameters;
126 _parameters = new JavaParameter[_numParameters];
128 for (i = 0; i < _numParameters; i++) {
129 _parameters[i] = other._parameters[i];
133 JavaConstructor(const JavaConstructor &other) : Constructor() {
137 JavaConstructor &operator=(const JavaConstructor &other)
142 delete [] _parameters;
149 virtual Parameter *parameterAt(long i) const { return &_parameters[i]; };
150 virtual long numParameters() const { return _numParameters; };
153 JavaParameter *_parameters;
158 class JavaField : public Field
161 JavaField() : _field(0) {};
162 JavaField (JNIEnv *env, jobject aField);
167 JavaField(const JavaField &other) :
168 Field(), _name(other._name), _type(other._type), _field(other._field) {};
170 JavaField &operator=(const JavaField &other)
179 _field = other._field;
184 virtual KJS::Value valueFromInstance(KJS::ExecState *exec, const Instance *instance) const;
185 virtual void setValueToInstance(KJS::ExecState *exec, const Instance *instance, const KJS::Value &aValue) const;
187 virtual const char *name() const { return _name.UTF8String(); }
188 virtual RuntimeType type() const { return _type.UTF8String(); }
190 JNIType getJNIType() const { return _JNIType; }
196 JavaInstance *_field;
200 class JavaMethod : public Method
203 JavaMethod() : Method(), _signature(0), _methodID(0) {};
205 JavaMethod (JNIEnv *env, jobject aMethod);
207 void _commonDelete() {
209 delete [] _parameters;
216 void _commonCopy(const JavaMethod &other) {
218 _returnType = other._returnType;
220 _numParameters = other._numParameters;
221 _parameters = new JavaParameter[_numParameters];
223 for (i = 0; i < _numParameters; i++) {
224 _parameters[i] = other._parameters[i];
226 _signature = other._signature;
229 JavaMethod(const JavaMethod &other) : Method() {
233 JavaMethod &operator=(const JavaMethod &other)
244 virtual const char *name() const { return _name.UTF8String(); };
245 RuntimeType returnType() const { return _returnType.UTF8String(); };
246 virtual Parameter *parameterAt(long i) const { return &_parameters[i]; };
247 virtual long numParameters() const { return _numParameters; };
249 const char *signature() const;
250 JNIType JNIReturnType() const;
252 jmethodID methodID (jobject obj) const;
255 JavaParameter *_parameters;
258 mutable KJS::UString *_signature;
259 JavaString _returnType;
260 JNIType _JNIReturnType;
261 mutable jmethodID _methodID;
264 class JavaArray : public Array
267 JavaArray (jobject a, const char *type, const RootObject *r);
269 JavaArray (const JavaArray &other);
271 JavaArray &operator=(const JavaArray &other){
275 free ((void *)_type);
276 _type = strdup(other._type);
279 JObjectWrapper *_oldArray = _array;
280 _array = other._array;
287 virtual void setValueAt(KJS::ExecState *exec, unsigned int index, const KJS::Value &aValue) const;
288 virtual KJS::Value valueAt(KJS::ExecState *exec, unsigned int index) const;
289 virtual unsigned int getLength() const;
291 virtual ~JavaArray();
293 jobject javaArray() const { return _array->_instance; }
295 static KJS::Value convertJObjectToArray (KJS::ExecState *exec, jobject anObject, const char *type, const RootObject *r);
297 const RootObject *executionContext() const { return _root; }
300 JObjectWrapper *_array;
301 unsigned int _length;
303 const RootObject *_root;
306 } // namespace Bindings