blob: 77066c43d54f65389e434f89e6e3299c27e6aabd [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Elliott Hughes40ef99e2011-08-11 17:44:34 -070025#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070027#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "logging.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029#include "mutex.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070030#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070032#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070033#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070035#include "ScopedLocalRef.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070036#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070037#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070038#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070039#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070040#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070041
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070042namespace art {
43
Elliott Hughes2ced6a52011-10-16 18:44:48 -070044static const size_t kMonitorsInitial = 32; // Arbitrary.
45static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
46
47static const size_t kLocalsInitial = 64; // Arbitrary.
48static const size_t kLocalsMax = 512; // Arbitrary sanity check.
49
50static const size_t kPinTableInitial = 16; // Arbitrary.
51static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
52
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053static size_t gGlobalsInitial = 512; // Arbitrary.
54static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070055
56static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
57static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
58
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
60 size_t method_count) {
Elliott Hugheseac76672012-05-24 21:56:51 -070061 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
62 if (c.get() == NULL) {
63 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
64 }
65 if (env->RegisterNatives(c.get(), methods, method_count) != JNI_OK) {
66 LOG(FATAL) << "Failed to register natives methods: " << jni_class_name;
67 }
68}
69
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070070void SetJniGlobalsMax(size_t max) {
71 if (max != 0) {
72 gGlobalsMax = max;
73 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
74 }
75}
Ian Rogersdf20fe02011-07-20 20:34:16 -070076
Ian Rogers45619fc2012-02-29 11:15:25 -080077size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
78 size_t num_bytes = 0;
79 for (size_t i = 1; i < shorty_len; ++i) {
80 char ch = shorty[i];
81 if (ch == 'D' || ch == 'J') {
82 num_bytes += 8;
83 } else if (ch == 'L') {
84 // Argument is a reference or an array. The shorty descriptor
85 // does not distinguish between these types.
86 num_bytes += sizeof(Object*);
87 } else {
88 num_bytes += 4;
89 }
90 }
91 return num_bytes;
92}
93
94class ArgArray {
95 public:
Mathieu Chartier66f19252012-09-18 08:57:04 -070096 explicit ArgArray(AbstractMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers45619fc2012-02-29 11:15:25 -080097 MethodHelper mh(method);
98 shorty_ = mh.GetShorty();
99 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -0700100 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800101 arg_array_ = small_arg_array_;
102 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700103 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800104 arg_array_ = large_arg_array_.get();
105 }
106 }
107
Elliott Hughes77405792012-03-15 15:22:12 -0700108 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800109 return arg_array_;
110 }
111
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 void BuildArgArray(const ScopedObjectAccess& soa, va_list ap)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes77405792012-03-15 15:22:12 -0700114 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800115 switch (shorty_[i]) {
116 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700117 arg_array_[offset].SetZ(va_arg(ap, jint));
118 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800119 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700120 arg_array_[offset].SetB(va_arg(ap, jint));
121 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800122 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700123 arg_array_[offset].SetC(va_arg(ap, jint));
124 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800125 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700126 arg_array_[offset].SetS(va_arg(ap, jint));
127 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800128 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700129 arg_array_[offset].SetI(va_arg(ap, jint));
Ian Rogers45619fc2012-02-29 11:15:25 -0800130 break;
131 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700132 arg_array_[offset].SetF(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800133 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700134 case 'L':
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 arg_array_[offset].SetL(soa.Decode<Object*>(va_arg(ap, jobject)));
Ian Rogers45619fc2012-02-29 11:15:25 -0800136 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800137 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700138 arg_array_[offset].SetD(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800139 break;
140 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700141 arg_array_[offset].SetJ(va_arg(ap, jlong));
Ian Rogers45619fc2012-02-29 11:15:25 -0800142 break;
143 }
144 }
145 }
146
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 void BuildArgArray(const ScopedObjectAccess& soa, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes77405792012-03-15 15:22:12 -0700149 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800150 switch (shorty_[i]) {
151 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700152 arg_array_[offset].SetZ(args[offset].z);
153 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800154 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700155 arg_array_[offset].SetB(args[offset].b);
156 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800157 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700158 arg_array_[offset].SetC(args[offset].c);
159 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800160 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700161 arg_array_[offset].SetS(args[offset].s);
162 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800163 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700164 arg_array_[offset].SetI(args[offset].i);
Ian Rogers45619fc2012-02-29 11:15:25 -0800165 break;
166 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700167 arg_array_[offset].SetF(args[offset].f);
Ian Rogers45619fc2012-02-29 11:15:25 -0800168 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700169 case 'L':
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 arg_array_[offset].SetL(soa.Decode<Object*>(args[offset].l));
Ian Rogers45619fc2012-02-29 11:15:25 -0800171 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800172 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700173 arg_array_[offset].SetD(args[offset].d);
Ian Rogers45619fc2012-02-29 11:15:25 -0800174 break;
175 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700176 arg_array_[offset].SetJ(args[offset].j);
Ian Rogers45619fc2012-02-29 11:15:25 -0800177 break;
178 }
179 }
180 }
181
Ian Rogers45619fc2012-02-29 11:15:25 -0800182 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700183 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800184 const char* shorty_;
185 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700186 JValue* arg_array_;
187 JValue small_arg_array_[kSmallArgArraySize];
188 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800189};
190
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700192 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700193 if (obj == NULL) {
194 return NULL;
195 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700196 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700197 IndirectReferenceTable& weak_globals = vm->weak_globals;
198 MutexLock mu(vm->weak_globals_lock);
199 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
200 return reinterpret_cast<jweak>(ref);
201}
202
Mathieu Chartier66f19252012-09-18 08:57:04 -0700203static void CheckMethodArguments(AbstractMethod* m, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700204 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700205 MethodHelper mh(m);
206 ObjectArray<Class>* parameter_types = mh.GetParameterTypes();
207 CHECK(parameter_types != NULL);
208 size_t error_count = 0;
209 for (int i = 0; i < parameter_types->GetLength(); ++i) {
210 Class* parameter_type = parameter_types->Get(i);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700211 // TODO: check primitives are in range.
Elliott Hughesb264f082012-04-06 17:10:10 -0700212 if (!parameter_type->IsPrimitive()) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700213 Object* argument = args[i].GetL();
Elliott Hughesb264f082012-04-06 17:10:10 -0700214 if (argument != NULL && !argument->InstanceOf(parameter_type)) {
215 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
216 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
217 ++error_count;
218 }
219 }
220 }
221 if (error_count > 0) {
222 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
223 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700224 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700225 }
226}
Elliott Hughesb264f082012-04-06 17:10:10 -0700227
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228static JValue InvokeWithArgArray(const ScopedObjectAccess& soa, Object* receiver,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700229 AbstractMethod* method, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 if (UNLIKELY(soa.Env()->check_jni)) {
Elliott Hughes4cacde82012-04-11 18:32:27 -0700232 CheckMethodArguments(method, args);
233 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700234 JValue result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700235 method->Invoke(soa.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700236 return result;
237}
238
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700239static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
240 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700241 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700243 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800244 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245 arg_array.BuildArgArray(soa, args);
246 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700247}
248
Mathieu Chartier66f19252012-09-18 08:57:04 -0700249static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700251 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700252}
253
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
255 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700256 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700258 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800259 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700260 arg_array.BuildArgArray(soa, args);
261 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700262}
263
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
265 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700266 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700268 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800269 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 arg_array.BuildArgArray(soa, args);
271 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700272}
273
Elliott Hughes6b436852011-08-12 10:16:44 -0700274// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
275// separated with slashes but aren't wrapped with "L;" like regular descriptors
276// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
277// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
278// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700279static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700280 std::string result;
281 // Add the missing "L;" if necessary.
282 if (name[0] == '[') {
283 result = name;
284 } else {
285 result += 'L';
286 result += name;
287 result += ';';
288 }
289 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700290 if (result.find('.') != std::string::npos) {
291 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
292 << "\"" << name << "\"";
293 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700294 }
295 return result;
296}
297
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
299 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800302 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700303}
304
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
306 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700307 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700309 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700310 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700311 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700312
Mathieu Chartier66f19252012-09-18 08:57:04 -0700313 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700314 if (is_static) {
315 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700316 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700317 method = c->FindVirtualMethod(name, sig);
318 if (method == NULL) {
319 // No virtual method matching the signature. Search declared
320 // private methods and constructors.
321 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700322 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700323 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700324
Elliott Hughescdf53122011-08-19 15:46:09 -0700325 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700327 return NULL;
328 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700329
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700330 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700331}
332
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700333static ClassLoader* GetClassLoader(Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700334 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700335 AbstractMethod* method = self->GetCurrentMethod();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700336 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
337 return self->GetClassLoaderOverride();
338 }
339 return method->GetDeclaringClass()->GetClassLoader();
340}
341
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
343 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700346 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700347 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700348 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700349
350 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700351 Class* field_type;
352 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
353 if (sig[1] != '\0') {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354 ClassLoader* cl = GetClassLoader(soa.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700356 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700357 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700358 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700359 if (field_type == NULL) {
360 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700361 DCHECK(soa.Self()->IsExceptionPending());
362 soa.Self()->ClearException();
363 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700364 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800365 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700366 return NULL;
367 }
368 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800369 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700370 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800371 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700372 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700373 if (field == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700375 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800376 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700377 return NULL;
378 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700379 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700380}
381
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700384 JavaVMExt* vm = soa.Vm();
Elliott Hughes75770752011-08-24 17:52:38 -0700385 MutexLock mu(vm->pins_lock);
386 vm->pin_table.Add(array);
387}
388
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700390 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700391 JavaVMExt* vm = soa.Vm();
Elliott Hughes75770752011-08-24 17:52:38 -0700392 MutexLock mu(vm->pins_lock);
393 vm->pin_table.Remove(array);
394}
395
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
397 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700398 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700399 std::string type(PrettyTypeOf(array));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700401 "%s offset=%d length=%d %s.length=%d",
402 type.c_str(), start, length, identifier, array->GetLength());
403}
Ian Rogers0571d352011-11-03 19:51:38 -0700404
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700405static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
406 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700409 "offset=%d length=%d string.length()=%d", start, length, array_length);
410}
Elliott Hughes814e4032011-08-23 12:07:56 -0700411
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700412int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700413 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700414 ScopedObjectAccess soa(env);
415
416 // Turn the const char* into a java.lang.String.
417 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
418 if (msg != NULL && s.get() == NULL) {
419 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700420 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700421
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700422 // Choose an appropriate constructor and set up the arguments.
423 jvalue args[2];
424 const char* signature;
425 if (msg == NULL && cause == NULL) {
426 signature = "()V";
427 } else if (msg != NULL && cause == NULL) {
428 signature = "(Ljava/lang/String;)V";
429 args[0].l = s.get();
430 } else if (msg == NULL && cause != NULL) {
431 signature = "(Ljava/lang/Throwable;)V";
432 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700433 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700434 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
435 args[0].l = s.get();
436 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700437 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700438 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
439 if (mid == NULL) {
440 LOG(ERROR) << "No <init>" << signature << " in "
441 << PrettyClass(soa.Decode<Class*>(exception_class));
442 return JNI_ERR;
443 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700444
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700445 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
446 if (exception.get() == NULL) {
447 return JNI_ERR;
448 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700449
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700450 soa.Self()->SetException(soa.Decode<Throwable*>(exception.get()));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700451
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700453}
454
Elliott Hughes462c9442012-03-23 18:47:50 -0700455static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700456 if (vm == NULL || p_env == NULL) {
457 return JNI_ERR;
458 }
459
Elliott Hughes462c9442012-03-23 18:47:50 -0700460 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700461 Thread* self = Thread::Current();
462 if (self != NULL) {
463 *p_env = self->GetJniEnv();
464 return JNI_OK;
465 }
466
467 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
468
469 // No threads allowed in zygote mode.
470 if (runtime->IsZygote()) {
471 LOG(ERROR) << "Attempt to attach a thread in the zygote";
472 return JNI_ERR;
473 }
474
Elliott Hughes462c9442012-03-23 18:47:50 -0700475 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
476 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700477 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700478 if (args != NULL) {
479 CHECK_GE(args->version, JNI_VERSION_1_2);
480 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700481 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700482 }
Elliott Hughes75770752011-08-24 17:52:38 -0700483
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.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700617 void* FindNativeMethod(const AbstractMethod* 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);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700655 AbstractMethod* 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
Mathieu Chartier66f19252012-09-18 08:57:04 -0700661JValue InvokeWithJValues(const ScopedObjectAccess& soa, Object* receiver, AbstractMethod* m,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 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:
Ian Rogers25e8b912012-09-07 11:31:36 -0700669 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 return JNI_VERSION_1_6;
671 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700672
Ian Rogers25e8b912012-09-07 11:31:36 -0700673 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700675 return NULL;
676 }
677
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700680 Runtime* runtime = Runtime::Current();
681 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700683 Class* c = NULL;
684 if (runtime->IsStarted()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700685 ClassLoader* cl = GetClassLoader(soa.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800686 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700687 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800688 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700689 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700690 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700691 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700692
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700695 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700697 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700698
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 ScopedObjectAccess soa(env);
701 Field* field = soa.Decode<Field*>(java_field);
702 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700704
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700706 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700707 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700710
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 ScopedObjectAccess soa(env);
713 Field* field = soa.DecodeField(fid);
714 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700715 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700716
Elliott Hughes37f7a402011-08-22 18:56:01 -0700717 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700718 ScopedObjectAccess soa(env);
719 Object* o = soa.Decode<Object*>(java_object);
720 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700721 }
722
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700723 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700724 ScopedObjectAccess soa(env);
725 Class* c = soa.Decode<Class*>(java_class);
726 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700727 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700728
Elliott Hughes37f7a402011-08-22 18:56:01 -0700729 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730 ScopedObjectAccess soa(env);
731 Class* c1 = soa.Decode<Class*>(java_class1);
732 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700733 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700734 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700735
Elliott Hughese84278b2012-03-22 10:06:53 -0700736 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700737 ScopedObjectAccess soa(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700738 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700739 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700740 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700741 return JNI_TRUE;
742 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700743 Object* obj = soa.Decode<Object*>(jobj);
744 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700745 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700746 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700747 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700748
Elliott Hughes37f7a402011-08-22 18:56:01 -0700749 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700750 ScopedObjectAccess soa(env);
751 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700752 if (exception == NULL) {
753 return JNI_ERR;
754 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700755 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700756 return JNI_OK;
757 }
758
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700759 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700760 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700761 }
762
763 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700764 ScopedObjectAccess soa(env);
765 return soa.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700766 }
767
768 static void ExceptionClear(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700769 ScopedObjectAccess soa(env);
770 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700771 }
772
773 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700774 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700775
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700776 Thread* self = soa.Self();
Elliott Hughes72025e52011-08-23 17:50:30 -0700777 Throwable* original_exception = self->GetException();
778 self->ClearException();
779
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700780 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700781 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
782 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
783 if (mid == NULL) {
784 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700785 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700786 } else {
787 env->CallVoidMethod(exception.get(), mid);
788 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700789 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700790 << " thrown while calling printStackTrace";
791 self->ClearException();
792 }
793 }
794
795 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700797
Elliott Hughescdf53122011-08-19 15:46:09 -0700798 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700799 ScopedObjectAccess soa(env);
800 Object* exception = soa.Self()->GetException();
801 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700802 }
803
Ian Rogers25e8b912012-09-07 11:31:36 -0700804 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 LOG(FATAL) << "JNI FatalError called: " << msg;
806 }
807
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700808 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700809 ScopedObjectAccess soa(env);
810 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700811 return JNI_ERR;
812 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700813 soa.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 return JNI_OK;
815 }
816
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700817 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700818 ScopedObjectAccess soa(env);
819 Object* survivor = soa.Decode<Object*>(java_survivor);
820 soa.Env()->PopFrame();
821 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 }
823
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700824 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700825 ScopedObjectAccess soa(env);
826 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700827 }
828
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 if (obj == NULL) {
831 return NULL;
832 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700833 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700834 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700836 Object* decoded_obj = soa.Decode<Object*>(obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 MutexLock mu(vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700838 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 return reinterpret_cast<jobject>(ref);
840 }
841
842 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700843 if (obj == NULL) {
844 return;
845 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700846 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700847 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 IndirectReferenceTable& globals = vm->globals;
849 MutexLock mu(vm->globals_lock);
850
851 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
852 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
853 << "failed to find entry";
854 }
855 }
856
857 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700858 ScopedObjectAccess soa(env);
859 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 }
861
862 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700863 if (obj == NULL) {
864 return;
865 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700866 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700867 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 IndirectReferenceTable& weak_globals = vm->weak_globals;
869 MutexLock mu(vm->weak_globals_lock);
870
871 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
872 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
873 << "failed to find entry";
874 }
875 }
876
877 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700878 if (obj == NULL) {
879 return NULL;
880 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700881 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700882 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700883
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700884 uint32_t cookie = soa.Env()->local_ref_cookie;
885 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700886 return reinterpret_cast<jobject>(ref);
887 }
888
889 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 if (obj == NULL) {
891 return;
892 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700893 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700894 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700895
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700896 uint32_t cookie = soa.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 if (!locals.Remove(cookie, obj)) {
898 // Attempting to delete a local reference that is not in the
899 // topmost local reference frame is a no-op. DeleteLocalRef returns
900 // void and doesn't throw any exceptions, but we should probably
901 // complain about it so the user will notice that things aren't
902 // going quite the way they expect.
903 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
904 << "failed to find entry";
905 }
906 }
907
908 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700909 ScopedObjectAccess soa(env);
910 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2))
Elliott Hughescdf53122011-08-19 15:46:09 -0700911 ? JNI_TRUE : JNI_FALSE;
912 }
913
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700914 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700915 ScopedObjectAccess soa(env);
916 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700917 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700918 return NULL;
919 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700920 return soa.AddLocalReference<jobject>(c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 }
922
Elliott Hughese84278b2012-03-22 10:06:53 -0700923 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700925 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700926 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 va_end(args);
928 return result;
929 }
930
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700932 ScopedObjectAccess soa(env);
933 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700934 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700935 return NULL;
936 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700937 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700938 if (result == NULL) {
939 return NULL;
940 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700941 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700943 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700944 return local_result;
945 } else {
946 return NULL;
947 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700951 ScopedObjectAccess soa(env);
952 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700953 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700954 return NULL;
955 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700956 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700957 if (result == NULL) {
958 return NULL;
959 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700962 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700963 return local_result;
964 } else {
965 return NULL;
966 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 ScopedObjectAccess soa(env);
971 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
974 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700975 ScopedObjectAccess soa(env);
976 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 va_list ap;
981 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700982 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700983 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700985 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 ScopedObjectAccess soa(env);
990 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
991 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 }
993
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700995 ScopedObjectAccess soa(env);
996 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
997 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 }
999
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 va_list ap;
1002 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001003 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001006 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001010 ScopedObjectAccess soa(env);
1011 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001015 ScopedObjectAccess soa(env);
1016 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001020 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 va_list ap;
1022 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001023 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001025 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029 ScopedObjectAccess soa(env);
1030 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001034 ScopedObjectAccess soa(env);
1035 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 va_list ap;
1040 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001041 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001042 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001044 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 ScopedObjectAccess soa(env);
1049 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001053 ScopedObjectAccess soa(env);
1054 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 va_list ap;
1059 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001060 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001061 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001063 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001067 ScopedObjectAccess soa(env);
1068 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001072 ScopedObjectAccess soa(env);
1073 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 va_list ap;
1079 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001082 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 ScopedObjectAccess soa(env);
1087 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001091 ScopedObjectAccess soa(env);
1092 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 va_list ap;
1097 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001098 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001099 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001101 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001105 ScopedObjectAccess soa(env);
1106 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001110 ScopedObjectAccess soa(env);
1111 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_list ap;
1116 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001117 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001120 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001124 ScopedObjectAccess soa(env);
1125 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001129 ScopedObjectAccess soa(env);
1130 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 va_list ap;
1135 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001136 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001137 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001139 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001143 ScopedObjectAccess soa(env);
1144 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 ScopedObjectAccess soa(env);
1149 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 va_list ap;
1154 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001155 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001156 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161 ScopedObjectAccess soa(env);
1162 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001166 ScopedObjectAccess soa(env);
1167 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001170 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001173 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001174 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1175 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 va_end(ap);
1177 return local_result;
1178 }
1179
1180 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001181 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001182 ScopedObjectAccess soa(env);
1183 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1184 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
1187 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001188 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001189 ScopedObjectAccess soa(env);
1190 JValue result(InvokeWithJValues(soa, obj, mid, args));
1191 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
1194 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001195 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001198 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001199 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001201 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
1204 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001205 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001206 ScopedObjectAccess soa(env);
1207 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
1210 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001211 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001212 ScopedObjectAccess soa(env);
1213 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
1215
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001216 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001219 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001220 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001222 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 }
1224
1225 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001226 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001227 ScopedObjectAccess soa(env);
1228 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
1231 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001232 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001233 ScopedObjectAccess soa(env);
1234 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 }
1236
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001237 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001241 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001243 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
1246 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001247 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001248 ScopedObjectAccess soa(env);
1249 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
1252 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001253 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001254 ScopedObjectAccess soa(env);
1255 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001258 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001261 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001262 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001264 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
1267 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001268 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001269 ScopedObjectAccess soa(env);
1270 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 }
1272
1273 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001274 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001275 ScopedObjectAccess soa(env);
1276 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 }
1278
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001279 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001282 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001283 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001285 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
1287
1288 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001289 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001290 ScopedObjectAccess soa(env);
1291 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
1294 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001295 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001296 ScopedObjectAccess soa(env);
1297 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001300 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001302 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001303 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001304 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001306 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
1309 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001310 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001311 ScopedObjectAccess soa(env);
1312 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
1315 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001316 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001317 ScopedObjectAccess soa(env);
1318 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001321 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001324 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001325 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001327 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
1330 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001331 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001332 ScopedObjectAccess soa(env);
1333 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
1336 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001337 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001338 ScopedObjectAccess soa(env);
1339 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 }
1341
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001342 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001345 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001346 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001348 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
1351 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001352 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001353 ScopedObjectAccess soa(env);
1354 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
1357 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001358 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001359 ScopedObjectAccess soa(env);
1360 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001363 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001365 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001366 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001367 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 va_end(ap);
1369 }
1370
1371 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001372 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001373 ScopedObjectAccess soa(env);
1374 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
1377 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001378 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001379 ScopedObjectAccess soa(env);
1380 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
1382
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001383 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001384 ScopedObjectAccess soa(env);
1385 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001387
1388
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001389 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001390 ScopedObjectAccess soa(env);
1391 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001393
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001395 ScopedObjectAccess soa(env);
1396 Object* o = soa.Decode<Object*>(obj);
1397 Field* f = soa.DecodeField(fid);
1398 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001400
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001401 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001402 ScopedObjectAccess soa(env);
1403 Field* f = soa.DecodeField(fid);
1404 return soa.AddLocalReference<jobject>(f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001408 ScopedObjectAccess soa(env);
1409 Object* o = soa.Decode<Object*>(java_object);
1410 Object* v = soa.Decode<Object*>(java_value);
1411 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001412 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001415 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001416 ScopedObjectAccess soa(env);
1417 Object* v = soa.Decode<Object*>(java_value);
1418 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001419 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001422#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001423 ScopedObjectAccess soa(env); \
1424 Object* o = soa.Decode<Object*>(instance); \
1425 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 return f->fn(o)
1427
1428#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001429 ScopedObjectAccess soa(env); \
1430 Object* o = soa.Decode<Object*>(instance); \
1431 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 f->fn(o, value)
1433
1434 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1435 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1439 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1443 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1447 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1451 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1455 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1459 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1463 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 }
1465
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001466 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001470 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001474 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001478 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 }
1481
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001482 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001483 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001486 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001487 GET_PRIMITIVE_FIELD(GetLong, NULL);
1488 }
1489
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001490 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1492 }
1493
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001494 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1496 }
1497
1498 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1499 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1500 }
1501
1502 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1503 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1504 }
1505
1506 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1507 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1508 }
1509
1510 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1511 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1512 }
1513
1514 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1515 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1516 }
1517
1518 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1519 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1520 }
1521
1522 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1523 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1524 }
1525
1526 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1527 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1528 }
1529
1530 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1531 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1532 }
1533
1534 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1535 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1536 }
1537
1538 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1539 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1540 }
1541
1542 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1543 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1544 }
1545
1546 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1547 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1548 }
1549
1550 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1551 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1552 }
1553
1554 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1555 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1556 }
1557
1558 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1559 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 }
1561
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001562 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001564 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001565 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001566 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1567 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 va_end(ap);
1569 return local_result;
1570 }
1571
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001572 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001573 ScopedObjectAccess soa(env);
1574 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1575 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 }
1577
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001578 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001579 ScopedObjectAccess soa(env);
1580 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1581 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001584 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001586 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001587 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001588 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001590 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001593 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001594 ScopedObjectAccess soa(env);
1595 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 }
1597
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001598 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001599 ScopedObjectAccess soa(env);
1600 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001603 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001606 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001607 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001609 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001612 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001613 ScopedObjectAccess soa(env);
1614 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 }
1616
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001617 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001618 ScopedObjectAccess soa(env);
1619 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001622 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001624 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001625 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001626 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001628 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001631 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001632 ScopedObjectAccess soa(env);
1633 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001637 ScopedObjectAccess soa(env);
1638 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001641 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001643 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001644 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001645 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001647 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001650 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001651 ScopedObjectAccess soa(env);
1652 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 }
1654
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001655 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001656 ScopedObjectAccess soa(env);
1657 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 }
1659
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001660 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001662 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001663 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001664 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001666 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001669 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 ScopedObjectAccess soa(env);
1671 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 }
1673
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001674 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001675 ScopedObjectAccess soa(env);
1676 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001679 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001681 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001682 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001683 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001685 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001688 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001689 ScopedObjectAccess soa(env);
1690 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001693 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001694 ScopedObjectAccess soa(env);
1695 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001698 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001700 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001701 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001702 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001704 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001707 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001708 ScopedObjectAccess soa(env);
1709 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 }
1711
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001712 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001713 ScopedObjectAccess soa(env);
1714 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001717 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001720 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001721 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001723 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001726 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001727 ScopedObjectAccess soa(env);
1728 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001731 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001732 ScopedObjectAccess soa(env);
1733 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001736 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001738 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001739 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001740 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 va_end(ap);
1742 }
1743
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001744 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001745 ScopedObjectAccess soa(env);
1746 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001749 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001750 ScopedObjectAccess soa(env);
1751 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 }
1753
Elliott Hughes814e4032011-08-23 12:07:56 -07001754 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001755 ScopedObjectAccess soa(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001756 String* result = String::AllocFromUtf16(char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001757 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 }
1759
1760 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 if (utf == NULL) {
1762 return NULL;
1763 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001764 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 String* result = String::AllocFromModifiedUtf8(utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001766 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
Elliott Hughes814e4032011-08-23 12:07:56 -07001769 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001770 ScopedObjectAccess soa(env);
1771 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001772 }
1773
1774 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001775 ScopedObjectAccess soa(env);
1776 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001777 }
1778
Elliott Hughesb465ab02011-08-24 11:21:21 -07001779 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001780 ScopedObjectAccess soa(env);
1781 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001782 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001783 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001784 } else {
1785 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1786 memcpy(buf, chars + start, length * sizeof(jchar));
1787 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001788 }
1789
Elliott Hughesb465ab02011-08-24 11:21:21 -07001790 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001791 ScopedObjectAccess soa(env);
1792 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001793 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001794 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001795 } else {
1796 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1797 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1798 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001799 }
1800
Elliott Hughes75770752011-08-24 17:52:38 -07001801 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001802 ScopedObjectAccess soa(env);
1803 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001804 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001805 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001806 if (is_copy != NULL) {
1807 *is_copy = JNI_FALSE;
1808 }
1809 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001810 }
1811
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001812 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001813 ScopedObjectAccess soa(env);
1814 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
Elliott Hughes75770752011-08-24 17:52:38 -07001817 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001818 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001819 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
Elliott Hughes75770752011-08-24 17:52:38 -07001822 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001823 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001824 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes75770752011-08-24 17:52:38 -07001827 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001828 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001829 if (java_string == NULL) {
1830 return NULL;
1831 }
1832 if (is_copy != NULL) {
1833 *is_copy = JNI_TRUE;
1834 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001835 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001836 size_t byte_count = s->GetUtfLength();
1837 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001838 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001839 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1840 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1841 bytes[byte_count] = '\0';
1842 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001843 }
1844
Elliott Hughes75770752011-08-24 17:52:38 -07001845 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001846 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001847 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001848 }
1849
Elliott Hughesbd935992011-08-22 11:59:34 -07001850 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001851 ScopedObjectAccess soa(env);
1852 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001853 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001854 Array* array = obj->AsArray();
1855 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001856 }
1857
Elliott Hughes814e4032011-08-23 12:07:56 -07001858 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001859 ScopedObjectAccess soa(env);
1860 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1861 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 }
1863
1864 static void SetObjectArrayElement(JNIEnv* env,
1865 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001866 ScopedObjectAccess soa(env);
1867 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1868 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 array->Set(index, value);
1870 }
1871
1872 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001873 ScopedObjectAccess soa(env);
1874 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 }
1876
1877 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001878 ScopedObjectAccess soa(env);
1879 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 }
1881
1882 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001883 ScopedObjectAccess soa(env);
1884 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 }
1886
1887 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001888 ScopedObjectAccess soa(env);
1889 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001890 }
1891
1892 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001893 ScopedObjectAccess soa(env);
1894 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001895 }
1896
1897 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001898 ScopedObjectAccess soa(env);
1899 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001900 }
1901
1902 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001903 ScopedObjectAccess soa(env);
1904 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 }
1906
1907 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001908 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001909 CHECK_GE(length, 0); // TODO: ReportJniError
1910
1911 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001912 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 std::string descriptor;
1914 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001915 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001916
1917 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001918 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1919 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001920 return NULL;
1921 }
1922
Elliott Hughes75770752011-08-24 17:52:38 -07001923 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001924 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001925 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001926 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001927 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001928 for (jsize i = 0; i < length; ++i) {
1929 result->Set(i, initial_object);
1930 }
1931 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001932 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
1935 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001936 ScopedObjectAccess soa(env);
1937 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 }
1939
Ian Rogersa15e67d2012-02-28 13:51:55 -08001940 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001941 ScopedObjectAccess soa(env);
1942 Array* array = soa.Decode<Array*>(java_array);
1943 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001944 if (is_copy != NULL) {
1945 *is_copy = JNI_FALSE;
1946 }
1947 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001948 }
1949
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001950 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001951 ScopedObjectAccess soa(env);
1952 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001953 }
1954
Elliott Hughes75770752011-08-24 17:52:38 -07001955 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001956 ScopedObjectAccess soa(env);
1957 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Elliott Hughes75770752011-08-24 17:52:38 -07001960 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001961 ScopedObjectAccess soa(env);
1962 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 }
1964
Elliott Hughes75770752011-08-24 17:52:38 -07001965 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001966 ScopedObjectAccess soa(env);
1967 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes75770752011-08-24 17:52:38 -07001970 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001971 ScopedObjectAccess soa(env);
1972 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 }
1974
Elliott Hughes75770752011-08-24 17:52:38 -07001975 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001976 ScopedObjectAccess soa(env);
1977 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
Elliott Hughes75770752011-08-24 17:52:38 -07001980 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001981 ScopedObjectAccess soa(env);
1982 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 }
1984
Elliott Hughes75770752011-08-24 17:52:38 -07001985 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001986 ScopedObjectAccess soa(env);
1987 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Elliott Hughes75770752011-08-24 17:52:38 -07001990 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001991 ScopedObjectAccess soa(env);
1992 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 }
1994
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001995 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001996 ScopedObjectAccess soa(env);
1997 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002000 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002001 ScopedObjectAccess soa(env);
2002 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002005 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002006 ScopedObjectAccess soa(env);
2007 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002010 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002011 ScopedObjectAccess soa(env);
2012 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002015 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002016 ScopedObjectAccess soa(env);
2017 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002020 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002021 ScopedObjectAccess soa(env);
2022 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002025 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002026 ScopedObjectAccess soa(env);
2027 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002030 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002031 ScopedObjectAccess soa(env);
2032 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
2034
Elliott Hughes814e4032011-08-23 12:07:56 -07002035 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002036 ScopedObjectAccess soa(env);
2037 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes814e4032011-08-23 12:07:56 -07002040 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002041 ScopedObjectAccess soa(env);
2042 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes814e4032011-08-23 12:07:56 -07002045 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002046 ScopedObjectAccess soa(env);
2047 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002051 ScopedObjectAccess soa(env);
2052 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002056 ScopedObjectAccess soa(env);
2057 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002061 ScopedObjectAccess soa(env);
2062 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002066 ScopedObjectAccess soa(env);
2067 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Elliott Hughes814e4032011-08-23 12:07:56 -07002070 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002071 ScopedObjectAccess soa(env);
2072 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002076 ScopedObjectAccess soa(env);
2077 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002081 ScopedObjectAccess soa(env);
2082 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002086 ScopedObjectAccess soa(env);
2087 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002091 ScopedObjectAccess soa(env);
2092 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002096 ScopedObjectAccess soa(env);
2097 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002101 ScopedObjectAccess soa(env);
2102 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002106 ScopedObjectAccess soa(env);
2107 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002111 ScopedObjectAccess soa(env);
2112 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
Elliott Hughes5174fe62011-08-23 15:12:35 -07002115 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002116 ScopedObjectAccess soa(env);
2117 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002118
Elliott Hughes5174fe62011-08-23 15:12:35 -07002119 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 const char* name = methods[i].name;
2121 const char* sig = methods[i].signature;
2122
2123 if (*sig == '!') {
2124 // TODO: fast jni. it's too noisy to log all these.
2125 ++sig;
2126 }
2127
Mathieu Chartier66f19252012-09-18 08:57:04 -07002128 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002129 if (m == NULL) {
2130 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002132 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002133 LOG(INFO) << "Failed to register native method " << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002134 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002136 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002137 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002138 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 return JNI_ERR;
2140 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002141
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002142 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002144 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 }
2146 return JNI_OK;
2147 }
2148
Elliott Hughes5174fe62011-08-23 15:12:35 -07002149 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002150 ScopedObjectAccess soa(env);
2151 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002152
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002153 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002154
2155 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002156 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002157 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002158 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002159 }
2160 }
2161 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002162 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002163 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002164 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002165 }
2166 }
2167
2168 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002171 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2172 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2173 ScopedObjectAccess soa(env);
2174 Object* o = soa.Decode<Object*>(java_object);
2175 o->MonitorEnter(soa.Self());
2176 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002177 return JNI_ERR;
2178 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002179 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002180 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002183 static jint MonitorExit(JNIEnv* env, jobject java_object)
2184 UNLOCK_FUNCTION(monitor_lock_) {
2185 ScopedObjectAccess soa(env);
2186 Object* o = soa.Decode<Object*>(java_object);
2187 o->MonitorExit(soa.Self());
2188 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002189 return JNI_ERR;
2190 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002191 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002192 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
2195 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002196 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 Runtime* runtime = Runtime::Current();
2198 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002199 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 } else {
2201 *vm = NULL;
2202 }
2203 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2204 }
2205
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002207 ScopedObjectAccess soa(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002208
2209 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002210 CHECK(address != NULL); // TODO: ReportJniError
2211 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002212
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002213 // At the moment, the Java side is limited to 32 bisoa.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002214 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2215 CHECK_LE(capacity, 0xffffffff);
2216 jint address_arg = reinterpret_cast<jint>(address);
2217 jint capacity_arg = static_cast<jint>(capacity);
2218
Elliott Hugheseac76672012-05-24 21:56:51 -07002219 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2220 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2221 address_arg, capacity_arg);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002222 return soa.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Elliott Hughesb465ab02011-08-24 11:21:21 -07002225 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002226 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002227 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
Elliott Hughesb465ab02011-08-24 11:21:21 -07002230 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002231 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002232 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Elliott Hughesb465ab02011-08-24 11:21:21 -07002235 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002236 ScopedObjectAccess soa(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002237
Elliott Hughes75770752011-08-24 17:52:38 -07002238 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002239
2240 // Do we definitely know what kind of reference this is?
2241 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2242 IndirectRefKind kind = GetIndirectRefKind(ref);
2243 switch (kind) {
2244 case kLocal:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002245 if (soa.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002246 return JNILocalRefType;
2247 }
2248 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249 case kGlobal:
2250 return JNIGlobalRefType;
2251 case kWeakGlobal:
2252 return JNIWeakGlobalRefType;
2253 case kSirtOrInvalid:
2254 // Is it in a stack IRT?
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002255 if (soa.Self()->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002256 return JNILocalRefType;
2257 }
2258
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002259 if (!soa.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002260 return JNIInvalidRefType;
2261 }
2262
Elliott Hughesb465ab02011-08-24 11:21:21 -07002263 // If we're handing out direct pointers, check whether it's a direct pointer
2264 // to a local reference.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002265 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2266 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002267 return JNILocalRefType;
2268 }
2269 }
2270
2271 return JNIInvalidRefType;
2272 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002273 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2274 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002276
2277 private:
2278 static jint EnsureLocalCapacity(const ScopedObjectAccess& soa, jint desired_capacity,
2279 const char* caller)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002280 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002281 // TODO: we should try to expand the table if necessary.
2282 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2283 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2284 return JNI_ERR;
2285 }
2286 // TODO: this isn't quite right, since "capacity" includes holes.
2287 size_t capacity = soa.Env()->locals.Capacity();
2288 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2289 if (!okay) {
2290 soa.Self()->ThrowOutOfMemoryError(caller);
2291 }
2292 return okay ? JNI_OK : JNI_ERR;
2293 }
2294
2295 template<typename JniT, typename ArtT>
2296 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002297 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002298 CHECK_GE(length, 0); // TODO: ReportJniError
2299 ArtT* result = ArtT::Alloc(length);
2300 return soa.AddLocalReference<JniT>(result);
2301 }
2302
2303 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2304 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2305 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002306 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002307 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2308 PinPrimitiveArray(soa, array);
2309 if (is_copy != NULL) {
2310 *is_copy = JNI_FALSE;
2311 }
2312 return array->GetData();
2313 }
2314
2315 template <typename ArrayT>
2316 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2317 jint mode)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002318 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002319 if (mode != JNI_COMMIT) {
2320 Array* array = soa.Decode<Array*>(java_array);
2321 UnpinPrimitiveArray(soa, array);
2322 }
2323 }
2324
2325 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2326 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2327 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002329 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2330 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2331 ThrowAIOOBE(soa, array, start, length, "src");
2332 } else {
2333 JavaT* data = array->GetData();
2334 memcpy(buf, data + start, length * sizeof(JavaT));
2335 }
2336 }
2337
2338 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2339 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2340 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002341 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002342 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2343 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2344 ThrowAIOOBE(soa, array, start, length, "dst");
2345 } else {
2346 JavaT* data = array->GetData();
2347 memcpy(data + start, buf, length * sizeof(JavaT));
2348 }
2349 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002350};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002351
Elliott Hughes88c5c352012-03-15 18:49:48 -07002352const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002353 NULL, // reserved0.
2354 NULL, // reserved1.
2355 NULL, // reserved2.
2356 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002357 JNI::GetVersion,
2358 JNI::DefineClass,
2359 JNI::FindClass,
2360 JNI::FromReflectedMethod,
2361 JNI::FromReflectedField,
2362 JNI::ToReflectedMethod,
2363 JNI::GetSuperclass,
2364 JNI::IsAssignableFrom,
2365 JNI::ToReflectedField,
2366 JNI::Throw,
2367 JNI::ThrowNew,
2368 JNI::ExceptionOccurred,
2369 JNI::ExceptionDescribe,
2370 JNI::ExceptionClear,
2371 JNI::FatalError,
2372 JNI::PushLocalFrame,
2373 JNI::PopLocalFrame,
2374 JNI::NewGlobalRef,
2375 JNI::DeleteGlobalRef,
2376 JNI::DeleteLocalRef,
2377 JNI::IsSameObject,
2378 JNI::NewLocalRef,
2379 JNI::EnsureLocalCapacity,
2380 JNI::AllocObject,
2381 JNI::NewObject,
2382 JNI::NewObjectV,
2383 JNI::NewObjectA,
2384 JNI::GetObjectClass,
2385 JNI::IsInstanceOf,
2386 JNI::GetMethodID,
2387 JNI::CallObjectMethod,
2388 JNI::CallObjectMethodV,
2389 JNI::CallObjectMethodA,
2390 JNI::CallBooleanMethod,
2391 JNI::CallBooleanMethodV,
2392 JNI::CallBooleanMethodA,
2393 JNI::CallByteMethod,
2394 JNI::CallByteMethodV,
2395 JNI::CallByteMethodA,
2396 JNI::CallCharMethod,
2397 JNI::CallCharMethodV,
2398 JNI::CallCharMethodA,
2399 JNI::CallShortMethod,
2400 JNI::CallShortMethodV,
2401 JNI::CallShortMethodA,
2402 JNI::CallIntMethod,
2403 JNI::CallIntMethodV,
2404 JNI::CallIntMethodA,
2405 JNI::CallLongMethod,
2406 JNI::CallLongMethodV,
2407 JNI::CallLongMethodA,
2408 JNI::CallFloatMethod,
2409 JNI::CallFloatMethodV,
2410 JNI::CallFloatMethodA,
2411 JNI::CallDoubleMethod,
2412 JNI::CallDoubleMethodV,
2413 JNI::CallDoubleMethodA,
2414 JNI::CallVoidMethod,
2415 JNI::CallVoidMethodV,
2416 JNI::CallVoidMethodA,
2417 JNI::CallNonvirtualObjectMethod,
2418 JNI::CallNonvirtualObjectMethodV,
2419 JNI::CallNonvirtualObjectMethodA,
2420 JNI::CallNonvirtualBooleanMethod,
2421 JNI::CallNonvirtualBooleanMethodV,
2422 JNI::CallNonvirtualBooleanMethodA,
2423 JNI::CallNonvirtualByteMethod,
2424 JNI::CallNonvirtualByteMethodV,
2425 JNI::CallNonvirtualByteMethodA,
2426 JNI::CallNonvirtualCharMethod,
2427 JNI::CallNonvirtualCharMethodV,
2428 JNI::CallNonvirtualCharMethodA,
2429 JNI::CallNonvirtualShortMethod,
2430 JNI::CallNonvirtualShortMethodV,
2431 JNI::CallNonvirtualShortMethodA,
2432 JNI::CallNonvirtualIntMethod,
2433 JNI::CallNonvirtualIntMethodV,
2434 JNI::CallNonvirtualIntMethodA,
2435 JNI::CallNonvirtualLongMethod,
2436 JNI::CallNonvirtualLongMethodV,
2437 JNI::CallNonvirtualLongMethodA,
2438 JNI::CallNonvirtualFloatMethod,
2439 JNI::CallNonvirtualFloatMethodV,
2440 JNI::CallNonvirtualFloatMethodA,
2441 JNI::CallNonvirtualDoubleMethod,
2442 JNI::CallNonvirtualDoubleMethodV,
2443 JNI::CallNonvirtualDoubleMethodA,
2444 JNI::CallNonvirtualVoidMethod,
2445 JNI::CallNonvirtualVoidMethodV,
2446 JNI::CallNonvirtualVoidMethodA,
2447 JNI::GetFieldID,
2448 JNI::GetObjectField,
2449 JNI::GetBooleanField,
2450 JNI::GetByteField,
2451 JNI::GetCharField,
2452 JNI::GetShortField,
2453 JNI::GetIntField,
2454 JNI::GetLongField,
2455 JNI::GetFloatField,
2456 JNI::GetDoubleField,
2457 JNI::SetObjectField,
2458 JNI::SetBooleanField,
2459 JNI::SetByteField,
2460 JNI::SetCharField,
2461 JNI::SetShortField,
2462 JNI::SetIntField,
2463 JNI::SetLongField,
2464 JNI::SetFloatField,
2465 JNI::SetDoubleField,
2466 JNI::GetStaticMethodID,
2467 JNI::CallStaticObjectMethod,
2468 JNI::CallStaticObjectMethodV,
2469 JNI::CallStaticObjectMethodA,
2470 JNI::CallStaticBooleanMethod,
2471 JNI::CallStaticBooleanMethodV,
2472 JNI::CallStaticBooleanMethodA,
2473 JNI::CallStaticByteMethod,
2474 JNI::CallStaticByteMethodV,
2475 JNI::CallStaticByteMethodA,
2476 JNI::CallStaticCharMethod,
2477 JNI::CallStaticCharMethodV,
2478 JNI::CallStaticCharMethodA,
2479 JNI::CallStaticShortMethod,
2480 JNI::CallStaticShortMethodV,
2481 JNI::CallStaticShortMethodA,
2482 JNI::CallStaticIntMethod,
2483 JNI::CallStaticIntMethodV,
2484 JNI::CallStaticIntMethodA,
2485 JNI::CallStaticLongMethod,
2486 JNI::CallStaticLongMethodV,
2487 JNI::CallStaticLongMethodA,
2488 JNI::CallStaticFloatMethod,
2489 JNI::CallStaticFloatMethodV,
2490 JNI::CallStaticFloatMethodA,
2491 JNI::CallStaticDoubleMethod,
2492 JNI::CallStaticDoubleMethodV,
2493 JNI::CallStaticDoubleMethodA,
2494 JNI::CallStaticVoidMethod,
2495 JNI::CallStaticVoidMethodV,
2496 JNI::CallStaticVoidMethodA,
2497 JNI::GetStaticFieldID,
2498 JNI::GetStaticObjectField,
2499 JNI::GetStaticBooleanField,
2500 JNI::GetStaticByteField,
2501 JNI::GetStaticCharField,
2502 JNI::GetStaticShortField,
2503 JNI::GetStaticIntField,
2504 JNI::GetStaticLongField,
2505 JNI::GetStaticFloatField,
2506 JNI::GetStaticDoubleField,
2507 JNI::SetStaticObjectField,
2508 JNI::SetStaticBooleanField,
2509 JNI::SetStaticByteField,
2510 JNI::SetStaticCharField,
2511 JNI::SetStaticShortField,
2512 JNI::SetStaticIntField,
2513 JNI::SetStaticLongField,
2514 JNI::SetStaticFloatField,
2515 JNI::SetStaticDoubleField,
2516 JNI::NewString,
2517 JNI::GetStringLength,
2518 JNI::GetStringChars,
2519 JNI::ReleaseStringChars,
2520 JNI::NewStringUTF,
2521 JNI::GetStringUTFLength,
2522 JNI::GetStringUTFChars,
2523 JNI::ReleaseStringUTFChars,
2524 JNI::GetArrayLength,
2525 JNI::NewObjectArray,
2526 JNI::GetObjectArrayElement,
2527 JNI::SetObjectArrayElement,
2528 JNI::NewBooleanArray,
2529 JNI::NewByteArray,
2530 JNI::NewCharArray,
2531 JNI::NewShortArray,
2532 JNI::NewIntArray,
2533 JNI::NewLongArray,
2534 JNI::NewFloatArray,
2535 JNI::NewDoubleArray,
2536 JNI::GetBooleanArrayElements,
2537 JNI::GetByteArrayElements,
2538 JNI::GetCharArrayElements,
2539 JNI::GetShortArrayElements,
2540 JNI::GetIntArrayElements,
2541 JNI::GetLongArrayElements,
2542 JNI::GetFloatArrayElements,
2543 JNI::GetDoubleArrayElements,
2544 JNI::ReleaseBooleanArrayElements,
2545 JNI::ReleaseByteArrayElements,
2546 JNI::ReleaseCharArrayElements,
2547 JNI::ReleaseShortArrayElements,
2548 JNI::ReleaseIntArrayElements,
2549 JNI::ReleaseLongArrayElements,
2550 JNI::ReleaseFloatArrayElements,
2551 JNI::ReleaseDoubleArrayElements,
2552 JNI::GetBooleanArrayRegion,
2553 JNI::GetByteArrayRegion,
2554 JNI::GetCharArrayRegion,
2555 JNI::GetShortArrayRegion,
2556 JNI::GetIntArrayRegion,
2557 JNI::GetLongArrayRegion,
2558 JNI::GetFloatArrayRegion,
2559 JNI::GetDoubleArrayRegion,
2560 JNI::SetBooleanArrayRegion,
2561 JNI::SetByteArrayRegion,
2562 JNI::SetCharArrayRegion,
2563 JNI::SetShortArrayRegion,
2564 JNI::SetIntArrayRegion,
2565 JNI::SetLongArrayRegion,
2566 JNI::SetFloatArrayRegion,
2567 JNI::SetDoubleArrayRegion,
2568 JNI::RegisterNatives,
2569 JNI::UnregisterNatives,
2570 JNI::MonitorEnter,
2571 JNI::MonitorExit,
2572 JNI::GetJavaVM,
2573 JNI::GetStringRegion,
2574 JNI::GetStringUTFRegion,
2575 JNI::GetPrimitiveArrayCritical,
2576 JNI::ReleasePrimitiveArrayCritical,
2577 JNI::GetStringCritical,
2578 JNI::ReleaseStringCritical,
2579 JNI::NewWeakGlobalRef,
2580 JNI::DeleteWeakGlobalRef,
2581 JNI::ExceptionCheck,
2582 JNI::NewDirectByteBuffer,
2583 JNI::GetDirectBufferAddress,
2584 JNI::GetDirectBufferCapacity,
2585 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002586};
2587
Elliott Hughes75770752011-08-24 17:52:38 -07002588JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002589 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002590 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002591 local_ref_cookie(IRT_FIRST_SEGMENT),
2592 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002593 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002594 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002595 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002596 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002597 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002598 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002599 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002600 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2601 // errors will ensue.
2602 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2603 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002604}
2605
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002606JNIEnvExt::~JNIEnvExt() {
2607}
2608
Elliott Hughes88c5c352012-03-15 18:49:48 -07002609void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2610 check_jni = enabled;
2611 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002612}
2613
Elliott Hughes73e66f72012-05-09 09:34:45 -07002614void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2615 locals.Dump(os);
2616 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002617}
2618
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002619void JNIEnvExt::PushFrame(int /*capacity*/) {
2620 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002621 stacked_local_ref_cookies.push_back(local_ref_cookie);
2622 local_ref_cookie = locals.GetSegmentState();
2623}
2624
2625void JNIEnvExt::PopFrame() {
2626 locals.SetSegmentState(local_ref_cookie);
2627 local_ref_cookie = stacked_local_ref_cookies.back();
2628 stacked_local_ref_cookies.pop_back();
2629}
2630
Carl Shapiroea4dca82011-08-01 13:45:38 -07002631// JNI Invocation interface.
2632
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002633extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2634 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2635 if (args->version < JNI_VERSION_1_2) {
2636 return JNI_EVERSION;
2637 }
2638 Runtime::Options options;
2639 for (int i = 0; i < args->nOptions; ++i) {
2640 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002641 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002642 }
2643 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002644 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002645 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002646 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002647 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002648 runtime->Start();
2649 *p_env = Thread::Current()->GetJniEnv();
2650 *p_vm = runtime->GetJavaVM();
2651 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002652}
2653
Elliott Hughesf2682d52011-08-15 16:37:04 -07002654extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002655 Runtime* runtime = Runtime::Current();
2656 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002657 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002658 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002659 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002660 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002661 }
2662 return JNI_OK;
2663}
2664
2665// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002666extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002667 return JNI_ERR;
2668}
2669
Elliott Hughescdf53122011-08-19 15:46:09 -07002670class JII {
2671 public:
2672 static jint DestroyJavaVM(JavaVM* vm) {
2673 if (vm == NULL) {
2674 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002675 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002676 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2677 delete raw_vm->runtime;
2678 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002679 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002680
Elliott Hughescdf53122011-08-19 15:46:09 -07002681 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002682 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002683 }
2684
2685 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002686 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002687 }
2688
2689 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002690 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002691 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002692 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002693 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2694 Runtime* runtime = raw_vm->runtime;
2695 runtime->DetachCurrentThread();
2696 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002697 }
2698
2699 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2700 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2701 return JNI_EVERSION;
2702 }
2703 if (vm == NULL || env == NULL) {
2704 return JNI_ERR;
2705 }
2706 Thread* thread = Thread::Current();
2707 if (thread == NULL) {
2708 *env = NULL;
2709 return JNI_EDETACHED;
2710 }
2711 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002712 return JNI_OK;
2713 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002714};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002715
Elliott Hughes88c5c352012-03-15 18:49:48 -07002716const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002717 NULL, // reserved0
2718 NULL, // reserved1
2719 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002720 JII::DestroyJavaVM,
2721 JII::AttachCurrentThread,
2722 JII::DetachCurrentThread,
2723 JII::GetEnv,
2724 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002725};
2726
Elliott Hughesa0957642011-09-02 14:27:33 -07002727JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002728 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002729 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002730 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002731 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002732 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002733 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002734 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002735 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002736 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002737 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002738 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002739 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002740 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002741 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002742 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002743 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002744 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002745 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002746 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002747}
2748
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002749JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002750 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002751}
2752
Elliott Hughes88c5c352012-03-15 18:49:48 -07002753void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2754 check_jni = enabled;
2755 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002756}
2757
Elliott Hughesae80b492012-04-24 10:43:17 -07002758void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2759 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2760 if (force_copy) {
2761 os << " (with forcecopy)";
2762 }
2763 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
2764 {
2765 MutexLock mu(pins_lock);
2766 os << "; pins=" << pin_table.Size();
2767 }
2768 {
2769 MutexLock mu(globals_lock);
2770 os << "; globals=" << globals.Capacity();
2771 }
2772 {
2773 MutexLock mu(weak_globals_lock);
2774 if (weak_globals.Capacity() > 0) {
2775 os << " (plus " << weak_globals.Capacity() << " weak)";
2776 }
2777 }
2778 os << '\n';
2779
2780 {
2781 MutexLock mu(libraries_lock);
2782 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2783 }
2784}
2785
Elliott Hughes73e66f72012-05-09 09:34:45 -07002786void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002787 {
2788 MutexLock mu(globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002789 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002790 }
2791 {
2792 MutexLock mu(weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002793 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002794 }
2795 {
2796 MutexLock mu(pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002797 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002798 }
2799}
2800
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002801bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2802 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002803 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002804
2805 // See if we've already loaded this library. If we have, and the class loader
2806 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002807 // TODO: for better results we should canonicalize the pathname (or even compare
2808 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002809 SharedLibrary* library;
2810 {
2811 // TODO: move the locking (and more of this logic) into Libraries.
2812 MutexLock mu(libraries_lock);
2813 library = libraries->Get(path);
2814 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002815 if (library != NULL) {
2816 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002817 // The library will be associated with class_loader. The JNI
2818 // spec says we can't load the same library into more than one
2819 // class loader.
2820 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2821 "ClassLoader %p; can't open in ClassLoader %p",
2822 path.c_str(), library->GetClassLoader(), class_loader);
2823 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002824 return false;
2825 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002826 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2827 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002828 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002829 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2830 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002831 return false;
2832 }
2833 return true;
2834 }
2835
2836 // Open the shared library. Because we're using a full path, the system
2837 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2838 // resolve this library's dependencies though.)
2839
2840 // Failures here are expected when java.library.path has several entries
2841 // and we have to hunt for the lib.
2842
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002843 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2844 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2845 // dlopen) becomes zero from dlclose.
2846
Elliott Hughescdf53122011-08-19 15:46:09 -07002847 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002848 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Elliott Hughescdf53122011-08-19 15:46:09 -07002849 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002850 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2851 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2852 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002853
Elliott Hughes84b2f142012-09-27 09:16:28 -07002854 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002855
2856 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002857 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002858 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002859 return false;
2860 }
2861
2862 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002863 // TODO: move the locking (and more of this logic) into Libraries.
2864 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002865 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002866 MutexLock mu(libraries_lock);
2867 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002868 if (library == NULL) { // We won race to get libraries_lock
2869 library = new SharedLibrary(path, handle, class_loader);
2870 libraries->Put(path, library);
2871 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002872 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002873 }
2874 if (!created_library) {
2875 LOG(INFO) << "WOW: we lost a race to add shared library: "
2876 << "\"" << path << "\" ClassLoader=" << class_loader;
2877 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002878 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002879
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002880 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002881
2882 bool result = true;
2883 void* sym = dlsym(handle, "JNI_OnLoad");
2884 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002885 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002886 } else {
2887 // Call JNI_OnLoad. We have to override the current class
2888 // loader, which will always be "null" since the stuff at the
2889 // top of the stack is around Runtime.loadLibrary(). (See
2890 // the comments in the JNI FindClass function.)
2891 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2892 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002893 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002894 self->SetClassLoaderOverride(class_loader);
2895
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002896 int version = 0;
2897 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002898 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002899 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002900 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002901 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002902
Brian Carlstromaded5f72011-10-07 17:15:04 -07002903 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002904
2905 if (version != JNI_VERSION_1_2 &&
2906 version != JNI_VERSION_1_4 &&
2907 version != JNI_VERSION_1_6) {
2908 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2909 << "bad version: " << version;
2910 // It's unwise to call dlclose() here, but we can mark it
2911 // as bad and ensure that future load attempts will fail.
2912 // We don't know how far JNI_OnLoad got, so there could
2913 // be some partially-initialized stuff accessible through
2914 // newly-registered native method calls. We could try to
2915 // unregister them, but that doesn't seem worthwhile.
2916 result = false;
2917 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002918 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2919 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002920 }
2921 }
2922
2923 library->SetResult(result);
2924 return result;
2925}
2926
Mathieu Chartier66f19252012-09-18 08:57:04 -07002927void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002928 CHECK(m->IsNative());
2929
2930 Class* c = m->GetDeclaringClass();
2931
2932 // If this is a static method, it could be called before the class
2933 // has been initialized.
2934 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002935 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002936 return NULL;
2937 }
2938 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002939 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002940 }
2941
Brian Carlstrom16192862011-09-12 17:50:06 -07002942 std::string detail;
2943 void* native_method;
2944 {
2945 MutexLock mu(libraries_lock);
2946 native_method = libraries->FindNativeMethod(m, detail);
2947 }
2948 // throwing can cause libraries_lock to be reacquired
2949 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002950 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002951 }
2952 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002953}
2954
Elliott Hughes410c0c82011-09-01 17:58:25 -07002955void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2956 {
2957 MutexLock mu(globals_lock);
2958 globals.VisitRoots(visitor, arg);
2959 }
2960 {
2961 MutexLock mu(pins_lock);
2962 pin_table.VisitRoots(visitor, arg);
2963 }
2964 // The weak_globals table is visited by the GC itself (because it mutates the table).
2965}
2966
Ian Rogersdf20fe02011-07-20 20:34:16 -07002967} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002968
2969std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2970 switch (rhs) {
2971 case JNIInvalidRefType:
2972 os << "JNIInvalidRefType";
2973 return os;
2974 case JNILocalRefType:
2975 os << "JNILocalRefType";
2976 return os;
2977 case JNIGlobalRefType:
2978 os << "JNIGlobalRefType";
2979 return os;
2980 case JNIWeakGlobalRefType:
2981 os << "JNIWeakGlobalRefType";
2982 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002983 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002984 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002985 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002986 }
2987}