blob: 80576a9a014deacdd1e555ecb3dfaad101f250db [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070021
22#include <cstdarg>
23#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070024#include <utility>
25#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070026
Elliott Hughes72025e52011-08-23 17:50:30 -070027#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070028#include "UniquePtr.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070029#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070030#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070031#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070033#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080034#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070035#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070036#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070037#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070038#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070039#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070040
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070041namespace art {
42
Elliott Hughes2ced6a52011-10-16 18:44:48 -070043static const size_t kMonitorsInitial = 32; // Arbitrary.
44static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
45
46static const size_t kLocalsInitial = 64; // Arbitrary.
47static const size_t kLocalsMax = 512; // Arbitrary sanity check.
48
49static const size_t kPinTableInitial = 16; // Arbitrary.
50static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
51
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070052static size_t gGlobalsInitial = 512; // Arbitrary.
53static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070054
55static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
56static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
57
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070058void SetJniGlobalsMax(size_t max) {
59 if (max != 0) {
60 gGlobalsMax = max;
61 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
62 }
63}
Ian Rogersdf20fe02011-07-20 20:34:16 -070064
Elliott Hughesc5f7c912011-08-18 14:00:42 -070065/*
66 * Add a local reference for an object to the current stack frame. When
67 * the native function returns, the reference will be discarded.
68 *
69 * We need to allow the same reference to be added multiple times.
70 *
71 * This will be called on otherwise unreferenced objects. We cannot do
72 * GC allocations here, and it's best if we don't grab a mutex.
73 *
74 * Returns the local reference (currently just the same pointer that was
75 * passed in), or NULL on failure.
76 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070077template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070078T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
79 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
80 Object* obj = const_cast<Object*>(const_obj);
81
Elliott Hughesc5f7c912011-08-18 14:00:42 -070082 if (obj == NULL) {
83 return NULL;
84 }
85
Elliott Hughes9c750f92012-04-05 12:07:59 -070086 DCHECK((reinterpret_cast<uintptr_t>(obj) & 0xffff0000) != 0xebad0000);
87
Elliott Hughesbf86d042011-08-31 17:53:14 -070088 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
89 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070090
Ian Rogers5a7a74a2011-09-26 16:32:29 -070091 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070092 IndirectRef ref = locals.Add(cookie, obj);
93 if (ref == NULL) {
94 // TODO: just change Add's DCHECK to CHECK and lose this?
95 locals.Dump();
96 LOG(FATAL) << "Failed adding to JNI local reference table "
97 << "(has " << locals.Capacity() << " entries)";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070098 }
99
100#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700101 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700102 size_t entry_count = locals.Capacity();
103 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700104 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700105 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700106 locals.Dump();
Elliott Hughes82188472011-11-07 18:11:48 -0800107 // TODO: LOG(FATAL) instead.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700108 }
109 }
110#endif
111
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800112 if (env->vm->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700113 // Hand out direct pointers to support broken old apps.
114 return reinterpret_cast<T>(obj);
115 }
116
117 return reinterpret_cast<T>(ref);
118}
Brian Carlstrom51477332012-03-25 20:20:26 -0700119// Explicit instantiations
120template jclass AddLocalReference<jclass>(JNIEnv* public_env, const Object* const_obj);
121template jobject AddLocalReference<jobject>(JNIEnv* public_env, const Object* const_obj);
122template jobjectArray AddLocalReference<jobjectArray>(JNIEnv* public_env, const Object* const_obj);
123template jstring AddLocalReference<jstring>(JNIEnv* public_env, const Object* const_obj);
124template jthrowable AddLocalReference<jthrowable>(JNIEnv* public_env, const Object* const_obj);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700125
Elliott Hughesbf86d042011-08-31 17:53:14 -0700126// For external use.
127template<typename T>
128T Decode(JNIEnv* public_env, jobject obj) {
129 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
130 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
131}
Shih-wei Liao24782c62012-01-08 12:46:11 -0800132// TODO: Change to use template when Mac OS build server no longer uses GCC 4.2.*.
133Object* DecodeObj(JNIEnv* public_env, jobject obj) {
134 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
135 return reinterpret_cast<Object*>(env->self->DecodeJObject(obj));
136}
Elliott Hughesbf86d042011-08-31 17:53:14 -0700137// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700138template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700139template Class* Decode<Class*>(JNIEnv*, jobject);
140template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
141template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700142template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700143template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700144template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700145template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400146template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700147template String* Decode<String*>(JNIEnv*, jobject);
148template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700149
Ian Rogers45619fc2012-02-29 11:15:25 -0800150size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
151 size_t num_bytes = 0;
152 for (size_t i = 1; i < shorty_len; ++i) {
153 char ch = shorty[i];
154 if (ch == 'D' || ch == 'J') {
155 num_bytes += 8;
156 } else if (ch == 'L') {
157 // Argument is a reference or an array. The shorty descriptor
158 // does not distinguish between these types.
159 num_bytes += sizeof(Object*);
160 } else {
161 num_bytes += 4;
162 }
163 }
164 return num_bytes;
165}
166
167class ArgArray {
168 public:
169 explicit ArgArray(Method* method) {
170 MethodHelper mh(method);
171 shorty_ = mh.GetShorty();
172 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -0700173 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800174 arg_array_ = small_arg_array_;
175 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700176 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800177 arg_array_ = large_arg_array_.get();
178 }
179 }
180
Elliott Hughes77405792012-03-15 15:22:12 -0700181 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800182 return arg_array_;
183 }
184
185 void BuildArgArray(JNIEnv* public_env, va_list ap) {
186 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700187 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800188 switch (shorty_[i]) {
189 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700190 arg_array_[offset].SetZ(va_arg(ap, jint));
191 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800192 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700193 arg_array_[offset].SetB(va_arg(ap, jint));
194 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800195 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700196 arg_array_[offset].SetC(va_arg(ap, jint));
197 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800198 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700199 arg_array_[offset].SetS(va_arg(ap, jint));
200 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800201 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700202 arg_array_[offset].SetI(va_arg(ap, jint));
Ian Rogers45619fc2012-02-29 11:15:25 -0800203 break;
204 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700205 arg_array_[offset].SetF(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800206 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700207 case 'L':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700208 arg_array_[offset].SetL(DecodeObj(env, va_arg(ap, jobject)));
Ian Rogers45619fc2012-02-29 11:15:25 -0800209 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800210 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700211 arg_array_[offset].SetD(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800212 break;
213 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700214 arg_array_[offset].SetJ(va_arg(ap, jlong));
Ian Rogers45619fc2012-02-29 11:15:25 -0800215 break;
216 }
217 }
218 }
219
220 void BuildArgArray(JNIEnv* public_env, jvalue* args) {
221 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700222 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800223 switch (shorty_[i]) {
224 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700225 arg_array_[offset].SetZ(args[offset].z);
226 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800227 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700228 arg_array_[offset].SetB(args[offset].b);
229 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800230 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700231 arg_array_[offset].SetC(args[offset].c);
232 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800233 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700234 arg_array_[offset].SetS(args[offset].s);
235 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800236 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700237 arg_array_[offset].SetI(args[offset].i);
Ian Rogers45619fc2012-02-29 11:15:25 -0800238 break;
239 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700240 arg_array_[offset].SetF(args[offset].f);
Ian Rogers45619fc2012-02-29 11:15:25 -0800241 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700242 case 'L':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700243 arg_array_[offset].SetL(DecodeObj(env, args[offset].l));
Ian Rogers45619fc2012-02-29 11:15:25 -0800244 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800245 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700246 arg_array_[offset].SetD(args[offset].d);
Ian Rogers45619fc2012-02-29 11:15:25 -0800247 break;
248 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700249 arg_array_[offset].SetJ(args[offset].j);
Ian Rogers45619fc2012-02-29 11:15:25 -0800250 break;
251 }
252 }
253 }
254
Ian Rogers45619fc2012-02-29 11:15:25 -0800255 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700256 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800257 const char* shorty_;
258 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700259 JValue* arg_array_;
260 JValue small_arg_array_[kSmallArgArraySize];
261 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800262};
263
Elliott Hughes0512f022012-03-15 22:10:52 -0700264static jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700265 if (obj == NULL) {
266 return NULL;
267 }
Elliott Hughes75770752011-08-24 17:52:38 -0700268 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700269 IndirectReferenceTable& weak_globals = vm->weak_globals;
270 MutexLock mu(vm->weak_globals_lock);
271 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
272 return reinterpret_cast<jweak>(ref);
273}
274
Elliott Hughesbf86d042011-08-31 17:53:14 -0700275// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700276template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -0700277static T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700278 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700279}
280
Elliott Hughesb264f082012-04-06 17:10:10 -0700281// TODO: can we make this available in non-debug builds if CheckJNI is on, or is it too expensive?
282#if !defined(NDEBUG)
283static void CheckMethodArguments(Method* m, JValue* args) {
284 MethodHelper mh(m);
285 ObjectArray<Class>* parameter_types = mh.GetParameterTypes();
286 CHECK(parameter_types != NULL);
287 size_t error_count = 0;
288 for (int i = 0; i < parameter_types->GetLength(); ++i) {
289 Class* parameter_type = parameter_types->Get(i);
290 if (!parameter_type->IsPrimitive()) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700291 Object* argument = args[i].GetL();
Elliott Hughesb264f082012-04-06 17:10:10 -0700292 if (argument != NULL && !argument->InstanceOf(parameter_type)) {
293 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
294 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
295 ++error_count;
296 }
297 }
298 }
299 if (error_count > 0) {
300 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
301 // with an argument.
302 JniAbort(NULL);
303 }
304}
305#else
306static void CheckMethodArguments(Method*, JValue*) { }
307#endif
308
Elliott Hughes77405792012-03-15 15:22:12 -0700309static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, JValue* args) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700310 CheckMethodArguments(method, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700311 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700312 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700313 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700314 return result;
315}
316
Ian Rogers0571d352011-11-03 19:51:38 -0700317static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700318 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800319 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700320 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800321 ArgArray arg_array(method);
322 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700323 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700324}
325
Ian Rogers0571d352011-11-03 19:51:38 -0700326static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700327 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700328}
329
Ian Rogers0571d352011-11-03 19:51:38 -0700330static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
331 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700332 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800333 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700334 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800335 ArgArray arg_array(method);
336 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700337 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700338}
339
Ian Rogers0571d352011-11-03 19:51:38 -0700340static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
341 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700342 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800343 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700344 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800345 ArgArray arg_array(method);
346 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700347 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700348}
349
Elliott Hughes6b436852011-08-12 10:16:44 -0700350// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
351// separated with slashes but aren't wrapped with "L;" like regular descriptors
352// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
353// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
354// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700355static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700356 std::string result;
357 // Add the missing "L;" if necessary.
358 if (name[0] == '[') {
359 result = name;
360 } else {
361 result += 'L';
362 result += name;
363 result += ';';
364 }
365 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700366 if (result.find('.') != std::string::npos) {
367 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
368 << "\"" << name << "\"";
369 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700370 }
371 return result;
372}
373
Ian Rogers0571d352011-11-03 19:51:38 -0700374static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700375 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800376 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700377}
378
Ian Rogers0571d352011-11-03 19:51:38 -0700379static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700380 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700381 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700382 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700383 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700384
385 Method* method = NULL;
386 if (is_static) {
387 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700388 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700389 method = c->FindVirtualMethod(name, sig);
390 if (method == NULL) {
391 // No virtual method matching the signature. Search declared
392 // private methods and constructors.
393 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700394 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700395 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700396
Elliott Hughescdf53122011-08-19 15:46:09 -0700397 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700398 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700399 return NULL;
400 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700401
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700402 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700403}
404
Ian Rogers0571d352011-11-03 19:51:38 -0700405static const ClassLoader* GetClassLoader(Thread* self) {
Elliott Hughes6a144332012-04-03 13:07:11 -0700406 Method* method = self->GetCurrentMethod();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700407 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
408 return self->GetClassLoaderOverride();
409 }
410 return method->GetDeclaringClass()->GetClassLoader();
411}
412
Ian Rogers0571d352011-11-03 19:51:38 -0700413static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700414 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700415 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700416 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700417 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700418
419 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700420 Class* field_type;
421 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
422 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700423 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700424 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700425 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700426 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700427 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700428 if (field_type == NULL) {
429 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700430 DCHECK(ts.Self()->IsExceptionPending());
431 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700432 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700433 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800434 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700435 return NULL;
436 }
437 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800438 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700439 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800440 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700441 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700442 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700443 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700444 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800445 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700446 return NULL;
447 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700448 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700449}
450
Ian Rogers0571d352011-11-03 19:51:38 -0700451static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700452 JavaVMExt* vm = ts.Vm();
453 MutexLock mu(vm->pins_lock);
454 vm->pin_table.Add(array);
455}
456
Ian Rogers0571d352011-11-03 19:51:38 -0700457static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700458 JavaVMExt* vm = ts.Vm();
459 MutexLock mu(vm->pins_lock);
460 vm->pin_table.Remove(array);
461}
462
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700463template<typename JniT, typename ArtT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700464static JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700465 CHECK_GE(length, 0); // TODO: ReportJniError
466 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700467 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700468}
469
Elliott Hughes75770752011-08-24 17:52:38 -0700470template <typename ArrayT, typename CArrayT, typename ArtArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700471static CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -0700472 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
473 PinPrimitiveArray(ts, array);
474 if (is_copy != NULL) {
475 *is_copy = JNI_FALSE;
476 }
477 return array->GetData();
478}
479
480template <typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700481static void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
Elliott Hughes75770752011-08-24 17:52:38 -0700482 if (mode != JNI_COMMIT) {
483 Array* array = Decode<Array*>(ts, java_array);
484 UnpinPrimitiveArray(ts, array);
485 }
486}
487
Ian Rogers0571d352011-11-03 19:51:38 -0700488static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700489 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700490 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700491 "%s offset=%d length=%d %s.length=%d",
492 type.c_str(), start, length, identifier, array->GetLength());
493}
Ian Rogers0571d352011-11-03 19:51:38 -0700494
495static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700496 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700497 "offset=%d length=%d string.length()=%d", start, length, array_length);
498}
Elliott Hughes814e4032011-08-23 12:07:56 -0700499
500template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700501static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700502 ArrayT* array = Decode<ArrayT*>(ts, java_array);
503 if (start < 0 || length < 0 || start + length > array->GetLength()) {
504 ThrowAIOOBE(ts, array, start, length, "src");
505 } else {
506 JavaT* data = array->GetData();
507 memcpy(buf, data + start, length * sizeof(JavaT));
508 }
509}
510
511template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700512static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700513 ArrayT* array = Decode<ArrayT*>(ts, java_array);
514 if (start < 0 || length < 0 || start + length > array->GetLength()) {
515 ThrowAIOOBE(ts, array, start, length, "dst");
516 } else {
517 JavaT* data = array->GetData();
518 memcpy(data + start, buf, length * sizeof(JavaT));
519 }
520}
521
Ian Rogers0571d352011-11-03 19:51:38 -0700522static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700523 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
524 CHECK(buffer_class.get() != NULL);
525 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
526}
527
Ian Rogers0571d352011-11-03 19:51:38 -0700528static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700529 static jclass buffer_class = InitDirectByteBufferClass(env);
530 return buffer_class;
531}
532
Elliott Hughes462c9442012-03-23 18:47:50 -0700533static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700534 if (vm == NULL || p_env == NULL) {
535 return JNI_ERR;
536 }
537
Elliott Hughes462c9442012-03-23 18:47:50 -0700538 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700539 Thread* self = Thread::Current();
540 if (self != NULL) {
541 *p_env = self->GetJniEnv();
542 return JNI_OK;
543 }
544
545 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
546
547 // No threads allowed in zygote mode.
548 if (runtime->IsZygote()) {
549 LOG(ERROR) << "Attempt to attach a thread in the zygote";
550 return JNI_ERR;
551 }
552
Elliott Hughes462c9442012-03-23 18:47:50 -0700553 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
554 const char* thread_name = NULL;
555 Object* thread_group = NULL;
556 if (args != NULL) {
557 CHECK_GE(args->version, JNI_VERSION_1_2);
558 thread_name = args->name;
559 thread_group = static_cast<Thread*>(NULL)->DecodeJObject(args->group);
Elliott Hughes75770752011-08-24 17:52:38 -0700560 }
Elliott Hughes75770752011-08-24 17:52:38 -0700561
Elliott Hughes462c9442012-03-23 18:47:50 -0700562 runtime->AttachCurrentThread(thread_name, as_daemon, thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700563 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700564 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700565}
566
Elliott Hughes79082e32011-08-25 12:07:32 -0700567class SharedLibrary {
568 public:
569 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
570 : path_(path),
571 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700572 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700573 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughese62934d2012-04-09 11:24:29 -0700574 jni_on_load_cond_("JNI_OnLoad condition variable"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700575 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700576 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700577 }
578
Elliott Hughes79082e32011-08-25 12:07:32 -0700579 Object* GetClassLoader() {
580 return class_loader_;
581 }
582
583 std::string GetPath() {
584 return path_;
585 }
586
587 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700588 * Check the result of an earlier call to JNI_OnLoad on this library.
589 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700591 bool CheckOnLoadResult() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700592 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700593 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700594 // Check this so we don't end up waiting for ourselves. We need
595 // to return "true" so the caller can continue.
596 LOG(INFO) << *self << " recursive attempt to load library "
597 << "\"" << path_ << "\"";
598 return true;
599 }
600
601 MutexLock mu(jni_on_load_lock_);
602 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800603 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
604 << "JNI_OnLoad...]";
Elliott Hughes34e06962012-04-09 13:55:55 -0700605 ScopedThreadStateChange tsc(self, kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700606 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700607 }
608
609 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800610 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
611 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700612 return okay;
613 }
614
615 void SetResult(bool result) {
616 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700617 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700618
619 // Broadcast a wakeup to anybody sleeping on the condition variable.
620 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700621 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700622 }
623
624 void* FindSymbol(const std::string& symbol_name) {
625 return dlsym(handle_, symbol_name.c_str());
626 }
627
628 private:
629 enum JNI_OnLoadState {
630 kPending,
631 kFailed,
632 kOkay,
633 };
634
635 // Path to library "/system/lib/libjni.so".
636 std::string path_;
637
638 // The void* returned by dlopen(3).
639 void* handle_;
640
641 // The ClassLoader this library is associated with.
642 Object* class_loader_;
643
644 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700645 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700646 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700647 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700648 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700649 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700650 // Result of earlier JNI_OnLoad call.
651 JNI_OnLoadState jni_on_load_result_;
652};
653
Elliott Hughes79082e32011-08-25 12:07:32 -0700654// This exists mainly to keep implementation details out of the header file.
655class Libraries {
656 public:
657 Libraries() {
658 }
659
660 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700661 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700662 }
663
664 SharedLibrary* Get(const std::string& path) {
665 return libraries_[path];
666 }
667
668 void Put(const std::string& path, SharedLibrary* library) {
669 libraries_[path] = library;
670 }
671
672 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700673 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700674 std::string jni_short_name(JniShortName(m));
675 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700676 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700677 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
678 SharedLibrary* library = it->second;
679 if (library->GetClassLoader() != declaring_class_loader) {
680 // We only search libraries loaded by the appropriate ClassLoader.
681 continue;
682 }
683 // Try the short name then the long name...
684 void* fn = library->FindSymbol(jni_short_name);
685 if (fn == NULL) {
686 fn = library->FindSymbol(jni_long_name);
687 }
688 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800689 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
690 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700691 return fn;
692 }
693 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700694 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700695 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700696 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700697 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700698 return NULL;
699 }
700
701 private:
702 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
703
704 std::map<std::string, SharedLibrary*> libraries_;
705};
706
Elliott Hughes418d20f2011-09-22 14:00:39 -0700707JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
708 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
709 Object* receiver = Decode<Object*>(env, obj);
710 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800711 ArgArray arg_array(method);
712 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700713 return InvokeWithArgArray(env, receiver, method, arg_array.get());
714}
715
Elliott Hughesd07986f2011-12-06 18:27:45 -0800716JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700717 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800718}
719
Elliott Hughescdf53122011-08-19 15:46:09 -0700720class JNI {
721 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700722
Elliott Hughescdf53122011-08-19 15:46:09 -0700723 static jint GetVersion(JNIEnv* env) {
724 ScopedJniThreadState ts(env);
725 return JNI_VERSION_1_6;
726 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700727
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
729 ScopedJniThreadState ts(env);
730 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700731 return NULL;
732 }
733
Elliott Hughescdf53122011-08-19 15:46:09 -0700734 static jclass FindClass(JNIEnv* env, const char* name) {
735 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700736 Runtime* runtime = Runtime::Current();
737 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700739 Class* c = NULL;
740 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700741 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800742 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700743 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800744 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700745 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700746 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700747 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700748
Elliott Hughescdf53122011-08-19 15:46:09 -0700749 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
750 ScopedJniThreadState ts(env);
751 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700752 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700753 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700754
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
756 ScopedJniThreadState ts(env);
757 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700758 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700759 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700760
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
762 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700763 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700764 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700766
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
768 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700769 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700770 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700771 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700772
Elliott Hughes37f7a402011-08-22 18:56:01 -0700773 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
774 ScopedJniThreadState ts(env);
775 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700776 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700777 }
778
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700779 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700781 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700782 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700784
Elliott Hughes37f7a402011-08-22 18:56:01 -0700785 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700786 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700787 Class* c1 = Decode<Class*>(ts, java_class1);
788 Class* c2 = Decode<Class*>(ts, java_class2);
789 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700790 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700791
Elliott Hughese84278b2012-03-22 10:06:53 -0700792 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700793 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700794 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700795 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700796 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700797 return JNI_TRUE;
798 } else {
799 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700800 Class* c = Decode<Class*>(ts, java_class);
801 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700802 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700804
Elliott Hughes37f7a402011-08-22 18:56:01 -0700805 static jint Throw(JNIEnv* env, jthrowable java_exception) {
806 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700807 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700808 if (exception == NULL) {
809 return JNI_ERR;
810 }
811 ts.Self()->SetException(exception);
812 return JNI_OK;
813 }
814
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700815 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700816 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700817 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700818 jmethodID mid = ((msg != NULL)
819 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
820 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700821 if (mid == NULL) {
822 return JNI_ERR;
823 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700824 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700825 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700826 return JNI_ERR;
827 }
828
829 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700830 args[0].l = s.get();
831 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
832 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700833 return JNI_ERR;
834 }
835
Elliott Hughes72025e52011-08-23 17:50:30 -0700836 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700837
Elliott Hughes37f7a402011-08-22 18:56:01 -0700838 return JNI_OK;
839 }
840
841 static jboolean ExceptionCheck(JNIEnv* env) {
842 ScopedJniThreadState ts(env);
843 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
844 }
845
846 static void ExceptionClear(JNIEnv* env) {
847 ScopedJniThreadState ts(env);
848 ts.Self()->ClearException();
849 }
850
851 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700853
854 Thread* self = ts.Self();
855 Throwable* original_exception = self->GetException();
856 self->ClearException();
857
Elliott Hughesbf86d042011-08-31 17:53:14 -0700858 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700859 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
860 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
861 if (mid == NULL) {
862 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700863 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700864 } else {
865 env->CallVoidMethod(exception.get(), mid);
866 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700867 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700868 << " thrown while calling printStackTrace";
869 self->ClearException();
870 }
871 }
872
873 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700875
Elliott Hughescdf53122011-08-19 15:46:09 -0700876 static jthrowable ExceptionOccurred(JNIEnv* env) {
877 ScopedJniThreadState ts(env);
878 Object* exception = ts.Self()->GetException();
Elliott Hughes81ff3182012-03-23 20:35:56 -0700879 return (exception != NULL) ? AddLocalReference<jthrowable>(env, exception) : NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700880 }
881
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 static void FatalError(JNIEnv* env, const char* msg) {
883 ScopedJniThreadState ts(env);
884 LOG(FATAL) << "JNI FatalError called: " << msg;
885 }
886
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700887 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700889 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
890 return JNI_ERR;
891 }
892 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 return JNI_OK;
894 }
895
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700896 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700898 Object* survivor = Decode<Object*>(ts, java_survivor);
899 ts.Env()->PopFrame();
900 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 }
902
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700903 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700905 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
906 }
907
908 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
909 // TODO: we should try to expand the table if necessary.
910 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
911 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
912 return JNI_ERR;
913 }
914 // TODO: this isn't quite right, since "capacity" includes holes.
915 size_t capacity = ts.Env()->locals.Capacity();
916 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
917 if (!okay) {
918 ts.Self()->ThrowOutOfMemoryError(caller);
919 }
920 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 }
922
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
924 ScopedJniThreadState ts(env);
925 if (obj == NULL) {
926 return NULL;
927 }
928
Elliott Hughes75770752011-08-24 17:52:38 -0700929 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 IndirectReferenceTable& globals = vm->globals;
931 MutexLock mu(vm->globals_lock);
932 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
933 return reinterpret_cast<jobject>(ref);
934 }
935
936 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
937 ScopedJniThreadState ts(env);
938 if (obj == NULL) {
939 return;
940 }
941
Elliott Hughes75770752011-08-24 17:52:38 -0700942 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 IndirectReferenceTable& globals = vm->globals;
944 MutexLock mu(vm->globals_lock);
945
946 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
947 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
948 << "failed to find entry";
949 }
950 }
951
952 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
953 ScopedJniThreadState ts(env);
954 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
955 }
956
957 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
958 ScopedJniThreadState ts(env);
959 if (obj == NULL) {
960 return;
961 }
962
Elliott Hughes75770752011-08-24 17:52:38 -0700963 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 IndirectReferenceTable& weak_globals = vm->weak_globals;
965 MutexLock mu(vm->weak_globals_lock);
966
967 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
968 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
969 << "failed to find entry";
970 }
971 }
972
973 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
974 ScopedJniThreadState ts(env);
975 if (obj == NULL) {
976 return NULL;
977 }
978
979 IndirectReferenceTable& locals = ts.Env()->locals;
980
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700981 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
983 return reinterpret_cast<jobject>(ref);
984 }
985
986 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
987 ScopedJniThreadState ts(env);
988 if (obj == NULL) {
989 return;
990 }
991
992 IndirectReferenceTable& locals = ts.Env()->locals;
993
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700994 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 if (!locals.Remove(cookie, obj)) {
996 // Attempting to delete a local reference that is not in the
997 // topmost local reference frame is a no-op. DeleteLocalRef returns
998 // void and doesn't throw any exceptions, but we should probably
999 // complain about it so the user will notice that things aren't
1000 // going quite the way they expect.
1001 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
1002 << "failed to find entry";
1003 }
1004 }
1005
1006 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
1007 ScopedJniThreadState ts(env);
1008 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
1009 ? JNI_TRUE : JNI_FALSE;
1010 }
1011
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001012 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001014 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001015 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001016 return NULL;
1017 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001018 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughese84278b2012-03-22 10:06:53 -07001021 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 ScopedJniThreadState ts(env);
1023 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001025 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 va_end(args);
1027 return result;
1028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001032 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001033 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001034 return NULL;
1035 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001036 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001037 if (result == NULL) {
1038 return NULL;
1039 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001040 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001042 if (!ts.Self()->IsExceptionPending()) {
1043 return local_result;
1044 } else {
1045 return NULL;
1046 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 }
1048
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001051 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001052 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001053 return NULL;
1054 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001055 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001056 if (result == NULL) {
1057 return NULL;
1058 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001059 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001061 if (!ts.Self()->IsExceptionPending()) {
1062 return local_result;
1063 } else {
1064 return NULL;
1065 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1069 ScopedJniThreadState ts(env);
1070 return FindMethodID(ts, c, name, sig, false);
1071 }
1072
1073 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1074 ScopedJniThreadState ts(env);
1075 return FindMethodID(ts, c, name, sig, true);
1076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 va_list ap;
1081 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001082 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001084 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001089 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001090 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 }
1092
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001095 JValue result(InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001096 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_list ap;
1102 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001103 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001105 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001110 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001115 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 va_list ap;
1121 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001122 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001124 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001129 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001134 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 va_list ap;
1140 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001141 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001143 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001148 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001153 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 }
1155
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 va_list ap;
1159 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001160 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001162 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001167 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001172 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 }
1174
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 va_list ap;
1178 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001179 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001181 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001186 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001191 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 va_list ap;
1197 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001198 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001200 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001205 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001210 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 va_list ap;
1216 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001217 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001219 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001224 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
1226
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001229 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 }
1231
Elliott Hughes72025e52011-08-23 17:50:30 -07001232 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 va_list ap;
1235 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001236 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001238 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001243 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001248 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 }
1250
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 va_list ap;
1254 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001255 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001256 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001261 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
Elliott Hughes72025e52011-08-23 17:50:30 -07001264 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001266 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 }
1268
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001269 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
1271 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001273 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001274 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 va_end(ap);
1276 return local_result;
1277 }
1278
1279 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001280 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001282 JValue result(InvokeWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001283 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
1286 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001287 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001289 JValue result(InvokeWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001290 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
1293 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001294 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 ScopedJniThreadState ts(env);
1296 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001298 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001300 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
1303 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001304 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001306 return InvokeWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
1309 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001310 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001312 return InvokeWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001315 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 ScopedJniThreadState ts(env);
1317 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001319 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001321 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
1324 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001325 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001327 return InvokeWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
1330 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001331 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001333 return InvokeWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001336 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 ScopedJniThreadState ts(env);
1338 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001339 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001340 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001342 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 }
1344
1345 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001346 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001348 return InvokeWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
1351 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001352 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001354 return InvokeWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001357 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 ScopedJniThreadState ts(env);
1359 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001360 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001361 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001363 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 }
1365
1366 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001367 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001369 return InvokeWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
1372 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001373 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001375 return InvokeWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
1377
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001378 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 ScopedJniThreadState ts(env);
1380 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001381 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001382 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001384 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
1386
1387 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001388 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001390 return InvokeWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
1392
1393 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001394 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001396 return InvokeWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001399 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 ScopedJniThreadState ts(env);
1401 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001402 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001403 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001405 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
1408 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001409 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001411 return InvokeWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
1414 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001415 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001417 return InvokeWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001420 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 ScopedJniThreadState ts(env);
1422 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001423 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001424 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001426 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 }
1428
1429 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001430 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001432 return InvokeWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
1435 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001436 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001438 return InvokeWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 }
1440
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001441 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 ScopedJniThreadState ts(env);
1443 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001444 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001445 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001447 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
1450 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001451 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001453 return InvokeWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
1456 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001457 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001459 return InvokeWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001462 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 ScopedJniThreadState ts(env);
1464 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001465 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001466 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 va_end(ap);
1468 }
1469
1470 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001471 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001473 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
1476 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001477 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001479 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 }
1481
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001482 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 ScopedJniThreadState ts(env);
1484 return FindFieldID(ts, c, name, sig, false);
1485 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001486
1487
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001488 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 ScopedJniThreadState ts(env);
1490 return FindFieldID(ts, c, name, sig, true);
1491 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001492
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001493 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001496 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001497 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001499
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001502 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001503 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 Object* o = Decode<Object*>(ts, java_object);
1509 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001510 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001517 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001518 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 }
1520
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001521#define GET_PRIMITIVE_FIELD(fn, instance) \
1522 ScopedJniThreadState ts(env); \
1523 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001524 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001525 return f->fn(o)
1526
1527#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1528 ScopedJniThreadState ts(env); \
1529 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001530 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001531 f->fn(o, value)
1532
1533 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1534 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
1536
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001537 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1538 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001539 }
1540
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001541 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1542 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1546 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001549 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1550 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001553 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1554 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 }
1556
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001557 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1558 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001561 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1562 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001565 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001566 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001569 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001570 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 }
1572
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001573 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001577 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 }
1580
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001581 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001582 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001585 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001586 GET_PRIMITIVE_FIELD(GetLong, NULL);
1587 }
1588
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001589 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1591 }
1592
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001593 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001594 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1595 }
1596
1597 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1598 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1599 }
1600
1601 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1602 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1603 }
1604
1605 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1606 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1607 }
1608
1609 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1610 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1611 }
1612
1613 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1614 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1615 }
1616
1617 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1618 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1619 }
1620
1621 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1622 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1623 }
1624
1625 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1626 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1627 }
1628
1629 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1630 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1631 }
1632
1633 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1634 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1635 }
1636
1637 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1638 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1639 }
1640
1641 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1642 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1643 }
1644
1645 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1646 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1647 }
1648
1649 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1650 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1651 }
1652
1653 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1654 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1655 }
1656
1657 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1658 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 }
1660
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001661 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 ScopedJniThreadState ts(env);
1663 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001664 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001665 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001666 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 va_end(ap);
1668 return local_result;
1669 }
1670
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001671 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001673 JValue result(InvokeWithVarArgs(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001674 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 }
1676
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001677 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001679 JValue result(InvokeWithJValues(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001680 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001683 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 ScopedJniThreadState ts(env);
1685 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001687 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001689 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 }
1691
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001692 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001694 return InvokeWithVarArgs(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 }
1696
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001697 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001699 return InvokeWithJValues(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 }
1701
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001702 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 ScopedJniThreadState ts(env);
1704 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001706 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001708 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001711 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001713 return InvokeWithVarArgs(env, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 }
1715
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001716 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001718 return InvokeWithJValues(env, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001721 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 ScopedJniThreadState ts(env);
1723 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001724 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001725 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001727 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001730 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001732 return InvokeWithVarArgs(env, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 }
1734
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001735 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001737 return InvokeWithJValues(env, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001740 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 ScopedJniThreadState ts(env);
1742 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001744 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001746 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001749 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001751 return InvokeWithVarArgs(env, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 }
1753
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001754 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001756 return InvokeWithJValues(env, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 }
1758
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001759 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 ScopedJniThreadState ts(env);
1761 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001762 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001763 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001765 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 }
1767
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001768 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001770 return InvokeWithVarArgs(env, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001773 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001775 return InvokeWithJValues(env, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 }
1777
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001778 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 ScopedJniThreadState ts(env);
1780 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001781 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001782 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001784 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001787 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001789 return InvokeWithVarArgs(env, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 }
1791
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001792 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001794 return InvokeWithJValues(env, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 }
1796
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001797 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 ScopedJniThreadState ts(env);
1799 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001800 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001801 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001803 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001806 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001808 return InvokeWithVarArgs(env, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001811 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001813 return InvokeWithJValues(env, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 }
1815
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001816 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 ScopedJniThreadState ts(env);
1818 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001819 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001820 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001822 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001825 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001827 return InvokeWithVarArgs(env, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001830 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001832 return InvokeWithJValues(env, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 }
1834
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001835 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 ScopedJniThreadState ts(env);
1837 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001838 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001839 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 va_end(ap);
1841 }
1842
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001843 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001845 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 }
1847
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001848 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001850 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 }
1852
Elliott Hughes814e4032011-08-23 12:07:56 -07001853 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001855 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001856 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 }
1858
1859 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1860 ScopedJniThreadState ts(env);
1861 if (utf == NULL) {
1862 return NULL;
1863 }
1864 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001865 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 }
1867
Elliott Hughes814e4032011-08-23 12:07:56 -07001868 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1869 ScopedJniThreadState ts(env);
1870 return Decode<String*>(ts, java_string)->GetLength();
1871 }
1872
1873 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1874 ScopedJniThreadState ts(env);
1875 return Decode<String*>(ts, java_string)->GetUtfLength();
1876 }
1877
Elliott Hughesb465ab02011-08-24 11:21:21 -07001878 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001880 String* s = Decode<String*>(ts, java_string);
1881 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1882 ThrowSIOOBE(ts, start, length, s->GetLength());
1883 } else {
1884 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1885 memcpy(buf, chars + start, length * sizeof(jchar));
1886 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001887 }
1888
Elliott Hughesb465ab02011-08-24 11:21:21 -07001889 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001890 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001891 String* s = Decode<String*>(ts, java_string);
1892 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1893 ThrowSIOOBE(ts, start, length, s->GetLength());
1894 } else {
1895 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1896 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1897 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001898 }
1899
Elliott Hughes75770752011-08-24 17:52:38 -07001900 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001901 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001902 String* s = Decode<String*>(ts, java_string);
1903 const CharArray* chars = s->GetCharArray();
1904 PinPrimitiveArray(ts, chars);
1905 if (is_copy != NULL) {
1906 *is_copy = JNI_FALSE;
1907 }
1908 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001909 }
1910
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001911 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001912 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001913 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001914 }
1915
Elliott Hughes75770752011-08-24 17:52:38 -07001916 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001917 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001918 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 }
1920
Elliott Hughes75770752011-08-24 17:52:38 -07001921 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001923 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001924 }
1925
Elliott Hughes75770752011-08-24 17:52:38 -07001926 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001927 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001928 if (java_string == NULL) {
1929 return NULL;
1930 }
1931 if (is_copy != NULL) {
1932 *is_copy = JNI_TRUE;
1933 }
1934 String* s = Decode<String*>(ts, java_string);
1935 size_t byte_count = s->GetUtfLength();
1936 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001937 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001938 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1939 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1940 bytes[byte_count] = '\0';
1941 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001942 }
1943
Elliott Hughes75770752011-08-24 17:52:38 -07001944 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001945 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001946 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001947 }
1948
Elliott Hughesbd935992011-08-22 11:59:34 -07001949 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001951 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001952 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001953 Array* array = obj->AsArray();
1954 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001955 }
1956
Elliott Hughes814e4032011-08-23 12:07:56 -07001957 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001959 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001960 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 }
1962
1963 static void SetObjectArrayElement(JNIEnv* env,
1964 jobjectArray java_array, jsize index, jobject java_value) {
1965 ScopedJniThreadState ts(env);
1966 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1967 Object* value = Decode<Object*>(ts, java_value);
1968 array->Set(index, value);
1969 }
1970
1971 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1972 ScopedJniThreadState ts(env);
1973 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1974 }
1975
1976 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1977 ScopedJniThreadState ts(env);
1978 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1979 }
1980
1981 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1982 ScopedJniThreadState ts(env);
1983 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1984 }
1985
1986 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1987 ScopedJniThreadState ts(env);
1988 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1989 }
1990
1991 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1992 ScopedJniThreadState ts(env);
1993 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1994 }
1995
1996 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1997 ScopedJniThreadState ts(env);
1998 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1999 }
2000
2001 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2002 ScopedJniThreadState ts(env);
2003 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
2004 }
2005
2006 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
2007 ScopedJniThreadState ts(env);
2008 CHECK_GE(length, 0); // TODO: ReportJniError
2009
2010 // Compute the array class corresponding to the given element class.
2011 Class* element_class = Decode<Class*>(ts, element_jclass);
2012 std::string descriptor;
2013 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002014 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002015
2016 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002017 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2018 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 return NULL;
2020 }
2021
Elliott Hughes75770752011-08-24 17:52:38 -07002022 // Allocate and initialize if necessary.
2023 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002025 if (initial_element != NULL) {
2026 Object* initial_object = Decode<Object*>(ts, initial_element);
2027 for (jsize i = 0; i < length; ++i) {
2028 result->Set(i, initial_object);
2029 }
2030 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002031 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
2034 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2035 ScopedJniThreadState ts(env);
2036 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2037 }
2038
Ian Rogersa15e67d2012-02-28 13:51:55 -08002039 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002040 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002041 Array* array = Decode<Array*>(ts, java_array);
2042 PinPrimitiveArray(ts, array);
2043 if (is_copy != NULL) {
2044 *is_copy = JNI_FALSE;
2045 }
2046 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002047 }
2048
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002049 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002050 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002051 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002052 }
2053
Elliott Hughes75770752011-08-24 17:52:38 -07002054 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002056 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes75770752011-08-24 17:52:38 -07002059 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002061 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes75770752011-08-24 17:52:38 -07002064 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002066 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes75770752011-08-24 17:52:38 -07002069 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002071 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes75770752011-08-24 17:52:38 -07002074 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002076 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Elliott Hughes75770752011-08-24 17:52:38 -07002079 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002081 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Elliott Hughes75770752011-08-24 17:52:38 -07002084 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002086 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes75770752011-08-24 17:52:38 -07002089 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002091 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002094 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002096 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002099 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002101 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002104 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002106 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002109 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002111 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002114 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002116 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002119 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002121 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002124 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002126 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002129 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002131 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
2133
Elliott Hughes814e4032011-08-23 12:07:56 -07002134 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002136 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138
Elliott Hughes814e4032011-08-23 12:07:56 -07002139 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002141 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 }
2143
Elliott Hughes814e4032011-08-23 12:07:56 -07002144 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002146 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
Elliott Hughes814e4032011-08-23 12:07:56 -07002149 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002151 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
2153
Elliott Hughes814e4032011-08-23 12:07:56 -07002154 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002156 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 }
2158
Elliott Hughes814e4032011-08-23 12:07:56 -07002159 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002161 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 }
2163
Elliott Hughes814e4032011-08-23 12:07:56 -07002164 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002166 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 }
2168
Elliott Hughes814e4032011-08-23 12:07:56 -07002169 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002171 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 }
2173
Elliott Hughes814e4032011-08-23 12:07:56 -07002174 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002176 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178
Elliott Hughes814e4032011-08-23 12:07:56 -07002179 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002181 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 }
2183
Elliott Hughes814e4032011-08-23 12:07:56 -07002184 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002186 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 }
2188
Elliott Hughes814e4032011-08-23 12:07:56 -07002189 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002191 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
2193
Elliott Hughes814e4032011-08-23 12:07:56 -07002194 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002196 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 }
2198
Elliott Hughes814e4032011-08-23 12:07:56 -07002199 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002201 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 }
2203
Elliott Hughes814e4032011-08-23 12:07:56 -07002204 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002206 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
Elliott Hughes814e4032011-08-23 12:07:56 -07002209 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002211 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 }
2213
Elliott Hughes5174fe62011-08-23 15:12:35 -07002214 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002216 Class* c = Decode<Class*>(ts, java_class);
2217
Elliott Hughes5174fe62011-08-23 15:12:35 -07002218 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 const char* name = methods[i].name;
2220 const char* sig = methods[i].signature;
2221
2222 if (*sig == '!') {
2223 // TODO: fast jni. it's too noisy to log all these.
2224 ++sig;
2225 }
2226
Elliott Hughes5174fe62011-08-23 15:12:35 -07002227 Method* m = c->FindDirectMethod(name, sig);
2228 if (m == NULL) {
2229 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002231 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002232 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002233 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002235 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002236 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002237 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 return JNI_ERR;
2239 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002240
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002241 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002242
Ian Rogers60db5ab2012-02-20 17:02:00 -08002243 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 }
2245 return JNI_OK;
2246 }
2247
Elliott Hughes5174fe62011-08-23 15:12:35 -07002248 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002250 Class* c = Decode<Class*>(ts, java_class);
2251
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002252 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002253
2254 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2255 Method* m = c->GetDirectMethod(i);
2256 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002257 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002258 }
2259 }
2260 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2261 Method* m = c->GetVirtualMethod(i);
2262 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002263 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002264 }
2265 }
2266
2267 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 }
2269
Elliott Hughes72025e52011-08-23 17:50:30 -07002270 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002272 Object* o = Decode<Object*>(ts, java_object);
2273 o->MonitorEnter(ts.Self());
2274 if (ts.Self()->IsExceptionPending()) {
2275 return JNI_ERR;
2276 }
2277 ts.Env()->monitors.Add(o);
2278 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002279 }
2280
Elliott Hughes72025e52011-08-23 17:50:30 -07002281 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002282 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002283 Object* o = Decode<Object*>(ts, java_object);
2284 o->MonitorExit(ts.Self());
2285 if (ts.Self()->IsExceptionPending()) {
2286 return JNI_ERR;
2287 }
2288 ts.Env()->monitors.Remove(o);
2289 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 }
2291
2292 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2293 ScopedJniThreadState ts(env);
2294 Runtime* runtime = Runtime::Current();
2295 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002296 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002297 } else {
2298 *vm = NULL;
2299 }
2300 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2301 }
2302
Elliott Hughescdf53122011-08-19 15:46:09 -07002303 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2304 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002305
2306 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002307 CHECK(address != NULL); // TODO: ReportJniError
2308 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002309
2310 jclass buffer_class = GetDirectByteBufferClass(env);
2311 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2312 if (mid == NULL) {
2313 return NULL;
2314 }
2315
2316 // At the moment, the Java side is limited to 32 bits.
2317 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2318 CHECK_LE(capacity, 0xffffffff);
2319 jint address_arg = reinterpret_cast<jint>(address);
2320 jint capacity_arg = static_cast<jint>(capacity);
2321
2322 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2323 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002324 }
2325
Elliott Hughesb465ab02011-08-24 11:21:21 -07002326 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002327 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002328 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2329 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002330 }
2331
Elliott Hughesb465ab02011-08-24 11:21:21 -07002332 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002333 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002334 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2335 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002336 }
2337
Elliott Hughesb465ab02011-08-24 11:21:21 -07002338 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002339 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002340
Elliott Hughes75770752011-08-24 17:52:38 -07002341 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002342
2343 // Do we definitely know what kind of reference this is?
2344 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2345 IndirectRefKind kind = GetIndirectRefKind(ref);
2346 switch (kind) {
2347 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002348 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2349 return JNILocalRefType;
2350 }
2351 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002352 case kGlobal:
2353 return JNIGlobalRefType;
2354 case kWeakGlobal:
2355 return JNIWeakGlobalRefType;
2356 case kSirtOrInvalid:
2357 // Is it in a stack IRT?
TDYa12728f1a142012-03-15 21:51:52 -07002358 if (ts.Self()->StackReferencesContain(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002359 return JNILocalRefType;
2360 }
2361
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002362 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002363 return JNIInvalidRefType;
2364 }
2365
Elliott Hughesb465ab02011-08-24 11:21:21 -07002366 // If we're handing out direct pointers, check whether it's a direct pointer
2367 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002368 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002369 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002370 return JNILocalRefType;
2371 }
2372 }
2373
2374 return JNIInvalidRefType;
2375 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002376 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2377 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002378 }
2379};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002380
Elliott Hughes88c5c352012-03-15 18:49:48 -07002381const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002382 NULL, // reserved0.
2383 NULL, // reserved1.
2384 NULL, // reserved2.
2385 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002386 JNI::GetVersion,
2387 JNI::DefineClass,
2388 JNI::FindClass,
2389 JNI::FromReflectedMethod,
2390 JNI::FromReflectedField,
2391 JNI::ToReflectedMethod,
2392 JNI::GetSuperclass,
2393 JNI::IsAssignableFrom,
2394 JNI::ToReflectedField,
2395 JNI::Throw,
2396 JNI::ThrowNew,
2397 JNI::ExceptionOccurred,
2398 JNI::ExceptionDescribe,
2399 JNI::ExceptionClear,
2400 JNI::FatalError,
2401 JNI::PushLocalFrame,
2402 JNI::PopLocalFrame,
2403 JNI::NewGlobalRef,
2404 JNI::DeleteGlobalRef,
2405 JNI::DeleteLocalRef,
2406 JNI::IsSameObject,
2407 JNI::NewLocalRef,
2408 JNI::EnsureLocalCapacity,
2409 JNI::AllocObject,
2410 JNI::NewObject,
2411 JNI::NewObjectV,
2412 JNI::NewObjectA,
2413 JNI::GetObjectClass,
2414 JNI::IsInstanceOf,
2415 JNI::GetMethodID,
2416 JNI::CallObjectMethod,
2417 JNI::CallObjectMethodV,
2418 JNI::CallObjectMethodA,
2419 JNI::CallBooleanMethod,
2420 JNI::CallBooleanMethodV,
2421 JNI::CallBooleanMethodA,
2422 JNI::CallByteMethod,
2423 JNI::CallByteMethodV,
2424 JNI::CallByteMethodA,
2425 JNI::CallCharMethod,
2426 JNI::CallCharMethodV,
2427 JNI::CallCharMethodA,
2428 JNI::CallShortMethod,
2429 JNI::CallShortMethodV,
2430 JNI::CallShortMethodA,
2431 JNI::CallIntMethod,
2432 JNI::CallIntMethodV,
2433 JNI::CallIntMethodA,
2434 JNI::CallLongMethod,
2435 JNI::CallLongMethodV,
2436 JNI::CallLongMethodA,
2437 JNI::CallFloatMethod,
2438 JNI::CallFloatMethodV,
2439 JNI::CallFloatMethodA,
2440 JNI::CallDoubleMethod,
2441 JNI::CallDoubleMethodV,
2442 JNI::CallDoubleMethodA,
2443 JNI::CallVoidMethod,
2444 JNI::CallVoidMethodV,
2445 JNI::CallVoidMethodA,
2446 JNI::CallNonvirtualObjectMethod,
2447 JNI::CallNonvirtualObjectMethodV,
2448 JNI::CallNonvirtualObjectMethodA,
2449 JNI::CallNonvirtualBooleanMethod,
2450 JNI::CallNonvirtualBooleanMethodV,
2451 JNI::CallNonvirtualBooleanMethodA,
2452 JNI::CallNonvirtualByteMethod,
2453 JNI::CallNonvirtualByteMethodV,
2454 JNI::CallNonvirtualByteMethodA,
2455 JNI::CallNonvirtualCharMethod,
2456 JNI::CallNonvirtualCharMethodV,
2457 JNI::CallNonvirtualCharMethodA,
2458 JNI::CallNonvirtualShortMethod,
2459 JNI::CallNonvirtualShortMethodV,
2460 JNI::CallNonvirtualShortMethodA,
2461 JNI::CallNonvirtualIntMethod,
2462 JNI::CallNonvirtualIntMethodV,
2463 JNI::CallNonvirtualIntMethodA,
2464 JNI::CallNonvirtualLongMethod,
2465 JNI::CallNonvirtualLongMethodV,
2466 JNI::CallNonvirtualLongMethodA,
2467 JNI::CallNonvirtualFloatMethod,
2468 JNI::CallNonvirtualFloatMethodV,
2469 JNI::CallNonvirtualFloatMethodA,
2470 JNI::CallNonvirtualDoubleMethod,
2471 JNI::CallNonvirtualDoubleMethodV,
2472 JNI::CallNonvirtualDoubleMethodA,
2473 JNI::CallNonvirtualVoidMethod,
2474 JNI::CallNonvirtualVoidMethodV,
2475 JNI::CallNonvirtualVoidMethodA,
2476 JNI::GetFieldID,
2477 JNI::GetObjectField,
2478 JNI::GetBooleanField,
2479 JNI::GetByteField,
2480 JNI::GetCharField,
2481 JNI::GetShortField,
2482 JNI::GetIntField,
2483 JNI::GetLongField,
2484 JNI::GetFloatField,
2485 JNI::GetDoubleField,
2486 JNI::SetObjectField,
2487 JNI::SetBooleanField,
2488 JNI::SetByteField,
2489 JNI::SetCharField,
2490 JNI::SetShortField,
2491 JNI::SetIntField,
2492 JNI::SetLongField,
2493 JNI::SetFloatField,
2494 JNI::SetDoubleField,
2495 JNI::GetStaticMethodID,
2496 JNI::CallStaticObjectMethod,
2497 JNI::CallStaticObjectMethodV,
2498 JNI::CallStaticObjectMethodA,
2499 JNI::CallStaticBooleanMethod,
2500 JNI::CallStaticBooleanMethodV,
2501 JNI::CallStaticBooleanMethodA,
2502 JNI::CallStaticByteMethod,
2503 JNI::CallStaticByteMethodV,
2504 JNI::CallStaticByteMethodA,
2505 JNI::CallStaticCharMethod,
2506 JNI::CallStaticCharMethodV,
2507 JNI::CallStaticCharMethodA,
2508 JNI::CallStaticShortMethod,
2509 JNI::CallStaticShortMethodV,
2510 JNI::CallStaticShortMethodA,
2511 JNI::CallStaticIntMethod,
2512 JNI::CallStaticIntMethodV,
2513 JNI::CallStaticIntMethodA,
2514 JNI::CallStaticLongMethod,
2515 JNI::CallStaticLongMethodV,
2516 JNI::CallStaticLongMethodA,
2517 JNI::CallStaticFloatMethod,
2518 JNI::CallStaticFloatMethodV,
2519 JNI::CallStaticFloatMethodA,
2520 JNI::CallStaticDoubleMethod,
2521 JNI::CallStaticDoubleMethodV,
2522 JNI::CallStaticDoubleMethodA,
2523 JNI::CallStaticVoidMethod,
2524 JNI::CallStaticVoidMethodV,
2525 JNI::CallStaticVoidMethodA,
2526 JNI::GetStaticFieldID,
2527 JNI::GetStaticObjectField,
2528 JNI::GetStaticBooleanField,
2529 JNI::GetStaticByteField,
2530 JNI::GetStaticCharField,
2531 JNI::GetStaticShortField,
2532 JNI::GetStaticIntField,
2533 JNI::GetStaticLongField,
2534 JNI::GetStaticFloatField,
2535 JNI::GetStaticDoubleField,
2536 JNI::SetStaticObjectField,
2537 JNI::SetStaticBooleanField,
2538 JNI::SetStaticByteField,
2539 JNI::SetStaticCharField,
2540 JNI::SetStaticShortField,
2541 JNI::SetStaticIntField,
2542 JNI::SetStaticLongField,
2543 JNI::SetStaticFloatField,
2544 JNI::SetStaticDoubleField,
2545 JNI::NewString,
2546 JNI::GetStringLength,
2547 JNI::GetStringChars,
2548 JNI::ReleaseStringChars,
2549 JNI::NewStringUTF,
2550 JNI::GetStringUTFLength,
2551 JNI::GetStringUTFChars,
2552 JNI::ReleaseStringUTFChars,
2553 JNI::GetArrayLength,
2554 JNI::NewObjectArray,
2555 JNI::GetObjectArrayElement,
2556 JNI::SetObjectArrayElement,
2557 JNI::NewBooleanArray,
2558 JNI::NewByteArray,
2559 JNI::NewCharArray,
2560 JNI::NewShortArray,
2561 JNI::NewIntArray,
2562 JNI::NewLongArray,
2563 JNI::NewFloatArray,
2564 JNI::NewDoubleArray,
2565 JNI::GetBooleanArrayElements,
2566 JNI::GetByteArrayElements,
2567 JNI::GetCharArrayElements,
2568 JNI::GetShortArrayElements,
2569 JNI::GetIntArrayElements,
2570 JNI::GetLongArrayElements,
2571 JNI::GetFloatArrayElements,
2572 JNI::GetDoubleArrayElements,
2573 JNI::ReleaseBooleanArrayElements,
2574 JNI::ReleaseByteArrayElements,
2575 JNI::ReleaseCharArrayElements,
2576 JNI::ReleaseShortArrayElements,
2577 JNI::ReleaseIntArrayElements,
2578 JNI::ReleaseLongArrayElements,
2579 JNI::ReleaseFloatArrayElements,
2580 JNI::ReleaseDoubleArrayElements,
2581 JNI::GetBooleanArrayRegion,
2582 JNI::GetByteArrayRegion,
2583 JNI::GetCharArrayRegion,
2584 JNI::GetShortArrayRegion,
2585 JNI::GetIntArrayRegion,
2586 JNI::GetLongArrayRegion,
2587 JNI::GetFloatArrayRegion,
2588 JNI::GetDoubleArrayRegion,
2589 JNI::SetBooleanArrayRegion,
2590 JNI::SetByteArrayRegion,
2591 JNI::SetCharArrayRegion,
2592 JNI::SetShortArrayRegion,
2593 JNI::SetIntArrayRegion,
2594 JNI::SetLongArrayRegion,
2595 JNI::SetFloatArrayRegion,
2596 JNI::SetDoubleArrayRegion,
2597 JNI::RegisterNatives,
2598 JNI::UnregisterNatives,
2599 JNI::MonitorEnter,
2600 JNI::MonitorExit,
2601 JNI::GetJavaVM,
2602 JNI::GetStringRegion,
2603 JNI::GetStringUTFRegion,
2604 JNI::GetPrimitiveArrayCritical,
2605 JNI::ReleasePrimitiveArrayCritical,
2606 JNI::GetStringCritical,
2607 JNI::ReleaseStringCritical,
2608 JNI::NewWeakGlobalRef,
2609 JNI::DeleteWeakGlobalRef,
2610 JNI::ExceptionCheck,
2611 JNI::NewDirectByteBuffer,
2612 JNI::GetDirectBufferAddress,
2613 JNI::GetDirectBufferCapacity,
2614 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002615};
2616
Elliott Hughes75770752011-08-24 17:52:38 -07002617JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002618 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002619 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002620 local_ref_cookie(IRT_FIRST_SEGMENT),
2621 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002622 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002623 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002624 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002625 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002626 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002627 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002628 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002629 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2630 // errors will ensue.
2631 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2632 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002633}
2634
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002635JNIEnvExt::~JNIEnvExt() {
2636}
2637
Elliott Hughes88c5c352012-03-15 18:49:48 -07002638void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2639 check_jni = enabled;
2640 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002641}
2642
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002643void JNIEnvExt::DumpReferenceTables() {
2644 locals.Dump();
2645 monitors.Dump();
2646}
2647
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002648void JNIEnvExt::PushFrame(int /*capacity*/) {
2649 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002650 stacked_local_ref_cookies.push_back(local_ref_cookie);
2651 local_ref_cookie = locals.GetSegmentState();
2652}
2653
2654void JNIEnvExt::PopFrame() {
2655 locals.SetSegmentState(local_ref_cookie);
2656 local_ref_cookie = stacked_local_ref_cookies.back();
2657 stacked_local_ref_cookies.pop_back();
2658}
2659
Carl Shapiroea4dca82011-08-01 13:45:38 -07002660// JNI Invocation interface.
2661
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002662extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2663 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2664 if (args->version < JNI_VERSION_1_2) {
2665 return JNI_EVERSION;
2666 }
2667 Runtime::Options options;
2668 for (int i = 0; i < args->nOptions; ++i) {
2669 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002670 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002671 }
2672 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002673 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002674 if (runtime == NULL) {
2675 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002676 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002677 runtime->Start();
2678 *p_env = Thread::Current()->GetJniEnv();
2679 *p_vm = runtime->GetJavaVM();
2680 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002681}
2682
Elliott Hughesf2682d52011-08-15 16:37:04 -07002683extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002684 Runtime* runtime = Runtime::Current();
2685 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002686 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002687 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002688 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002689 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002690 }
2691 return JNI_OK;
2692}
2693
2694// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002695extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002696 return JNI_ERR;
2697}
2698
Elliott Hughescdf53122011-08-19 15:46:09 -07002699class JII {
2700 public:
2701 static jint DestroyJavaVM(JavaVM* vm) {
2702 if (vm == NULL) {
2703 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002704 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002705 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2706 delete raw_vm->runtime;
2707 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002708 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002709
Elliott Hughescdf53122011-08-19 15:46:09 -07002710 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002711 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002712 }
2713
2714 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002715 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002716 }
2717
2718 static jint DetachCurrentThread(JavaVM* vm) {
2719 if (vm == NULL) {
2720 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002721 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002722 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2723 Runtime* runtime = raw_vm->runtime;
2724 runtime->DetachCurrentThread();
2725 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002726 }
2727
2728 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2729 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2730 return JNI_EVERSION;
2731 }
2732 if (vm == NULL || env == NULL) {
2733 return JNI_ERR;
2734 }
2735 Thread* thread = Thread::Current();
2736 if (thread == NULL) {
2737 *env = NULL;
2738 return JNI_EDETACHED;
2739 }
2740 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002741 return JNI_OK;
2742 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002743};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002744
Elliott Hughes88c5c352012-03-15 18:49:48 -07002745const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002746 NULL, // reserved0
2747 NULL, // reserved1
2748 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002749 JII::DestroyJavaVM,
2750 JII::AttachCurrentThread,
2751 JII::DetachCurrentThread,
2752 JII::GetEnv,
2753 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002754};
2755
Elliott Hughesa0957642011-09-02 14:27:33 -07002756JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002757 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002758 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002759 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002760 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002761 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002762 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002763 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002764 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002765 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002766 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002767 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002768 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002769 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002770 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002771 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002772 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002773 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002774 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002775 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002776}
2777
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002778JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002779 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002780}
2781
Elliott Hughes88c5c352012-03-15 18:49:48 -07002782void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2783 check_jni = enabled;
2784 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002785}
2786
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002787void JavaVMExt::DumpReferenceTables() {
2788 {
2789 MutexLock mu(globals_lock);
2790 globals.Dump();
2791 }
2792 {
2793 MutexLock mu(weak_globals_lock);
2794 weak_globals.Dump();
2795 }
2796 {
2797 MutexLock mu(pins_lock);
2798 pin_table.Dump();
2799 }
2800}
2801
Elliott Hughes75770752011-08-24 17:52:38 -07002802bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2803 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002804
2805 // See if we've already loaded this library. If we have, and the class loader
2806 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002807 // TODO: for better results we should canonicalize the pathname (or even compare
2808 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002809 SharedLibrary* library;
2810 {
2811 // TODO: move the locking (and more of this logic) into Libraries.
2812 MutexLock mu(libraries_lock);
2813 library = libraries->Get(path);
2814 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002815 if (library != NULL) {
2816 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002817 // The library will be associated with class_loader. The JNI
2818 // spec says we can't load the same library into more than one
2819 // class loader.
2820 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2821 "ClassLoader %p; can't open in ClassLoader %p",
2822 path.c_str(), library->GetClassLoader(), class_loader);
2823 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002824 return false;
2825 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002826 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2827 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002828 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002829 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2830 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002831 return false;
2832 }
2833 return true;
2834 }
2835
2836 // Open the shared library. Because we're using a full path, the system
2837 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2838 // resolve this library's dependencies though.)
2839
2840 // Failures here are expected when java.library.path has several entries
2841 // and we have to hunt for the lib.
2842
2843 // The current version of the dynamic linker prints detailed information
2844 // about dlopen() failures. Some things to check if the message is
2845 // cryptic:
2846 // - make sure the library exists on the device
2847 // - verify that the right path is being opened (the debug log message
2848 // above can help with that)
2849 // - check to see if the library is valid (e.g. not zero bytes long)
2850 // - check config/prelink-linux-arm.map to ensure that the library
2851 // is listed and is not being overrun by the previous entry (if
2852 // loading suddenly stops working on a prelinked library, this is
2853 // a good one to check)
2854 // - write a trivial app that calls sleep() then dlopen(), attach
2855 // to it with "strace -p <pid>" while it sleeps, and watch for
2856 // attempts to open nonexistent dependent shared libs
2857
2858 // TODO: automate some of these checks!
2859
2860 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002861 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002862 // the GC to ignore us.
2863 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002864 void* handle = NULL;
2865 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002866 ScopedThreadStateChange tsc(self, kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002867 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002868 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002869
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002870 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002871
2872 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002873 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002874 return false;
2875 }
2876
2877 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002878 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002879 // TODO: move the locking (and more of this logic) into Libraries.
2880 MutexLock mu(libraries_lock);
2881 library = libraries->Get(path);
2882 if (library != NULL) {
2883 LOG(INFO) << "WOW: we lost a race to add shared library: "
2884 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002885 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002886 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002887 library = new SharedLibrary(path, handle, class_loader);
2888 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002889 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002890
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002891 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002892
2893 bool result = true;
2894 void* sym = dlsym(handle, "JNI_OnLoad");
2895 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002896 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002897 } else {
2898 // Call JNI_OnLoad. We have to override the current class
2899 // loader, which will always be "null" since the stuff at the
2900 // top of the stack is around Runtime.loadLibrary(). (See
2901 // the comments in the JNI FindClass function.)
2902 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2903 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002904 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002905 self->SetClassLoaderOverride(class_loader);
2906
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002907 int version = 0;
2908 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002909 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002910 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002911 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002912 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002913
Brian Carlstromaded5f72011-10-07 17:15:04 -07002914 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002915
2916 if (version != JNI_VERSION_1_2 &&
2917 version != JNI_VERSION_1_4 &&
2918 version != JNI_VERSION_1_6) {
2919 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2920 << "bad version: " << version;
2921 // It's unwise to call dlclose() here, but we can mark it
2922 // as bad and ensure that future load attempts will fail.
2923 // We don't know how far JNI_OnLoad got, so there could
2924 // be some partially-initialized stuff accessible through
2925 // newly-registered native method calls. We could try to
2926 // unregister them, but that doesn't seem worthwhile.
2927 result = false;
2928 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002929 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2930 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002931 }
2932 }
2933
2934 library->SetResult(result);
2935 return result;
2936}
2937
2938void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2939 CHECK(m->IsNative());
2940
2941 Class* c = m->GetDeclaringClass();
2942
2943 // If this is a static method, it could be called before the class
2944 // has been initialized.
2945 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002946 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002947 return NULL;
2948 }
2949 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002950 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002951 }
2952
Brian Carlstrom16192862011-09-12 17:50:06 -07002953 std::string detail;
2954 void* native_method;
2955 {
2956 MutexLock mu(libraries_lock);
2957 native_method = libraries->FindNativeMethod(m, detail);
2958 }
2959 // throwing can cause libraries_lock to be reacquired
2960 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002961 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002962 }
2963 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002964}
2965
Elliott Hughes410c0c82011-09-01 17:58:25 -07002966void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2967 {
2968 MutexLock mu(globals_lock);
2969 globals.VisitRoots(visitor, arg);
2970 }
2971 {
2972 MutexLock mu(pins_lock);
2973 pin_table.VisitRoots(visitor, arg);
2974 }
2975 // The weak_globals table is visited by the GC itself (because it mutates the table).
2976}
2977
Elliott Hughes844f9a02012-01-24 20:19:58 -08002978jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
2979 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2980 if (c.get() == NULL) {
2981 return NULL;
2982 }
2983 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
2984}
2985
Ian Rogersdf20fe02011-07-20 20:34:16 -07002986} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002987
2988std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2989 switch (rhs) {
2990 case JNIInvalidRefType:
2991 os << "JNIInvalidRefType";
2992 return os;
2993 case JNILocalRefType:
2994 os << "JNILocalRefType";
2995 return os;
2996 case JNIGlobalRefType:
2997 os << "JNIGlobalRefType";
2998 return os;
2999 case JNIWeakGlobalRefType:
3000 os << "JNIWeakGlobalRefType";
3001 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003002 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003003 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003004 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003005 }
3006}