blob: 75ab1f07d8e548bbab3f8a59b947440e86b39c67 [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>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Elliott Hughes40ef99e2011-08-11 17:44:34 -070025#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070027#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "logging.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029#include "mutex.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070030#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070032#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070033#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070035#include "ScopedLocalRef.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070036#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070037#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070038#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070039#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070040#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070041
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070042namespace art {
43
Elliott Hughes2ced6a52011-10-16 18:44:48 -070044static const size_t kMonitorsInitial = 32; // Arbitrary.
45static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
46
47static const size_t kLocalsInitial = 64; // Arbitrary.
48static const size_t kLocalsMax = 512; // Arbitrary sanity check.
49
50static const size_t kPinTableInitial = 16; // Arbitrary.
51static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
52
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053static size_t gGlobalsInitial = 512; // Arbitrary.
54static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070055
56static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
57static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
58
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
60 size_t method_count) {
Elliott Hugheseac76672012-05-24 21:56:51 -070061 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
62 if (c.get() == NULL) {
63 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
64 }
65 if (env->RegisterNatives(c.get(), methods, method_count) != JNI_OK) {
66 LOG(FATAL) << "Failed to register natives methods: " << jni_class_name;
67 }
68}
69
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070070void SetJniGlobalsMax(size_t max) {
71 if (max != 0) {
72 gGlobalsMax = max;
73 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
74 }
75}
Ian Rogersdf20fe02011-07-20 20:34:16 -070076
Ian Rogers45619fc2012-02-29 11:15:25 -080077size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
78 size_t num_bytes = 0;
79 for (size_t i = 1; i < shorty_len; ++i) {
80 char ch = shorty[i];
81 if (ch == 'D' || ch == 'J') {
82 num_bytes += 8;
83 } else if (ch == 'L') {
84 // Argument is a reference or an array. The shorty descriptor
85 // does not distinguish between these types.
86 num_bytes += sizeof(Object*);
87 } else {
88 num_bytes += 4;
89 }
90 }
91 return num_bytes;
92}
93
94class ArgArray {
95 public:
Mathieu Chartier66f19252012-09-18 08:57:04 -070096 explicit ArgArray(AbstractMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers45619fc2012-02-29 11:15:25 -080097 MethodHelper mh(method);
98 shorty_ = mh.GetShorty();
99 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -0700100 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800101 arg_array_ = small_arg_array_;
102 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700103 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800104 arg_array_ = large_arg_array_.get();
105 }
106 }
107
Elliott Hughes77405792012-03-15 15:22:12 -0700108 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800109 return arg_array_;
110 }
111
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 void BuildArgArray(const ScopedObjectAccess& soa, va_list ap)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes77405792012-03-15 15:22:12 -0700114 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800115 switch (shorty_[i]) {
116 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700117 arg_array_[offset].SetZ(va_arg(ap, jint));
118 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800119 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700120 arg_array_[offset].SetB(va_arg(ap, jint));
121 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800122 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700123 arg_array_[offset].SetC(va_arg(ap, jint));
124 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800125 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700126 arg_array_[offset].SetS(va_arg(ap, jint));
127 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800128 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700129 arg_array_[offset].SetI(va_arg(ap, jint));
Ian Rogers45619fc2012-02-29 11:15:25 -0800130 break;
131 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700132 arg_array_[offset].SetF(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800133 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700134 case 'L':
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 arg_array_[offset].SetL(soa.Decode<Object*>(va_arg(ap, jobject)));
Ian Rogers45619fc2012-02-29 11:15:25 -0800136 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800137 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700138 arg_array_[offset].SetD(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800139 break;
140 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700141 arg_array_[offset].SetJ(va_arg(ap, jlong));
Ian Rogers45619fc2012-02-29 11:15:25 -0800142 break;
143 }
144 }
145 }
146
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 void BuildArgArray(const ScopedObjectAccess& soa, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes77405792012-03-15 15:22:12 -0700149 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800150 switch (shorty_[i]) {
151 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700152 arg_array_[offset].SetZ(args[offset].z);
153 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800154 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700155 arg_array_[offset].SetB(args[offset].b);
156 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800157 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700158 arg_array_[offset].SetC(args[offset].c);
159 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800160 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700161 arg_array_[offset].SetS(args[offset].s);
162 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800163 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700164 arg_array_[offset].SetI(args[offset].i);
Ian Rogers45619fc2012-02-29 11:15:25 -0800165 break;
166 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700167 arg_array_[offset].SetF(args[offset].f);
Ian Rogers45619fc2012-02-29 11:15:25 -0800168 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700169 case 'L':
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 arg_array_[offset].SetL(soa.Decode<Object*>(args[offset].l));
Ian Rogers45619fc2012-02-29 11:15:25 -0800171 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800172 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700173 arg_array_[offset].SetD(args[offset].d);
Ian Rogers45619fc2012-02-29 11:15:25 -0800174 break;
175 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700176 arg_array_[offset].SetJ(args[offset].j);
Ian Rogers45619fc2012-02-29 11:15:25 -0800177 break;
178 }
179 }
180 }
181
Ian Rogers45619fc2012-02-29 11:15:25 -0800182 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700183 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800184 const char* shorty_;
185 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700186 JValue* arg_array_;
187 JValue small_arg_array_[kSmallArgArraySize];
188 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800189};
190
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700192 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700193 if (obj == NULL) {
194 return NULL;
195 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700196 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700197 IndirectReferenceTable& weak_globals = vm->weak_globals;
198 MutexLock mu(vm->weak_globals_lock);
199 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
200 return reinterpret_cast<jweak>(ref);
201}
202
Mathieu Chartier66f19252012-09-18 08:57:04 -0700203static void CheckMethodArguments(AbstractMethod* m, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700204 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700205 MethodHelper mh(m);
206 ObjectArray<Class>* parameter_types = mh.GetParameterTypes();
207 CHECK(parameter_types != NULL);
208 size_t error_count = 0;
209 for (int i = 0; i < parameter_types->GetLength(); ++i) {
210 Class* parameter_type = parameter_types->Get(i);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700211 // TODO: check primitives are in range.
Elliott Hughesb264f082012-04-06 17:10:10 -0700212 if (!parameter_type->IsPrimitive()) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700213 Object* argument = args[i].GetL();
Elliott Hughesb264f082012-04-06 17:10:10 -0700214 if (argument != NULL && !argument->InstanceOf(parameter_type)) {
215 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
216 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
217 ++error_count;
218 }
219 }
220 }
221 if (error_count > 0) {
222 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
223 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700224 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700225 }
226}
Elliott Hughesb264f082012-04-06 17:10:10 -0700227
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228static JValue InvokeWithArgArray(const ScopedObjectAccess& soa, Object* receiver,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700229 AbstractMethod* method, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 if (UNLIKELY(soa.Env()->check_jni)) {
Elliott Hughes4cacde82012-04-11 18:32:27 -0700232 CheckMethodArguments(method, args);
233 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700234 JValue result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700235 method->Invoke(soa.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700236 return result;
237}
238
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700239static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
240 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700241 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700243 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800244 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245 arg_array.BuildArgArray(soa, args);
246 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700247}
248
Mathieu Chartier66f19252012-09-18 08:57:04 -0700249static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700251 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700252}
253
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
255 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700256 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700258 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800259 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700260 arg_array.BuildArgArray(soa, args);
261 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700262}
263
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
265 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700266 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700268 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800269 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 arg_array.BuildArgArray(soa, args);
271 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700272}
273
Elliott Hughes6b436852011-08-12 10:16:44 -0700274// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
275// separated with slashes but aren't wrapped with "L;" like regular descriptors
276// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
277// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
278// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700279static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700280 std::string result;
281 // Add the missing "L;" if necessary.
282 if (name[0] == '[') {
283 result = name;
284 } else {
285 result += 'L';
286 result += name;
287 result += ';';
288 }
289 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700290 if (result.find('.') != std::string::npos) {
291 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
292 << "\"" << name << "\"";
293 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700294 }
295 return result;
296}
297
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
299 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800302 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700303}
304
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
306 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700307 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700309 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700310 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700311 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700312
Mathieu Chartier66f19252012-09-18 08:57:04 -0700313 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700314 if (is_static) {
315 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700316 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700317 method = c->FindVirtualMethod(name, sig);
318 if (method == NULL) {
319 // No virtual method matching the signature. Search declared
320 // private methods and constructors.
321 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700322 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700323 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700324
Elliott Hughescdf53122011-08-19 15:46:09 -0700325 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700327 return NULL;
328 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700329
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700330 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700331}
332
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700333static ClassLoader* GetClassLoader(Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700334 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700335 AbstractMethod* method = self->GetCurrentMethod();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700336 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
337 return self->GetClassLoaderOverride();
338 }
339 return method->GetDeclaringClass()->GetClassLoader();
340}
341
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
343 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700346 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700347 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700348 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700349
350 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700351 Class* field_type;
352 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
353 if (sig[1] != '\0') {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354 ClassLoader* cl = GetClassLoader(soa.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700356 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700357 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700358 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700359 if (field_type == NULL) {
360 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700361 DCHECK(soa.Self()->IsExceptionPending());
362 soa.Self()->ClearException();
363 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700364 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800365 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700366 return NULL;
367 }
368 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800369 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700370 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800371 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700372 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700373 if (field == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700375 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800376 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700377 return NULL;
378 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700379 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700380}
381
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700384 JavaVMExt* vm = soa.Vm();
Elliott Hughes75770752011-08-24 17:52:38 -0700385 MutexLock mu(vm->pins_lock);
386 vm->pin_table.Add(array);
387}
388
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700390 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700391 JavaVMExt* vm = soa.Vm();
Elliott Hughes75770752011-08-24 17:52:38 -0700392 MutexLock mu(vm->pins_lock);
393 vm->pin_table.Remove(array);
394}
395
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
397 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700398 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700399 std::string type(PrettyTypeOf(array));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700401 "%s offset=%d length=%d %s.length=%d",
402 type.c_str(), start, length, identifier, array->GetLength());
403}
Ian Rogers0571d352011-11-03 19:51:38 -0700404
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700405static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
406 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700409 "offset=%d length=%d string.length()=%d", start, length, array_length);
410}
Elliott Hughes814e4032011-08-23 12:07:56 -0700411
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700412int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700413 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700414 ScopedObjectAccess soa(env);
415
416 // Turn the const char* into a java.lang.String.
417 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
418 if (msg != NULL && s.get() == NULL) {
419 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700420 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700421
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700422 // Choose an appropriate constructor and set up the arguments.
423 jvalue args[2];
424 const char* signature;
425 if (msg == NULL && cause == NULL) {
426 signature = "()V";
427 } else if (msg != NULL && cause == NULL) {
428 signature = "(Ljava/lang/String;)V";
429 args[0].l = s.get();
430 } else if (msg == NULL && cause != NULL) {
431 signature = "(Ljava/lang/Throwable;)V";
432 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700433 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700434 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
435 args[0].l = s.get();
436 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700437 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700438 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
439 if (mid == NULL) {
440 LOG(ERROR) << "No <init>" << signature << " in "
441 << PrettyClass(soa.Decode<Class*>(exception_class));
442 return JNI_ERR;
443 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700444
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700445 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
446 if (exception.get() == NULL) {
447 return JNI_ERR;
448 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700449
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700450 soa.Self()->SetException(soa.Decode<Throwable*>(exception.get()));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700451
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700453}
454
Elliott Hughes462c9442012-03-23 18:47:50 -0700455static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700456 if (vm == NULL || p_env == NULL) {
457 return JNI_ERR;
458 }
459
Elliott Hughes462c9442012-03-23 18:47:50 -0700460 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700461 Thread* self = Thread::Current();
462 if (self != NULL) {
463 *p_env = self->GetJniEnv();
464 return JNI_OK;
465 }
466
467 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
468
469 // No threads allowed in zygote mode.
470 if (runtime->IsZygote()) {
471 LOG(ERROR) << "Attempt to attach a thread in the zygote";
472 return JNI_ERR;
473 }
474
Elliott Hughes462c9442012-03-23 18:47:50 -0700475 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
476 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700477 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700478 if (args != NULL) {
479 CHECK_GE(args->version, JNI_VERSION_1_2);
480 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700481 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700482 }
Elliott Hughes75770752011-08-24 17:52:38 -0700483
Ian Rogers120f1c72012-09-28 17:17:10 -0700484 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group)) {
485 *p_env = NULL;
486 return JNI_ERR;
487 } else {
488 *p_env = Thread::Current()->GetJniEnv();
489 return JNI_OK;
490 }
Elliott Hughes75770752011-08-24 17:52:38 -0700491}
492
Elliott Hughes79082e32011-08-25 12:07:32 -0700493class SharedLibrary {
494 public:
495 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
496 : path_(path),
497 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700498 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700499 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughese62934d2012-04-09 11:24:29 -0700500 jni_on_load_cond_("JNI_OnLoad condition variable"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700501 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700502 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700503 }
504
Elliott Hughes79082e32011-08-25 12:07:32 -0700505 Object* GetClassLoader() {
506 return class_loader_;
507 }
508
509 std::string GetPath() {
510 return path_;
511 }
512
513 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700514 * Check the result of an earlier call to JNI_OnLoad on this library.
515 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700516 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700517 bool CheckOnLoadResult()
518 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700519 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700520 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700521 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
522 bool okay;
523 {
524 MutexLock mu(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700525
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700526 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
527 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
528 // caller can continue.
529 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
530 okay = true;
531 } else {
532 while (jni_on_load_result_ == kPending) {
533 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogers81d425b2012-09-27 16:03:43 -0700534 jni_on_load_cond_.Wait(self, jni_on_load_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700535 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700536
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700537 okay = (jni_on_load_result_ == kOkay);
538 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
539 << (okay ? "succeeded" : "failed") << "]";
540 }
541 }
542 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700543 return okay;
544 }
545
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700546 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700547 MutexLock mu(jni_on_load_lock_);
548
Elliott Hughes79082e32011-08-25 12:07:32 -0700549 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700550 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700551
552 // Broadcast a wakeup to anybody sleeping on the condition variable.
Elliott Hughes5f791332011-09-15 17:45:30 -0700553 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 }
555
556 void* FindSymbol(const std::string& symbol_name) {
557 return dlsym(handle_, symbol_name.c_str());
558 }
559
560 private:
561 enum JNI_OnLoadState {
562 kPending,
563 kFailed,
564 kOkay,
565 };
566
567 // Path to library "/system/lib/libjni.so".
568 std::string path_;
569
570 // The void* returned by dlopen(3).
571 void* handle_;
572
573 // The ClassLoader this library is associated with.
574 Object* class_loader_;
575
576 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700577 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700578 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700579 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700580 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700581 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700582 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700583 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700584};
585
Elliott Hughes79082e32011-08-25 12:07:32 -0700586// This exists mainly to keep implementation details out of the header file.
587class Libraries {
588 public:
589 Libraries() {
590 }
591
592 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700593 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700594 }
595
Elliott Hughesae80b492012-04-24 10:43:17 -0700596 void Dump(std::ostream& os) const {
597 bool first = true;
598 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
599 if (!first) {
600 os << ' ';
601 }
602 first = false;
603 os << it->first;
604 }
605 }
606
607 size_t size() const {
608 return libraries_.size();
609 }
610
Elliott Hughes79082e32011-08-25 12:07:32 -0700611 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700612 It it = libraries_.find(path);
613 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700614 }
615
616 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700617 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700618 }
619
620 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700621 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700622 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700623 std::string jni_short_name(JniShortName(m));
624 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700625 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700626 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
627 SharedLibrary* library = it->second;
628 if (library->GetClassLoader() != declaring_class_loader) {
629 // We only search libraries loaded by the appropriate ClassLoader.
630 continue;
631 }
632 // Try the short name then the long name...
633 void* fn = library->FindSymbol(jni_short_name);
634 if (fn == NULL) {
635 fn = library->FindSymbol(jni_long_name);
636 }
637 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800638 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
639 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700640 return fn;
641 }
642 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700643 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700644 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700645 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700646 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700647 return NULL;
648 }
649
650 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700651 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700652
Elliott Hughesa0e18062012-04-13 15:59:59 -0700653 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700654};
655
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700656JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
657 jvalue* args) {
658 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700659 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800660 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661 arg_array.BuildArgArray(soa, args);
662 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700663}
664
Mathieu Chartier66f19252012-09-18 08:57:04 -0700665JValue InvokeWithJValues(const ScopedObjectAccess& soa, Object* receiver, AbstractMethod* m,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700666 JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700667 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 return InvokeWithArgArray(soa, receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800669}
670
Elliott Hughescdf53122011-08-19 15:46:09 -0700671class JNI {
672 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700673 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 return JNI_VERSION_1_6;
675 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700676
Ian Rogers25e8b912012-09-07 11:31:36 -0700677 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700679 return NULL;
680 }
681
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700684 Runtime* runtime = Runtime::Current();
685 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700687 Class* c = NULL;
688 if (runtime->IsStarted()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700689 ClassLoader* cl = GetClassLoader(soa.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800690 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700691 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800692 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700693 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700695 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700696
Elliott Hughescdf53122011-08-19 15:46:09 -0700697 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700698 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700699 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700701 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700702
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700704 ScopedObjectAccess soa(env);
705 Field* field = soa.Decode<Field*>(java_field);
706 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700707 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700708
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700711 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700713 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700714
Elliott Hughescdf53122011-08-19 15:46:09 -0700715 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 ScopedObjectAccess soa(env);
717 Field* field = soa.DecodeField(fid);
718 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700720
Elliott Hughes37f7a402011-08-22 18:56:01 -0700721 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700722 ScopedObjectAccess soa(env);
723 Object* o = soa.Decode<Object*>(java_object);
724 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700725 }
726
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700727 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728 ScopedObjectAccess soa(env);
729 Class* c = soa.Decode<Class*>(java_class);
730 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700732
Elliott Hughes37f7a402011-08-22 18:56:01 -0700733 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 ScopedObjectAccess soa(env);
735 Class* c1 = soa.Decode<Class*>(java_class1);
736 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700737 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700739
Elliott Hughese84278b2012-03-22 10:06:53 -0700740 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 ScopedObjectAccess soa(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700742 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700743 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700744 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700745 return JNI_TRUE;
746 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700747 Object* obj = soa.Decode<Object*>(jobj);
748 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700749 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700750 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700752
Elliott Hughes37f7a402011-08-22 18:56:01 -0700753 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700754 ScopedObjectAccess soa(env);
755 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700756 if (exception == NULL) {
757 return JNI_ERR;
758 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700759 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700760 return JNI_OK;
761 }
762
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700763 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700764 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700765 }
766
767 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700768 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700769 }
770
771 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700772 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700773 }
774
775 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700776 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700777
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700778 Thread* self = soa.Self();
Elliott Hughes72025e52011-08-23 17:50:30 -0700779 Throwable* original_exception = self->GetException();
780 self->ClearException();
781
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700782 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700783 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
784 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
785 if (mid == NULL) {
786 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700787 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700788 } else {
789 env->CallVoidMethod(exception.get(), mid);
790 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700791 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700792 << " thrown while calling printStackTrace";
793 self->ClearException();
794 }
795 }
796
797 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700798 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700799
Elliott Hughescdf53122011-08-19 15:46:09 -0700800 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700801 ScopedObjectAccess soa(env);
802 Object* exception = soa.Self()->GetException();
803 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 }
805
Ian Rogers25e8b912012-09-07 11:31:36 -0700806 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 LOG(FATAL) << "JNI FatalError called: " << msg;
808 }
809
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700810 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700811 ScopedObjectAccess soa(env);
812 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700813 return JNI_ERR;
814 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700815 soa.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 return JNI_OK;
817 }
818
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700819 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700820 ScopedObjectAccess soa(env);
821 Object* survivor = soa.Decode<Object*>(java_survivor);
822 soa.Env()->PopFrame();
823 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 }
825
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700826 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700827 ScopedObjectAccess soa(env);
828 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700829 }
830
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 if (obj == NULL) {
833 return NULL;
834 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700835 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700836 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700838 Object* decoded_obj = soa.Decode<Object*>(obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 MutexLock mu(vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700840 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 return reinterpret_cast<jobject>(ref);
842 }
843
844 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700845 if (obj == NULL) {
846 return;
847 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700848 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700849 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700850 IndirectReferenceTable& globals = vm->globals;
851 MutexLock mu(vm->globals_lock);
852
853 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
854 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
855 << "failed to find entry";
856 }
857 }
858
859 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700860 ScopedObjectAccess soa(env);
861 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 }
863
864 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 if (obj == NULL) {
866 return;
867 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700868 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700869 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 IndirectReferenceTable& weak_globals = vm->weak_globals;
871 MutexLock mu(vm->weak_globals_lock);
872
873 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
874 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
875 << "failed to find entry";
876 }
877 }
878
879 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700880 if (obj == NULL) {
881 return NULL;
882 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700883 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700884 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700885
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700886 uint32_t cookie = soa.Env()->local_ref_cookie;
887 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 return reinterpret_cast<jobject>(ref);
889 }
890
891 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 if (obj == NULL) {
893 return;
894 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700895 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700896 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700897
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 uint32_t cookie = soa.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 if (!locals.Remove(cookie, obj)) {
900 // Attempting to delete a local reference that is not in the
901 // topmost local reference frame is a no-op. DeleteLocalRef returns
902 // void and doesn't throw any exceptions, but we should probably
903 // complain about it so the user will notice that things aren't
904 // going quite the way they expect.
905 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
906 << "failed to find entry";
907 }
908 }
909
910 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700911 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700912 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 }
914
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700915 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700916 ScopedObjectAccess soa(env);
917 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700918 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700919 return NULL;
920 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700921 return soa.AddLocalReference<jobject>(c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 }
923
Elliott Hughese84278b2012-03-22 10:06:53 -0700924 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700927 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 va_end(args);
929 return result;
930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933 ScopedObjectAccess soa(env);
934 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700935 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700936 return NULL;
937 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700938 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700939 if (result == NULL) {
940 return NULL;
941 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700942 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700945 return local_result;
946 } else {
947 return NULL;
948 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 }
950
Elliott Hughes72025e52011-08-23 17:50:30 -0700951 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700952 ScopedObjectAccess soa(env);
953 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700954 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700955 return NULL;
956 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700957 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700958 if (result == NULL) {
959 return NULL;
960 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700963 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700964 return local_result;
965 } else {
966 return NULL;
967 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700971 ScopedObjectAccess soa(env);
972 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
975 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700976 ScopedObjectAccess soa(env);
977 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 }
979
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 va_list ap;
982 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700983 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700984 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700990 ScopedObjectAccess soa(env);
991 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
992 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 }
994
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 ScopedObjectAccess soa(env);
997 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
998 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 va_list ap;
1003 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001004 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001005 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001007 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 ScopedObjectAccess soa(env);
1012 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001016 ScopedObjectAccess soa(env);
1017 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 }
1019
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001021 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_list ap;
1023 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001026 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001030 ScopedObjectAccess soa(env);
1031 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001035 ScopedObjectAccess soa(env);
1036 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 va_list ap;
1041 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001042 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001045 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001049 ScopedObjectAccess soa(env);
1050 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001054 ScopedObjectAccess soa(env);
1055 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 va_list ap;
1060 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001061 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001064 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 ScopedObjectAccess soa(env);
1069 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 ScopedObjectAccess soa(env);
1074 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001078 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 va_list ap;
1080 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001083 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001087 ScopedObjectAccess soa(env);
1088 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001092 ScopedObjectAccess soa(env);
1093 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_list ap;
1098 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001099 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001102 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 ScopedObjectAccess soa(env);
1107 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001111 ScopedObjectAccess soa(env);
1112 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 va_list ap;
1117 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001118 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001121 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001125 ScopedObjectAccess soa(env);
1126 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001130 ScopedObjectAccess soa(env);
1131 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 va_list ap;
1136 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001137 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001138 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001140 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 }
1142
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001144 ScopedObjectAccess soa(env);
1145 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 }
1147
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001149 ScopedObjectAccess soa(env);
1150 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_list ap;
1155 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001156 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001157 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162 ScopedObjectAccess soa(env);
1163 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001167 ScopedObjectAccess soa(env);
1168 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001171 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001174 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001175 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1176 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 va_end(ap);
1178 return local_result;
1179 }
1180
1181 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001182 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001183 ScopedObjectAccess soa(env);
1184 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1185 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 }
1187
1188 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001189 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190 ScopedObjectAccess soa(env);
1191 JValue result(InvokeWithJValues(soa, obj, mid, args));
1192 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 }
1194
1195 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001196 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001199 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001200 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001202 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
1205 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001206 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001207 ScopedObjectAccess soa(env);
1208 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 }
1210
1211 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001212 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001213 ScopedObjectAccess soa(env);
1214 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001217 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001220 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001221 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001223 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 }
1225
1226 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001227 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 ScopedObjectAccess soa(env);
1229 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 }
1231
1232 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001233 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001234 ScopedObjectAccess soa(env);
1235 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 }
1237
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001238 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001239 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001242 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001244 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 }
1246
1247 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001248 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001249 ScopedObjectAccess soa(env);
1250 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 }
1252
1253 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001254 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001255 ScopedObjectAccess soa(env);
1256 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001259 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001261 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001262 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001263 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001265 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
1268 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001269 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001270 ScopedObjectAccess soa(env);
1271 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 }
1273
1274 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001275 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001276 ScopedObjectAccess soa(env);
1277 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001280 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001283 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001284 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001286 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
1289 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001290 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001291 ScopedObjectAccess soa(env);
1292 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
1295 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001296 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001297 ScopedObjectAccess soa(env);
1298 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001301 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001304 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001305 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001307 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
1310 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001311 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001312 ScopedObjectAccess soa(env);
1313 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
1316 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001317 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001318 ScopedObjectAccess soa(env);
1319 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001322 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001324 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001325 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001326 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001328 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
1331 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001332 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001333 ScopedObjectAccess soa(env);
1334 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
1337 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001338 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001339 ScopedObjectAccess soa(env);
1340 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001343 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001346 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001347 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001349 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
1352 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001353 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001354 ScopedObjectAccess soa(env);
1355 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
1358 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001359 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001360 ScopedObjectAccess soa(env);
1361 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001364 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001366 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001367 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001368 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 va_end(ap);
1370 }
1371
1372 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001373 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001374 ScopedObjectAccess soa(env);
1375 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
1377
1378 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001379 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001380 ScopedObjectAccess soa(env);
1381 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 }
1383
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001384 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001385 ScopedObjectAccess soa(env);
1386 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001388
1389
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001390 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001391 ScopedObjectAccess soa(env);
1392 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001394
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001395 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001396 ScopedObjectAccess soa(env);
1397 Object* o = soa.Decode<Object*>(obj);
1398 Field* f = soa.DecodeField(fid);
1399 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001401
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001403 ScopedObjectAccess soa(env);
1404 Field* f = soa.DecodeField(fid);
1405 return soa.AddLocalReference<jobject>(f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001408 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001409 ScopedObjectAccess soa(env);
1410 Object* o = soa.Decode<Object*>(java_object);
1411 Object* v = soa.Decode<Object*>(java_value);
1412 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001413 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001416 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001417 ScopedObjectAccess soa(env);
1418 Object* v = soa.Decode<Object*>(java_value);
1419 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001420 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001423#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001424 ScopedObjectAccess soa(env); \
1425 Object* o = soa.Decode<Object*>(instance); \
1426 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001427 return f->fn(o)
1428
1429#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001430 ScopedObjectAccess soa(env); \
1431 Object* o = soa.Decode<Object*>(instance); \
1432 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001433 f->fn(o, value)
1434
1435 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1436 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1440 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1444 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1448 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001467 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001468 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001471 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001472 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001475 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001479 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001483 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001487 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 GET_PRIMITIVE_FIELD(GetLong, NULL);
1489 }
1490
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001491 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1493 }
1494
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001495 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1497 }
1498
1499 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1500 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1501 }
1502
1503 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1504 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1505 }
1506
1507 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1508 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1509 }
1510
1511 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1512 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1513 }
1514
1515 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1516 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1517 }
1518
1519 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1520 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1521 }
1522
1523 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1524 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1525 }
1526
1527 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1528 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1529 }
1530
1531 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1532 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1533 }
1534
1535 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1536 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1537 }
1538
1539 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1540 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1541 }
1542
1543 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1544 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1545 }
1546
1547 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1548 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1549 }
1550
1551 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1552 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1553 }
1554
1555 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1556 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1557 }
1558
1559 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1560 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001563 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001564 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001565 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001566 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001567 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1568 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 va_end(ap);
1570 return local_result;
1571 }
1572
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001573 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001574 ScopedObjectAccess soa(env);
1575 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1576 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001579 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001580 ScopedObjectAccess soa(env);
1581 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1582 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001585 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001587 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001588 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001589 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001591 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001594 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001595 ScopedObjectAccess soa(env);
1596 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001599 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001600 ScopedObjectAccess soa(env);
1601 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001604 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001606 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001607 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001608 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001610 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001613 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001614 ScopedObjectAccess soa(env);
1615 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001618 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001619 ScopedObjectAccess soa(env);
1620 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 }
1622
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001623 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001625 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001626 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001627 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001629 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001632 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001633 ScopedObjectAccess soa(env);
1634 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001637 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001638 ScopedObjectAccess soa(env);
1639 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001642 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001645 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001646 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001648 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001651 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001652 ScopedObjectAccess soa(env);
1653 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 }
1655
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001656 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001657 ScopedObjectAccess soa(env);
1658 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 }
1660
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001661 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001663 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001664 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001665 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001667 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 }
1669
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001670 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001671 ScopedObjectAccess soa(env);
1672 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001675 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001676 ScopedObjectAccess soa(env);
1677 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 }
1679
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001680 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001682 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001683 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001684 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001686 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001689 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001690 ScopedObjectAccess soa(env);
1691 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 }
1693
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001694 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001695 ScopedObjectAccess soa(env);
1696 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 }
1698
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001699 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001701 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001702 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001703 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001705 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 }
1707
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001708 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001709 ScopedObjectAccess soa(env);
1710 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 }
1712
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001713 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001714 ScopedObjectAccess soa(env);
1715 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 }
1717
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001718 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001721 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001722 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001724 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001727 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001728 ScopedObjectAccess soa(env);
1729 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 }
1731
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001732 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001733 ScopedObjectAccess soa(env);
1734 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 }
1736
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001737 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001739 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001740 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 va_end(ap);
1743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001746 ScopedObjectAccess soa(env);
1747 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001750 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001751 ScopedObjectAccess soa(env);
1752 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes814e4032011-08-23 12:07:56 -07001755 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001756 ScopedObjectAccess soa(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001757 String* result = String::AllocFromUtf16(char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001758 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
1761 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 if (utf == NULL) {
1763 return NULL;
1764 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001765 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 String* result = String::AllocFromModifiedUtf8(utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001767 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 }
1769
Elliott Hughes814e4032011-08-23 12:07:56 -07001770 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001771 ScopedObjectAccess soa(env);
1772 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001773 }
1774
1775 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001776 ScopedObjectAccess soa(env);
1777 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001778 }
1779
Elliott Hughesb465ab02011-08-24 11:21:21 -07001780 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001781 ScopedObjectAccess soa(env);
1782 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001783 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001784 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001785 } else {
1786 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1787 memcpy(buf, chars + start, length * sizeof(jchar));
1788 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001789 }
1790
Elliott Hughesb465ab02011-08-24 11:21:21 -07001791 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001792 ScopedObjectAccess soa(env);
1793 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001794 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001795 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001796 } else {
1797 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1798 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1799 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001800 }
1801
Elliott Hughes75770752011-08-24 17:52:38 -07001802 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001803 ScopedObjectAccess soa(env);
1804 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001805 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001806 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001807 if (is_copy != NULL) {
1808 *is_copy = JNI_FALSE;
1809 }
1810 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001811 }
1812
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001813 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001814 ScopedObjectAccess soa(env);
1815 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
Elliott Hughes75770752011-08-24 17:52:38 -07001818 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001819 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001820 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
Elliott Hughes75770752011-08-24 17:52:38 -07001823 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001824 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001825 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
Elliott Hughes75770752011-08-24 17:52:38 -07001828 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001829 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001830 if (java_string == NULL) {
1831 return NULL;
1832 }
1833 if (is_copy != NULL) {
1834 *is_copy = JNI_TRUE;
1835 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001836 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001837 size_t byte_count = s->GetUtfLength();
1838 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001839 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001840 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1841 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1842 bytes[byte_count] = '\0';
1843 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001844 }
1845
Elliott Hughes75770752011-08-24 17:52:38 -07001846 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001847 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001848 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001849 }
1850
Elliott Hughesbd935992011-08-22 11:59:34 -07001851 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001852 ScopedObjectAccess soa(env);
1853 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001854 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001855 Array* array = obj->AsArray();
1856 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 }
1858
Elliott Hughes814e4032011-08-23 12:07:56 -07001859 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001860 ScopedObjectAccess soa(env);
1861 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1862 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
1865 static void SetObjectArrayElement(JNIEnv* env,
1866 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001867 ScopedObjectAccess soa(env);
1868 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1869 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 array->Set(index, value);
1871 }
1872
1873 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001874 ScopedObjectAccess soa(env);
1875 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 }
1877
1878 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001879 ScopedObjectAccess soa(env);
1880 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001881 }
1882
1883 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 ScopedObjectAccess soa(env);
1885 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001886 }
1887
1888 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889 ScopedObjectAccess soa(env);
1890 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
1893 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001894 ScopedObjectAccess soa(env);
1895 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001896 }
1897
1898 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001899 ScopedObjectAccess soa(env);
1900 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 }
1902
1903 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001904 ScopedObjectAccess soa(env);
1905 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 }
1907
1908 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001909 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 CHECK_GE(length, 0); // TODO: ReportJniError
1911
1912 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001913 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001914 std::string descriptor;
1915 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001916 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001917
1918 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001919 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1920 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 return NULL;
1922 }
1923
Elliott Hughes75770752011-08-24 17:52:38 -07001924 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001925 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001927 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001928 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001929 for (jsize i = 0; i < length; ++i) {
1930 result->Set(i, initial_object);
1931 }
1932 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001933 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 }
1935
1936 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001937 ScopedObjectAccess soa(env);
1938 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Ian Rogersa15e67d2012-02-28 13:51:55 -08001941 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001942 ScopedObjectAccess soa(env);
1943 Array* array = soa.Decode<Array*>(java_array);
1944 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001945 if (is_copy != NULL) {
1946 *is_copy = JNI_FALSE;
1947 }
1948 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001949 }
1950
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001951 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001952 ScopedObjectAccess soa(env);
1953 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001954 }
1955
Elliott Hughes75770752011-08-24 17:52:38 -07001956 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001957 ScopedObjectAccess soa(env);
1958 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 }
1960
Elliott Hughes75770752011-08-24 17:52:38 -07001961 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001962 ScopedObjectAccess soa(env);
1963 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Elliott Hughes75770752011-08-24 17:52:38 -07001966 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001967 ScopedObjectAccess soa(env);
1968 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 }
1970
Elliott Hughes75770752011-08-24 17:52:38 -07001971 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001972 ScopedObjectAccess soa(env);
1973 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Elliott Hughes75770752011-08-24 17:52:38 -07001976 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001977 ScopedObjectAccess soa(env);
1978 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Elliott Hughes75770752011-08-24 17:52:38 -07001981 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001982 ScopedObjectAccess soa(env);
1983 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Elliott Hughes75770752011-08-24 17:52:38 -07001986 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001987 ScopedObjectAccess soa(env);
1988 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes75770752011-08-24 17:52:38 -07001991 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001992 ScopedObjectAccess soa(env);
1993 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001996 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001997 ScopedObjectAccess soa(env);
1998 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002001 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002002 ScopedObjectAccess soa(env);
2003 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002006 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002007 ScopedObjectAccess soa(env);
2008 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002011 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002012 ScopedObjectAccess soa(env);
2013 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002016 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002017 ScopedObjectAccess soa(env);
2018 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002021 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002022 ScopedObjectAccess soa(env);
2023 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002026 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002027 ScopedObjectAccess soa(env);
2028 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002031 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002032 ScopedObjectAccess soa(env);
2033 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes814e4032011-08-23 12:07:56 -07002036 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002037 ScopedObjectAccess soa(env);
2038 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes814e4032011-08-23 12:07:56 -07002041 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002042 ScopedObjectAccess soa(env);
2043 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Elliott Hughes814e4032011-08-23 12:07:56 -07002046 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002047 ScopedObjectAccess soa(env);
2048 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Elliott Hughes814e4032011-08-23 12:07:56 -07002051 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002052 ScopedObjectAccess soa(env);
2053 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002057 ScopedObjectAccess soa(env);
2058 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002062 ScopedObjectAccess soa(env);
2063 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002067 ScopedObjectAccess soa(env);
2068 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002072 ScopedObjectAccess soa(env);
2073 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002077 ScopedObjectAccess soa(env);
2078 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002082 ScopedObjectAccess soa(env);
2083 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002087 ScopedObjectAccess soa(env);
2088 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002092 ScopedObjectAccess soa(env);
2093 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002097 ScopedObjectAccess soa(env);
2098 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002102 ScopedObjectAccess soa(env);
2103 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002107 ScopedObjectAccess soa(env);
2108 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002112 ScopedObjectAccess soa(env);
2113 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
Elliott Hughes5174fe62011-08-23 15:12:35 -07002116 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002117 ScopedObjectAccess soa(env);
2118 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002119
Elliott Hughes5174fe62011-08-23 15:12:35 -07002120 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 const char* name = methods[i].name;
2122 const char* sig = methods[i].signature;
2123
2124 if (*sig == '!') {
2125 // TODO: fast jni. it's too noisy to log all these.
2126 ++sig;
2127 }
2128
Mathieu Chartier66f19252012-09-18 08:57:04 -07002129 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002130 if (m == NULL) {
2131 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002133 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002134 LOG(INFO) << "Failed to register native method " << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002135 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002137 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002138 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002139 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 return JNI_ERR;
2141 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002142
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002143 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002144
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002145 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147 return JNI_OK;
2148 }
2149
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002151 ScopedObjectAccess soa(env);
2152 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002153
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002154 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155
2156 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002157 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002159 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002160 }
2161 }
2162 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002163 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002164 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002165 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002166 }
2167 }
2168
2169 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002172 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2173 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2174 ScopedObjectAccess soa(env);
2175 Object* o = soa.Decode<Object*>(java_object);
2176 o->MonitorEnter(soa.Self());
2177 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002178 return JNI_ERR;
2179 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002180 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002181 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 }
2183
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002184 static jint MonitorExit(JNIEnv* env, jobject java_object)
2185 UNLOCK_FUNCTION(monitor_lock_) {
2186 ScopedObjectAccess soa(env);
2187 Object* o = soa.Decode<Object*>(java_object);
2188 o->MonitorExit(soa.Self());
2189 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002190 return JNI_ERR;
2191 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002192 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002193 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
2195
2196 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002197 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 Runtime* runtime = Runtime::Current();
2199 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002200 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 } else {
2202 *vm = NULL;
2203 }
2204 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2205 }
2206
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002208 ScopedObjectAccess soa(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002209
2210 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002211 CHECK(address != NULL); // TODO: ReportJniError
2212 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002213
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002214 // At the moment, the Java side is limited to 32 bisoa.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002215 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2216 CHECK_LE(capacity, 0xffffffff);
2217 jint address_arg = reinterpret_cast<jint>(address);
2218 jint capacity_arg = static_cast<jint>(capacity);
2219
Elliott Hugheseac76672012-05-24 21:56:51 -07002220 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2221 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2222 address_arg, capacity_arg);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002223 return soa.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225
Elliott Hughesb465ab02011-08-24 11:21:21 -07002226 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002227 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002228 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Elliott Hughesb465ab02011-08-24 11:21:21 -07002231 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002232 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002233 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Elliott Hughesb465ab02011-08-24 11:21:21 -07002236 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002237 ScopedObjectAccess soa(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002238
Elliott Hughes75770752011-08-24 17:52:38 -07002239 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002240
2241 // Do we definitely know what kind of reference this is?
2242 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2243 IndirectRefKind kind = GetIndirectRefKind(ref);
2244 switch (kind) {
2245 case kLocal:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002246 if (soa.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002247 return JNILocalRefType;
2248 }
2249 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002250 case kGlobal:
2251 return JNIGlobalRefType;
2252 case kWeakGlobal:
2253 return JNIWeakGlobalRefType;
2254 case kSirtOrInvalid:
2255 // Is it in a stack IRT?
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002256 if (soa.Self()->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002257 return JNILocalRefType;
2258 }
2259
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002260 if (!soa.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002261 return JNIInvalidRefType;
2262 }
2263
Elliott Hughesb465ab02011-08-24 11:21:21 -07002264 // If we're handing out direct pointers, check whether it's a direct pointer
2265 // to a local reference.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002266 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2267 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002268 return JNILocalRefType;
2269 }
2270 }
2271
2272 return JNIInvalidRefType;
2273 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002274 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2275 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002276 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002277
2278 private:
2279 static jint EnsureLocalCapacity(const ScopedObjectAccess& soa, jint desired_capacity,
2280 const char* caller)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002281 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002282 // TODO: we should try to expand the table if necessary.
2283 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2284 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2285 return JNI_ERR;
2286 }
2287 // TODO: this isn't quite right, since "capacity" includes holes.
2288 size_t capacity = soa.Env()->locals.Capacity();
2289 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2290 if (!okay) {
2291 soa.Self()->ThrowOutOfMemoryError(caller);
2292 }
2293 return okay ? JNI_OK : JNI_ERR;
2294 }
2295
2296 template<typename JniT, typename ArtT>
2297 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002298 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002299 CHECK_GE(length, 0); // TODO: ReportJniError
2300 ArtT* result = ArtT::Alloc(length);
2301 return soa.AddLocalReference<JniT>(result);
2302 }
2303
2304 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2305 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2306 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002307 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002308 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2309 PinPrimitiveArray(soa, array);
2310 if (is_copy != NULL) {
2311 *is_copy = JNI_FALSE;
2312 }
2313 return array->GetData();
2314 }
2315
2316 template <typename ArrayT>
2317 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2318 jint mode)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002320 if (mode != JNI_COMMIT) {
2321 Array* array = soa.Decode<Array*>(java_array);
2322 UnpinPrimitiveArray(soa, array);
2323 }
2324 }
2325
2326 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2327 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2328 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002329 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002330 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2331 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2332 ThrowAIOOBE(soa, array, start, length, "src");
2333 } else {
2334 JavaT* data = array->GetData();
2335 memcpy(buf, data + start, length * sizeof(JavaT));
2336 }
2337 }
2338
2339 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2340 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2341 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002342 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002343 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2344 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2345 ThrowAIOOBE(soa, array, start, length, "dst");
2346 } else {
2347 JavaT* data = array->GetData();
2348 memcpy(data + start, buf, length * sizeof(JavaT));
2349 }
2350 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002351};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002352
Elliott Hughes88c5c352012-03-15 18:49:48 -07002353const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002354 NULL, // reserved0.
2355 NULL, // reserved1.
2356 NULL, // reserved2.
2357 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002358 JNI::GetVersion,
2359 JNI::DefineClass,
2360 JNI::FindClass,
2361 JNI::FromReflectedMethod,
2362 JNI::FromReflectedField,
2363 JNI::ToReflectedMethod,
2364 JNI::GetSuperclass,
2365 JNI::IsAssignableFrom,
2366 JNI::ToReflectedField,
2367 JNI::Throw,
2368 JNI::ThrowNew,
2369 JNI::ExceptionOccurred,
2370 JNI::ExceptionDescribe,
2371 JNI::ExceptionClear,
2372 JNI::FatalError,
2373 JNI::PushLocalFrame,
2374 JNI::PopLocalFrame,
2375 JNI::NewGlobalRef,
2376 JNI::DeleteGlobalRef,
2377 JNI::DeleteLocalRef,
2378 JNI::IsSameObject,
2379 JNI::NewLocalRef,
2380 JNI::EnsureLocalCapacity,
2381 JNI::AllocObject,
2382 JNI::NewObject,
2383 JNI::NewObjectV,
2384 JNI::NewObjectA,
2385 JNI::GetObjectClass,
2386 JNI::IsInstanceOf,
2387 JNI::GetMethodID,
2388 JNI::CallObjectMethod,
2389 JNI::CallObjectMethodV,
2390 JNI::CallObjectMethodA,
2391 JNI::CallBooleanMethod,
2392 JNI::CallBooleanMethodV,
2393 JNI::CallBooleanMethodA,
2394 JNI::CallByteMethod,
2395 JNI::CallByteMethodV,
2396 JNI::CallByteMethodA,
2397 JNI::CallCharMethod,
2398 JNI::CallCharMethodV,
2399 JNI::CallCharMethodA,
2400 JNI::CallShortMethod,
2401 JNI::CallShortMethodV,
2402 JNI::CallShortMethodA,
2403 JNI::CallIntMethod,
2404 JNI::CallIntMethodV,
2405 JNI::CallIntMethodA,
2406 JNI::CallLongMethod,
2407 JNI::CallLongMethodV,
2408 JNI::CallLongMethodA,
2409 JNI::CallFloatMethod,
2410 JNI::CallFloatMethodV,
2411 JNI::CallFloatMethodA,
2412 JNI::CallDoubleMethod,
2413 JNI::CallDoubleMethodV,
2414 JNI::CallDoubleMethodA,
2415 JNI::CallVoidMethod,
2416 JNI::CallVoidMethodV,
2417 JNI::CallVoidMethodA,
2418 JNI::CallNonvirtualObjectMethod,
2419 JNI::CallNonvirtualObjectMethodV,
2420 JNI::CallNonvirtualObjectMethodA,
2421 JNI::CallNonvirtualBooleanMethod,
2422 JNI::CallNonvirtualBooleanMethodV,
2423 JNI::CallNonvirtualBooleanMethodA,
2424 JNI::CallNonvirtualByteMethod,
2425 JNI::CallNonvirtualByteMethodV,
2426 JNI::CallNonvirtualByteMethodA,
2427 JNI::CallNonvirtualCharMethod,
2428 JNI::CallNonvirtualCharMethodV,
2429 JNI::CallNonvirtualCharMethodA,
2430 JNI::CallNonvirtualShortMethod,
2431 JNI::CallNonvirtualShortMethodV,
2432 JNI::CallNonvirtualShortMethodA,
2433 JNI::CallNonvirtualIntMethod,
2434 JNI::CallNonvirtualIntMethodV,
2435 JNI::CallNonvirtualIntMethodA,
2436 JNI::CallNonvirtualLongMethod,
2437 JNI::CallNonvirtualLongMethodV,
2438 JNI::CallNonvirtualLongMethodA,
2439 JNI::CallNonvirtualFloatMethod,
2440 JNI::CallNonvirtualFloatMethodV,
2441 JNI::CallNonvirtualFloatMethodA,
2442 JNI::CallNonvirtualDoubleMethod,
2443 JNI::CallNonvirtualDoubleMethodV,
2444 JNI::CallNonvirtualDoubleMethodA,
2445 JNI::CallNonvirtualVoidMethod,
2446 JNI::CallNonvirtualVoidMethodV,
2447 JNI::CallNonvirtualVoidMethodA,
2448 JNI::GetFieldID,
2449 JNI::GetObjectField,
2450 JNI::GetBooleanField,
2451 JNI::GetByteField,
2452 JNI::GetCharField,
2453 JNI::GetShortField,
2454 JNI::GetIntField,
2455 JNI::GetLongField,
2456 JNI::GetFloatField,
2457 JNI::GetDoubleField,
2458 JNI::SetObjectField,
2459 JNI::SetBooleanField,
2460 JNI::SetByteField,
2461 JNI::SetCharField,
2462 JNI::SetShortField,
2463 JNI::SetIntField,
2464 JNI::SetLongField,
2465 JNI::SetFloatField,
2466 JNI::SetDoubleField,
2467 JNI::GetStaticMethodID,
2468 JNI::CallStaticObjectMethod,
2469 JNI::CallStaticObjectMethodV,
2470 JNI::CallStaticObjectMethodA,
2471 JNI::CallStaticBooleanMethod,
2472 JNI::CallStaticBooleanMethodV,
2473 JNI::CallStaticBooleanMethodA,
2474 JNI::CallStaticByteMethod,
2475 JNI::CallStaticByteMethodV,
2476 JNI::CallStaticByteMethodA,
2477 JNI::CallStaticCharMethod,
2478 JNI::CallStaticCharMethodV,
2479 JNI::CallStaticCharMethodA,
2480 JNI::CallStaticShortMethod,
2481 JNI::CallStaticShortMethodV,
2482 JNI::CallStaticShortMethodA,
2483 JNI::CallStaticIntMethod,
2484 JNI::CallStaticIntMethodV,
2485 JNI::CallStaticIntMethodA,
2486 JNI::CallStaticLongMethod,
2487 JNI::CallStaticLongMethodV,
2488 JNI::CallStaticLongMethodA,
2489 JNI::CallStaticFloatMethod,
2490 JNI::CallStaticFloatMethodV,
2491 JNI::CallStaticFloatMethodA,
2492 JNI::CallStaticDoubleMethod,
2493 JNI::CallStaticDoubleMethodV,
2494 JNI::CallStaticDoubleMethodA,
2495 JNI::CallStaticVoidMethod,
2496 JNI::CallStaticVoidMethodV,
2497 JNI::CallStaticVoidMethodA,
2498 JNI::GetStaticFieldID,
2499 JNI::GetStaticObjectField,
2500 JNI::GetStaticBooleanField,
2501 JNI::GetStaticByteField,
2502 JNI::GetStaticCharField,
2503 JNI::GetStaticShortField,
2504 JNI::GetStaticIntField,
2505 JNI::GetStaticLongField,
2506 JNI::GetStaticFloatField,
2507 JNI::GetStaticDoubleField,
2508 JNI::SetStaticObjectField,
2509 JNI::SetStaticBooleanField,
2510 JNI::SetStaticByteField,
2511 JNI::SetStaticCharField,
2512 JNI::SetStaticShortField,
2513 JNI::SetStaticIntField,
2514 JNI::SetStaticLongField,
2515 JNI::SetStaticFloatField,
2516 JNI::SetStaticDoubleField,
2517 JNI::NewString,
2518 JNI::GetStringLength,
2519 JNI::GetStringChars,
2520 JNI::ReleaseStringChars,
2521 JNI::NewStringUTF,
2522 JNI::GetStringUTFLength,
2523 JNI::GetStringUTFChars,
2524 JNI::ReleaseStringUTFChars,
2525 JNI::GetArrayLength,
2526 JNI::NewObjectArray,
2527 JNI::GetObjectArrayElement,
2528 JNI::SetObjectArrayElement,
2529 JNI::NewBooleanArray,
2530 JNI::NewByteArray,
2531 JNI::NewCharArray,
2532 JNI::NewShortArray,
2533 JNI::NewIntArray,
2534 JNI::NewLongArray,
2535 JNI::NewFloatArray,
2536 JNI::NewDoubleArray,
2537 JNI::GetBooleanArrayElements,
2538 JNI::GetByteArrayElements,
2539 JNI::GetCharArrayElements,
2540 JNI::GetShortArrayElements,
2541 JNI::GetIntArrayElements,
2542 JNI::GetLongArrayElements,
2543 JNI::GetFloatArrayElements,
2544 JNI::GetDoubleArrayElements,
2545 JNI::ReleaseBooleanArrayElements,
2546 JNI::ReleaseByteArrayElements,
2547 JNI::ReleaseCharArrayElements,
2548 JNI::ReleaseShortArrayElements,
2549 JNI::ReleaseIntArrayElements,
2550 JNI::ReleaseLongArrayElements,
2551 JNI::ReleaseFloatArrayElements,
2552 JNI::ReleaseDoubleArrayElements,
2553 JNI::GetBooleanArrayRegion,
2554 JNI::GetByteArrayRegion,
2555 JNI::GetCharArrayRegion,
2556 JNI::GetShortArrayRegion,
2557 JNI::GetIntArrayRegion,
2558 JNI::GetLongArrayRegion,
2559 JNI::GetFloatArrayRegion,
2560 JNI::GetDoubleArrayRegion,
2561 JNI::SetBooleanArrayRegion,
2562 JNI::SetByteArrayRegion,
2563 JNI::SetCharArrayRegion,
2564 JNI::SetShortArrayRegion,
2565 JNI::SetIntArrayRegion,
2566 JNI::SetLongArrayRegion,
2567 JNI::SetFloatArrayRegion,
2568 JNI::SetDoubleArrayRegion,
2569 JNI::RegisterNatives,
2570 JNI::UnregisterNatives,
2571 JNI::MonitorEnter,
2572 JNI::MonitorExit,
2573 JNI::GetJavaVM,
2574 JNI::GetStringRegion,
2575 JNI::GetStringUTFRegion,
2576 JNI::GetPrimitiveArrayCritical,
2577 JNI::ReleasePrimitiveArrayCritical,
2578 JNI::GetStringCritical,
2579 JNI::ReleaseStringCritical,
2580 JNI::NewWeakGlobalRef,
2581 JNI::DeleteWeakGlobalRef,
2582 JNI::ExceptionCheck,
2583 JNI::NewDirectByteBuffer,
2584 JNI::GetDirectBufferAddress,
2585 JNI::GetDirectBufferCapacity,
2586 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002587};
2588
Elliott Hughes75770752011-08-24 17:52:38 -07002589JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002590 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002591 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002592 local_ref_cookie(IRT_FIRST_SEGMENT),
2593 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002594 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002595 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002596 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002597 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002598 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002599 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002600 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002601 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2602 // errors will ensue.
2603 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2604 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002605}
2606
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002607JNIEnvExt::~JNIEnvExt() {
2608}
2609
Elliott Hughes88c5c352012-03-15 18:49:48 -07002610void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2611 check_jni = enabled;
2612 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002613}
2614
Elliott Hughes73e66f72012-05-09 09:34:45 -07002615void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2616 locals.Dump(os);
2617 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002618}
2619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002620void JNIEnvExt::PushFrame(int /*capacity*/) {
2621 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002622 stacked_local_ref_cookies.push_back(local_ref_cookie);
2623 local_ref_cookie = locals.GetSegmentState();
2624}
2625
2626void JNIEnvExt::PopFrame() {
2627 locals.SetSegmentState(local_ref_cookie);
2628 local_ref_cookie = stacked_local_ref_cookies.back();
2629 stacked_local_ref_cookies.pop_back();
2630}
2631
Carl Shapiroea4dca82011-08-01 13:45:38 -07002632// JNI Invocation interface.
2633
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002634extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2635 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2636 if (args->version < JNI_VERSION_1_2) {
2637 return JNI_EVERSION;
2638 }
2639 Runtime::Options options;
2640 for (int i = 0; i < args->nOptions; ++i) {
2641 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002642 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002643 }
2644 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002645 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002646 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002647 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002648 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002649 runtime->Start();
2650 *p_env = Thread::Current()->GetJniEnv();
2651 *p_vm = runtime->GetJavaVM();
2652 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002653}
2654
Elliott Hughesf2682d52011-08-15 16:37:04 -07002655extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002656 Runtime* runtime = Runtime::Current();
2657 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002658 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002659 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002660 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002661 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002662 }
2663 return JNI_OK;
2664}
2665
2666// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002667extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002668 return JNI_ERR;
2669}
2670
Elliott Hughescdf53122011-08-19 15:46:09 -07002671class JII {
2672 public:
2673 static jint DestroyJavaVM(JavaVM* vm) {
2674 if (vm == NULL) {
2675 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002676 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002677 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2678 delete raw_vm->runtime;
2679 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002680 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002681
Elliott Hughescdf53122011-08-19 15:46:09 -07002682 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002683 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002684 }
2685
2686 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002687 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002688 }
2689
2690 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002691 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002692 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002693 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002694 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2695 Runtime* runtime = raw_vm->runtime;
2696 runtime->DetachCurrentThread();
2697 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002698 }
2699
2700 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2701 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2702 return JNI_EVERSION;
2703 }
2704 if (vm == NULL || env == NULL) {
2705 return JNI_ERR;
2706 }
2707 Thread* thread = Thread::Current();
2708 if (thread == NULL) {
2709 *env = NULL;
2710 return JNI_EDETACHED;
2711 }
2712 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002713 return JNI_OK;
2714 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002715};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002716
Elliott Hughes88c5c352012-03-15 18:49:48 -07002717const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002718 NULL, // reserved0
2719 NULL, // reserved1
2720 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002721 JII::DestroyJavaVM,
2722 JII::AttachCurrentThread,
2723 JII::DetachCurrentThread,
2724 JII::GetEnv,
2725 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002726};
2727
Elliott Hughesa0957642011-09-02 14:27:33 -07002728JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002729 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002730 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002731 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002732 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002733 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002734 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002735 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002736 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002737 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002738 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002739 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002740 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002741 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002742 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002743 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002744 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002745 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002746 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002747 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002748}
2749
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002750JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002751 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002752}
2753
Elliott Hughes88c5c352012-03-15 18:49:48 -07002754void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2755 check_jni = enabled;
2756 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002757}
2758
Elliott Hughesae80b492012-04-24 10:43:17 -07002759void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2760 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2761 if (force_copy) {
2762 os << " (with forcecopy)";
2763 }
2764 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
2765 {
2766 MutexLock mu(pins_lock);
2767 os << "; pins=" << pin_table.Size();
2768 }
2769 {
2770 MutexLock mu(globals_lock);
2771 os << "; globals=" << globals.Capacity();
2772 }
2773 {
2774 MutexLock mu(weak_globals_lock);
2775 if (weak_globals.Capacity() > 0) {
2776 os << " (plus " << weak_globals.Capacity() << " weak)";
2777 }
2778 }
2779 os << '\n';
2780
2781 {
2782 MutexLock mu(libraries_lock);
2783 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2784 }
2785}
2786
Elliott Hughes73e66f72012-05-09 09:34:45 -07002787void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002788 {
2789 MutexLock mu(globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002790 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002791 }
2792 {
2793 MutexLock mu(weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002794 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002795 }
2796 {
2797 MutexLock mu(pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002798 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002799 }
2800}
2801
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002802bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2803 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002804 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002805
2806 // See if we've already loaded this library. If we have, and the class loader
2807 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002808 // TODO: for better results we should canonicalize the pathname (or even compare
2809 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002810 SharedLibrary* library;
2811 {
2812 // TODO: move the locking (and more of this logic) into Libraries.
2813 MutexLock mu(libraries_lock);
2814 library = libraries->Get(path);
2815 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002816 if (library != NULL) {
2817 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002818 // The library will be associated with class_loader. The JNI
2819 // spec says we can't load the same library into more than one
2820 // class loader.
2821 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2822 "ClassLoader %p; can't open in ClassLoader %p",
2823 path.c_str(), library->GetClassLoader(), class_loader);
2824 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002825 return false;
2826 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002827 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2828 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002829 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002830 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2831 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002832 return false;
2833 }
2834 return true;
2835 }
2836
2837 // Open the shared library. Because we're using a full path, the system
2838 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2839 // resolve this library's dependencies though.)
2840
2841 // Failures here are expected when java.library.path has several entries
2842 // and we have to hunt for the lib.
2843
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002844 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2845 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2846 // dlopen) becomes zero from dlclose.
2847
Elliott Hughescdf53122011-08-19 15:46:09 -07002848 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002849 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Elliott Hughescdf53122011-08-19 15:46:09 -07002850 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002851 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2852 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2853 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002854
Elliott Hughes84b2f142012-09-27 09:16:28 -07002855 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002856
2857 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002858 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002859 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002860 return false;
2861 }
2862
2863 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002864 // TODO: move the locking (and more of this logic) into Libraries.
2865 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002866 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002867 MutexLock mu(libraries_lock);
2868 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002869 if (library == NULL) { // We won race to get libraries_lock
2870 library = new SharedLibrary(path, handle, class_loader);
2871 libraries->Put(path, library);
2872 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002873 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002874 }
2875 if (!created_library) {
2876 LOG(INFO) << "WOW: we lost a race to add shared library: "
2877 << "\"" << path << "\" ClassLoader=" << class_loader;
2878 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002879 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002880
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002881 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002882
2883 bool result = true;
2884 void* sym = dlsym(handle, "JNI_OnLoad");
2885 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002886 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002887 } else {
2888 // Call JNI_OnLoad. We have to override the current class
2889 // loader, which will always be "null" since the stuff at the
2890 // top of the stack is around Runtime.loadLibrary(). (See
2891 // the comments in the JNI FindClass function.)
2892 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2893 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002894 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002895 self->SetClassLoaderOverride(class_loader);
2896
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002897 int version = 0;
2898 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002899 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002900 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002901 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002902 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002903
Brian Carlstromaded5f72011-10-07 17:15:04 -07002904 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002905
2906 if (version != JNI_VERSION_1_2 &&
2907 version != JNI_VERSION_1_4 &&
2908 version != JNI_VERSION_1_6) {
2909 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2910 << "bad version: " << version;
2911 // It's unwise to call dlclose() here, but we can mark it
2912 // as bad and ensure that future load attempts will fail.
2913 // We don't know how far JNI_OnLoad got, so there could
2914 // be some partially-initialized stuff accessible through
2915 // newly-registered native method calls. We could try to
2916 // unregister them, but that doesn't seem worthwhile.
2917 result = false;
2918 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002919 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2920 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002921 }
2922 }
2923
2924 library->SetResult(result);
2925 return result;
2926}
2927
Mathieu Chartier66f19252012-09-18 08:57:04 -07002928void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002929 CHECK(m->IsNative());
2930
2931 Class* c = m->GetDeclaringClass();
2932
2933 // If this is a static method, it could be called before the class
2934 // has been initialized.
2935 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002936 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002937 return NULL;
2938 }
2939 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002940 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002941 }
2942
Brian Carlstrom16192862011-09-12 17:50:06 -07002943 std::string detail;
2944 void* native_method;
2945 {
2946 MutexLock mu(libraries_lock);
2947 native_method = libraries->FindNativeMethod(m, detail);
2948 }
2949 // throwing can cause libraries_lock to be reacquired
2950 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002951 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002952 }
2953 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002954}
2955
Elliott Hughes410c0c82011-09-01 17:58:25 -07002956void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2957 {
2958 MutexLock mu(globals_lock);
2959 globals.VisitRoots(visitor, arg);
2960 }
2961 {
2962 MutexLock mu(pins_lock);
2963 pin_table.VisitRoots(visitor, arg);
2964 }
2965 // The weak_globals table is visited by the GC itself (because it mutates the table).
2966}
2967
Ian Rogersdf20fe02011-07-20 20:34:16 -07002968} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002969
2970std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2971 switch (rhs) {
2972 case JNIInvalidRefType:
2973 os << "JNIInvalidRefType";
2974 return os;
2975 case JNILocalRefType:
2976 os << "JNILocalRefType";
2977 return os;
2978 case JNIGlobalRefType:
2979 os << "JNIGlobalRefType";
2980 return os;
2981 case JNIWeakGlobalRefType:
2982 os << "JNIWeakGlobalRefType";
2983 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002984 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002985 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002986 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002987 }
2988}