blob: 6dc1a73267c51b61832c685754b754df0add4b25 [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:
Ian Rogersb726dcb2012-09-05 08:57:23 -070096 explicit ArgArray(Method* 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
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203static void CheckMethodArguments(Method* 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,
229 Method* 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);
243 Method* 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
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249static Method* FindVirtualMethod(Object* receiver, Method* 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);
258 Method* 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);
268 Method* 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
313 Method* method = NULL;
314 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_) {
Elliott Hughes6a144332012-04-03 13:07:11 -0700335 Method* 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
Elliott Hughes462c9442012-03-23 18:47:50 -0700484 runtime->AttachCurrentThread(thread_name, as_daemon, thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700485 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700486 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700487}
488
Elliott Hughes79082e32011-08-25 12:07:32 -0700489class SharedLibrary {
490 public:
491 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
492 : path_(path),
493 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700494 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700495 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughese62934d2012-04-09 11:24:29 -0700496 jni_on_load_cond_("JNI_OnLoad condition variable"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700497 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700498 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700499 }
500
Elliott Hughes79082e32011-08-25 12:07:32 -0700501 Object* GetClassLoader() {
502 return class_loader_;
503 }
504
505 std::string GetPath() {
506 return path_;
507 }
508
509 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700510 * Check the result of an earlier call to JNI_OnLoad on this library.
511 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700512 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700513 bool CheckOnLoadResult()
514 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700515 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700516 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700517 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
518 bool okay;
519 {
520 MutexLock mu(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700521
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700522 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
523 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
524 // caller can continue.
525 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
526 okay = true;
527 } else {
528 while (jni_on_load_result_ == kPending) {
529 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
530 jni_on_load_cond_.Wait(jni_on_load_lock_);
531 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700532
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700533 okay = (jni_on_load_result_ == kOkay);
534 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
535 << (okay ? "succeeded" : "failed") << "]";
536 }
537 }
538 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700539 return okay;
540 }
541
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700542 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700543 MutexLock mu(jni_on_load_lock_);
544
Elliott Hughes79082e32011-08-25 12:07:32 -0700545 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700546 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700547
548 // Broadcast a wakeup to anybody sleeping on the condition variable.
Elliott Hughes5f791332011-09-15 17:45:30 -0700549 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700550 }
551
552 void* FindSymbol(const std::string& symbol_name) {
553 return dlsym(handle_, symbol_name.c_str());
554 }
555
556 private:
557 enum JNI_OnLoadState {
558 kPending,
559 kFailed,
560 kOkay,
561 };
562
563 // Path to library "/system/lib/libjni.so".
564 std::string path_;
565
566 // The void* returned by dlopen(3).
567 void* handle_;
568
569 // The ClassLoader this library is associated with.
570 Object* class_loader_;
571
572 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700573 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700574 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700575 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700576 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700577 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700578 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700579 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700580};
581
Elliott Hughes79082e32011-08-25 12:07:32 -0700582// This exists mainly to keep implementation details out of the header file.
583class Libraries {
584 public:
585 Libraries() {
586 }
587
588 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700589 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 }
591
Elliott Hughesae80b492012-04-24 10:43:17 -0700592 void Dump(std::ostream& os) const {
593 bool first = true;
594 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
595 if (!first) {
596 os << ' ';
597 }
598 first = false;
599 os << it->first;
600 }
601 }
602
603 size_t size() const {
604 return libraries_.size();
605 }
606
Elliott Hughes79082e32011-08-25 12:07:32 -0700607 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700608 It it = libraries_.find(path);
609 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700610 }
611
612 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700613 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700614 }
615
616 // See section 11.3 "Linking Native Methods" of the JNI spec.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700617 void* FindNativeMethod(const Method* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700618 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700619 std::string jni_short_name(JniShortName(m));
620 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700621 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700622 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
623 SharedLibrary* library = it->second;
624 if (library->GetClassLoader() != declaring_class_loader) {
625 // We only search libraries loaded by the appropriate ClassLoader.
626 continue;
627 }
628 // Try the short name then the long name...
629 void* fn = library->FindSymbol(jni_short_name);
630 if (fn == NULL) {
631 fn = library->FindSymbol(jni_long_name);
632 }
633 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800634 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
635 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700636 return fn;
637 }
638 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700639 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700640 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700641 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700642 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700643 return NULL;
644 }
645
646 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700647 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700648
Elliott Hughesa0e18062012-04-13 15:59:59 -0700649 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700650};
651
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
653 jvalue* args) {
654 Object* receiver = soa.Decode<Object*>(obj);
655 Method* method = soa.DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800656 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 arg_array.BuildArgArray(soa, args);
658 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700659}
660
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661JValue InvokeWithJValues(const ScopedObjectAccess& soa, Object* receiver, Method* m,
662 JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700663 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 return InvokeWithArgArray(soa, receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800665}
666
Elliott Hughescdf53122011-08-19 15:46:09 -0700667class JNI {
668 public:
Elliott Hughescdf53122011-08-19 15:46:09 -0700669 static jint GetVersion(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 return JNI_VERSION_1_6;
672 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700673
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700675 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700677 return NULL;
678 }
679
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700681 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700682 Runtime* runtime = Runtime::Current();
683 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700685 Class* c = NULL;
686 if (runtime->IsStarted()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700687 ClassLoader* cl = GetClassLoader(soa.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800688 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700689 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800690 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700691 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700692 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700693 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700694
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 ScopedObjectAccess soa(env);
697 Method* method = soa.Decode<Method*>(java_method);
698 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700699 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700700
Elliott Hughescdf53122011-08-19 15:46:09 -0700701 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700702 ScopedObjectAccess soa(env);
703 Field* field = soa.Decode<Field*>(java_field);
704 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700706
Elliott Hughescdf53122011-08-19 15:46:09 -0700707 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708 ScopedObjectAccess soa(env);
709 Method* method = soa.DecodeMethod(mid);
710 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700712
Elliott Hughescdf53122011-08-19 15:46:09 -0700713 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 ScopedObjectAccess soa(env);
715 Field* field = soa.DecodeField(fid);
716 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700717 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700718
Elliott Hughes37f7a402011-08-22 18:56:01 -0700719 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700720 ScopedObjectAccess soa(env);
721 Object* o = soa.Decode<Object*>(java_object);
722 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700723 }
724
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700725 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700726 ScopedObjectAccess soa(env);
727 Class* c = soa.Decode<Class*>(java_class);
728 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700729 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700730
Elliott Hughes37f7a402011-08-22 18:56:01 -0700731 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700732 ScopedObjectAccess soa(env);
733 Class* c1 = soa.Decode<Class*>(java_class1);
734 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700735 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700737
Elliott Hughese84278b2012-03-22 10:06:53 -0700738 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700739 ScopedObjectAccess soa(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700740 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700741 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700742 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700743 return JNI_TRUE;
744 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700745 Object* obj = soa.Decode<Object*>(jobj);
746 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700747 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700748 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700749 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700750
Elliott Hughes37f7a402011-08-22 18:56:01 -0700751 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700752 ScopedObjectAccess soa(env);
753 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700754 if (exception == NULL) {
755 return JNI_ERR;
756 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700757 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700758 return JNI_OK;
759 }
760
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700761 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700762 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700763 }
764
765 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766 ScopedObjectAccess soa(env);
767 return soa.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700768 }
769
770 static void ExceptionClear(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700771 ScopedObjectAccess soa(env);
772 soa.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
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 static void FatalError(JNIEnv* env, const char* msg) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700807 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -0700808 LOG(FATAL) << "JNI FatalError called: " << msg;
809 }
810
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700811 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812 ScopedObjectAccess soa(env);
813 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700814 return JNI_ERR;
815 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700816 soa.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 return JNI_OK;
818 }
819
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700820 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700821 ScopedObjectAccess soa(env);
822 Object* survivor = soa.Decode<Object*>(java_survivor);
823 soa.Env()->PopFrame();
824 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700825 }
826
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700827 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700828 ScopedObjectAccess soa(env);
829 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700830 }
831
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700833 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 if (obj == NULL) {
835 return NULL;
836 }
837
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700838 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700840 Object* decoded_obj = soa.Decode<Object*>(obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 MutexLock mu(vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700842 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700843 return reinterpret_cast<jobject>(ref);
844 }
845
846 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700847 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 if (obj == NULL) {
849 return;
850 }
851
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700852 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700853 IndirectReferenceTable& globals = vm->globals;
854 MutexLock mu(vm->globals_lock);
855
856 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
857 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
858 << "failed to find entry";
859 }
860 }
861
862 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 ScopedObjectAccess soa(env);
864 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 }
866
867 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700868 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 if (obj == NULL) {
870 return;
871 }
872
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700873 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 IndirectReferenceTable& weak_globals = vm->weak_globals;
875 MutexLock mu(vm->weak_globals_lock);
876
877 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
878 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
879 << "failed to find entry";
880 }
881 }
882
883 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700884 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -0700885 if (obj == NULL) {
886 return NULL;
887 }
888
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700889 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700890
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700891 uint32_t cookie = soa.Env()->local_ref_cookie;
892 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 return reinterpret_cast<jobject>(ref);
894 }
895
896 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700897 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -0700898 if (obj == NULL) {
899 return;
900 }
901
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700902 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700903
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700904 uint32_t cookie = soa.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700905 if (!locals.Remove(cookie, obj)) {
906 // Attempting to delete a local reference that is not in the
907 // topmost local reference frame is a no-op. DeleteLocalRef returns
908 // void and doesn't throw any exceptions, but we should probably
909 // complain about it so the user will notice that things aren't
910 // going quite the way they expect.
911 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
912 << "failed to find entry";
913 }
914 }
915
916 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700917 ScopedObjectAccess soa(env);
918 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2))
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 ? JNI_TRUE : JNI_FALSE;
920 }
921
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700922 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700923 ScopedObjectAccess soa(env);
924 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700925 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700926 return NULL;
927 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700928 return soa.AddLocalReference<jobject>(c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 }
930
Elliott Hughese84278b2012-03-22 10:06:53 -0700931 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700932 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700935 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 va_end(args);
937 return result;
938 }
939
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700941 ScopedObjectAccess soa(env);
942 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700943 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700944 return NULL;
945 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700946 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700947 if (result == NULL) {
948 return NULL;
949 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700950 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700951 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700952 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700953 return local_result;
954 } else {
955 return NULL;
956 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 }
958
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 ScopedObjectAccess soa(env);
961 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700962 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700963 return NULL;
964 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700965 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700966 if (result == NULL) {
967 return NULL;
968 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700969 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700971 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700972 return local_result;
973 } else {
974 return NULL;
975 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700979 ScopedObjectAccess soa(env);
980 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
983 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700984 ScopedObjectAccess soa(env);
985 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 va_list ap;
991 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700992 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 ScopedObjectAccess soa(env);
999 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1000 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 ScopedObjectAccess soa(env);
1005 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1006 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001010 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 va_list ap;
1012 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001015 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001019 ScopedObjectAccess soa(env);
1020 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024 ScopedObjectAccess soa(env);
1025 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_list ap;
1031 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001032 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001034 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038 ScopedObjectAccess soa(env);
1039 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 ScopedObjectAccess soa(env);
1044 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_list ap;
1050 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001053 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001057 ScopedObjectAccess soa(env);
1058 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062 ScopedObjectAccess soa(env);
1063 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001067 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 va_list ap;
1069 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001070 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001072 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 ScopedObjectAccess soa(env);
1077 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 ScopedObjectAccess soa(env);
1082 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 va_list ap;
1088 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001089 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001091 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001095 ScopedObjectAccess soa(env);
1096 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 ScopedObjectAccess soa(env);
1101 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001105 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 va_list ap;
1107 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001108 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001110 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001114 ScopedObjectAccess soa(env);
1115 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 ScopedObjectAccess soa(env);
1120 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001124 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 va_list ap;
1126 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001127 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001129 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001133 ScopedObjectAccess soa(env);
1134 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001138 ScopedObjectAccess soa(env);
1139 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001143 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 va_list ap;
1145 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001146 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001148 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001152 ScopedObjectAccess soa(env);
1153 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 }
1155
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001157 ScopedObjectAccess soa(env);
1158 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 va_list ap;
1164 va_start(ap, mid);
Ian Rogers1b09b092012-08-20 15:35:52 -07001165 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001170 ScopedObjectAccess soa(env);
1171 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001175 ScopedObjectAccess soa(env);
1176 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001179 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001180 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001183 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1184 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 va_end(ap);
1186 return local_result;
1187 }
1188
1189 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001190 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001191 ScopedObjectAccess soa(env);
1192 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1193 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
1196 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001197 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 ScopedObjectAccess soa(env);
1199 JValue result(InvokeWithJValues(soa, obj, mid, args));
1200 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
1203 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001204 jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001205 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001208 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001210 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
1213 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001214 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001215 ScopedObjectAccess soa(env);
1216 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 }
1218
1219 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001220 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001221 ScopedObjectAccess soa(env);
1222 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 }
1224
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001225 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001226 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001229 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001231 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
1234 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001235 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001236 ScopedObjectAccess soa(env);
1237 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
1240 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001241 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001242 ScopedObjectAccess soa(env);
1243 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001246 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001247 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001250 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001252 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 }
1254
1255 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001256 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001257 ScopedObjectAccess soa(env);
1258 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 }
1260
1261 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001262 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001263 ScopedObjectAccess soa(env);
1264 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001267 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001268 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001270 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001271 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001273 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 }
1275
1276 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001277 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001278 ScopedObjectAccess soa(env);
1279 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
1281
1282 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001283 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001284 ScopedObjectAccess soa(env);
1285 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
1287
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001288 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001289 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001292 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001294 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
1297 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001298 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001299 ScopedObjectAccess soa(env);
1300 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
1303 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001304 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001305 ScopedObjectAccess soa(env);
1306 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001309 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001310 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001313 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001315 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
1318 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001319 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001320 ScopedObjectAccess soa(env);
1321 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
1324 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001325 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001326 ScopedObjectAccess soa(env);
1327 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001331 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001333 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001334 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001336 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 }
1338
1339 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001340 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001341 ScopedObjectAccess soa(env);
1342 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 }
1344
1345 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001346 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001347 ScopedObjectAccess soa(env);
1348 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001351 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001352 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001355 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001357 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 }
1359
1360 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001361 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001362 ScopedObjectAccess soa(env);
1363 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 }
1365
1366 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001367 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001368 ScopedObjectAccess soa(env);
1369 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001372 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001373 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001375 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001376 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 va_end(ap);
1378 }
1379
1380 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001381 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001382 ScopedObjectAccess soa(env);
1383 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
1385
1386 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001387 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001388 ScopedObjectAccess soa(env);
1389 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001392 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001393 ScopedObjectAccess soa(env);
1394 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001396
1397
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001398 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001399 ScopedObjectAccess soa(env);
1400 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001402
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001403 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001404 ScopedObjectAccess soa(env);
1405 Object* o = soa.Decode<Object*>(obj);
1406 Field* f = soa.DecodeField(fid);
1407 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001409
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001410 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001411 ScopedObjectAccess soa(env);
1412 Field* f = soa.DecodeField(fid);
1413 return soa.AddLocalReference<jobject>(f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001416 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001417 ScopedObjectAccess soa(env);
1418 Object* o = soa.Decode<Object*>(java_object);
1419 Object* v = soa.Decode<Object*>(java_value);
1420 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001421 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001424 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001425 ScopedObjectAccess soa(env);
1426 Object* v = soa.Decode<Object*>(java_value);
1427 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001428 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001431#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001432 ScopedObjectAccess soa(env); \
1433 Object* o = soa.Decode<Object*>(instance); \
1434 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435 return f->fn(o)
1436
1437#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001438 ScopedObjectAccess soa(env); \
1439 Object* o = soa.Decode<Object*>(instance); \
1440 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001441 f->fn(o, value)
1442
1443 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1444 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1448 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001475 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001479 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001483 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001487 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001491 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001495 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 GET_PRIMITIVE_FIELD(GetLong, NULL);
1497 }
1498
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001499 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1501 }
1502
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001503 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1505 }
1506
1507 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1508 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1509 }
1510
1511 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1512 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1513 }
1514
1515 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1516 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1517 }
1518
1519 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1520 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1521 }
1522
1523 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1524 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1525 }
1526
1527 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1528 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1529 }
1530
1531 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1532 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1533 }
1534
1535 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1536 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1537 }
1538
1539 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1540 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1541 }
1542
1543 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1544 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1545 }
1546
1547 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1548 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1549 }
1550
1551 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1552 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1553 }
1554
1555 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1556 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1557 }
1558
1559 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1560 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1561 }
1562
1563 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1564 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1565 }
1566
1567 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1568 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001571 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001572 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001574 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001575 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1576 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 va_end(ap);
1578 return local_result;
1579 }
1580
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001581 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001582 ScopedObjectAccess soa(env);
1583 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1584 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001587 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001588 ScopedObjectAccess soa(env);
1589 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1590 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001593 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001594 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001596 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001597 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001599 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 }
1601
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001602 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001603 ScopedObjectAccess soa(env);
1604 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001607 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001608 ScopedObjectAccess soa(env);
1609 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001612 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001613 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001616 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001618 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001621 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001622 ScopedObjectAccess soa(env);
1623 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001626 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001627 ScopedObjectAccess soa(env);
1628 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001631 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001632 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001635 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001637 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 }
1639
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001640 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001641 ScopedObjectAccess soa(env);
1642 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001645 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001646 ScopedObjectAccess soa(env);
1647 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001650 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001651 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001654 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001656 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 }
1658
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001659 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001660 ScopedObjectAccess soa(env);
1661 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001664 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001665 ScopedObjectAccess soa(env);
1666 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001669 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001673 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001675 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 }
1677
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001678 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001679 ScopedObjectAccess soa(env);
1680 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001683 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001684 ScopedObjectAccess soa(env);
1685 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001688 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001689 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001692 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001694 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 }
1696
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001697 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001698 ScopedObjectAccess soa(env);
1699 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 }
1701
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001702 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001703 ScopedObjectAccess soa(env);
1704 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001707 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001708 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001711 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001713 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 }
1715
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001716 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001717 ScopedObjectAccess soa(env);
1718 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001721 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001722 ScopedObjectAccess soa(env);
1723 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001726 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001727 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001729 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001730 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001732 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 }
1734
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001735 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001736 ScopedObjectAccess soa(env);
1737 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001740 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 ScopedObjectAccess soa(env);
1742 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001746 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001748 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001749 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 va_end(ap);
1751 }
1752
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001753 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001754 ScopedObjectAccess soa(env);
1755 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 }
1757
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001758 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001759 ScopedObjectAccess soa(env);
1760 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes814e4032011-08-23 12:07:56 -07001763 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001764 ScopedObjectAccess soa(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001765 String* result = String::AllocFromUtf16(char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001766 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
1769 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 if (utf == NULL) {
1771 return NULL;
1772 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001773 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 String* result = String::AllocFromModifiedUtf8(utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001775 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 }
1777
Elliott Hughes814e4032011-08-23 12:07:56 -07001778 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001779 ScopedObjectAccess soa(env);
1780 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001781 }
1782
1783 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001784 ScopedObjectAccess soa(env);
1785 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001786 }
1787
Elliott Hughesb465ab02011-08-24 11:21:21 -07001788 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 ScopedObjectAccess soa(env);
1790 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001791 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001792 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001793 } else {
1794 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1795 memcpy(buf, chars + start, length * sizeof(jchar));
1796 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001797 }
1798
Elliott Hughesb465ab02011-08-24 11:21:21 -07001799 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001800 ScopedObjectAccess soa(env);
1801 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001802 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001803 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001804 } else {
1805 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1806 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1807 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001808 }
1809
Elliott Hughes75770752011-08-24 17:52:38 -07001810 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001811 ScopedObjectAccess soa(env);
1812 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001813 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001814 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001815 if (is_copy != NULL) {
1816 *is_copy = JNI_FALSE;
1817 }
1818 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001819 }
1820
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001821 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001822 ScopedObjectAccess soa(env);
1823 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
Elliott Hughes75770752011-08-24 17:52:38 -07001826 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001827 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001828 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
Elliott Hughes75770752011-08-24 17:52:38 -07001831 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001832 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001833 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 }
1835
Elliott Hughes75770752011-08-24 17:52:38 -07001836 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001837 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001838 if (java_string == NULL) {
1839 return NULL;
1840 }
1841 if (is_copy != NULL) {
1842 *is_copy = JNI_TRUE;
1843 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001844 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001845 size_t byte_count = s->GetUtfLength();
1846 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001847 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001848 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1849 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1850 bytes[byte_count] = '\0';
1851 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001852 }
1853
Elliott Hughes75770752011-08-24 17:52:38 -07001854 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001855 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001856 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001857 }
1858
Elliott Hughesbd935992011-08-22 11:59:34 -07001859 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001860 ScopedObjectAccess soa(env);
1861 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001862 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001863 Array* array = obj->AsArray();
1864 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
Elliott Hughes814e4032011-08-23 12:07:56 -07001867 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001868 ScopedObjectAccess soa(env);
1869 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1870 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 }
1872
1873 static void SetObjectArrayElement(JNIEnv* env,
1874 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001875 ScopedObjectAccess soa(env);
1876 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1877 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 array->Set(index, value);
1879 }
1880
1881 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001882 ScopedObjectAccess soa(env);
1883 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001884 }
1885
1886 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001887 ScopedObjectAccess soa(env);
1888 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 }
1890
1891 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001892 ScopedObjectAccess soa(env);
1893 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001894 }
1895
1896 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001897 ScopedObjectAccess soa(env);
1898 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001899 }
1900
1901 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001902 ScopedObjectAccess soa(env);
1903 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 }
1905
1906 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001907 ScopedObjectAccess soa(env);
1908 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001909 }
1910
1911 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001912 ScopedObjectAccess soa(env);
1913 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001914 }
1915
1916 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001917 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 CHECK_GE(length, 0); // TODO: ReportJniError
1919
1920 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001921 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 std::string descriptor;
1923 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001924 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001925
1926 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001927 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1928 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001929 return NULL;
1930 }
1931
Elliott Hughes75770752011-08-24 17:52:38 -07001932 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001933 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001935 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001936 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001937 for (jsize i = 0; i < length; ++i) {
1938 result->Set(i, initial_object);
1939 }
1940 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001941 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001942 }
1943
1944 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001945 ScopedObjectAccess soa(env);
1946 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 }
1948
Ian Rogersa15e67d2012-02-28 13:51:55 -08001949 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001950 ScopedObjectAccess soa(env);
1951 Array* array = soa.Decode<Array*>(java_array);
1952 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001953 if (is_copy != NULL) {
1954 *is_copy = JNI_FALSE;
1955 }
1956 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001957 }
1958
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001959 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001960 ScopedObjectAccess soa(env);
1961 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001962 }
1963
Elliott Hughes75770752011-08-24 17:52:38 -07001964 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001965 ScopedObjectAccess soa(env);
1966 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 }
1968
Elliott Hughes75770752011-08-24 17:52:38 -07001969 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001970 ScopedObjectAccess soa(env);
1971 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Elliott Hughes75770752011-08-24 17:52:38 -07001974 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001975 ScopedObjectAccess soa(env);
1976 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Elliott Hughes75770752011-08-24 17:52:38 -07001979 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001980 ScopedObjectAccess soa(env);
1981 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes75770752011-08-24 17:52:38 -07001984 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001985 ScopedObjectAccess soa(env);
1986 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Elliott Hughes75770752011-08-24 17:52:38 -07001989 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001990 ScopedObjectAccess soa(env);
1991 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Elliott Hughes75770752011-08-24 17:52:38 -07001994 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001995 ScopedObjectAccess soa(env);
1996 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Elliott Hughes75770752011-08-24 17:52:38 -07001999 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002000 ScopedObjectAccess soa(env);
2001 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002004 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002005 ScopedObjectAccess soa(env);
2006 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002009 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002010 ScopedObjectAccess soa(env);
2011 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002014 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002015 ScopedObjectAccess soa(env);
2016 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002019 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002020 ScopedObjectAccess soa(env);
2021 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002024 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002025 ScopedObjectAccess soa(env);
2026 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002029 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002030 ScopedObjectAccess soa(env);
2031 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002034 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002035 ScopedObjectAccess soa(env);
2036 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002039 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002040 ScopedObjectAccess soa(env);
2041 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes814e4032011-08-23 12:07:56 -07002044 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002045 ScopedObjectAccess soa(env);
2046 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes814e4032011-08-23 12:07:56 -07002049 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002050 ScopedObjectAccess soa(env);
2051 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes814e4032011-08-23 12:07:56 -07002054 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002055 ScopedObjectAccess soa(env);
2056 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002060 ScopedObjectAccess soa(env);
2061 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002065 ScopedObjectAccess soa(env);
2066 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002070 ScopedObjectAccess soa(env);
2071 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002075 ScopedObjectAccess soa(env);
2076 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002080 ScopedObjectAccess soa(env);
2081 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002085 ScopedObjectAccess soa(env);
2086 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002090 ScopedObjectAccess soa(env);
2091 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002095 ScopedObjectAccess soa(env);
2096 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002100 ScopedObjectAccess soa(env);
2101 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002105 ScopedObjectAccess soa(env);
2106 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002110 ScopedObjectAccess soa(env);
2111 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002115 ScopedObjectAccess soa(env);
2116 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002120 ScopedObjectAccess soa(env);
2121 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughes5174fe62011-08-23 15:12:35 -07002124 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002125 ScopedObjectAccess soa(env);
2126 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002127
Elliott Hughes5174fe62011-08-23 15:12:35 -07002128 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 const char* name = methods[i].name;
2130 const char* sig = methods[i].signature;
2131
2132 if (*sig == '!') {
2133 // TODO: fast jni. it's too noisy to log all these.
2134 ++sig;
2135 }
2136
Elliott Hughes5174fe62011-08-23 15:12:35 -07002137 Method* m = c->FindDirectMethod(name, sig);
2138 if (m == NULL) {
2139 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002141 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002142 LOG(INFO) << "Failed to register native method " << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002143 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002145 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002146 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002147 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 return JNI_ERR;
2149 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002151 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002152
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002153 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 }
2155 return JNI_OK;
2156 }
2157
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002159 ScopedObjectAccess soa(env);
2160 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002161
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002162 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002163
2164 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2165 Method* m = c->GetDirectMethod(i);
2166 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002167 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002168 }
2169 }
2170 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2171 Method* m = c->GetVirtualMethod(i);
2172 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002173 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002174 }
2175 }
2176
2177 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002178 }
2179
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002180 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2181 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2182 ScopedObjectAccess soa(env);
2183 Object* o = soa.Decode<Object*>(java_object);
2184 o->MonitorEnter(soa.Self());
2185 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002186 return JNI_ERR;
2187 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002188 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002189 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002192 static jint MonitorExit(JNIEnv* env, jobject java_object)
2193 UNLOCK_FUNCTION(monitor_lock_) {
2194 ScopedObjectAccess soa(env);
2195 Object* o = soa.Decode<Object*>(java_object);
2196 o->MonitorExit(soa.Self());
2197 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002198 return JNI_ERR;
2199 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002200 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002201 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 }
2203
2204 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002205 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 Runtime* runtime = Runtime::Current();
2207 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002208 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 } else {
2210 *vm = NULL;
2211 }
2212 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2213 }
2214
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002216 ScopedObjectAccess soa(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002217
2218 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002219 CHECK(address != NULL); // TODO: ReportJniError
2220 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002221
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002222 // At the moment, the Java side is limited to 32 bisoa.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002223 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2224 CHECK_LE(capacity, 0xffffffff);
2225 jint address_arg = reinterpret_cast<jint>(address);
2226 jint capacity_arg = static_cast<jint>(capacity);
2227
Elliott Hugheseac76672012-05-24 21:56:51 -07002228 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2229 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2230 address_arg, capacity_arg);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002231 return soa.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 }
2233
Elliott Hughesb465ab02011-08-24 11:21:21 -07002234 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002235 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002236 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 }
2238
Elliott Hughesb465ab02011-08-24 11:21:21 -07002239 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002240 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002241 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Elliott Hughesb465ab02011-08-24 11:21:21 -07002244 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002245 ScopedObjectAccess soa(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002246
Elliott Hughes75770752011-08-24 17:52:38 -07002247 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002248
2249 // Do we definitely know what kind of reference this is?
2250 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2251 IndirectRefKind kind = GetIndirectRefKind(ref);
2252 switch (kind) {
2253 case kLocal:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002254 if (soa.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002255 return JNILocalRefType;
2256 }
2257 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002258 case kGlobal:
2259 return JNIGlobalRefType;
2260 case kWeakGlobal:
2261 return JNIWeakGlobalRefType;
2262 case kSirtOrInvalid:
2263 // Is it in a stack IRT?
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002264 if (soa.Self()->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002265 return JNILocalRefType;
2266 }
2267
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002268 if (!soa.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002269 return JNIInvalidRefType;
2270 }
2271
Elliott Hughesb465ab02011-08-24 11:21:21 -07002272 // If we're handing out direct pointers, check whether it's a direct pointer
2273 // to a local reference.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002274 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2275 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002276 return JNILocalRefType;
2277 }
2278 }
2279
2280 return JNIInvalidRefType;
2281 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002282 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2283 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002284 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002285
2286 private:
2287 static jint EnsureLocalCapacity(const ScopedObjectAccess& soa, jint desired_capacity,
2288 const char* caller)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002289 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002290 // TODO: we should try to expand the table if necessary.
2291 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2292 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2293 return JNI_ERR;
2294 }
2295 // TODO: this isn't quite right, since "capacity" includes holes.
2296 size_t capacity = soa.Env()->locals.Capacity();
2297 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2298 if (!okay) {
2299 soa.Self()->ThrowOutOfMemoryError(caller);
2300 }
2301 return okay ? JNI_OK : JNI_ERR;
2302 }
2303
2304 template<typename JniT, typename ArtT>
2305 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002306 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002307 CHECK_GE(length, 0); // TODO: ReportJniError
2308 ArtT* result = ArtT::Alloc(length);
2309 return soa.AddLocalReference<JniT>(result);
2310 }
2311
2312 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2313 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2314 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002315 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002316 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2317 PinPrimitiveArray(soa, array);
2318 if (is_copy != NULL) {
2319 *is_copy = JNI_FALSE;
2320 }
2321 return array->GetData();
2322 }
2323
2324 template <typename ArrayT>
2325 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2326 jint mode)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002328 if (mode != JNI_COMMIT) {
2329 Array* array = soa.Decode<Array*>(java_array);
2330 UnpinPrimitiveArray(soa, array);
2331 }
2332 }
2333
2334 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2335 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2336 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002338 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2339 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2340 ThrowAIOOBE(soa, array, start, length, "src");
2341 } else {
2342 JavaT* data = array->GetData();
2343 memcpy(buf, data + start, length * sizeof(JavaT));
2344 }
2345 }
2346
2347 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2348 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2349 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002350 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002351 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2352 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2353 ThrowAIOOBE(soa, array, start, length, "dst");
2354 } else {
2355 JavaT* data = array->GetData();
2356 memcpy(data + start, buf, length * sizeof(JavaT));
2357 }
2358 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002359};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002360
Elliott Hughes88c5c352012-03-15 18:49:48 -07002361const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002362 NULL, // reserved0.
2363 NULL, // reserved1.
2364 NULL, // reserved2.
2365 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002366 JNI::GetVersion,
2367 JNI::DefineClass,
2368 JNI::FindClass,
2369 JNI::FromReflectedMethod,
2370 JNI::FromReflectedField,
2371 JNI::ToReflectedMethod,
2372 JNI::GetSuperclass,
2373 JNI::IsAssignableFrom,
2374 JNI::ToReflectedField,
2375 JNI::Throw,
2376 JNI::ThrowNew,
2377 JNI::ExceptionOccurred,
2378 JNI::ExceptionDescribe,
2379 JNI::ExceptionClear,
2380 JNI::FatalError,
2381 JNI::PushLocalFrame,
2382 JNI::PopLocalFrame,
2383 JNI::NewGlobalRef,
2384 JNI::DeleteGlobalRef,
2385 JNI::DeleteLocalRef,
2386 JNI::IsSameObject,
2387 JNI::NewLocalRef,
2388 JNI::EnsureLocalCapacity,
2389 JNI::AllocObject,
2390 JNI::NewObject,
2391 JNI::NewObjectV,
2392 JNI::NewObjectA,
2393 JNI::GetObjectClass,
2394 JNI::IsInstanceOf,
2395 JNI::GetMethodID,
2396 JNI::CallObjectMethod,
2397 JNI::CallObjectMethodV,
2398 JNI::CallObjectMethodA,
2399 JNI::CallBooleanMethod,
2400 JNI::CallBooleanMethodV,
2401 JNI::CallBooleanMethodA,
2402 JNI::CallByteMethod,
2403 JNI::CallByteMethodV,
2404 JNI::CallByteMethodA,
2405 JNI::CallCharMethod,
2406 JNI::CallCharMethodV,
2407 JNI::CallCharMethodA,
2408 JNI::CallShortMethod,
2409 JNI::CallShortMethodV,
2410 JNI::CallShortMethodA,
2411 JNI::CallIntMethod,
2412 JNI::CallIntMethodV,
2413 JNI::CallIntMethodA,
2414 JNI::CallLongMethod,
2415 JNI::CallLongMethodV,
2416 JNI::CallLongMethodA,
2417 JNI::CallFloatMethod,
2418 JNI::CallFloatMethodV,
2419 JNI::CallFloatMethodA,
2420 JNI::CallDoubleMethod,
2421 JNI::CallDoubleMethodV,
2422 JNI::CallDoubleMethodA,
2423 JNI::CallVoidMethod,
2424 JNI::CallVoidMethodV,
2425 JNI::CallVoidMethodA,
2426 JNI::CallNonvirtualObjectMethod,
2427 JNI::CallNonvirtualObjectMethodV,
2428 JNI::CallNonvirtualObjectMethodA,
2429 JNI::CallNonvirtualBooleanMethod,
2430 JNI::CallNonvirtualBooleanMethodV,
2431 JNI::CallNonvirtualBooleanMethodA,
2432 JNI::CallNonvirtualByteMethod,
2433 JNI::CallNonvirtualByteMethodV,
2434 JNI::CallNonvirtualByteMethodA,
2435 JNI::CallNonvirtualCharMethod,
2436 JNI::CallNonvirtualCharMethodV,
2437 JNI::CallNonvirtualCharMethodA,
2438 JNI::CallNonvirtualShortMethod,
2439 JNI::CallNonvirtualShortMethodV,
2440 JNI::CallNonvirtualShortMethodA,
2441 JNI::CallNonvirtualIntMethod,
2442 JNI::CallNonvirtualIntMethodV,
2443 JNI::CallNonvirtualIntMethodA,
2444 JNI::CallNonvirtualLongMethod,
2445 JNI::CallNonvirtualLongMethodV,
2446 JNI::CallNonvirtualLongMethodA,
2447 JNI::CallNonvirtualFloatMethod,
2448 JNI::CallNonvirtualFloatMethodV,
2449 JNI::CallNonvirtualFloatMethodA,
2450 JNI::CallNonvirtualDoubleMethod,
2451 JNI::CallNonvirtualDoubleMethodV,
2452 JNI::CallNonvirtualDoubleMethodA,
2453 JNI::CallNonvirtualVoidMethod,
2454 JNI::CallNonvirtualVoidMethodV,
2455 JNI::CallNonvirtualVoidMethodA,
2456 JNI::GetFieldID,
2457 JNI::GetObjectField,
2458 JNI::GetBooleanField,
2459 JNI::GetByteField,
2460 JNI::GetCharField,
2461 JNI::GetShortField,
2462 JNI::GetIntField,
2463 JNI::GetLongField,
2464 JNI::GetFloatField,
2465 JNI::GetDoubleField,
2466 JNI::SetObjectField,
2467 JNI::SetBooleanField,
2468 JNI::SetByteField,
2469 JNI::SetCharField,
2470 JNI::SetShortField,
2471 JNI::SetIntField,
2472 JNI::SetLongField,
2473 JNI::SetFloatField,
2474 JNI::SetDoubleField,
2475 JNI::GetStaticMethodID,
2476 JNI::CallStaticObjectMethod,
2477 JNI::CallStaticObjectMethodV,
2478 JNI::CallStaticObjectMethodA,
2479 JNI::CallStaticBooleanMethod,
2480 JNI::CallStaticBooleanMethodV,
2481 JNI::CallStaticBooleanMethodA,
2482 JNI::CallStaticByteMethod,
2483 JNI::CallStaticByteMethodV,
2484 JNI::CallStaticByteMethodA,
2485 JNI::CallStaticCharMethod,
2486 JNI::CallStaticCharMethodV,
2487 JNI::CallStaticCharMethodA,
2488 JNI::CallStaticShortMethod,
2489 JNI::CallStaticShortMethodV,
2490 JNI::CallStaticShortMethodA,
2491 JNI::CallStaticIntMethod,
2492 JNI::CallStaticIntMethodV,
2493 JNI::CallStaticIntMethodA,
2494 JNI::CallStaticLongMethod,
2495 JNI::CallStaticLongMethodV,
2496 JNI::CallStaticLongMethodA,
2497 JNI::CallStaticFloatMethod,
2498 JNI::CallStaticFloatMethodV,
2499 JNI::CallStaticFloatMethodA,
2500 JNI::CallStaticDoubleMethod,
2501 JNI::CallStaticDoubleMethodV,
2502 JNI::CallStaticDoubleMethodA,
2503 JNI::CallStaticVoidMethod,
2504 JNI::CallStaticVoidMethodV,
2505 JNI::CallStaticVoidMethodA,
2506 JNI::GetStaticFieldID,
2507 JNI::GetStaticObjectField,
2508 JNI::GetStaticBooleanField,
2509 JNI::GetStaticByteField,
2510 JNI::GetStaticCharField,
2511 JNI::GetStaticShortField,
2512 JNI::GetStaticIntField,
2513 JNI::GetStaticLongField,
2514 JNI::GetStaticFloatField,
2515 JNI::GetStaticDoubleField,
2516 JNI::SetStaticObjectField,
2517 JNI::SetStaticBooleanField,
2518 JNI::SetStaticByteField,
2519 JNI::SetStaticCharField,
2520 JNI::SetStaticShortField,
2521 JNI::SetStaticIntField,
2522 JNI::SetStaticLongField,
2523 JNI::SetStaticFloatField,
2524 JNI::SetStaticDoubleField,
2525 JNI::NewString,
2526 JNI::GetStringLength,
2527 JNI::GetStringChars,
2528 JNI::ReleaseStringChars,
2529 JNI::NewStringUTF,
2530 JNI::GetStringUTFLength,
2531 JNI::GetStringUTFChars,
2532 JNI::ReleaseStringUTFChars,
2533 JNI::GetArrayLength,
2534 JNI::NewObjectArray,
2535 JNI::GetObjectArrayElement,
2536 JNI::SetObjectArrayElement,
2537 JNI::NewBooleanArray,
2538 JNI::NewByteArray,
2539 JNI::NewCharArray,
2540 JNI::NewShortArray,
2541 JNI::NewIntArray,
2542 JNI::NewLongArray,
2543 JNI::NewFloatArray,
2544 JNI::NewDoubleArray,
2545 JNI::GetBooleanArrayElements,
2546 JNI::GetByteArrayElements,
2547 JNI::GetCharArrayElements,
2548 JNI::GetShortArrayElements,
2549 JNI::GetIntArrayElements,
2550 JNI::GetLongArrayElements,
2551 JNI::GetFloatArrayElements,
2552 JNI::GetDoubleArrayElements,
2553 JNI::ReleaseBooleanArrayElements,
2554 JNI::ReleaseByteArrayElements,
2555 JNI::ReleaseCharArrayElements,
2556 JNI::ReleaseShortArrayElements,
2557 JNI::ReleaseIntArrayElements,
2558 JNI::ReleaseLongArrayElements,
2559 JNI::ReleaseFloatArrayElements,
2560 JNI::ReleaseDoubleArrayElements,
2561 JNI::GetBooleanArrayRegion,
2562 JNI::GetByteArrayRegion,
2563 JNI::GetCharArrayRegion,
2564 JNI::GetShortArrayRegion,
2565 JNI::GetIntArrayRegion,
2566 JNI::GetLongArrayRegion,
2567 JNI::GetFloatArrayRegion,
2568 JNI::GetDoubleArrayRegion,
2569 JNI::SetBooleanArrayRegion,
2570 JNI::SetByteArrayRegion,
2571 JNI::SetCharArrayRegion,
2572 JNI::SetShortArrayRegion,
2573 JNI::SetIntArrayRegion,
2574 JNI::SetLongArrayRegion,
2575 JNI::SetFloatArrayRegion,
2576 JNI::SetDoubleArrayRegion,
2577 JNI::RegisterNatives,
2578 JNI::UnregisterNatives,
2579 JNI::MonitorEnter,
2580 JNI::MonitorExit,
2581 JNI::GetJavaVM,
2582 JNI::GetStringRegion,
2583 JNI::GetStringUTFRegion,
2584 JNI::GetPrimitiveArrayCritical,
2585 JNI::ReleasePrimitiveArrayCritical,
2586 JNI::GetStringCritical,
2587 JNI::ReleaseStringCritical,
2588 JNI::NewWeakGlobalRef,
2589 JNI::DeleteWeakGlobalRef,
2590 JNI::ExceptionCheck,
2591 JNI::NewDirectByteBuffer,
2592 JNI::GetDirectBufferAddress,
2593 JNI::GetDirectBufferCapacity,
2594 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002595};
2596
Elliott Hughes75770752011-08-24 17:52:38 -07002597JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002598 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002599 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002600 local_ref_cookie(IRT_FIRST_SEGMENT),
2601 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002602 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002603 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002604 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002605 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002606 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002607 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002608 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002609 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2610 // errors will ensue.
2611 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2612 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002613}
2614
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002615JNIEnvExt::~JNIEnvExt() {
2616}
2617
Elliott Hughes88c5c352012-03-15 18:49:48 -07002618void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2619 check_jni = enabled;
2620 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002621}
2622
Elliott Hughes73e66f72012-05-09 09:34:45 -07002623void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2624 locals.Dump(os);
2625 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002626}
2627
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002628void JNIEnvExt::PushFrame(int /*capacity*/) {
2629 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002630 stacked_local_ref_cookies.push_back(local_ref_cookie);
2631 local_ref_cookie = locals.GetSegmentState();
2632}
2633
2634void JNIEnvExt::PopFrame() {
2635 locals.SetSegmentState(local_ref_cookie);
2636 local_ref_cookie = stacked_local_ref_cookies.back();
2637 stacked_local_ref_cookies.pop_back();
2638}
2639
Carl Shapiroea4dca82011-08-01 13:45:38 -07002640// JNI Invocation interface.
2641
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002642extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2643 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2644 if (args->version < JNI_VERSION_1_2) {
2645 return JNI_EVERSION;
2646 }
2647 Runtime::Options options;
2648 for (int i = 0; i < args->nOptions; ++i) {
2649 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002650 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002651 }
2652 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002653 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002654 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002655 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002656 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002657 runtime->Start();
2658 *p_env = Thread::Current()->GetJniEnv();
2659 *p_vm = runtime->GetJavaVM();
2660 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002661}
2662
Elliott Hughesf2682d52011-08-15 16:37:04 -07002663extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002664 Runtime* runtime = Runtime::Current();
2665 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002666 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002667 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002668 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002669 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002670 }
2671 return JNI_OK;
2672}
2673
2674// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002675extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002676 return JNI_ERR;
2677}
2678
Elliott Hughescdf53122011-08-19 15:46:09 -07002679class JII {
2680 public:
2681 static jint DestroyJavaVM(JavaVM* vm) {
2682 if (vm == NULL) {
2683 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002684 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002685 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2686 delete raw_vm->runtime;
2687 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002688 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002689
Elliott Hughescdf53122011-08-19 15:46:09 -07002690 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002691 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002692 }
2693
2694 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002695 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002696 }
2697
2698 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002699 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002700 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002701 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002702 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2703 Runtime* runtime = raw_vm->runtime;
2704 runtime->DetachCurrentThread();
2705 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002706 }
2707
2708 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2709 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2710 return JNI_EVERSION;
2711 }
2712 if (vm == NULL || env == NULL) {
2713 return JNI_ERR;
2714 }
2715 Thread* thread = Thread::Current();
2716 if (thread == NULL) {
2717 *env = NULL;
2718 return JNI_EDETACHED;
2719 }
2720 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002721 return JNI_OK;
2722 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002723};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002724
Elliott Hughes88c5c352012-03-15 18:49:48 -07002725const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002726 NULL, // reserved0
2727 NULL, // reserved1
2728 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002729 JII::DestroyJavaVM,
2730 JII::AttachCurrentThread,
2731 JII::DetachCurrentThread,
2732 JII::GetEnv,
2733 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002734};
2735
Elliott Hughesa0957642011-09-02 14:27:33 -07002736JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002737 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002738 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002739 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002740 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002741 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002742 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002743 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002744 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002745 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002746 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002747 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002748 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002749 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002750 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002751 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002752 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002753 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002754 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002755 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002756}
2757
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002758JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002759 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002760}
2761
Elliott Hughes88c5c352012-03-15 18:49:48 -07002762void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2763 check_jni = enabled;
2764 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002765}
2766
Elliott Hughesae80b492012-04-24 10:43:17 -07002767void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2768 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2769 if (force_copy) {
2770 os << " (with forcecopy)";
2771 }
2772 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
2773 {
2774 MutexLock mu(pins_lock);
2775 os << "; pins=" << pin_table.Size();
2776 }
2777 {
2778 MutexLock mu(globals_lock);
2779 os << "; globals=" << globals.Capacity();
2780 }
2781 {
2782 MutexLock mu(weak_globals_lock);
2783 if (weak_globals.Capacity() > 0) {
2784 os << " (plus " << weak_globals.Capacity() << " weak)";
2785 }
2786 }
2787 os << '\n';
2788
2789 {
2790 MutexLock mu(libraries_lock);
2791 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2792 }
2793}
2794
Elliott Hughes73e66f72012-05-09 09:34:45 -07002795void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002796 {
2797 MutexLock mu(globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002798 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002799 }
2800 {
2801 MutexLock mu(weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002802 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002803 }
2804 {
2805 MutexLock mu(pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002806 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002807 }
2808}
2809
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002810bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2811 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002812 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002813
2814 // See if we've already loaded this library. If we have, and the class loader
2815 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002816 // TODO: for better results we should canonicalize the pathname (or even compare
2817 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002818 SharedLibrary* library;
2819 {
2820 // TODO: move the locking (and more of this logic) into Libraries.
2821 MutexLock mu(libraries_lock);
2822 library = libraries->Get(path);
2823 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002824 if (library != NULL) {
2825 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002826 // The library will be associated with class_loader. The JNI
2827 // spec says we can't load the same library into more than one
2828 // class loader.
2829 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2830 "ClassLoader %p; can't open in ClassLoader %p",
2831 path.c_str(), library->GetClassLoader(), class_loader);
2832 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002833 return false;
2834 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002835 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2836 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002837 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002838 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2839 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002840 return false;
2841 }
2842 return true;
2843 }
2844
2845 // Open the shared library. Because we're using a full path, the system
2846 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2847 // resolve this library's dependencies though.)
2848
2849 // Failures here are expected when java.library.path has several entries
2850 // and we have to hunt for the lib.
2851
2852 // The current version of the dynamic linker prints detailed information
2853 // about dlopen() failures. Some things to check if the message is
2854 // cryptic:
2855 // - make sure the library exists on the device
2856 // - verify that the right path is being opened (the debug log message
2857 // above can help with that)
2858 // - check to see if the library is valid (e.g. not zero bytes long)
2859 // - check config/prelink-linux-arm.map to ensure that the library
2860 // is listed and is not being overrun by the previous entry (if
2861 // loading suddenly stops working on a prelinked library, this is
2862 // a good one to check)
2863 // - write a trivial app that calls sleep() then dlopen(), attach
2864 // to it with "strace -p <pid>" while it sleeps, and watch for
2865 // attempts to open nonexistent dependent shared libs
Elliott Hughescdf53122011-08-19 15:46:09 -07002866 // TODO: automate some of these checks!
2867
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002868 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2869 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2870 // dlopen) becomes zero from dlclose.
2871
Elliott Hughescdf53122011-08-19 15:46:09 -07002872 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002873 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Elliott Hughescdf53122011-08-19 15:46:09 -07002874 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002875 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2876 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2877 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002878
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002879 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002880
2881 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002882 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002883 return false;
2884 }
2885
2886 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002887 // TODO: move the locking (and more of this logic) into Libraries.
2888 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002889 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002890 MutexLock mu(libraries_lock);
2891 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002892 if (library == NULL) { // We won race to get libraries_lock
2893 library = new SharedLibrary(path, handle, class_loader);
2894 libraries->Put(path, library);
2895 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002896 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002897 }
2898 if (!created_library) {
2899 LOG(INFO) << "WOW: we lost a race to add shared library: "
2900 << "\"" << path << "\" ClassLoader=" << class_loader;
2901 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002902 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002903
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002904 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002905
2906 bool result = true;
2907 void* sym = dlsym(handle, "JNI_OnLoad");
2908 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002909 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002910 } else {
2911 // Call JNI_OnLoad. We have to override the current class
2912 // loader, which will always be "null" since the stuff at the
2913 // top of the stack is around Runtime.loadLibrary(). (See
2914 // the comments in the JNI FindClass function.)
2915 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2916 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002917 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002918 self->SetClassLoaderOverride(class_loader);
2919
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002920 int version = 0;
2921 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002922 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002923 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002924 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002925 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002926
Brian Carlstromaded5f72011-10-07 17:15:04 -07002927 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002928
2929 if (version != JNI_VERSION_1_2 &&
2930 version != JNI_VERSION_1_4 &&
2931 version != JNI_VERSION_1_6) {
2932 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2933 << "bad version: " << version;
2934 // It's unwise to call dlclose() here, but we can mark it
2935 // as bad and ensure that future load attempts will fail.
2936 // We don't know how far JNI_OnLoad got, so there could
2937 // be some partially-initialized stuff accessible through
2938 // newly-registered native method calls. We could try to
2939 // unregister them, but that doesn't seem worthwhile.
2940 result = false;
2941 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002942 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2943 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002944 }
2945 }
2946
2947 library->SetResult(result);
2948 return result;
2949}
2950
2951void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2952 CHECK(m->IsNative());
2953
2954 Class* c = m->GetDeclaringClass();
2955
2956 // If this is a static method, it could be called before the class
2957 // has been initialized.
2958 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002959 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002960 return NULL;
2961 }
2962 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002963 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002964 }
2965
Brian Carlstrom16192862011-09-12 17:50:06 -07002966 std::string detail;
2967 void* native_method;
2968 {
2969 MutexLock mu(libraries_lock);
2970 native_method = libraries->FindNativeMethod(m, detail);
2971 }
2972 // throwing can cause libraries_lock to be reacquired
2973 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002974 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002975 }
2976 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002977}
2978
Elliott Hughes410c0c82011-09-01 17:58:25 -07002979void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2980 {
2981 MutexLock mu(globals_lock);
2982 globals.VisitRoots(visitor, arg);
2983 }
2984 {
2985 MutexLock mu(pins_lock);
2986 pin_table.VisitRoots(visitor, arg);
2987 }
2988 // The weak_globals table is visited by the GC itself (because it mutates the table).
2989}
2990
Ian Rogersdf20fe02011-07-20 20:34:16 -07002991} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002992
2993std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2994 switch (rhs) {
2995 case JNIInvalidRefType:
2996 os << "JNIInvalidRefType";
2997 return os;
2998 case JNILocalRefType:
2999 os << "JNILocalRefType";
3000 return os;
3001 case JNIGlobalRefType:
3002 os << "JNIGlobalRefType";
3003 return os;
3004 case JNIWeakGlobalRefType:
3005 os << "JNIWeakGlobalRefType";
3006 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003007 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003008 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003009 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003010 }
3011}