blob: 60fad6ee481efb608f39a27043a7ba3c78492268 [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_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070089 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070090}
91
Jeff Hao19c5d372013-03-15 14:33:43 -070092static bool IsBadJniVersion(int version) {
93 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
94 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
95}
96
Brian Carlstromea46f952013-07-30 01:26:50 -070097static void CheckMethodArguments(ArtMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -070099 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -0700100 const DexFile::TypeList* params = mh.GetParameterTypeList();
101 if (params == NULL) {
102 return; // No arguments so nothing to check.
103 }
Jeff Hao5d917302013-02-27 17:57:33 -0800104 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700105 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700106 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -0800107 if (!m->IsStatic()) {
108 offset = 1;
109 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700110 for (uint32_t i = 0; i < num_params; i++) {
111 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
112 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
113 if (param_type == NULL) {
114 Thread* self = Thread::Current();
115 CHECK(self->IsExceptionPending());
116 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
117 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800118 << self->GetException(NULL)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700119 self->ClearException();
120 ++error_count;
121 } else if (!param_type->IsPrimitive()) {
122 // TODO: check primitives are in range.
Jeff Hao5d917302013-02-27 17:57:33 -0800123 Object* argument = reinterpret_cast<Object*>(args[i + offset]);
Ian Rogers50b35e22012-10-04 10:09:15 -0700124 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700125 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
126 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
127 ++error_count;
128 }
Jeff Hao5d917302013-02-27 17:57:33 -0800129 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
130 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700131 }
132 }
133 if (error_count > 0) {
134 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
135 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700136 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700137 }
138}
Elliott Hughesb264f082012-04-06 17:10:10 -0700139
Brian Carlstromea46f952013-07-30 01:26:50 -0700140void InvokeWithArgArray(const ScopedObjectAccess& soa, ArtMethod* method,
Jeff Hao6474d192013-03-26 14:08:09 -0700141 ArgArray* arg_array, JValue* result, char result_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700143 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700145 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700146 }
Sebastien Hertz7d658cf2013-07-09 10:56:11 +0200147 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700148}
149
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
151 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700153 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700154 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700155 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800156 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700157 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800158 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700159 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
160 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700161}
162
Brian Carlstromea46f952013-07-30 01:26:50 -0700163static ArtMethod* FindVirtualMethod(Object* receiver, ArtMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700165 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700166}
167
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
169 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700171 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700172 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700173 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800174 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700175 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800176 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700177 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
178 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700179}
180
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700181static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
182 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700184 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700185 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700186 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800187 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700188 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800189 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700190 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
191 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700192}
193
Elliott Hughes6b436852011-08-12 10:16:44 -0700194// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
195// separated with slashes but aren't wrapped with "L;" like regular descriptors
196// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
197// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
198// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700199static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700200 std::string result;
201 // Add the missing "L;" if necessary.
202 if (name[0] == '[') {
203 result = name;
204 } else {
205 result += 'L';
206 result += name;
207 result += ';';
208 }
209 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700210 if (result.find('.') != std::string::npos) {
211 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
212 << "\"" << name << "\"";
213 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700214 }
215 return result;
216}
217
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
219 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700220 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800221 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
222 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
223 "no %s method \"%s.%s%s\"",
224 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700225}
226
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
228 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700230 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700231 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700232 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700233 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700234
Brian Carlstromea46f952013-07-30 01:26:50 -0700235 ArtMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700236 if (is_static) {
237 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700238 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700239 method = c->FindVirtualMethod(name, sig);
240 if (method == NULL) {
241 // No virtual method matching the signature. Search declared
242 // private methods and constructors.
243 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700244 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700245 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700246
Elliott Hughescdf53122011-08-19 15:46:09 -0700247 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700249 return NULL;
250 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700251
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700253}
254
Ian Rogersef28b142012-11-30 14:22:18 -0800255static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700256 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700257 ArtMethod* method = soa.Self()->GetCurrentMethod(NULL);
Ian Rogersef28b142012-11-30 14:22:18 -0800258 if (method == NULL ||
259 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
260 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700261 }
262 return method->GetDeclaringClass()->GetClassLoader();
263}
264
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
266 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700267 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700269 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700270 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700271 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700272
Brian Carlstromea46f952013-07-30 01:26:50 -0700273 ArtField* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700274 Class* field_type;
275 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
276 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800277 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700278 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700279 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700280 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700281 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700282 if (field_type == NULL) {
283 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800285 ThrowLocation throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700286 SirtRef<Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800288 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
289 "no type \"%s\" found and so no field \"%s\" could be found in class "
290 "\"%s\" or its superclasses", sig, name,
291 ClassHelper(c).GetDescriptor());
292 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700293 return NULL;
294 }
295 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800296 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700297 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700299 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700300 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800301 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
302 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
303 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
304 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700305 return NULL;
306 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700308}
309
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700310static void PinPrimitiveArray(const ScopedObjectAccess& soa, Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700311 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700312 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700313 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700314 vm->pin_table.Add(array);
315}
316
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700317static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, 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.Remove(array);
322}
323
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
325 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700326 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700327 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800328 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
329 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
330 "%s offset=%d length=%d %s.length=%d",
331 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700332}
Ian Rogers0571d352011-11-03 19:51:38 -0700333
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700334static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
335 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800337 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
338 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
339 "offset=%d length=%d string.length()=%d", start, length,
340 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700341}
Elliott Hughes814e4032011-08-23 12:07:56 -0700342
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700344 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 // Turn the const char* into a java.lang.String.
346 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
347 if (msg != NULL && s.get() == NULL) {
348 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700349 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700350
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700351 // Choose an appropriate constructor and set up the arguments.
352 jvalue args[2];
353 const char* signature;
354 if (msg == NULL && cause == NULL) {
355 signature = "()V";
356 } else if (msg != NULL && cause == NULL) {
357 signature = "(Ljava/lang/String;)V";
358 args[0].l = s.get();
359 } else if (msg == NULL && cause != NULL) {
360 signature = "(Ljava/lang/Throwable;)V";
361 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700362 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700363 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
364 args[0].l = s.get();
365 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700366 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700367 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
368 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800369 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 LOG(ERROR) << "No <init>" << signature << " in "
371 << PrettyClass(soa.Decode<Class*>(exception_class));
372 return JNI_ERR;
373 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700374
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700375 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
376 if (exception.get() == NULL) {
377 return JNI_ERR;
378 }
Ian Rogersef28b142012-11-30 14:22:18 -0800379 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800380 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
381 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700383}
384
Elliott Hughes462c9442012-03-23 18:47:50 -0700385static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700386 if (vm == NULL || p_env == NULL) {
387 return JNI_ERR;
388 }
389
Elliott Hughes462c9442012-03-23 18:47:50 -0700390 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700391 Thread* self = Thread::Current();
392 if (self != NULL) {
393 *p_env = self->GetJniEnv();
394 return JNI_OK;
395 }
396
397 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
398
399 // No threads allowed in zygote mode.
400 if (runtime->IsZygote()) {
401 LOG(ERROR) << "Attempt to attach a thread in the zygote";
402 return JNI_ERR;
403 }
404
Elliott Hughes462c9442012-03-23 18:47:50 -0700405 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
406 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700407 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700408 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700409 if (IsBadJniVersion(args->version)) {
410 LOG(ERROR) << "Bad JNI version passed to "
411 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
412 << args->version;
413 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700414 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700415 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700416 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700417 }
Elliott Hughes75770752011-08-24 17:52:38 -0700418
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800419 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700420 *p_env = NULL;
421 return JNI_ERR;
422 } else {
423 *p_env = Thread::Current()->GetJniEnv();
424 return JNI_OK;
425 }
Elliott Hughes75770752011-08-24 17:52:38 -0700426}
427
Elliott Hughes79082e32011-08-25 12:07:32 -0700428class SharedLibrary {
429 public:
430 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
431 : path_(path),
432 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700433 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700434 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700435 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700436 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700437 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700438 }
439
Elliott Hughes79082e32011-08-25 12:07:32 -0700440 Object* GetClassLoader() {
441 return class_loader_;
442 }
443
444 std::string GetPath() {
445 return path_;
446 }
447
448 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700449 * Check the result of an earlier call to JNI_OnLoad on this library.
450 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700451 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 bool CheckOnLoadResult()
453 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700454 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700455 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700456 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
457 bool okay;
458 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700459 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700460
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700461 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
462 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
463 // caller can continue.
464 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
465 okay = true;
466 } else {
467 while (jni_on_load_result_ == kPending) {
468 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700469 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700470 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700471
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700472 okay = (jni_on_load_result_ == kOkay);
473 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
474 << (okay ? "succeeded" : "failed") << "]";
475 }
476 }
477 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700478 return okay;
479 }
480
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700481 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700482 Thread* self = Thread::Current();
483 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700484
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700486 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700487
488 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700489 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700490 }
491
492 void* FindSymbol(const std::string& symbol_name) {
493 return dlsym(handle_, symbol_name.c_str());
494 }
495
496 private:
497 enum JNI_OnLoadState {
498 kPending,
499 kFailed,
500 kOkay,
501 };
502
503 // Path to library "/system/lib/libjni.so".
504 std::string path_;
505
506 // The void* returned by dlopen(3).
507 void* handle_;
508
509 // The ClassLoader this library is associated with.
510 Object* class_loader_;
511
512 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700513 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700515 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700516 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700517 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700518 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700519 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700520};
521
Elliott Hughes79082e32011-08-25 12:07:32 -0700522// This exists mainly to keep implementation details out of the header file.
523class Libraries {
524 public:
525 Libraries() {
526 }
527
528 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700529 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700530 }
531
Elliott Hughesae80b492012-04-24 10:43:17 -0700532 void Dump(std::ostream& os) const {
533 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700534 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700535 if (!first) {
536 os << ' ';
537 }
538 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700539 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700540 }
541 }
542
543 size_t size() const {
544 return libraries_.size();
545 }
546
Elliott Hughes79082e32011-08-25 12:07:32 -0700547 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700548 auto it = libraries_.find(path);
Ian Rogers712462a2012-04-12 16:32:29 -0700549 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700550 }
551
552 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700553 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 }
555
556 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstromea46f952013-07-30 01:26:50 -0700557 void* FindNativeMethod(const ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700558 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700559 std::string jni_short_name(JniShortName(m));
560 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700561 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700562 for (const auto& lib : libraries_) {
563 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700564 if (library->GetClassLoader() != declaring_class_loader) {
565 // We only search libraries loaded by the appropriate ClassLoader.
566 continue;
567 }
568 // Try the short name then the long name...
569 void* fn = library->FindSymbol(jni_short_name);
570 if (fn == NULL) {
571 fn = library->FindSymbol(jni_long_name);
572 }
573 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800574 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
575 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700576 return fn;
577 }
578 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700579 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700580 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700581 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700582 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 return NULL;
584 }
585
586 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700587 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700588};
589
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700590JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
591 jvalue* args) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700592 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700593 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700594 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800595 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700596 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800597 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700598 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
599 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800600}
601
Ian Rogersbc939662013-08-15 10:26:54 -0700602#define CHECK_NON_NULL_ARGUMENT(fn, value) \
603 if (UNLIKELY(value == NULL)) { \
604 JniAbortF(#fn, #value " == null"); \
605 }
606
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700607#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
608 if (UNLIKELY(length != 0 && value == NULL)) { \
609 JniAbortF(#fn, #value " == null"); \
610 }
611
Elliott Hughescdf53122011-08-19 15:46:09 -0700612class JNI {
613 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700614 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700615 return JNI_VERSION_1_6;
616 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700617
Ian Rogers25e8b912012-09-07 11:31:36 -0700618 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700619 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700620 return NULL;
621 }
622
Elliott Hughescdf53122011-08-19 15:46:09 -0700623 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700624 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700625 Runtime* runtime = Runtime::Current();
626 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700627 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700628 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700629 Class* c = NULL;
630 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800631 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800632 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700633 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800634 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700635 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700637 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700638
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700640 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700641 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700642 jobject art_method = env->GetObjectField(java_method,
643 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
644 ArtMethod* method = soa.Decode<ArtMethod*>(art_method);
645 DCHECK(method != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700646 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700647 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700648
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700650 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700651 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700652 jobject art_field = env->GetObjectField(java_field,
653 WellKnownClasses::java_lang_reflect_Field_artField);
654 ArtField* field = soa.Decode<ArtField*>(art_field);
655 DCHECK(field != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700656 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700658
Elliott Hughescdf53122011-08-19 15:46:09 -0700659 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700660 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700662 ArtMethod* m = soa.DecodeMethod(mid);
663 jobject art_method = soa.AddLocalReference<jobject>(m);
664 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
665 if (env->ExceptionCheck()) {
666 return NULL;
667 }
668 SetObjectField(env,
669 reflect_method,
670 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod,
671 art_method);
672 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700674
Elliott Hughescdf53122011-08-19 15:46:09 -0700675 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700676 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700677 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700678 ArtField* f = soa.DecodeField(fid);
679 jobject art_field = soa.AddLocalReference<jobject>(f);
680 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
681 if (env->ExceptionCheck()) {
682 return NULL;
683 }
684 SetObjectField(env,
685 reflect_field,
686 WellKnownClasses::java_lang_reflect_Field_artField,
687 art_field);
688 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700690
Elliott Hughes37f7a402011-08-22 18:56:01 -0700691 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700692 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700693 ScopedObjectAccess soa(env);
694 Object* o = soa.Decode<Object*>(java_object);
695 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700696 }
697
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700698 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700699 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 ScopedObjectAccess soa(env);
701 Class* c = soa.Decode<Class*>(java_class);
702 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700704
Elliott Hughes37f7a402011-08-22 18:56:01 -0700705 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700706 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
707 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708 ScopedObjectAccess soa(env);
709 Class* c1 = soa.Decode<Class*>(java_class1);
710 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700711 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700712 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700713
Elliott Hughese84278b2012-03-22 10:06:53 -0700714 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700715 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700716 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700717 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700718 return JNI_TRUE;
719 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700720 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721 Object* obj = soa.Decode<Object*>(jobj);
722 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700723 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700724 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700725 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700726
Elliott Hughes37f7a402011-08-22 18:56:01 -0700727 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728 ScopedObjectAccess soa(env);
729 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700730 if (exception == NULL) {
731 return JNI_ERR;
732 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800733 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
734 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700735 return JNI_OK;
736 }
737
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700738 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700739 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Elliott Hughesa4f94742012-05-29 16:28:38 -0700740 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700741 }
742
743 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700744 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700745 }
746
747 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700748 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700749 }
750
751 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700752 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700753
Brian Carlstromea46f952013-07-30 01:26:50 -0700754 SirtRef<Object> old_throw_this_object(soa.Self(), NULL);
755 SirtRef<ArtMethod> old_throw_method(soa.Self(), NULL);
756 SirtRef<Throwable> old_exception(soa.Self(), NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800757 uint32_t old_throw_dex_pc;
758 {
759 ThrowLocation old_throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700760 Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800761 old_throw_this_object.reset(old_throw_location.GetThis());
762 old_throw_method.reset(old_throw_location.GetMethod());
763 old_exception.reset(old_exception_obj);
764 old_throw_dex_pc = old_throw_location.GetDexPc();
765 soa.Self()->ClearException();
766 }
767 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700768 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
769 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
770 if (mid == NULL) {
771 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800772 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700773 } else {
774 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800775 if (soa.Self()->IsExceptionPending()) {
776 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700777 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800778 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700779 }
780 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800781 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
782 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700783
Ian Rogers62d6c772013-02-27 08:32:07 -0800784 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700786
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700788 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800789 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700790 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 }
792
Ian Rogers25e8b912012-09-07 11:31:36 -0700793 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700794 LOG(FATAL) << "JNI FatalError called: " << msg;
795 }
796
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700797 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800798 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700799 return JNI_ERR;
800 }
Ian Rogersef28b142012-11-30 14:22:18 -0800801 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700802 return JNI_OK;
803 }
804
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700805 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700806 ScopedObjectAccess soa(env);
807 Object* survivor = soa.Decode<Object*>(java_survivor);
808 soa.Env()->PopFrame();
809 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 }
811
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700812 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800813 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700814 }
815
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 if (obj == NULL) {
818 return NULL;
819 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700820 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700821 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700823 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogersb8a0b942013-08-20 18:09:52 -0700824 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700825 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700826 return reinterpret_cast<jobject>(ref);
827 }
828
829 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 if (obj == NULL) {
831 return;
832 }
Ian Rogersef28b142012-11-30 14:22:18 -0800833 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800835 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700836 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700837
838 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
839 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
840 << "failed to find entry";
841 }
842 }
843
844 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 ScopedObjectAccess soa(env);
846 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 }
848
849 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700850 if (obj != nullptr) {
851 ScopedObjectAccess soa(env);
852 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700853 }
854 }
855
856 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 if (obj == NULL) {
858 return NULL;
859 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700860 ScopedObjectAccess soa(env);
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700861 return soa.AddLocalReference<jobject>(soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 }
863
864 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 if (obj == NULL) {
866 return;
867 }
Ian Rogersef28b142012-11-30 14:22:18 -0800868 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700869
Ian Rogersef28b142012-11-30 14:22:18 -0800870 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700871 if (!locals.Remove(cookie, obj)) {
872 // Attempting to delete a local reference that is not in the
873 // topmost local reference frame is a no-op. DeleteLocalRef returns
874 // void and doesn't throw any exceptions, but we should probably
875 // complain about it so the user will notice that things aren't
876 // going quite the way they expect.
877 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
878 << "failed to find entry";
879 }
880 }
881
882 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700883 if (obj1 == obj2) {
884 return JNI_TRUE;
885 } else {
886 ScopedObjectAccess soa(env);
887 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
888 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 }
890
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700891 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700892 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700893 ScopedObjectAccess soa(env);
894 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700895 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700896 return NULL;
897 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700898 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 }
900
Ian Rogersbc939662013-08-15 10:26:54 -0700901 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700903 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700904 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
905 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
906 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 va_end(args);
908 return result;
909 }
910
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700912 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
913 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700914 ScopedObjectAccess soa(env);
915 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700916 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700917 return NULL;
918 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700919 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700920 if (result == NULL) {
921 return NULL;
922 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700923 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700925 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700926 return local_result;
927 } else {
928 return NULL;
929 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700933 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
934 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935 ScopedObjectAccess soa(env);
936 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700937 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700938 return NULL;
939 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700940 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700941 if (result == NULL) {
942 return NULL;
943 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700946 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700947 return local_result;
948 } else {
949 return NULL;
950 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 }
952
Ian Rogersbc939662013-08-15 10:26:54 -0700953 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
954 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
955 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
956 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700957 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700958 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700959 }
960
Ian Rogersbc939662013-08-15 10:26:54 -0700961 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
962 const char* sig) {
963 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
964 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
965 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700966 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700967 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 va_list ap;
972 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700973 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
974 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700975 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700976 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700978 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700982 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
983 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700984 ScopedObjectAccess soa(env);
985 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
986 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700990 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
991 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700992 ScopedObjectAccess soa(env);
993 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
994 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 va_list ap;
999 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001000 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1001 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001002 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001005 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001009 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1010 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 ScopedObjectAccess soa(env);
1012 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001016 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1017 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001018 ScopedObjectAccess soa(env);
1019 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 va_list ap;
1024 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001025 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1026 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1027 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001028 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001030 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001034 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1035 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001036 ScopedObjectAccess soa(env);
1037 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001041 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1042 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 ScopedObjectAccess soa(env);
1044 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 va_list ap;
1049 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001050 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1051 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001052 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001053 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001055 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001059 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1060 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001061 ScopedObjectAccess soa(env);
1062 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 }
1064
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001066 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1067 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 ScopedObjectAccess soa(env);
1069 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 va_list ap;
1074 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001075 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1076 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001077 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001078 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001080 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001084 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1085 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 ScopedObjectAccess soa(env);
1087 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001091 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1092 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093 ScopedObjectAccess soa(env);
1094 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 va_list ap;
1099 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001100 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1101 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1102 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001105 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001109 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1110 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001111 ScopedObjectAccess soa(env);
1112 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001116 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1117 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118 ScopedObjectAccess soa(env);
1119 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_list ap;
1124 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001125 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1126 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001127 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001128 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001130 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001134 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1135 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001136 ScopedObjectAccess soa(env);
1137 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001141 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1142 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001143 ScopedObjectAccess soa(env);
1144 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_list ap;
1149 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001150 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1151 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001152 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001153 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001155 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001159 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1160 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161 ScopedObjectAccess soa(env);
1162 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001166 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1167 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001168 ScopedObjectAccess soa(env);
1169 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 }
1171
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_list ap;
1174 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001175 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1176 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001177 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001178 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001180 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 }
1182
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001184 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1185 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186 ScopedObjectAccess soa(env);
1187 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001191 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1192 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001193 ScopedObjectAccess soa(env);
1194 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 va_list ap;
1199 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001200 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1201 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001202 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001203 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001208 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1209 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001210 ScopedObjectAccess soa(env);
1211 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001215 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1216 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 ScopedObjectAccess soa(env);
1218 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001221 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001224 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1225 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001226 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001227 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1228 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 va_end(ap);
1230 return local_result;
1231 }
1232
Ian Rogersbc939662013-08-15 10:26:54 -07001233 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1234 va_list args) {
1235 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1236 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001237 ScopedObjectAccess soa(env);
1238 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1239 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
Ian Rogersbc939662013-08-15 10:26:54 -07001242 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1243 jvalue* args) {
1244 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1245 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 ScopedObjectAccess soa(env);
1247 JValue result(InvokeWithJValues(soa, obj, mid, args));
1248 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 }
1250
Ian Rogersbc939662013-08-15 10:26:54 -07001251 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1252 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001255 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1256 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001257 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001258 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001260 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 }
1262
Ian Rogersbc939662013-08-15 10:26:54 -07001263 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1264 va_list args) {
1265 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1266 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001267 ScopedObjectAccess soa(env);
1268 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
Ian Rogersbc939662013-08-15 10:26:54 -07001271 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1272 jvalue* args) {
1273 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1274 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001275 ScopedObjectAccess soa(env);
1276 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 }
1278
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001279 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001282 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1283 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001284 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001285 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001287 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Ian Rogersbc939662013-08-15 10:26:54 -07001290 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1291 va_list args) {
1292 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1293 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001294 ScopedObjectAccess soa(env);
1295 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 }
1297
Ian Rogersbc939662013-08-15 10:26:54 -07001298 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1299 jvalue* args) {
1300 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1301 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001302 ScopedObjectAccess soa(env);
1303 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001306 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001308 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001309 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1310 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1311 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001312 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001314 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
Ian Rogersbc939662013-08-15 10:26:54 -07001317 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1318 va_list args) {
1319 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1320 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001321 ScopedObjectAccess soa(env);
1322 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
Ian Rogersbc939662013-08-15 10:26:54 -07001325 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1326 jvalue* args) {
1327 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1328 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001329 ScopedObjectAccess soa(env);
1330 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001333 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001336 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1337 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001338 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001339 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001341 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
Ian Rogersbc939662013-08-15 10:26:54 -07001344 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1345 va_list args) {
1346 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1347 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001348 ScopedObjectAccess soa(env);
1349 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
Ian Rogersbc939662013-08-15 10:26:54 -07001352 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1353 jvalue* args) {
1354 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1355 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001356 ScopedObjectAccess soa(env);
1357 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 }
1359
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001360 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001363 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1364 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001365 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001366 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001368 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
Ian Rogersbc939662013-08-15 10:26:54 -07001371 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1372 va_list args) {
1373 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1374 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001375 ScopedObjectAccess soa(env);
1376 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
Ian Rogersbc939662013-08-15 10:26:54 -07001379 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1380 jvalue* args) {
1381 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1382 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001383 ScopedObjectAccess soa(env);
1384 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
1386
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001387 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001389 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001390 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1391 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001392 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001393 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001395 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
Ian Rogersbc939662013-08-15 10:26:54 -07001398 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1399 va_list args) {
1400 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1401 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001402 ScopedObjectAccess soa(env);
1403 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Ian Rogersbc939662013-08-15 10:26:54 -07001406 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1407 jvalue* args) {
1408 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1409 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001410 ScopedObjectAccess soa(env);
1411 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001414 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001416 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001417 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1418 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001419 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001420 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001422 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
Ian Rogersbc939662013-08-15 10:26:54 -07001425 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1426 va_list args) {
1427 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1428 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001429 ScopedObjectAccess soa(env);
1430 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
1432
Ian Rogersbc939662013-08-15 10:26:54 -07001433 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1434 jvalue* args) {
1435 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1436 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001437 ScopedObjectAccess soa(env);
1438 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 }
1440
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001441 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001443 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001444 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1445 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001446 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001447 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001449 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Ian Rogersbc939662013-08-15 10:26:54 -07001452 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1453 va_list args) {
1454 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1455 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001456 ScopedObjectAccess soa(env);
1457 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Ian Rogersbc939662013-08-15 10:26:54 -07001460 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1461 jvalue* args) {
1462 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1463 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001464 ScopedObjectAccess soa(env);
1465 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001468 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001470 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001471 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1472 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001473 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001474 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 va_end(ap);
1476 }
1477
Brian Carlstromea46f952013-07-30 01:26:50 -07001478 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1479 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001480 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1481 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001482 ScopedObjectAccess soa(env);
1483 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Ian Rogersbc939662013-08-15 10:26:54 -07001486 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1487 jvalue* args) {
1488 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1489 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001490 ScopedObjectAccess soa(env);
1491 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 }
1493
Ian Rogersbc939662013-08-15 10:26:54 -07001494 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1495 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1496 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1497 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001498 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001499 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001501
Ian Rogersbc939662013-08-15 10:26:54 -07001502 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1503 const char* sig) {
1504 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1505 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1506 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001507 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001508 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001510
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001512 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1513 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001514 ScopedObjectAccess soa(env);
1515 Object* o = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -07001516 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001517 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001519
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001521 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001522 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -07001523 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001524 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
1526
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001527 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001528 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1529 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001530 ScopedObjectAccess soa(env);
1531 Object* o = soa.Decode<Object*>(java_object);
1532 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001533 ArtField* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001534 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
1536
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001537 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001538 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001539 ScopedObjectAccess soa(env);
1540 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001541 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001542 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001546 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1547 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001548 ScopedObjectAccess soa(env); \
1549 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001550 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001551 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001552
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001553#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001554 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001555 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001556 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001557 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001558
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001559#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001560 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1561 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001562 ScopedObjectAccess soa(env); \
1563 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001564 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001565 f->Set ##fn(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001566
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001567#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001568 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001569 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001570 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001571 f->Set ##fn(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001572
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001573 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001574 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001577 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001578 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 }
1580
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001581 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001582 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001585 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001586 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001589 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001590 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001593 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001594 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001597 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001598 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001601 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001602 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001605 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001606 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001609 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001610 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001613 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001614 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 }
1616
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001617 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001618 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001621 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001622 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001626 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001627 }
1628
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001629 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001630 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001631 }
1632
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001633 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001634 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001635 }
1636
1637 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001638 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001639 }
1640
1641 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001642 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001643 }
1644
1645 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001646 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001647 }
1648
1649 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001650 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001651 }
1652
1653 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001654 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001655 }
1656
1657 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001658 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001659 }
1660
1661 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001662 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001663 }
1664
1665 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001666 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001667 }
1668
1669 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001670 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001671 }
1672
1673 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001674 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001675 }
1676
1677 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001678 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001679 }
1680
1681 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001682 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001683 }
1684
1685 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001686 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001687 }
1688
1689 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001690 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001691 }
1692
1693 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001694 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001695 }
1696
1697 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001698 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 }
1700
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001701 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001704 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001705 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001706 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1707 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 va_end(ap);
1709 return local_result;
1710 }
1711
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001712 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001713 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001714 ScopedObjectAccess soa(env);
1715 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1716 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001719 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001720 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001721 ScopedObjectAccess soa(env);
1722 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1723 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001726 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001728 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001729 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001730 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001731 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001733 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001736 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001737 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001738 ScopedObjectAccess soa(env);
1739 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 }
1741
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001742 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001743 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001744 ScopedObjectAccess soa(env);
1745 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 }
1747
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001748 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001750 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001751 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001752 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001753 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001755 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 }
1757
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001758 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001759 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001760 ScopedObjectAccess soa(env);
1761 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 }
1763
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001764 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001765 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001766 ScopedObjectAccess soa(env);
1767 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 }
1769
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001770 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001772 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001773 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001774 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001775 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001777 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 }
1779
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001780 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001781 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001782 ScopedObjectAccess soa(env);
1783 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 }
1785
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001786 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001787 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001788 ScopedObjectAccess soa(env);
1789 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 }
1791
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001792 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001794 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001795 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001796 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001797 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001799 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001802 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001803 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001804 ScopedObjectAccess soa(env);
1805 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 }
1807
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001808 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001809 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001810 ScopedObjectAccess soa(env);
1811 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001814 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001816 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001817 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001818 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001819 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001821 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 }
1823
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001824 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001825 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001826 ScopedObjectAccess soa(env);
1827 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001830 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001831 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001832 ScopedObjectAccess soa(env);
1833 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 }
1835
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001836 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001838 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001839 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001840 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001841 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001843 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 }
1845
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001846 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001847 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001848 ScopedObjectAccess soa(env);
1849 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 }
1851
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001852 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001853 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001854 ScopedObjectAccess soa(env);
1855 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001856 }
1857
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001858 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001860 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001861 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001862 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001863 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001865 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 }
1867
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001868 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001869 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001870 ScopedObjectAccess soa(env);
1871 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 }
1873
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001874 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001875 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001876 ScopedObjectAccess soa(env);
1877 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 }
1879
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001880 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001881 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001882 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001883 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001884 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001885 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001886 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001887 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 }
1889
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001890 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001891 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001892 ScopedObjectAccess soa(env);
1893 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001894 }
1895
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001896 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001897 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001898 ScopedObjectAccess soa(env);
1899 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001900 }
1901
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001902 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001903 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001904 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001905 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001906 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001907 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001908 va_end(ap);
1909 }
1910
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001911 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001912 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001913 ScopedObjectAccess soa(env);
1914 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001915 }
1916
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001917 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001918 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001919 ScopedObjectAccess soa(env);
1920 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 }
1922
Elliott Hughes814e4032011-08-23 12:07:56 -07001923 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogersbc939662013-08-15 10:26:54 -07001924 if (UNLIKELY(chars == NULL && char_count > 0)) { \
1925 JniAbortF("NewString", "char == null && char_count > 0"); \
1926 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001927 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001928 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001929 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001930 }
1931
1932 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 if (utf == NULL) {
1934 return NULL;
1935 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001936 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001937 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001938 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Elliott Hughes814e4032011-08-23 12:07:56 -07001941 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001942 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001943 ScopedObjectAccess soa(env);
1944 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001945 }
1946
1947 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001948 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001949 ScopedObjectAccess soa(env);
1950 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001951 }
1952
Ian Rogersbc939662013-08-15 10:26:54 -07001953 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1954 jchar* buf) {
1955 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001956 ScopedObjectAccess soa(env);
1957 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001958 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001959 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001960 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001961 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001962 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1963 memcpy(buf, chars + start, length * sizeof(jchar));
1964 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001965 }
1966
Ian Rogersbc939662013-08-15 10:26:54 -07001967 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1968 char* buf) {
1969 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001970 ScopedObjectAccess soa(env);
1971 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001972 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001973 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001974 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001975 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001976 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1977 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1978 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001979 }
1980
Elliott Hughes75770752011-08-24 17:52:38 -07001981 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07001982 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001983 ScopedObjectAccess soa(env);
1984 String* s = soa.Decode<String*>(java_string);
Mathieu Chartier423d2a32013-09-12 17:33:56 -07001985 CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001986 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001987 if (is_copy != NULL) {
1988 *is_copy = JNI_FALSE;
1989 }
1990 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001991 }
1992
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001993 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogersbc939662013-08-15 10:26:54 -07001994 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001995 ScopedObjectAccess soa(env);
1996 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Elliott Hughes75770752011-08-24 17:52:38 -07001999 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002000 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes75770752011-08-24 17:52:38 -07002003 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002004 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 }
2006
Elliott Hughes75770752011-08-24 17:52:38 -07002007 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002008 if (java_string == NULL) {
2009 return NULL;
2010 }
2011 if (is_copy != NULL) {
2012 *is_copy = JNI_TRUE;
2013 }
Ian Rogersef28b142012-11-30 14:22:18 -08002014 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002015 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002016 size_t byte_count = s->GetUtfLength();
2017 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002018 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002019 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2020 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2021 bytes[byte_count] = '\0';
2022 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002023 }
2024
Elliott Hughes75770752011-08-24 17:52:38 -07002025 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002026 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002027 }
2028
Elliott Hughesbd935992011-08-22 11:59:34 -07002029 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002030 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002031 ScopedObjectAccess soa(env);
2032 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002033 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002034 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2035 }
Elliott Hughesbd935992011-08-22 11:59:34 -07002036 Array* array = obj->AsArray();
2037 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes814e4032011-08-23 12:07:56 -07002040 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002041 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002042 ScopedObjectAccess soa(env);
2043 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2044 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Ian Rogersbc939662013-08-15 10:26:54 -07002047 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2048 jobject java_value) {
2049 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002050 ScopedObjectAccess soa(env);
2051 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2052 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 array->Set(index, value);
2054 }
2055
2056 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002057 ScopedObjectAccess soa(env);
2058 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
2061 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002062 ScopedObjectAccess soa(env);
2063 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
2066 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002067 ScopedObjectAccess soa(env);
2068 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
2071 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002072 ScopedObjectAccess soa(env);
2073 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
2076 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002077 ScopedObjectAccess soa(env);
2078 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
2081 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002082 ScopedObjectAccess soa(env);
2083 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
2086 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002087 ScopedObjectAccess soa(env);
2088 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
2091 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002092 if (length < 0) {
2093 JniAbortF("NewObjectArray", "negative array length: %d", length);
2094 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002095
2096 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002097 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002098 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 std::string descriptor;
2100 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002101 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002102
2103 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002104 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2105 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 return NULL;
2107 }
2108
Elliott Hughes75770752011-08-24 17:52:38 -07002109 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002110 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07002111 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002112 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002113 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07002114 for (jsize i = 0; i < length; ++i) {
2115 result->Set(i, initial_object);
2116 }
2117 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002118 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
2121 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002122 ScopedObjectAccess soa(env);
2123 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Ian Rogersa15e67d2012-02-28 13:51:55 -08002126 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002127 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002128 ScopedObjectAccess soa(env);
2129 Array* array = soa.Decode<Array*>(java_array);
2130 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002131 if (is_copy != NULL) {
2132 *is_copy = JNI_FALSE;
2133 }
2134 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002135 }
2136
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002137 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002138 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Ian Rogersef28b142012-11-30 14:22:18 -08002139 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002140 }
2141
Elliott Hughes75770752011-08-24 17:52:38 -07002142 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002143 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002144 ScopedObjectAccess soa(env);
2145 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes75770752011-08-24 17:52:38 -07002148 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002149 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002150 ScopedObjectAccess soa(env);
2151 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
2153
Elliott Hughes75770752011-08-24 17:52:38 -07002154 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002155 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002156 ScopedObjectAccess soa(env);
2157 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 }
2159
Elliott Hughes75770752011-08-24 17:52:38 -07002160 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002161 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002162 ScopedObjectAccess soa(env);
2163 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 }
2165
Elliott Hughes75770752011-08-24 17:52:38 -07002166 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002167 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002168 ScopedObjectAccess soa(env);
2169 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Elliott Hughes75770752011-08-24 17:52:38 -07002172 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002173 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002174 ScopedObjectAccess soa(env);
2175 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes75770752011-08-24 17:52:38 -07002178 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002179 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002180 ScopedObjectAccess soa(env);
2181 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 }
2183
Elliott Hughes75770752011-08-24 17:52:38 -07002184 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002185 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002186 ScopedObjectAccess soa(env);
2187 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 }
2189
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002190 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002191 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
2193
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002194 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002195 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002198 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002199 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002202 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002203 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 }
2205
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002206 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002207 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 }
2209
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002210 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002211 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 }
2213
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002214 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002215 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 }
2217
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002218 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002219 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 }
2221
Ian Rogersbc939662013-08-15 10:26:54 -07002222 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2223 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002224 ScopedObjectAccess soa(env);
2225 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227
Ian Rogersbc939662013-08-15 10:26:54 -07002228 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2229 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002230 ScopedObjectAccess soa(env);
2231 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 }
2233
Ian Rogersbc939662013-08-15 10:26:54 -07002234 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2235 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002236 ScopedObjectAccess soa(env);
2237 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239
Ian Rogersbc939662013-08-15 10:26:54 -07002240 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2241 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002242 ScopedObjectAccess soa(env);
2243 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 }
2245
Ian Rogersbc939662013-08-15 10:26:54 -07002246 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2247 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002248 ScopedObjectAccess soa(env);
2249 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Ian Rogersbc939662013-08-15 10:26:54 -07002252 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2253 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002254 ScopedObjectAccess soa(env);
2255 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
Ian Rogersbc939662013-08-15 10:26:54 -07002258 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2259 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002260 ScopedObjectAccess soa(env);
2261 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 }
2263
Ian Rogersbc939662013-08-15 10:26:54 -07002264 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2265 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002266 ScopedObjectAccess soa(env);
2267 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 }
2269
Ian Rogersbc939662013-08-15 10:26:54 -07002270 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2271 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002272 ScopedObjectAccess soa(env);
2273 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002274 }
2275
Ian Rogersbc939662013-08-15 10:26:54 -07002276 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2277 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002278 ScopedObjectAccess soa(env);
2279 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 }
2281
Ian Rogersbc939662013-08-15 10:26:54 -07002282 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2283 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002284 ScopedObjectAccess soa(env);
2285 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002286 }
2287
Ian Rogersbc939662013-08-15 10:26:54 -07002288 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2289 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002290 ScopedObjectAccess soa(env);
2291 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002292 }
2293
Ian Rogersbc939662013-08-15 10:26:54 -07002294 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2295 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002296 ScopedObjectAccess soa(env);
2297 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002298 }
2299
Ian Rogersbc939662013-08-15 10:26:54 -07002300 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2301 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002302 ScopedObjectAccess soa(env);
2303 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002304 }
2305
Ian Rogersbc939662013-08-15 10:26:54 -07002306 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2307 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002308 ScopedObjectAccess soa(env);
2309 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002310 }
2311
Ian Rogersbc939662013-08-15 10:26:54 -07002312 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2313 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002314 ScopedObjectAccess soa(env);
2315 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002316 }
2317
Ian Rogersbc939662013-08-15 10:26:54 -07002318 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2319 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002320 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2321 }
2322
Ian Rogersbc939662013-08-15 10:26:54 -07002323 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2324 jint method_count, bool return_errors) {
2325 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002326 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002327 return JNI_ERR; // Not reached.
2328 }
2329 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002330 ScopedObjectAccess soa(env);
2331 Class* c = soa.Decode<Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002332 if (UNLIKELY(method_count == 0)) {
2333 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2334 << PrettyDescriptor(c);
2335 return JNI_OK;
2336 }
2337 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2338 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002339 const char* name = methods[i].name;
2340 const char* sig = methods[i].signature;
2341
2342 if (*sig == '!') {
2343 // TODO: fast jni. it's too noisy to log all these.
2344 ++sig;
2345 }
2346
Brian Carlstromea46f952013-07-30 01:26:50 -07002347 ArtMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002348 if (m == NULL) {
2349 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002351 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002352 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002353 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002354 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002355 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002356 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002357 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002358 << PrettyDescriptor(c) << "." << name << sig
2359 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002360 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002361 return JNI_ERR;
2362 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002363
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002364 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002365
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002366 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002367 }
2368 return JNI_OK;
2369 }
2370
Elliott Hughes5174fe62011-08-23 15:12:35 -07002371 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002372 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002373 ScopedObjectAccess soa(env);
2374 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002375
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002376 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002377
2378 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002379 ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002380 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002381 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002382 }
2383 }
2384 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002385 ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002386 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002387 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002388 }
2389 }
2390
2391 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002392 }
2393
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002394 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2395 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002396 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002397 ScopedObjectAccess soa(env);
2398 Object* o = soa.Decode<Object*>(java_object);
2399 o->MonitorEnter(soa.Self());
2400 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002401 return JNI_ERR;
2402 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002403 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002404 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002405 }
2406
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002407 static jint MonitorExit(JNIEnv* env, jobject java_object)
2408 UNLOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002409 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002410 ScopedObjectAccess soa(env);
2411 Object* o = soa.Decode<Object*>(java_object);
2412 o->MonitorExit(soa.Self());
2413 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002414 return JNI_ERR;
2415 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002416 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002417 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002418 }
2419
2420 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002421 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002422 Runtime* runtime = Runtime::Current();
2423 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002424 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002425 } else {
2426 *vm = NULL;
2427 }
2428 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2429 }
2430
Elliott Hughescdf53122011-08-19 15:46:09 -07002431 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002432 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002433 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002434 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002435 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002436 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002437 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002438
Elliott Hughes96a98872012-12-19 14:21:15 -08002439 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002440 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2441 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002442 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002443 jint capacity_arg = static_cast<jint>(capacity);
2444
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002445 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2446 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002447 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002448 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002449 }
2450
Elliott Hughesb465ab02011-08-24 11:21:21 -07002451 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002452 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002453 }
2454
Elliott Hughesb465ab02011-08-24 11:21:21 -07002455 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002456 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002457 }
2458
Elliott Hughesb465ab02011-08-24 11:21:21 -07002459 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002460 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002461
2462 // Do we definitely know what kind of reference this is?
2463 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2464 IndirectRefKind kind = GetIndirectRefKind(ref);
2465 switch (kind) {
2466 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002467 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002468 return JNILocalRefType;
2469 }
2470 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002471 case kGlobal:
2472 return JNIGlobalRefType;
2473 case kWeakGlobal:
2474 return JNIWeakGlobalRefType;
2475 case kSirtOrInvalid:
2476 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002477 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002478 return JNILocalRefType;
2479 }
2480
Ian Rogersef28b142012-11-30 14:22:18 -08002481 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002482 return JNIInvalidRefType;
2483 }
2484
Elliott Hughesb465ab02011-08-24 11:21:21 -07002485 // If we're handing out direct pointers, check whether it's a direct pointer
2486 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002487 {
2488 ScopedObjectAccess soa(env);
2489 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2490 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2491 return JNILocalRefType;
2492 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002493 }
2494 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002495 return JNIInvalidRefType;
2496 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002497 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2498 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002499 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002500
2501 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002502 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2503 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002504 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002505 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002506 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2507 return JNI_ERR;
2508 }
2509 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002510 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002511 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2512 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002513 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002514 soa.Self()->ThrowOutOfMemoryError(caller);
2515 }
2516 return okay ? JNI_OK : JNI_ERR;
2517 }
2518
2519 template<typename JniT, typename ArtT>
2520 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002521 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002522 if (length < 0) {
2523 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2524 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002525 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002526 return soa.AddLocalReference<JniT>(result);
2527 }
2528
2529 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2530 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2531 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002532 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002533 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2534 PinPrimitiveArray(soa, array);
2535 if (is_copy != NULL) {
2536 *is_copy = JNI_FALSE;
2537 }
2538 return array->GetData();
2539 }
2540
2541 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002542 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002543 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002544 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002545 Array* array = soa.Decode<Array*>(java_array);
2546 UnpinPrimitiveArray(soa, array);
2547 }
2548 }
2549
2550 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2551 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2552 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002553 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002554 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002555 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2556 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2557 ThrowAIOOBE(soa, array, start, length, "src");
2558 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002559 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002560 JavaT* data = array->GetData();
2561 memcpy(buf, data + start, length * sizeof(JavaT));
2562 }
2563 }
2564
2565 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2566 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2567 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002568 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002569 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002570 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2571 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2572 ThrowAIOOBE(soa, array, start, length, "dst");
2573 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002574 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002575 JavaT* data = array->GetData();
2576 memcpy(data + start, buf, length * sizeof(JavaT));
2577 }
2578 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002579};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002580
Elliott Hughes88c5c352012-03-15 18:49:48 -07002581const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002582 NULL, // reserved0.
2583 NULL, // reserved1.
2584 NULL, // reserved2.
2585 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002586 JNI::GetVersion,
2587 JNI::DefineClass,
2588 JNI::FindClass,
2589 JNI::FromReflectedMethod,
2590 JNI::FromReflectedField,
2591 JNI::ToReflectedMethod,
2592 JNI::GetSuperclass,
2593 JNI::IsAssignableFrom,
2594 JNI::ToReflectedField,
2595 JNI::Throw,
2596 JNI::ThrowNew,
2597 JNI::ExceptionOccurred,
2598 JNI::ExceptionDescribe,
2599 JNI::ExceptionClear,
2600 JNI::FatalError,
2601 JNI::PushLocalFrame,
2602 JNI::PopLocalFrame,
2603 JNI::NewGlobalRef,
2604 JNI::DeleteGlobalRef,
2605 JNI::DeleteLocalRef,
2606 JNI::IsSameObject,
2607 JNI::NewLocalRef,
2608 JNI::EnsureLocalCapacity,
2609 JNI::AllocObject,
2610 JNI::NewObject,
2611 JNI::NewObjectV,
2612 JNI::NewObjectA,
2613 JNI::GetObjectClass,
2614 JNI::IsInstanceOf,
2615 JNI::GetMethodID,
2616 JNI::CallObjectMethod,
2617 JNI::CallObjectMethodV,
2618 JNI::CallObjectMethodA,
2619 JNI::CallBooleanMethod,
2620 JNI::CallBooleanMethodV,
2621 JNI::CallBooleanMethodA,
2622 JNI::CallByteMethod,
2623 JNI::CallByteMethodV,
2624 JNI::CallByteMethodA,
2625 JNI::CallCharMethod,
2626 JNI::CallCharMethodV,
2627 JNI::CallCharMethodA,
2628 JNI::CallShortMethod,
2629 JNI::CallShortMethodV,
2630 JNI::CallShortMethodA,
2631 JNI::CallIntMethod,
2632 JNI::CallIntMethodV,
2633 JNI::CallIntMethodA,
2634 JNI::CallLongMethod,
2635 JNI::CallLongMethodV,
2636 JNI::CallLongMethodA,
2637 JNI::CallFloatMethod,
2638 JNI::CallFloatMethodV,
2639 JNI::CallFloatMethodA,
2640 JNI::CallDoubleMethod,
2641 JNI::CallDoubleMethodV,
2642 JNI::CallDoubleMethodA,
2643 JNI::CallVoidMethod,
2644 JNI::CallVoidMethodV,
2645 JNI::CallVoidMethodA,
2646 JNI::CallNonvirtualObjectMethod,
2647 JNI::CallNonvirtualObjectMethodV,
2648 JNI::CallNonvirtualObjectMethodA,
2649 JNI::CallNonvirtualBooleanMethod,
2650 JNI::CallNonvirtualBooleanMethodV,
2651 JNI::CallNonvirtualBooleanMethodA,
2652 JNI::CallNonvirtualByteMethod,
2653 JNI::CallNonvirtualByteMethodV,
2654 JNI::CallNonvirtualByteMethodA,
2655 JNI::CallNonvirtualCharMethod,
2656 JNI::CallNonvirtualCharMethodV,
2657 JNI::CallNonvirtualCharMethodA,
2658 JNI::CallNonvirtualShortMethod,
2659 JNI::CallNonvirtualShortMethodV,
2660 JNI::CallNonvirtualShortMethodA,
2661 JNI::CallNonvirtualIntMethod,
2662 JNI::CallNonvirtualIntMethodV,
2663 JNI::CallNonvirtualIntMethodA,
2664 JNI::CallNonvirtualLongMethod,
2665 JNI::CallNonvirtualLongMethodV,
2666 JNI::CallNonvirtualLongMethodA,
2667 JNI::CallNonvirtualFloatMethod,
2668 JNI::CallNonvirtualFloatMethodV,
2669 JNI::CallNonvirtualFloatMethodA,
2670 JNI::CallNonvirtualDoubleMethod,
2671 JNI::CallNonvirtualDoubleMethodV,
2672 JNI::CallNonvirtualDoubleMethodA,
2673 JNI::CallNonvirtualVoidMethod,
2674 JNI::CallNonvirtualVoidMethodV,
2675 JNI::CallNonvirtualVoidMethodA,
2676 JNI::GetFieldID,
2677 JNI::GetObjectField,
2678 JNI::GetBooleanField,
2679 JNI::GetByteField,
2680 JNI::GetCharField,
2681 JNI::GetShortField,
2682 JNI::GetIntField,
2683 JNI::GetLongField,
2684 JNI::GetFloatField,
2685 JNI::GetDoubleField,
2686 JNI::SetObjectField,
2687 JNI::SetBooleanField,
2688 JNI::SetByteField,
2689 JNI::SetCharField,
2690 JNI::SetShortField,
2691 JNI::SetIntField,
2692 JNI::SetLongField,
2693 JNI::SetFloatField,
2694 JNI::SetDoubleField,
2695 JNI::GetStaticMethodID,
2696 JNI::CallStaticObjectMethod,
2697 JNI::CallStaticObjectMethodV,
2698 JNI::CallStaticObjectMethodA,
2699 JNI::CallStaticBooleanMethod,
2700 JNI::CallStaticBooleanMethodV,
2701 JNI::CallStaticBooleanMethodA,
2702 JNI::CallStaticByteMethod,
2703 JNI::CallStaticByteMethodV,
2704 JNI::CallStaticByteMethodA,
2705 JNI::CallStaticCharMethod,
2706 JNI::CallStaticCharMethodV,
2707 JNI::CallStaticCharMethodA,
2708 JNI::CallStaticShortMethod,
2709 JNI::CallStaticShortMethodV,
2710 JNI::CallStaticShortMethodA,
2711 JNI::CallStaticIntMethod,
2712 JNI::CallStaticIntMethodV,
2713 JNI::CallStaticIntMethodA,
2714 JNI::CallStaticLongMethod,
2715 JNI::CallStaticLongMethodV,
2716 JNI::CallStaticLongMethodA,
2717 JNI::CallStaticFloatMethod,
2718 JNI::CallStaticFloatMethodV,
2719 JNI::CallStaticFloatMethodA,
2720 JNI::CallStaticDoubleMethod,
2721 JNI::CallStaticDoubleMethodV,
2722 JNI::CallStaticDoubleMethodA,
2723 JNI::CallStaticVoidMethod,
2724 JNI::CallStaticVoidMethodV,
2725 JNI::CallStaticVoidMethodA,
2726 JNI::GetStaticFieldID,
2727 JNI::GetStaticObjectField,
2728 JNI::GetStaticBooleanField,
2729 JNI::GetStaticByteField,
2730 JNI::GetStaticCharField,
2731 JNI::GetStaticShortField,
2732 JNI::GetStaticIntField,
2733 JNI::GetStaticLongField,
2734 JNI::GetStaticFloatField,
2735 JNI::GetStaticDoubleField,
2736 JNI::SetStaticObjectField,
2737 JNI::SetStaticBooleanField,
2738 JNI::SetStaticByteField,
2739 JNI::SetStaticCharField,
2740 JNI::SetStaticShortField,
2741 JNI::SetStaticIntField,
2742 JNI::SetStaticLongField,
2743 JNI::SetStaticFloatField,
2744 JNI::SetStaticDoubleField,
2745 JNI::NewString,
2746 JNI::GetStringLength,
2747 JNI::GetStringChars,
2748 JNI::ReleaseStringChars,
2749 JNI::NewStringUTF,
2750 JNI::GetStringUTFLength,
2751 JNI::GetStringUTFChars,
2752 JNI::ReleaseStringUTFChars,
2753 JNI::GetArrayLength,
2754 JNI::NewObjectArray,
2755 JNI::GetObjectArrayElement,
2756 JNI::SetObjectArrayElement,
2757 JNI::NewBooleanArray,
2758 JNI::NewByteArray,
2759 JNI::NewCharArray,
2760 JNI::NewShortArray,
2761 JNI::NewIntArray,
2762 JNI::NewLongArray,
2763 JNI::NewFloatArray,
2764 JNI::NewDoubleArray,
2765 JNI::GetBooleanArrayElements,
2766 JNI::GetByteArrayElements,
2767 JNI::GetCharArrayElements,
2768 JNI::GetShortArrayElements,
2769 JNI::GetIntArrayElements,
2770 JNI::GetLongArrayElements,
2771 JNI::GetFloatArrayElements,
2772 JNI::GetDoubleArrayElements,
2773 JNI::ReleaseBooleanArrayElements,
2774 JNI::ReleaseByteArrayElements,
2775 JNI::ReleaseCharArrayElements,
2776 JNI::ReleaseShortArrayElements,
2777 JNI::ReleaseIntArrayElements,
2778 JNI::ReleaseLongArrayElements,
2779 JNI::ReleaseFloatArrayElements,
2780 JNI::ReleaseDoubleArrayElements,
2781 JNI::GetBooleanArrayRegion,
2782 JNI::GetByteArrayRegion,
2783 JNI::GetCharArrayRegion,
2784 JNI::GetShortArrayRegion,
2785 JNI::GetIntArrayRegion,
2786 JNI::GetLongArrayRegion,
2787 JNI::GetFloatArrayRegion,
2788 JNI::GetDoubleArrayRegion,
2789 JNI::SetBooleanArrayRegion,
2790 JNI::SetByteArrayRegion,
2791 JNI::SetCharArrayRegion,
2792 JNI::SetShortArrayRegion,
2793 JNI::SetIntArrayRegion,
2794 JNI::SetLongArrayRegion,
2795 JNI::SetFloatArrayRegion,
2796 JNI::SetDoubleArrayRegion,
2797 JNI::RegisterNatives,
2798 JNI::UnregisterNatives,
2799 JNI::MonitorEnter,
2800 JNI::MonitorExit,
2801 JNI::GetJavaVM,
2802 JNI::GetStringRegion,
2803 JNI::GetStringUTFRegion,
2804 JNI::GetPrimitiveArrayCritical,
2805 JNI::ReleasePrimitiveArrayCritical,
2806 JNI::GetStringCritical,
2807 JNI::ReleaseStringCritical,
2808 JNI::NewWeakGlobalRef,
2809 JNI::DeleteWeakGlobalRef,
2810 JNI::ExceptionCheck,
2811 JNI::NewDirectByteBuffer,
2812 JNI::GetDirectBufferAddress,
2813 JNI::GetDirectBufferCapacity,
2814 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002815};
2816
Elliott Hughes75770752011-08-24 17:52:38 -07002817JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002818 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002819 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002820 local_ref_cookie(IRT_FIRST_SEGMENT),
2821 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002822 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002823 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002824 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002825 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002826 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002827 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002828 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002829 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2830 // errors will ensue.
2831 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2832 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002833}
2834
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002835JNIEnvExt::~JNIEnvExt() {
2836}
2837
Elliott Hughes88c5c352012-03-15 18:49:48 -07002838void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2839 check_jni = enabled;
2840 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002841}
2842
Elliott Hughes73e66f72012-05-09 09:34:45 -07002843void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2844 locals.Dump(os);
2845 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002846}
2847
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002848void JNIEnvExt::PushFrame(int /*capacity*/) {
2849 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002850 stacked_local_ref_cookies.push_back(local_ref_cookie);
2851 local_ref_cookie = locals.GetSegmentState();
2852}
2853
2854void JNIEnvExt::PopFrame() {
2855 locals.SetSegmentState(local_ref_cookie);
2856 local_ref_cookie = stacked_local_ref_cookies.back();
2857 stacked_local_ref_cookies.pop_back();
2858}
2859
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002860Offset JNIEnvExt::SegmentStateOffset() {
2861 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2862 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2863}
2864
Carl Shapiroea4dca82011-08-01 13:45:38 -07002865// JNI Invocation interface.
2866
Brian Carlstrombddf9762013-05-14 11:35:37 -07002867extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002868 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002869 if (IsBadJniVersion(args->version)) {
2870 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002871 return JNI_EVERSION;
2872 }
2873 Runtime::Options options;
2874 for (int i = 0; i < args->nOptions; ++i) {
2875 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002876 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002877 }
2878 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002879 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002880 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002881 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002882 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002883 bool started = runtime->Start();
2884 if (!started) {
2885 delete Thread::Current()->GetJniEnv();
2886 delete runtime->GetJavaVM();
2887 LOG(WARNING) << "CreateJavaVM failed";
2888 return JNI_ERR;
2889 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002890 *p_env = Thread::Current()->GetJniEnv();
2891 *p_vm = runtime->GetJavaVM();
2892 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002893}
2894
Elliott Hughesf2682d52011-08-15 16:37:04 -07002895extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002896 Runtime* runtime = Runtime::Current();
2897 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002898 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002899 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002900 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002901 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002902 }
2903 return JNI_OK;
2904}
2905
2906// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002907extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002908 return JNI_ERR;
2909}
2910
Elliott Hughescdf53122011-08-19 15:46:09 -07002911class JII {
2912 public:
2913 static jint DestroyJavaVM(JavaVM* vm) {
2914 if (vm == NULL) {
2915 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002916 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002917 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2918 delete raw_vm->runtime;
2919 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002920 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002921
Elliott Hughescdf53122011-08-19 15:46:09 -07002922 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002923 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002924 }
2925
2926 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002927 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002928 }
2929
2930 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002931 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002932 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002933 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002934 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2935 Runtime* runtime = raw_vm->runtime;
2936 runtime->DetachCurrentThread();
2937 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002938 }
2939
2940 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002941 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2942 // and unlike other calls that take a JNI version doesn't care if you supply
2943 // JNI_VERSION_1_1, which we don't otherwise support.
2944 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002945 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002946 return JNI_EVERSION;
2947 }
2948 if (vm == NULL || env == NULL) {
2949 return JNI_ERR;
2950 }
2951 Thread* thread = Thread::Current();
2952 if (thread == NULL) {
2953 *env = NULL;
2954 return JNI_EDETACHED;
2955 }
2956 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002957 return JNI_OK;
2958 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002959};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002960
Elliott Hughes88c5c352012-03-15 18:49:48 -07002961const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002962 NULL, // reserved0
2963 NULL, // reserved1
2964 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002965 JII::DestroyJavaVM,
2966 JII::AttachCurrentThread,
2967 JII::DetachCurrentThread,
2968 JII::GetEnv,
2969 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002970};
2971
Elliott Hughesa0957642011-09-02 14:27:33 -07002972JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002973 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002974 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002975 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002976 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002977 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002978 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002979 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08002980 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002981 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002982 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002983 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002984 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07002985 libraries(new Libraries),
2986 weak_globals_lock_("JNI weak global reference table lock"),
2987 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2988 allow_new_weak_globals_(true),
2989 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002990 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002991 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002992 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002993 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002994}
2995
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002996JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002997 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002998}
2999
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003000jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3001 if (obj == nullptr) {
3002 return nullptr;
3003 }
3004 MutexLock mu(self, weak_globals_lock_);
3005 while (UNLIKELY(!allow_new_weak_globals_)) {
3006 weak_globals_add_condition_.WaitHoldingLocks(self);
3007 }
3008 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3009 return reinterpret_cast<jweak>(ref);
3010}
3011
3012void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3013 MutexLock mu(self, weak_globals_lock_);
3014 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3015 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3016 << "failed to find entry";
3017 }
3018}
3019
Elliott Hughes88c5c352012-03-15 18:49:48 -07003020void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3021 check_jni = enabled;
3022 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003023}
3024
Elliott Hughesae80b492012-04-24 10:43:17 -07003025void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3026 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3027 if (force_copy) {
3028 os << " (with forcecopy)";
3029 }
3030 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003031 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003032 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003033 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003034 os << "; pins=" << pin_table.Size();
3035 }
3036 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003037 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003038 os << "; globals=" << globals.Capacity();
3039 }
3040 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003041 MutexLock mu(self, weak_globals_lock_);
3042 if (weak_globals_.Capacity() > 0) {
3043 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003044 }
3045 }
3046 os << '\n';
3047
3048 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003049 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003050 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3051 }
3052}
3053
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003054void JavaVMExt::DisallowNewWeakGlobals() {
3055 MutexLock mu(Thread::Current(), weak_globals_lock_);
3056 allow_new_weak_globals_ = false;
3057}
3058
3059void JavaVMExt::AllowNewWeakGlobals() {
3060 Thread* self = Thread::Current();
3061 MutexLock mu(self, weak_globals_lock_);
3062 allow_new_weak_globals_ = true;
3063 weak_globals_add_condition_.Broadcast(self);
3064}
3065
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003066mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3067 MutexLock mu(self, weak_globals_lock_);
3068 while (UNLIKELY(!allow_new_weak_globals_)) {
3069 weak_globals_add_condition_.WaitHoldingLocks(self);
3070 }
3071 return const_cast<mirror::Object*>(weak_globals_.Get(ref));
3072}
3073
Elliott Hughes73e66f72012-05-09 09:34:45 -07003074void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003075 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003076 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003077 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003078 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003079 }
3080 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003081 MutexLock mu(self, weak_globals_lock_);
3082 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003083 }
3084 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003085 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003086 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003087 }
3088}
3089
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003090bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
3091 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07003092 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003093
3094 // See if we've already loaded this library. If we have, and the class loader
3095 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003096 // TODO: for better results we should canonicalize the pathname (or even compare
3097 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003098 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003099 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003100 {
3101 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003102 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003103 library = libraries->Get(path);
3104 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003105 if (library != NULL) {
3106 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07003107 // The library will be associated with class_loader. The JNI
3108 // spec says we can't load the same library into more than one
3109 // class loader.
3110 StringAppendF(&detail, "Shared library \"%s\" already opened by "
3111 "ClassLoader %p; can't open in ClassLoader %p",
3112 path.c_str(), library->GetClassLoader(), class_loader);
3113 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003114 return false;
3115 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003116 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
3117 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003118 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003119 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
3120 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003121 return false;
3122 }
3123 return true;
3124 }
3125
3126 // Open the shared library. Because we're using a full path, the system
3127 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3128 // resolve this library's dependencies though.)
3129
3130 // Failures here are expected when java.library.path has several entries
3131 // and we have to hunt for the lib.
3132
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003133 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3134 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3135 // dlopen) becomes zero from dlclose.
3136
Elliott Hughescdf53122011-08-19 15:46:09 -07003137 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003138 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003139 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
3140 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
3141 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003142
Elliott Hughes84b2f142012-09-27 09:16:28 -07003143 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003144
3145 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07003146 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003147 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003148 return false;
3149 }
3150
3151 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003152 // TODO: move the locking (and more of this logic) into Libraries.
3153 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003154 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003155 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003156 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003157 if (library == NULL) { // We won race to get libraries_lock
3158 library = new SharedLibrary(path, handle, class_loader);
3159 libraries->Put(path, library);
3160 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003161 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003162 }
3163 if (!created_library) {
3164 LOG(INFO) << "WOW: we lost a race to add shared library: "
3165 << "\"" << path << "\" ClassLoader=" << class_loader;
3166 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003167 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003168
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003169 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003170
Elliott Hughes79353722013-08-02 16:52:18 -07003171 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003172 void* sym = dlsym(handle, "JNI_OnLoad");
3173 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003174 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003175 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003176 } else {
3177 // Call JNI_OnLoad. We have to override the current class
3178 // loader, which will always be "null" since the stuff at the
3179 // top of the stack is around Runtime.loadLibrary(). (See
3180 // the comments in the JNI FindClass function.)
3181 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3182 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07003183 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07003184 self->SetClassLoaderOverride(class_loader);
3185
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003186 int version = 0;
3187 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003188 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003189 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003190 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07003191 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003192
Brian Carlstromaded5f72011-10-07 17:15:04 -07003193 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07003194
Elliott Hughes79353722013-08-02 16:52:18 -07003195 if (version == JNI_ERR) {
3196 StringAppendF(&detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
3197 } else if (IsBadJniVersion(version)) {
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003198 StringAppendF(&detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
3199 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003200 // It's unwise to call dlclose() here, but we can mark it
3201 // as bad and ensure that future load attempts will fail.
3202 // We don't know how far JNI_OnLoad got, so there could
3203 // be some partially-initialized stuff accessible through
3204 // newly-registered native method calls. We could try to
3205 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003206 } else {
3207 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003208 }
Elliott Hughes79353722013-08-02 16:52:18 -07003209 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003210 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003211 }
3212
Elliott Hughes79353722013-08-02 16:52:18 -07003213 library->SetResult(was_successful);
3214 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003215}
3216
Brian Carlstromea46f952013-07-30 01:26:50 -07003217void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003218 CHECK(m->IsNative());
3219
3220 Class* c = m->GetDeclaringClass();
3221
3222 // If this is a static method, it could be called before the class
3223 // has been initialized.
3224 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07003225 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003226 return NULL;
3227 }
3228 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003229 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003230 }
3231
Brian Carlstrom16192862011-09-12 17:50:06 -07003232 std::string detail;
3233 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003234 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003235 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003236 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003237 native_method = libraries->FindNativeMethod(m, detail);
3238 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003239 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07003240 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003241 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3242 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003243 }
3244 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003245}
3246
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003247void JavaVMExt::SweepJniWeakGlobals(RootVisitor visitor, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003248 MutexLock mu(Thread::Current(), weak_globals_lock_);
3249 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003250 mirror::Object* obj = *entry;
3251 mirror::Object* new_obj = visitor(obj, arg);
3252 if (new_obj == nullptr) {
3253 new_obj = kClearedJniWeakGlobal;
3254 }
3255 *entry = new_obj;
3256 }
3257}
3258
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003259void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003260 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003261 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003262 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003263 globals.VisitRoots(visitor, arg);
3264 }
3265 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003266 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003267 pin_table.VisitRoots(visitor, arg);
3268 }
3269 // The weak_globals table is visited by the GC itself (because it mutates the table).
3270}
3271
Elliott Hughesc8fece32013-01-02 11:27:23 -08003272void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003273 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003274 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
3275 if (c.get() == NULL) {
3276 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3277 }
3278 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3279}
3280
Ian Rogersdf20fe02011-07-20 20:34:16 -07003281} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003282
3283std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3284 switch (rhs) {
3285 case JNIInvalidRefType:
3286 os << "JNIInvalidRefType";
3287 return os;
3288 case JNILocalRefType:
3289 os << "JNILocalRefType";
3290 return os;
3291 case JNIGlobalRefType:
3292 os << "JNIGlobalRefType";
3293 return os;
3294 case JNIWeakGlobalRefType:
3295 os << "JNIWeakGlobalRefType";
3296 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003297 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003298 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003299 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003300 }
3301}