blob: a18a2612f802a27f7ee1b6d1d98ca0b5681be54f [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 Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080026#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080027#include "base/stl_util.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080028#include "base/stringpiece.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070029#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070032#include "interpreter/interpreter.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070033#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070034#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070035#include "mirror/art_field-inl.h"
36#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/class-inl.h"
38#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
41#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080042#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070043#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070044#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070046#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070047#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070049#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070050#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070051
Brian Carlstromea46f952013-07-30 01:26:50 -070052using ::art::mirror::ArtField;
53using ::art::mirror::ArtMethod;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070054using ::art::mirror::Array;
55using ::art::mirror::BooleanArray;
56using ::art::mirror::ByteArray;
57using ::art::mirror::CharArray;
58using ::art::mirror::Class;
59using ::art::mirror::ClassLoader;
60using ::art::mirror::DoubleArray;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070061using ::art::mirror::FloatArray;
62using ::art::mirror::IntArray;
63using ::art::mirror::LongArray;
64using ::art::mirror::Object;
65using ::art::mirror::ObjectArray;
66using ::art::mirror::ShortArray;
67using ::art::mirror::String;
68using ::art::mirror::Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070070namespace art {
71
Brian Carlstrom7934ac22013-07-26 10:54:15 -070072static const size_t kMonitorsInitial = 32; // Arbitrary.
73static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070074
Brian Carlstrom7934ac22013-07-26 10:54:15 -070075static const size_t kLocalsInitial = 64; // Arbitrary.
76static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070077
Brian Carlstrom7934ac22013-07-26 10:54:15 -070078static const size_t kPinTableInitial = 16; // Arbitrary.
79static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070080
Brian Carlstrom7934ac22013-07-26 10:54:15 -070081static size_t gGlobalsInitial = 512; // Arbitrary.
82static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070083
Brian Carlstrom7934ac22013-07-26 10:54:15 -070084static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
85static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070086
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -070089 if (obj == NULL) {
90 return NULL;
91 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -070093 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -070094 WriterMutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -070095 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
96 return reinterpret_cast<jweak>(ref);
97}
98
Jeff Hao19c5d372013-03-15 14:33:43 -070099static bool IsBadJniVersion(int version) {
100 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
101 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
102}
103
Brian Carlstromea46f952013-07-30 01:26:50 -0700104static void CheckMethodArguments(ArtMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700106 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -0700107 const DexFile::TypeList* params = mh.GetParameterTypeList();
108 if (params == NULL) {
109 return; // No arguments so nothing to check.
110 }
Jeff Hao5d917302013-02-27 17:57:33 -0800111 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700112 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700113 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -0800114 if (!m->IsStatic()) {
115 offset = 1;
116 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700117 for (uint32_t i = 0; i < num_params; i++) {
118 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
119 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
120 if (param_type == NULL) {
121 Thread* self = Thread::Current();
122 CHECK(self->IsExceptionPending());
123 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
124 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800125 << self->GetException(NULL)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700126 self->ClearException();
127 ++error_count;
128 } else if (!param_type->IsPrimitive()) {
129 // TODO: check primitives are in range.
Jeff Hao5d917302013-02-27 17:57:33 -0800130 Object* argument = reinterpret_cast<Object*>(args[i + offset]);
Ian Rogers50b35e22012-10-04 10:09:15 -0700131 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700132 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
133 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
134 ++error_count;
135 }
Jeff Hao5d917302013-02-27 17:57:33 -0800136 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
137 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700138 }
139 }
140 if (error_count > 0) {
141 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
142 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700143 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700144 }
145}
Elliott Hughesb264f082012-04-06 17:10:10 -0700146
Brian Carlstromea46f952013-07-30 01:26:50 -0700147void InvokeWithArgArray(const ScopedObjectAccess& soa, ArtMethod* method,
Jeff Hao6474d192013-03-26 14:08:09 -0700148 ArgArray* arg_array, JValue* result, char result_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700150 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700152 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700153 }
Sebastien Hertz7d658cf2013-07-09 10:56:11 +0200154 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700155}
156
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
158 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700160 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700161 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700162 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800163 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700164 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800165 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700166 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
167 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700168}
169
Brian Carlstromea46f952013-07-30 01:26:50 -0700170static ArtMethod* FindVirtualMethod(Object* receiver, ArtMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700172 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700173}
174
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
176 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700178 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700179 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700180 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800181 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700182 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800183 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700184 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
185 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700186}
187
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
189 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700192 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700193 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800194 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700195 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800196 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700197 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
198 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700199}
200
Elliott Hughes6b436852011-08-12 10:16:44 -0700201// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
202// separated with slashes but aren't wrapped with "L;" like regular descriptors
203// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
204// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
205// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700206static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700207 std::string result;
208 // Add the missing "L;" if necessary.
209 if (name[0] == '[') {
210 result = name;
211 } else {
212 result += 'L';
213 result += name;
214 result += ';';
215 }
216 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700217 if (result.find('.') != std::string::npos) {
218 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
219 << "\"" << name << "\"";
220 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700221 }
222 return result;
223}
224
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
226 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800228 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
229 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
230 "no %s method \"%s.%s%s\"",
231 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700232}
233
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
235 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700238 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700239 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700240 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700241
Brian Carlstromea46f952013-07-30 01:26:50 -0700242 ArtMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700243 if (is_static) {
244 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700245 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700246 method = c->FindVirtualMethod(name, sig);
247 if (method == NULL) {
248 // No virtual method matching the signature. Search declared
249 // private methods and constructors.
250 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700251 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700252 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700253
Elliott Hughescdf53122011-08-19 15:46:09 -0700254 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700256 return NULL;
257 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700258
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700260}
261
Ian Rogersef28b142012-11-30 14:22:18 -0800262static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700264 ArtMethod* method = soa.Self()->GetCurrentMethod(NULL);
Ian Rogersef28b142012-11-30 14:22:18 -0800265 if (method == NULL ||
266 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
267 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700268 }
269 return method->GetDeclaringClass()->GetClassLoader();
270}
271
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
273 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700276 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700277 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700278 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700279
Brian Carlstromea46f952013-07-30 01:26:50 -0700280 ArtField* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700281 Class* field_type;
282 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
283 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800284 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700286 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700287 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700288 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 if (field_type == NULL) {
290 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 ThrowLocation throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700293 SirtRef<Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700294 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
296 "no type \"%s\" found and so no field \"%s\" could be found in class "
297 "\"%s\" or its superclasses", sig, name,
298 ClassHelper(c).GetDescriptor());
299 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 return NULL;
301 }
302 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800303 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800305 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700307 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800308 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
309 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
310 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
311 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700312 return NULL;
313 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700315}
316
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700318 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700319 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700320 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700321 vm->pin_table.Add(array);
322}
323
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700325 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700327 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700328 vm->pin_table.Remove(array);
329}
330
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
332 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700334 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
336 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
337 "%s offset=%d length=%d %s.length=%d",
338 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700339}
Ian Rogers0571d352011-11-03 19:51:38 -0700340
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
342 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700343 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
345 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
346 "offset=%d length=%d string.length()=%d", start, length,
347 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700348}
Elliott Hughes814e4032011-08-23 12:07:56 -0700349
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700351 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 // Turn the const char* into a java.lang.String.
353 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
354 if (msg != NULL && s.get() == NULL) {
355 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700356 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700357
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358 // Choose an appropriate constructor and set up the arguments.
359 jvalue args[2];
360 const char* signature;
361 if (msg == NULL && cause == NULL) {
362 signature = "()V";
363 } else if (msg != NULL && cause == NULL) {
364 signature = "(Ljava/lang/String;)V";
365 args[0].l = s.get();
366 } else if (msg == NULL && cause != NULL) {
367 signature = "(Ljava/lang/Throwable;)V";
368 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700369 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
371 args[0].l = s.get();
372 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700373 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
375 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800376 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377 LOG(ERROR) << "No <init>" << signature << " in "
378 << PrettyClass(soa.Decode<Class*>(exception_class));
379 return JNI_ERR;
380 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700381
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
383 if (exception.get() == NULL) {
384 return JNI_ERR;
385 }
Ian Rogersef28b142012-11-30 14:22:18 -0800386 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
388 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700390}
391
Elliott Hughes462c9442012-03-23 18:47:50 -0700392static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700393 if (vm == NULL || p_env == NULL) {
394 return JNI_ERR;
395 }
396
Elliott Hughes462c9442012-03-23 18:47:50 -0700397 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700398 Thread* self = Thread::Current();
399 if (self != NULL) {
400 *p_env = self->GetJniEnv();
401 return JNI_OK;
402 }
403
404 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
405
406 // No threads allowed in zygote mode.
407 if (runtime->IsZygote()) {
408 LOG(ERROR) << "Attempt to attach a thread in the zygote";
409 return JNI_ERR;
410 }
411
Elliott Hughes462c9442012-03-23 18:47:50 -0700412 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
413 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700414 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700415 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700416 if (IsBadJniVersion(args->version)) {
417 LOG(ERROR) << "Bad JNI version passed to "
418 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
419 << args->version;
420 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700421 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700422 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700423 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700424 }
Elliott Hughes75770752011-08-24 17:52:38 -0700425
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800426 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700427 *p_env = NULL;
428 return JNI_ERR;
429 } else {
430 *p_env = Thread::Current()->GetJniEnv();
431 return JNI_OK;
432 }
Elliott Hughes75770752011-08-24 17:52:38 -0700433}
434
Elliott Hughes79082e32011-08-25 12:07:32 -0700435class SharedLibrary {
436 public:
437 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
438 : path_(path),
439 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700440 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700441 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700442 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700443 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700444 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700445 }
446
Elliott Hughes79082e32011-08-25 12:07:32 -0700447 Object* GetClassLoader() {
448 return class_loader_;
449 }
450
451 std::string GetPath() {
452 return path_;
453 }
454
455 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700456 * Check the result of an earlier call to JNI_OnLoad on this library.
457 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700458 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700459 bool CheckOnLoadResult()
460 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700462 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700463 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
464 bool okay;
465 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700466 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700467
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700468 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
469 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
470 // caller can continue.
471 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
472 okay = true;
473 } else {
474 while (jni_on_load_result_ == kPending) {
475 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700476 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700477 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700478
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700479 okay = (jni_on_load_result_ == kOkay);
480 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
481 << (okay ? "succeeded" : "failed") << "]";
482 }
483 }
484 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 return okay;
486 }
487
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700489 Thread* self = Thread::Current();
490 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700491
Elliott Hughes79082e32011-08-25 12:07:32 -0700492 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700493 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700494
495 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700496 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 }
498
499 void* FindSymbol(const std::string& symbol_name) {
500 return dlsym(handle_, symbol_name.c_str());
501 }
502
503 private:
504 enum JNI_OnLoadState {
505 kPending,
506 kFailed,
507 kOkay,
508 };
509
510 // Path to library "/system/lib/libjni.so".
511 std::string path_;
512
513 // The void* returned by dlopen(3).
514 void* handle_;
515
516 // The ClassLoader this library is associated with.
517 Object* class_loader_;
518
519 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700520 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700522 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700523 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700524 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700525 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700526 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700527};
528
Elliott Hughes79082e32011-08-25 12:07:32 -0700529// This exists mainly to keep implementation details out of the header file.
530class Libraries {
531 public:
532 Libraries() {
533 }
534
535 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700536 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700537 }
538
Elliott Hughesae80b492012-04-24 10:43:17 -0700539 void Dump(std::ostream& os) const {
540 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700541 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700542 if (!first) {
543 os << ' ';
544 }
545 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700546 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700547 }
548 }
549
550 size_t size() const {
551 return libraries_.size();
552 }
553
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700555 auto it = libraries_.find(path);
Ian Rogers712462a2012-04-12 16:32:29 -0700556 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700557 }
558
559 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700560 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700561 }
562
563 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstromea46f952013-07-30 01:26:50 -0700564 void* FindNativeMethod(const ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700565 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 std::string jni_short_name(JniShortName(m));
567 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700568 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700569 for (const auto& lib : libraries_) {
570 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700571 if (library->GetClassLoader() != declaring_class_loader) {
572 // We only search libraries loaded by the appropriate ClassLoader.
573 continue;
574 }
575 // Try the short name then the long name...
576 void* fn = library->FindSymbol(jni_short_name);
577 if (fn == NULL) {
578 fn = library->FindSymbol(jni_long_name);
579 }
580 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800581 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
582 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 return fn;
584 }
585 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700586 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700587 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700588 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700589 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 return NULL;
591 }
592
593 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700594 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700595};
596
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700597JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
598 jvalue* args) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700599 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700600 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700601 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800602 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700603 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800604 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700605 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
606 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800607}
608
Ian Rogersbc939662013-08-15 10:26:54 -0700609#define CHECK_NON_NULL_ARGUMENT(fn, value) \
610 if (UNLIKELY(value == NULL)) { \
611 JniAbortF(#fn, #value " == null"); \
612 }
613
Elliott Hughescdf53122011-08-19 15:46:09 -0700614class JNI {
615 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700616 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700617 return JNI_VERSION_1_6;
618 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700619
Ian Rogers25e8b912012-09-07 11:31:36 -0700620 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700621 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700622 return NULL;
623 }
624
Elliott Hughescdf53122011-08-19 15:46:09 -0700625 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700626 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700627 Runtime* runtime = Runtime::Current();
628 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700629 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700630 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700631 Class* c = NULL;
632 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800633 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800634 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700635 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800636 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700637 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700639 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700640
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700642 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700643 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700644 jobject art_method = env->GetObjectField(java_method,
645 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
646 ArtMethod* method = soa.Decode<ArtMethod*>(art_method);
647 DCHECK(method != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700649 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700650
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700652 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700653 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700654 jobject art_field = env->GetObjectField(java_field,
655 WellKnownClasses::java_lang_reflect_Field_artField);
656 ArtField* field = soa.Decode<ArtField*>(art_field);
657 DCHECK(field != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700658 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700659 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700660
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700662 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700664 ArtMethod* m = soa.DecodeMethod(mid);
665 jobject art_method = soa.AddLocalReference<jobject>(m);
666 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
667 if (env->ExceptionCheck()) {
668 return NULL;
669 }
670 SetObjectField(env,
671 reflect_method,
672 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod,
673 art_method);
674 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700675 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700676
Elliott Hughescdf53122011-08-19 15:46:09 -0700677 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700678 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700680 ArtField* f = soa.DecodeField(fid);
681 jobject art_field = soa.AddLocalReference<jobject>(f);
682 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
683 if (env->ExceptionCheck()) {
684 return NULL;
685 }
686 SetObjectField(env,
687 reflect_field,
688 WellKnownClasses::java_lang_reflect_Field_artField,
689 art_field);
690 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700692
Elliott Hughes37f7a402011-08-22 18:56:01 -0700693 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700694 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700695 ScopedObjectAccess soa(env);
696 Object* o = soa.Decode<Object*>(java_object);
697 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 }
699
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700700 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700701 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700702 ScopedObjectAccess soa(env);
703 Class* c = soa.Decode<Class*>(java_class);
704 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700706
Elliott Hughes37f7a402011-08-22 18:56:01 -0700707 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700708 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
709 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 ScopedObjectAccess soa(env);
711 Class* c1 = soa.Decode<Class*>(java_class1);
712 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700713 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700715
Elliott Hughese84278b2012-03-22 10:06:53 -0700716 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700717 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700718 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700719 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700720 return JNI_TRUE;
721 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700722 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700723 Object* obj = soa.Decode<Object*>(jobj);
724 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700725 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700726 }
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 jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730 ScopedObjectAccess soa(env);
731 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700732 if (exception == NULL) {
733 return JNI_ERR;
734 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800735 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
736 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700737 return JNI_OK;
738 }
739
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700740 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700741 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Elliott Hughesa4f94742012-05-29 16:28:38 -0700742 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700743 }
744
745 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700746 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700747 }
748
749 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700750 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700751 }
752
753 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700754 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700755
Brian Carlstromea46f952013-07-30 01:26:50 -0700756 SirtRef<Object> old_throw_this_object(soa.Self(), NULL);
757 SirtRef<ArtMethod> old_throw_method(soa.Self(), NULL);
758 SirtRef<Throwable> old_exception(soa.Self(), NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800759 uint32_t old_throw_dex_pc;
760 {
761 ThrowLocation old_throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700762 Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800763 old_throw_this_object.reset(old_throw_location.GetThis());
764 old_throw_method.reset(old_throw_location.GetMethod());
765 old_exception.reset(old_exception_obj);
766 old_throw_dex_pc = old_throw_location.GetDexPc();
767 soa.Self()->ClearException();
768 }
769 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700770 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
771 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
772 if (mid == NULL) {
773 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800774 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700775 } else {
776 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800777 if (soa.Self()->IsExceptionPending()) {
778 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700779 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800780 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700781 }
782 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800783 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
784 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700785
Ian Rogers62d6c772013-02-27 08:32:07 -0800786 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700788
Elliott Hughescdf53122011-08-19 15:46:09 -0700789 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700790 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800791 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700792 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700793 }
794
Ian Rogers25e8b912012-09-07 11:31:36 -0700795 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 LOG(FATAL) << "JNI FatalError called: " << msg;
797 }
798
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700799 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800800 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700801 return JNI_ERR;
802 }
Ian Rogersef28b142012-11-30 14:22:18 -0800803 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 return JNI_OK;
805 }
806
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700807 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700808 ScopedObjectAccess soa(env);
809 Object* survivor = soa.Decode<Object*>(java_survivor);
810 soa.Env()->PopFrame();
811 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 }
813
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700814 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800815 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700816 }
817
Elliott Hughescdf53122011-08-19 15:46:09 -0700818 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 if (obj == NULL) {
820 return NULL;
821 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700822 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700823 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700825 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogersb8a0b942013-08-20 18:09:52 -0700826 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700827 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 return reinterpret_cast<jobject>(ref);
829 }
830
831 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 if (obj == NULL) {
833 return;
834 }
Ian Rogersef28b142012-11-30 14:22:18 -0800835 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800837 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700838 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700839
840 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
841 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
842 << "failed to find entry";
843 }
844 }
845
846 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700847 ScopedObjectAccess soa(env);
848 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 }
850
851 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 if (obj == NULL) {
853 return;
854 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700855 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700856 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700858 WriterMutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700859
860 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
861 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
862 << "failed to find entry";
863 }
864 }
865
866 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700867 if (obj == NULL) {
868 return NULL;
869 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700870 ScopedObjectAccess soa(env);
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700871 return soa.AddLocalReference<jobject>(soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 }
873
874 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 if (obj == NULL) {
876 return;
877 }
Ian Rogersef28b142012-11-30 14:22:18 -0800878 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700879
Ian Rogersef28b142012-11-30 14:22:18 -0800880 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700881 if (!locals.Remove(cookie, obj)) {
882 // Attempting to delete a local reference that is not in the
883 // topmost local reference frame is a no-op. DeleteLocalRef returns
884 // void and doesn't throw any exceptions, but we should probably
885 // complain about it so the user will notice that things aren't
886 // going quite the way they expect.
887 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
888 << "failed to find entry";
889 }
890 }
891
892 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700893 if (obj1 == obj2) {
894 return JNI_TRUE;
895 } else {
896 ScopedObjectAccess soa(env);
897 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
898 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 }
900
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700901 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700902 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700903 ScopedObjectAccess soa(env);
904 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700905 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700906 return NULL;
907 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700908 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700909 }
910
Ian Rogersbc939662013-08-15 10:26:54 -0700911 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700912 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700913 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700914 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
915 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
916 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 va_end(args);
918 return result;
919 }
920
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700922 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
923 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700924 ScopedObjectAccess soa(env);
925 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700926 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700927 return NULL;
928 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700929 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700930 if (result == NULL) {
931 return NULL;
932 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700936 return local_result;
937 } else {
938 return NULL;
939 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 }
941
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700943 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
944 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 ScopedObjectAccess soa(env);
946 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700947 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700948 return NULL;
949 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700950 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700951 if (result == NULL) {
952 return NULL;
953 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700957 return local_result;
958 } else {
959 return NULL;
960 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 }
962
Ian Rogersbc939662013-08-15 10:26:54 -0700963 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
964 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
965 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
966 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700967 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700968 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 }
970
Ian Rogersbc939662013-08-15 10:26:54 -0700971 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
972 const char* sig) {
973 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
974 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
975 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700976 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700977 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 }
979
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 va_list ap;
982 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700983 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
984 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700985 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700988 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 }
990
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700992 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
993 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 ScopedObjectAccess soa(env);
995 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
996 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001000 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
1001 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002 ScopedObjectAccess soa(env);
1003 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1004 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 va_list ap;
1009 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001010 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1011 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001012 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001015 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001019 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1020 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001021 ScopedObjectAccess soa(env);
1022 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 }
1024
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001026 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1027 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001028 ScopedObjectAccess soa(env);
1029 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 va_list ap;
1034 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001035 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1036 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1037 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001040 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001044 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1045 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001046 ScopedObjectAccess soa(env);
1047 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001051 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1052 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001053 ScopedObjectAccess soa(env);
1054 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 va_list ap;
1059 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001060 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1061 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001062 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001065 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001069 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1070 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001071 ScopedObjectAccess soa(env);
1072 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001076 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1077 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001078 ScopedObjectAccess soa(env);
1079 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 va_list ap;
1084 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001085 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1086 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001087 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001090 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 }
1092
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001094 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1095 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001096 ScopedObjectAccess soa(env);
1097 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001101 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1102 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103 ScopedObjectAccess soa(env);
1104 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 va_list ap;
1109 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001110 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1111 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1112 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001113 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001115 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001119 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1120 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001121 ScopedObjectAccess soa(env);
1122 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001126 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1127 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001128 ScopedObjectAccess soa(env);
1129 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 va_list ap;
1134 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001135 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1136 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001137 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001138 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001140 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 }
1142
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001144 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1145 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001146 ScopedObjectAccess soa(env);
1147 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001151 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1152 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001153 ScopedObjectAccess soa(env);
1154 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 va_list ap;
1159 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001160 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1161 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001162 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001165 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001169 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1170 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001171 ScopedObjectAccess soa(env);
1172 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 }
1174
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001176 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1177 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001178 ScopedObjectAccess soa(env);
1179 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 va_list ap;
1184 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001185 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1186 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001187 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001190 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001194 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1195 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001196 ScopedObjectAccess soa(env);
1197 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 }
1199
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001201 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1202 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001203 ScopedObjectAccess soa(env);
1204 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 va_list ap;
1209 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001210 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1211 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001212 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001213 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001218 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1219 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001220 ScopedObjectAccess soa(env);
1221 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001225 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1226 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001227 ScopedObjectAccess soa(env);
1228 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001231 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001234 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1235 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001236 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001237 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1238 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 va_end(ap);
1240 return local_result;
1241 }
1242
Ian Rogersbc939662013-08-15 10:26:54 -07001243 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1244 va_list args) {
1245 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1246 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001247 ScopedObjectAccess soa(env);
1248 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1249 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
Ian Rogersbc939662013-08-15 10:26:54 -07001252 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1253 jvalue* args) {
1254 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1255 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001256 ScopedObjectAccess soa(env);
1257 JValue result(InvokeWithJValues(soa, obj, mid, args));
1258 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 }
1260
Ian Rogersbc939662013-08-15 10:26:54 -07001261 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1262 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001264 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001265 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1266 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001267 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001268 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001270 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 }
1272
Ian Rogersbc939662013-08-15 10:26:54 -07001273 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1274 va_list args) {
1275 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1276 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001277 ScopedObjectAccess soa(env);
1278 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 }
1280
Ian Rogersbc939662013-08-15 10:26:54 -07001281 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1282 jvalue* args) {
1283 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1284 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001285 ScopedObjectAccess soa(env);
1286 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001289 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001292 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1293 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001294 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001295 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001297 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
Ian Rogersbc939662013-08-15 10:26:54 -07001300 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1301 va_list args) {
1302 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1303 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001304 ScopedObjectAccess soa(env);
1305 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
Ian Rogersbc939662013-08-15 10:26:54 -07001308 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1309 jvalue* args) {
1310 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1311 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001312 ScopedObjectAccess soa(env);
1313 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001316 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001319 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1320 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1321 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001322 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001324 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
Ian Rogersbc939662013-08-15 10:26:54 -07001327 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1328 va_list args) {
1329 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1330 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001331 ScopedObjectAccess soa(env);
1332 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Ian Rogersbc939662013-08-15 10:26:54 -07001335 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1336 jvalue* args) {
1337 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1338 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001339 ScopedObjectAccess soa(env);
1340 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001343 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001346 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1347 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001348 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001349 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001351 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 }
1353
Ian Rogersbc939662013-08-15 10:26:54 -07001354 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1355 va_list args) {
1356 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1357 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001358 ScopedObjectAccess soa(env);
1359 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 }
1361
Ian Rogersbc939662013-08-15 10:26:54 -07001362 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1363 jvalue* args) {
1364 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1365 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001366 ScopedObjectAccess soa(env);
1367 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
1369
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001370 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001372 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001373 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1374 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001375 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001376 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001378 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
1380
Ian Rogersbc939662013-08-15 10:26:54 -07001381 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1382 va_list args) {
1383 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1384 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001385 ScopedObjectAccess soa(env);
1386 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
Ian Rogersbc939662013-08-15 10:26:54 -07001389 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1390 jvalue* args) {
1391 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1392 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001393 ScopedObjectAccess soa(env);
1394 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
1396
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001397 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001399 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001400 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1401 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001402 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001403 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001405 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Ian Rogersbc939662013-08-15 10:26:54 -07001408 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1409 va_list args) {
1410 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1411 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001412 ScopedObjectAccess soa(env);
1413 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Ian Rogersbc939662013-08-15 10:26:54 -07001416 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1417 jvalue* args) {
1418 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1419 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001420 ScopedObjectAccess soa(env);
1421 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001424 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001426 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001427 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1428 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001429 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001430 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001432 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Ian Rogersbc939662013-08-15 10:26:54 -07001435 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1436 va_list args) {
1437 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1438 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 ScopedObjectAccess soa(env);
1440 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Ian Rogersbc939662013-08-15 10:26:54 -07001443 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1444 jvalue* args) {
1445 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1446 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001447 ScopedObjectAccess soa(env);
1448 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001451 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001453 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001454 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1455 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001456 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001457 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001459 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Ian Rogersbc939662013-08-15 10:26:54 -07001462 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1463 va_list args) {
1464 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1465 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001466 ScopedObjectAccess soa(env);
1467 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Ian Rogersbc939662013-08-15 10:26:54 -07001470 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1471 jvalue* args) {
1472 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1473 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001474 ScopedObjectAccess soa(env);
1475 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001478 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001479 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001480 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001481 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1482 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001483 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001484 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 va_end(ap);
1486 }
1487
Brian Carlstromea46f952013-07-30 01:26:50 -07001488 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1489 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001490 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1491 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001492 ScopedObjectAccess soa(env);
1493 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Ian Rogersbc939662013-08-15 10:26:54 -07001496 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1497 jvalue* args) {
1498 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1499 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001500 ScopedObjectAccess soa(env);
1501 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
Ian Rogersbc939662013-08-15 10:26:54 -07001504 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1505 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1506 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1507 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001508 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001509 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001511
Ian Rogersbc939662013-08-15 10:26:54 -07001512 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1513 const char* sig) {
1514 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1515 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1516 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001517 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001518 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001520
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001521 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001522 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1523 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001524 ScopedObjectAccess soa(env);
1525 Object* o = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -07001526 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001527 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001529
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001530 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001531 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001532 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -07001533 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001534 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
1536
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001537 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001538 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1539 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001540 ScopedObjectAccess soa(env);
1541 Object* o = soa.Decode<Object*>(java_object);
1542 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001543 ArtField* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001547 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001548 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001549 ScopedObjectAccess soa(env);
1550 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001551 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001552 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001555#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001556 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1557 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001558 ScopedObjectAccess soa(env); \
1559 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001560 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001561 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001562
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001563#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001564 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001565 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001566 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001567 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001568
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001569#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001570 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1571 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001572 ScopedObjectAccess soa(env); \
1573 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001574 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001575 f->Set ##fn(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001576
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001577#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001578 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001579 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001580 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001581 f->Set ##fn(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001582
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001583 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001584 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001587 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001588 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001591 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001592 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 }
1594
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001595 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001596 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001599 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001600 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001603 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001604 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001607 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001608 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 }
1610
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001611 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001612 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001615 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001616 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 }
1618
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001619 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001620 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 }
1622
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001623 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001624 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 }
1626
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001627 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001628 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001631 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001632 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 }
1634
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001635 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001636 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001637 }
1638
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001639 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001640 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001641 }
1642
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001643 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001644 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001645 }
1646
1647 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001648 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001649 }
1650
1651 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001652 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001653 }
1654
1655 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001656 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001657 }
1658
1659 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001660 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001661 }
1662
1663 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001664 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001665 }
1666
1667 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001668 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001669 }
1670
1671 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001672 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001673 }
1674
1675 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001676 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001677 }
1678
1679 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001680 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001681 }
1682
1683 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001684 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001685 }
1686
1687 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001688 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001689 }
1690
1691 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001692 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001693 }
1694
1695 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001696 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001697 }
1698
1699 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001700 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001701 }
1702
1703 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001704 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001705 }
1706
1707 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001708 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001711 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001713 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001714 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001715 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001716 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1717 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 va_end(ap);
1719 return local_result;
1720 }
1721
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001722 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001723 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001724 ScopedObjectAccess soa(env);
1725 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1726 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001730 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001731 ScopedObjectAccess soa(env);
1732 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1733 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001736 static jboolean CallStaticBooleanMethod(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 Rogersbc939662013-08-15 10:26:54 -07001739 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001740 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001743 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001746 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001747 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001748 ScopedObjectAccess soa(env);
1749 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001752 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001753 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001754 ScopedObjectAccess soa(env);
1755 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 }
1757
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001758 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001760 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001761 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001762 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001763 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001765 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 }
1767
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001768 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001769 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001770 ScopedObjectAccess soa(env);
1771 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 }
1773
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001774 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001775 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001776 ScopedObjectAccess soa(env);
1777 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 }
1779
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001780 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001782 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001783 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001784 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001785 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001787 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001790 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001791 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001792 ScopedObjectAccess soa(env);
1793 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001796 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001797 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001798 ScopedObjectAccess soa(env);
1799 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001802 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001804 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001805 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001806 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001807 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001809 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 }
1811
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001812 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001813 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001814 ScopedObjectAccess soa(env);
1815 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001818 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001819 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001820 ScopedObjectAccess soa(env);
1821 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 }
1823
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001824 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001826 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001827 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001828 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001829 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001831 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001834 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001835 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001836 ScopedObjectAccess soa(env);
1837 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 }
1839
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001840 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001841 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001842 ScopedObjectAccess soa(env);
1843 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 }
1845
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001846 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001848 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001849 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001850 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001851 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001853 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 }
1855
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001856 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001857 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001858 ScopedObjectAccess soa(env);
1859 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 }
1861
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001862 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001863 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001864 ScopedObjectAccess soa(env);
1865 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 }
1867
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001868 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001870 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001871 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001872 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001873 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001875 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 }
1877
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001878 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001879 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001880 ScopedObjectAccess soa(env);
1881 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 }
1883
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001884 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001885 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886 ScopedObjectAccess soa(env);
1887 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 }
1889
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001890 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001892 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001893 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001894 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001895 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001896 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001897 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 }
1899
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001900 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001901 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001902 ScopedObjectAccess soa(env);
1903 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 }
1905
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001906 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001907 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001908 ScopedObjectAccess soa(env);
1909 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 }
1911
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001912 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001914 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001915 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001916 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001917 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 va_end(ap);
1919 }
1920
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001921 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001922 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001923 ScopedObjectAccess soa(env);
1924 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001925 }
1926
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001927 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001928 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001929 ScopedObjectAccess soa(env);
1930 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 }
1932
Elliott Hughes814e4032011-08-23 12:07:56 -07001933 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogersbc939662013-08-15 10:26:54 -07001934 if (UNLIKELY(chars == NULL && char_count > 0)) { \
1935 JniAbortF("NewString", "char == null && char_count > 0"); \
1936 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001937 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001938 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001939 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
1942 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 if (utf == NULL) {
1944 return NULL;
1945 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001946 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001947 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001948 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
Elliott Hughes814e4032011-08-23 12:07:56 -07001951 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001952 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001953 ScopedObjectAccess soa(env);
1954 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001955 }
1956
1957 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001958 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001959 ScopedObjectAccess soa(env);
1960 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001961 }
1962
Ian Rogersbc939662013-08-15 10:26:54 -07001963 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1964 jchar* buf) {
1965 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001966 ScopedObjectAccess soa(env);
1967 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001968 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001969 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001970 } else {
Ian Rogersbc939662013-08-15 10:26:54 -07001971 CHECK_NON_NULL_ARGUMENT(GetStringRegion, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001972 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1973 memcpy(buf, chars + start, length * sizeof(jchar));
1974 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001975 }
1976
Ian Rogersbc939662013-08-15 10:26:54 -07001977 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1978 char* buf) {
1979 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001980 ScopedObjectAccess soa(env);
1981 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001982 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001983 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001984 } else {
Ian Rogersbc939662013-08-15 10:26:54 -07001985 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001986 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1987 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1988 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001989 }
1990
Elliott Hughes75770752011-08-24 17:52:38 -07001991 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07001992 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001993 ScopedObjectAccess soa(env);
1994 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001995 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001996 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001997 if (is_copy != NULL) {
1998 *is_copy = JNI_FALSE;
1999 }
2000 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07002001 }
2002
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002003 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogersbc939662013-08-15 10:26:54 -07002004 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002005 ScopedObjectAccess soa(env);
2006 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Elliott Hughes75770752011-08-24 17:52:38 -07002009 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002010 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002014 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 }
2016
Elliott Hughes75770752011-08-24 17:52:38 -07002017 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002018 if (java_string == NULL) {
2019 return NULL;
2020 }
2021 if (is_copy != NULL) {
2022 *is_copy = JNI_TRUE;
2023 }
Ian Rogersef28b142012-11-30 14:22:18 -08002024 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002025 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002026 size_t byte_count = s->GetUtfLength();
2027 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002028 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002029 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2030 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2031 bytes[byte_count] = '\0';
2032 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002033 }
2034
Elliott Hughes75770752011-08-24 17:52:38 -07002035 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002036 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002037 }
2038
Elliott Hughesbd935992011-08-22 11:59:34 -07002039 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002040 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002041 ScopedObjectAccess soa(env);
2042 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002043 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002044 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2045 }
Elliott Hughesbd935992011-08-22 11:59:34 -07002046 Array* array = obj->AsArray();
2047 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002051 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002052 ScopedObjectAccess soa(env);
2053 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2054 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Ian Rogersbc939662013-08-15 10:26:54 -07002057 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2058 jobject java_value) {
2059 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002060 ScopedObjectAccess soa(env);
2061 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2062 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 array->Set(index, value);
2064 }
2065
2066 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002067 ScopedObjectAccess soa(env);
2068 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
2071 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002072 ScopedObjectAccess soa(env);
2073 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
2076 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002077 ScopedObjectAccess soa(env);
2078 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
2081 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002082 ScopedObjectAccess soa(env);
2083 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
2086 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002087 ScopedObjectAccess soa(env);
2088 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
2091 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002092 ScopedObjectAccess soa(env);
2093 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
2096 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002097 ScopedObjectAccess soa(env);
2098 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
2101 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002102 if (length < 0) {
2103 JniAbortF("NewObjectArray", "negative array length: %d", length);
2104 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002105
2106 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002107 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002108 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 std::string descriptor;
2110 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002111 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002112
2113 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002114 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2115 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 return NULL;
2117 }
2118
Elliott Hughes75770752011-08-24 17:52:38 -07002119 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002120 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07002121 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002122 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002123 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07002124 for (jsize i = 0; i < length; ++i) {
2125 result->Set(i, initial_object);
2126 }
2127 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002128 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
2131 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002132 ScopedObjectAccess soa(env);
2133 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
2135
Ian Rogersa15e67d2012-02-28 13:51:55 -08002136 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002137 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002138 ScopedObjectAccess soa(env);
2139 Array* array = soa.Decode<Array*>(java_array);
2140 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002141 if (is_copy != NULL) {
2142 *is_copy = JNI_FALSE;
2143 }
2144 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002145 }
2146
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002147 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002148 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Ian Rogersef28b142012-11-30 14:22:18 -08002149 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002150 }
2151
Elliott Hughes75770752011-08-24 17:52:38 -07002152 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002153 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002154 ScopedObjectAccess soa(env);
2155 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Elliott Hughes75770752011-08-24 17:52:38 -07002158 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002159 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002160 ScopedObjectAccess soa(env);
2161 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 }
2163
Elliott Hughes75770752011-08-24 17:52:38 -07002164 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002165 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002166 ScopedObjectAccess soa(env);
2167 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 }
2169
Elliott Hughes75770752011-08-24 17:52:38 -07002170 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002171 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002172 ScopedObjectAccess soa(env);
2173 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
2175
Elliott Hughes75770752011-08-24 17:52:38 -07002176 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002177 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002178 ScopedObjectAccess soa(env);
2179 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
Elliott Hughes75770752011-08-24 17:52:38 -07002182 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002183 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002184 ScopedObjectAccess soa(env);
2185 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
Elliott Hughes75770752011-08-24 17:52:38 -07002188 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002189 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002190 ScopedObjectAccess soa(env);
2191 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
2193
Elliott Hughes75770752011-08-24 17:52:38 -07002194 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002195 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002196 ScopedObjectAccess soa(env);
2197 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 }
2199
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002200 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002201 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 }
2203
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002204 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002205 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002208 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002209 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 }
2211
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002212 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002213 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 }
2215
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002216 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002217 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 }
2219
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002220 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002221 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002222 }
2223
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002224 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002225 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002228 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002229 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
2231
Ian Rogersbc939662013-08-15 10:26:54 -07002232 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2233 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002234 ScopedObjectAccess soa(env);
2235 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 }
2237
Ian Rogersbc939662013-08-15 10:26:54 -07002238 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2239 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002240 ScopedObjectAccess soa(env);
2241 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Ian Rogersbc939662013-08-15 10:26:54 -07002244 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2245 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002246 ScopedObjectAccess soa(env);
2247 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 }
2249
Ian Rogersbc939662013-08-15 10:26:54 -07002250 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2251 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002252 ScopedObjectAccess soa(env);
2253 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
Ian Rogersbc939662013-08-15 10:26:54 -07002256 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2257 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002258 ScopedObjectAccess soa(env);
2259 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002260 }
2261
Ian Rogersbc939662013-08-15 10:26:54 -07002262 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2263 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002264 ScopedObjectAccess soa(env);
2265 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002266 }
2267
Ian Rogersbc939662013-08-15 10:26:54 -07002268 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2269 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002270 ScopedObjectAccess soa(env);
2271 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273
Ian Rogersbc939662013-08-15 10:26:54 -07002274 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2275 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002276 ScopedObjectAccess soa(env);
2277 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 }
2279
Ian Rogersbc939662013-08-15 10:26:54 -07002280 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2281 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002282 ScopedObjectAccess soa(env);
2283 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002284 }
2285
Ian Rogersbc939662013-08-15 10:26:54 -07002286 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2287 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002288 ScopedObjectAccess soa(env);
2289 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 }
2291
Ian Rogersbc939662013-08-15 10:26:54 -07002292 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2293 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002294 ScopedObjectAccess soa(env);
2295 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 }
2297
Ian Rogersbc939662013-08-15 10:26:54 -07002298 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2299 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002300 ScopedObjectAccess soa(env);
2301 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 }
2303
Ian Rogersbc939662013-08-15 10:26:54 -07002304 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2305 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002306 ScopedObjectAccess soa(env);
2307 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 }
2309
Ian Rogersbc939662013-08-15 10:26:54 -07002310 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2311 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002312 ScopedObjectAccess soa(env);
2313 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002314 }
2315
Ian Rogersbc939662013-08-15 10:26:54 -07002316 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2317 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002318 ScopedObjectAccess soa(env);
2319 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002320 }
2321
Ian Rogersbc939662013-08-15 10:26:54 -07002322 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2323 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002324 ScopedObjectAccess soa(env);
2325 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002326 }
2327
Ian Rogersbc939662013-08-15 10:26:54 -07002328 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2329 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002330 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2331 }
2332
Ian Rogersbc939662013-08-15 10:26:54 -07002333 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2334 jint method_count, bool return_errors) {
2335 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002336 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002337 return JNI_ERR; // Not reached.
2338 }
2339 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002340 ScopedObjectAccess soa(env);
2341 Class* c = soa.Decode<Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002342 if (UNLIKELY(method_count == 0)) {
2343 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2344 << PrettyDescriptor(c);
2345 return JNI_OK;
2346 }
2347 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2348 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002349 const char* name = methods[i].name;
2350 const char* sig = methods[i].signature;
2351
2352 if (*sig == '!') {
2353 // TODO: fast jni. it's too noisy to log all these.
2354 ++sig;
2355 }
2356
Brian Carlstromea46f952013-07-30 01:26:50 -07002357 ArtMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002358 if (m == NULL) {
2359 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002360 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002361 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002362 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002363 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002364 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002365 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002366 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002367 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002368 << PrettyDescriptor(c) << "." << name << sig
2369 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002370 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002371 return JNI_ERR;
2372 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002373
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002374 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002375
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002376 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002377 }
2378 return JNI_OK;
2379 }
2380
Elliott Hughes5174fe62011-08-23 15:12:35 -07002381 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002382 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002383 ScopedObjectAccess soa(env);
2384 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002385
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002386 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002387
2388 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002389 ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002390 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002391 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002392 }
2393 }
2394 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002395 ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002396 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002397 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002398 }
2399 }
2400
2401 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002402 }
2403
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002404 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2405 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002406 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002407 ScopedObjectAccess soa(env);
2408 Object* o = soa.Decode<Object*>(java_object);
2409 o->MonitorEnter(soa.Self());
2410 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002411 return JNI_ERR;
2412 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002413 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002414 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002415 }
2416
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002417 static jint MonitorExit(JNIEnv* env, jobject java_object)
2418 UNLOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002419 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002420 ScopedObjectAccess soa(env);
2421 Object* o = soa.Decode<Object*>(java_object);
2422 o->MonitorExit(soa.Self());
2423 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002424 return JNI_ERR;
2425 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002426 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002427 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002428 }
2429
2430 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002431 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002432 Runtime* runtime = Runtime::Current();
2433 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002434 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002435 } else {
2436 *vm = NULL;
2437 }
2438 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2439 }
2440
Elliott Hughescdf53122011-08-19 15:46:09 -07002441 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002442 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002443 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002444 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002445 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002446 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002447 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002448
Elliott Hughes96a98872012-12-19 14:21:15 -08002449 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002450 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2451 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002452 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002453 jint capacity_arg = static_cast<jint>(capacity);
2454
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002455 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2456 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002457 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002458 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002459 }
2460
Elliott Hughesb465ab02011-08-24 11:21:21 -07002461 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002462 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002463 }
2464
Elliott Hughesb465ab02011-08-24 11:21:21 -07002465 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002466 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002467 }
2468
Elliott Hughesb465ab02011-08-24 11:21:21 -07002469 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002470 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002471
2472 // Do we definitely know what kind of reference this is?
2473 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2474 IndirectRefKind kind = GetIndirectRefKind(ref);
2475 switch (kind) {
2476 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002477 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002478 return JNILocalRefType;
2479 }
2480 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002481 case kGlobal:
2482 return JNIGlobalRefType;
2483 case kWeakGlobal:
2484 return JNIWeakGlobalRefType;
2485 case kSirtOrInvalid:
2486 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002487 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002488 return JNILocalRefType;
2489 }
2490
Ian Rogersef28b142012-11-30 14:22:18 -08002491 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002492 return JNIInvalidRefType;
2493 }
2494
Elliott Hughesb465ab02011-08-24 11:21:21 -07002495 // If we're handing out direct pointers, check whether it's a direct pointer
2496 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002497 {
2498 ScopedObjectAccess soa(env);
2499 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2500 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2501 return JNILocalRefType;
2502 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002503 }
2504 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002505 return JNIInvalidRefType;
2506 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002507 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2508 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002509 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002510
2511 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002512 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2513 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002514 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002515 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002516 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2517 return JNI_ERR;
2518 }
2519 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002520 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002521 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2522 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002523 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002524 soa.Self()->ThrowOutOfMemoryError(caller);
2525 }
2526 return okay ? JNI_OK : JNI_ERR;
2527 }
2528
2529 template<typename JniT, typename ArtT>
2530 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002531 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002532 if (length < 0) {
2533 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2534 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002535 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002536 return soa.AddLocalReference<JniT>(result);
2537 }
2538
2539 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2540 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2541 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002542 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002543 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2544 PinPrimitiveArray(soa, array);
2545 if (is_copy != NULL) {
2546 *is_copy = JNI_FALSE;
2547 }
2548 return array->GetData();
2549 }
2550
2551 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002552 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002553 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002554 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002555 Array* array = soa.Decode<Array*>(java_array);
2556 UnpinPrimitiveArray(soa, array);
2557 }
2558 }
2559
2560 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2561 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2562 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002563 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002564 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002565 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2566 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2567 ThrowAIOOBE(soa, array, start, length, "src");
2568 } else {
Ian Rogersbc939662013-08-15 10:26:54 -07002569 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002570 JavaT* data = array->GetData();
2571 memcpy(buf, data + start, length * sizeof(JavaT));
2572 }
2573 }
2574
2575 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2576 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2577 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002578 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002579 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002580 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2581 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2582 ThrowAIOOBE(soa, array, start, length, "dst");
2583 } else {
Ian Rogersbc939662013-08-15 10:26:54 -07002584 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002585 JavaT* data = array->GetData();
2586 memcpy(data + start, buf, length * sizeof(JavaT));
2587 }
2588 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002589};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002590
Elliott Hughes88c5c352012-03-15 18:49:48 -07002591const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002592 NULL, // reserved0.
2593 NULL, // reserved1.
2594 NULL, // reserved2.
2595 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002596 JNI::GetVersion,
2597 JNI::DefineClass,
2598 JNI::FindClass,
2599 JNI::FromReflectedMethod,
2600 JNI::FromReflectedField,
2601 JNI::ToReflectedMethod,
2602 JNI::GetSuperclass,
2603 JNI::IsAssignableFrom,
2604 JNI::ToReflectedField,
2605 JNI::Throw,
2606 JNI::ThrowNew,
2607 JNI::ExceptionOccurred,
2608 JNI::ExceptionDescribe,
2609 JNI::ExceptionClear,
2610 JNI::FatalError,
2611 JNI::PushLocalFrame,
2612 JNI::PopLocalFrame,
2613 JNI::NewGlobalRef,
2614 JNI::DeleteGlobalRef,
2615 JNI::DeleteLocalRef,
2616 JNI::IsSameObject,
2617 JNI::NewLocalRef,
2618 JNI::EnsureLocalCapacity,
2619 JNI::AllocObject,
2620 JNI::NewObject,
2621 JNI::NewObjectV,
2622 JNI::NewObjectA,
2623 JNI::GetObjectClass,
2624 JNI::IsInstanceOf,
2625 JNI::GetMethodID,
2626 JNI::CallObjectMethod,
2627 JNI::CallObjectMethodV,
2628 JNI::CallObjectMethodA,
2629 JNI::CallBooleanMethod,
2630 JNI::CallBooleanMethodV,
2631 JNI::CallBooleanMethodA,
2632 JNI::CallByteMethod,
2633 JNI::CallByteMethodV,
2634 JNI::CallByteMethodA,
2635 JNI::CallCharMethod,
2636 JNI::CallCharMethodV,
2637 JNI::CallCharMethodA,
2638 JNI::CallShortMethod,
2639 JNI::CallShortMethodV,
2640 JNI::CallShortMethodA,
2641 JNI::CallIntMethod,
2642 JNI::CallIntMethodV,
2643 JNI::CallIntMethodA,
2644 JNI::CallLongMethod,
2645 JNI::CallLongMethodV,
2646 JNI::CallLongMethodA,
2647 JNI::CallFloatMethod,
2648 JNI::CallFloatMethodV,
2649 JNI::CallFloatMethodA,
2650 JNI::CallDoubleMethod,
2651 JNI::CallDoubleMethodV,
2652 JNI::CallDoubleMethodA,
2653 JNI::CallVoidMethod,
2654 JNI::CallVoidMethodV,
2655 JNI::CallVoidMethodA,
2656 JNI::CallNonvirtualObjectMethod,
2657 JNI::CallNonvirtualObjectMethodV,
2658 JNI::CallNonvirtualObjectMethodA,
2659 JNI::CallNonvirtualBooleanMethod,
2660 JNI::CallNonvirtualBooleanMethodV,
2661 JNI::CallNonvirtualBooleanMethodA,
2662 JNI::CallNonvirtualByteMethod,
2663 JNI::CallNonvirtualByteMethodV,
2664 JNI::CallNonvirtualByteMethodA,
2665 JNI::CallNonvirtualCharMethod,
2666 JNI::CallNonvirtualCharMethodV,
2667 JNI::CallNonvirtualCharMethodA,
2668 JNI::CallNonvirtualShortMethod,
2669 JNI::CallNonvirtualShortMethodV,
2670 JNI::CallNonvirtualShortMethodA,
2671 JNI::CallNonvirtualIntMethod,
2672 JNI::CallNonvirtualIntMethodV,
2673 JNI::CallNonvirtualIntMethodA,
2674 JNI::CallNonvirtualLongMethod,
2675 JNI::CallNonvirtualLongMethodV,
2676 JNI::CallNonvirtualLongMethodA,
2677 JNI::CallNonvirtualFloatMethod,
2678 JNI::CallNonvirtualFloatMethodV,
2679 JNI::CallNonvirtualFloatMethodA,
2680 JNI::CallNonvirtualDoubleMethod,
2681 JNI::CallNonvirtualDoubleMethodV,
2682 JNI::CallNonvirtualDoubleMethodA,
2683 JNI::CallNonvirtualVoidMethod,
2684 JNI::CallNonvirtualVoidMethodV,
2685 JNI::CallNonvirtualVoidMethodA,
2686 JNI::GetFieldID,
2687 JNI::GetObjectField,
2688 JNI::GetBooleanField,
2689 JNI::GetByteField,
2690 JNI::GetCharField,
2691 JNI::GetShortField,
2692 JNI::GetIntField,
2693 JNI::GetLongField,
2694 JNI::GetFloatField,
2695 JNI::GetDoubleField,
2696 JNI::SetObjectField,
2697 JNI::SetBooleanField,
2698 JNI::SetByteField,
2699 JNI::SetCharField,
2700 JNI::SetShortField,
2701 JNI::SetIntField,
2702 JNI::SetLongField,
2703 JNI::SetFloatField,
2704 JNI::SetDoubleField,
2705 JNI::GetStaticMethodID,
2706 JNI::CallStaticObjectMethod,
2707 JNI::CallStaticObjectMethodV,
2708 JNI::CallStaticObjectMethodA,
2709 JNI::CallStaticBooleanMethod,
2710 JNI::CallStaticBooleanMethodV,
2711 JNI::CallStaticBooleanMethodA,
2712 JNI::CallStaticByteMethod,
2713 JNI::CallStaticByteMethodV,
2714 JNI::CallStaticByteMethodA,
2715 JNI::CallStaticCharMethod,
2716 JNI::CallStaticCharMethodV,
2717 JNI::CallStaticCharMethodA,
2718 JNI::CallStaticShortMethod,
2719 JNI::CallStaticShortMethodV,
2720 JNI::CallStaticShortMethodA,
2721 JNI::CallStaticIntMethod,
2722 JNI::CallStaticIntMethodV,
2723 JNI::CallStaticIntMethodA,
2724 JNI::CallStaticLongMethod,
2725 JNI::CallStaticLongMethodV,
2726 JNI::CallStaticLongMethodA,
2727 JNI::CallStaticFloatMethod,
2728 JNI::CallStaticFloatMethodV,
2729 JNI::CallStaticFloatMethodA,
2730 JNI::CallStaticDoubleMethod,
2731 JNI::CallStaticDoubleMethodV,
2732 JNI::CallStaticDoubleMethodA,
2733 JNI::CallStaticVoidMethod,
2734 JNI::CallStaticVoidMethodV,
2735 JNI::CallStaticVoidMethodA,
2736 JNI::GetStaticFieldID,
2737 JNI::GetStaticObjectField,
2738 JNI::GetStaticBooleanField,
2739 JNI::GetStaticByteField,
2740 JNI::GetStaticCharField,
2741 JNI::GetStaticShortField,
2742 JNI::GetStaticIntField,
2743 JNI::GetStaticLongField,
2744 JNI::GetStaticFloatField,
2745 JNI::GetStaticDoubleField,
2746 JNI::SetStaticObjectField,
2747 JNI::SetStaticBooleanField,
2748 JNI::SetStaticByteField,
2749 JNI::SetStaticCharField,
2750 JNI::SetStaticShortField,
2751 JNI::SetStaticIntField,
2752 JNI::SetStaticLongField,
2753 JNI::SetStaticFloatField,
2754 JNI::SetStaticDoubleField,
2755 JNI::NewString,
2756 JNI::GetStringLength,
2757 JNI::GetStringChars,
2758 JNI::ReleaseStringChars,
2759 JNI::NewStringUTF,
2760 JNI::GetStringUTFLength,
2761 JNI::GetStringUTFChars,
2762 JNI::ReleaseStringUTFChars,
2763 JNI::GetArrayLength,
2764 JNI::NewObjectArray,
2765 JNI::GetObjectArrayElement,
2766 JNI::SetObjectArrayElement,
2767 JNI::NewBooleanArray,
2768 JNI::NewByteArray,
2769 JNI::NewCharArray,
2770 JNI::NewShortArray,
2771 JNI::NewIntArray,
2772 JNI::NewLongArray,
2773 JNI::NewFloatArray,
2774 JNI::NewDoubleArray,
2775 JNI::GetBooleanArrayElements,
2776 JNI::GetByteArrayElements,
2777 JNI::GetCharArrayElements,
2778 JNI::GetShortArrayElements,
2779 JNI::GetIntArrayElements,
2780 JNI::GetLongArrayElements,
2781 JNI::GetFloatArrayElements,
2782 JNI::GetDoubleArrayElements,
2783 JNI::ReleaseBooleanArrayElements,
2784 JNI::ReleaseByteArrayElements,
2785 JNI::ReleaseCharArrayElements,
2786 JNI::ReleaseShortArrayElements,
2787 JNI::ReleaseIntArrayElements,
2788 JNI::ReleaseLongArrayElements,
2789 JNI::ReleaseFloatArrayElements,
2790 JNI::ReleaseDoubleArrayElements,
2791 JNI::GetBooleanArrayRegion,
2792 JNI::GetByteArrayRegion,
2793 JNI::GetCharArrayRegion,
2794 JNI::GetShortArrayRegion,
2795 JNI::GetIntArrayRegion,
2796 JNI::GetLongArrayRegion,
2797 JNI::GetFloatArrayRegion,
2798 JNI::GetDoubleArrayRegion,
2799 JNI::SetBooleanArrayRegion,
2800 JNI::SetByteArrayRegion,
2801 JNI::SetCharArrayRegion,
2802 JNI::SetShortArrayRegion,
2803 JNI::SetIntArrayRegion,
2804 JNI::SetLongArrayRegion,
2805 JNI::SetFloatArrayRegion,
2806 JNI::SetDoubleArrayRegion,
2807 JNI::RegisterNatives,
2808 JNI::UnregisterNatives,
2809 JNI::MonitorEnter,
2810 JNI::MonitorExit,
2811 JNI::GetJavaVM,
2812 JNI::GetStringRegion,
2813 JNI::GetStringUTFRegion,
2814 JNI::GetPrimitiveArrayCritical,
2815 JNI::ReleasePrimitiveArrayCritical,
2816 JNI::GetStringCritical,
2817 JNI::ReleaseStringCritical,
2818 JNI::NewWeakGlobalRef,
2819 JNI::DeleteWeakGlobalRef,
2820 JNI::ExceptionCheck,
2821 JNI::NewDirectByteBuffer,
2822 JNI::GetDirectBufferAddress,
2823 JNI::GetDirectBufferCapacity,
2824 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002825};
2826
Elliott Hughes75770752011-08-24 17:52:38 -07002827JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002828 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002829 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002830 local_ref_cookie(IRT_FIRST_SEGMENT),
2831 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002832 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002833 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002834 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002835 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002836 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002837 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002838 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002839 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2840 // errors will ensue.
2841 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2842 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002843}
2844
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002845JNIEnvExt::~JNIEnvExt() {
2846}
2847
Elliott Hughes88c5c352012-03-15 18:49:48 -07002848void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2849 check_jni = enabled;
2850 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002851}
2852
Elliott Hughes73e66f72012-05-09 09:34:45 -07002853void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2854 locals.Dump(os);
2855 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002856}
2857
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002858void JNIEnvExt::PushFrame(int /*capacity*/) {
2859 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002860 stacked_local_ref_cookies.push_back(local_ref_cookie);
2861 local_ref_cookie = locals.GetSegmentState();
2862}
2863
2864void JNIEnvExt::PopFrame() {
2865 locals.SetSegmentState(local_ref_cookie);
2866 local_ref_cookie = stacked_local_ref_cookies.back();
2867 stacked_local_ref_cookies.pop_back();
2868}
2869
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002870Offset JNIEnvExt::SegmentStateOffset() {
2871 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2872 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2873}
2874
Carl Shapiroea4dca82011-08-01 13:45:38 -07002875// JNI Invocation interface.
2876
Brian Carlstrombddf9762013-05-14 11:35:37 -07002877extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002878 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002879 if (IsBadJniVersion(args->version)) {
2880 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002881 return JNI_EVERSION;
2882 }
2883 Runtime::Options options;
2884 for (int i = 0; i < args->nOptions; ++i) {
2885 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002886 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002887 }
2888 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002889 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002890 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002891 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002892 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002893 bool started = runtime->Start();
2894 if (!started) {
2895 delete Thread::Current()->GetJniEnv();
2896 delete runtime->GetJavaVM();
2897 LOG(WARNING) << "CreateJavaVM failed";
2898 return JNI_ERR;
2899 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002900 *p_env = Thread::Current()->GetJniEnv();
2901 *p_vm = runtime->GetJavaVM();
2902 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002903}
2904
Elliott Hughesf2682d52011-08-15 16:37:04 -07002905extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002906 Runtime* runtime = Runtime::Current();
2907 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002908 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002909 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002910 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002911 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002912 }
2913 return JNI_OK;
2914}
2915
2916// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002917extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002918 return JNI_ERR;
2919}
2920
Elliott Hughescdf53122011-08-19 15:46:09 -07002921class JII {
2922 public:
2923 static jint DestroyJavaVM(JavaVM* vm) {
2924 if (vm == NULL) {
2925 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002926 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002927 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2928 delete raw_vm->runtime;
2929 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002930 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002931
Elliott Hughescdf53122011-08-19 15:46:09 -07002932 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002933 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002934 }
2935
2936 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002937 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002938 }
2939
2940 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002941 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002942 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002943 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002944 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2945 Runtime* runtime = raw_vm->runtime;
2946 runtime->DetachCurrentThread();
2947 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002948 }
2949
2950 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002951 if (IsBadJniVersion(version)) {
2952 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002953 return JNI_EVERSION;
2954 }
2955 if (vm == NULL || env == NULL) {
2956 return JNI_ERR;
2957 }
2958 Thread* thread = Thread::Current();
2959 if (thread == NULL) {
2960 *env = NULL;
2961 return JNI_EDETACHED;
2962 }
2963 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002964 return JNI_OK;
2965 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002966};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002967
Elliott Hughes88c5c352012-03-15 18:49:48 -07002968const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002969 NULL, // reserved0
2970 NULL, // reserved1
2971 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002972 JII::DestroyJavaVM,
2973 JII::AttachCurrentThread,
2974 JII::DetachCurrentThread,
2975 JII::GetEnv,
2976 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002977};
2978
Elliott Hughesa0957642011-09-02 14:27:33 -07002979JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002980 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002981 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002982 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002983 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002984 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002985 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002986 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08002987 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002988 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002989 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002990 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002991 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002992 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002993 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002994 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002995 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002996 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002997 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002998 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002999}
3000
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003001JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003002 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003003}
3004
Elliott Hughes88c5c352012-03-15 18:49:48 -07003005void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3006 check_jni = enabled;
3007 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003008}
3009
Elliott Hughesae80b492012-04-24 10:43:17 -07003010void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3011 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3012 if (force_copy) {
3013 os << " (with forcecopy)";
3014 }
3015 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003016 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003017 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003018 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003019 os << "; pins=" << pin_table.Size();
3020 }
3021 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003022 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003023 os << "; globals=" << globals.Capacity();
3024 }
3025 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003026 ReaderMutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003027 if (weak_globals.Capacity() > 0) {
3028 os << " (plus " << weak_globals.Capacity() << " weak)";
3029 }
3030 }
3031 os << '\n';
3032
3033 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003034 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003035 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3036 }
3037}
3038
Elliott Hughes73e66f72012-05-09 09:34:45 -07003039void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003040 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003041 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003042 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003043 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003044 }
3045 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003046 ReaderMutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003047 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003048 }
3049 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003050 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003051 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003052 }
3053}
3054
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003055bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
3056 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07003057 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003058
3059 // See if we've already loaded this library. If we have, and the class loader
3060 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003061 // TODO: for better results we should canonicalize the pathname (or even compare
3062 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003063 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003064 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003065 {
3066 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003067 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003068 library = libraries->Get(path);
3069 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003070 if (library != NULL) {
3071 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07003072 // The library will be associated with class_loader. The JNI
3073 // spec says we can't load the same library into more than one
3074 // class loader.
3075 StringAppendF(&detail, "Shared library \"%s\" already opened by "
3076 "ClassLoader %p; can't open in ClassLoader %p",
3077 path.c_str(), library->GetClassLoader(), class_loader);
3078 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003079 return false;
3080 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003081 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
3082 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003083 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003084 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
3085 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003086 return false;
3087 }
3088 return true;
3089 }
3090
3091 // Open the shared library. Because we're using a full path, the system
3092 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3093 // resolve this library's dependencies though.)
3094
3095 // Failures here are expected when java.library.path has several entries
3096 // and we have to hunt for the lib.
3097
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003098 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3099 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3100 // dlopen) becomes zero from dlclose.
3101
Elliott Hughescdf53122011-08-19 15:46:09 -07003102 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003103 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003104 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
3105 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
3106 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003107
Elliott Hughes84b2f142012-09-27 09:16:28 -07003108 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003109
3110 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07003111 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003112 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003113 return false;
3114 }
3115
3116 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003117 // TODO: move the locking (and more of this logic) into Libraries.
3118 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003119 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003120 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003121 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003122 if (library == NULL) { // We won race to get libraries_lock
3123 library = new SharedLibrary(path, handle, class_loader);
3124 libraries->Put(path, library);
3125 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003126 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003127 }
3128 if (!created_library) {
3129 LOG(INFO) << "WOW: we lost a race to add shared library: "
3130 << "\"" << path << "\" ClassLoader=" << class_loader;
3131 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003132 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003133
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003134 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003135
Elliott Hughes79353722013-08-02 16:52:18 -07003136 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003137 void* sym = dlsym(handle, "JNI_OnLoad");
3138 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003139 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003140 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003141 } else {
3142 // Call JNI_OnLoad. We have to override the current class
3143 // loader, which will always be "null" since the stuff at the
3144 // top of the stack is around Runtime.loadLibrary(). (See
3145 // the comments in the JNI FindClass function.)
3146 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3147 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07003148 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07003149 self->SetClassLoaderOverride(class_loader);
3150
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003151 int version = 0;
3152 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003153 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003154 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003155 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07003156 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003157
Brian Carlstromaded5f72011-10-07 17:15:04 -07003158 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07003159
Elliott Hughes79353722013-08-02 16:52:18 -07003160 if (version == JNI_ERR) {
3161 StringAppendF(&detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
3162 } else if (IsBadJniVersion(version)) {
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003163 StringAppendF(&detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
3164 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003165 // It's unwise to call dlclose() here, but we can mark it
3166 // as bad and ensure that future load attempts will fail.
3167 // We don't know how far JNI_OnLoad got, so there could
3168 // be some partially-initialized stuff accessible through
3169 // newly-registered native method calls. We could try to
3170 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003171 } else {
3172 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003173 }
Elliott Hughes79353722013-08-02 16:52:18 -07003174 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003175 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003176 }
3177
Elliott Hughes79353722013-08-02 16:52:18 -07003178 library->SetResult(was_successful);
3179 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003180}
3181
Brian Carlstromea46f952013-07-30 01:26:50 -07003182void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003183 CHECK(m->IsNative());
3184
3185 Class* c = m->GetDeclaringClass();
3186
3187 // If this is a static method, it could be called before the class
3188 // has been initialized.
3189 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07003190 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003191 return NULL;
3192 }
3193 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003194 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003195 }
3196
Brian Carlstrom16192862011-09-12 17:50:06 -07003197 std::string detail;
3198 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003199 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003200 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003201 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003202 native_method = libraries->FindNativeMethod(m, detail);
3203 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003204 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07003205 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003206 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3207 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003208 }
3209 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003210}
3211
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003212void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003213 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003214 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003215 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003216 globals.VisitRoots(visitor, arg);
3217 }
3218 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003219 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003220 pin_table.VisitRoots(visitor, arg);
3221 }
3222 // The weak_globals table is visited by the GC itself (because it mutates the table).
3223}
3224
Elliott Hughesc8fece32013-01-02 11:27:23 -08003225void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003226 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003227 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
3228 if (c.get() == NULL) {
3229 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3230 }
3231 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3232}
3233
Ian Rogersdf20fe02011-07-20 20:34:16 -07003234} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003235
3236std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3237 switch (rhs) {
3238 case JNIInvalidRefType:
3239 os << "JNIInvalidRefType";
3240 return os;
3241 case JNILocalRefType:
3242 os << "JNILocalRefType";
3243 return os;
3244 case JNIGlobalRefType:
3245 os << "JNIGlobalRefType";
3246 return os;
3247 case JNIWeakGlobalRefType:
3248 os << "JNIWeakGlobalRefType";
3249 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003250 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003251 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003252 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003253 }
3254}