blob: b1b664d481b08cda3ec8a1fb91bda837c4aada46 [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 Rogersd91d6d62013-09-25 20:26:14 -0700231 DCHECK(c != nullptr);
Ian Rogers0045a292012-03-31 21:08:41 -0700232 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700233 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700234 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700235
Brian Carlstromea46f952013-07-30 01:26:50 -0700236 ArtMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700237 if (is_static) {
238 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700239 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700240 method = c->FindVirtualMethod(name, sig);
241 if (method == NULL) {
242 // No virtual method matching the signature. Search declared
243 // private methods and constructors.
244 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700245 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700246 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700247
Elliott Hughescdf53122011-08-19 15:46:09 -0700248 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700250 return NULL;
251 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700252
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700254}
255
Ian Rogersef28b142012-11-30 14:22:18 -0800256static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700258 ArtMethod* method = soa.Self()->GetCurrentMethod(NULL);
Ian Rogersef28b142012-11-30 14:22:18 -0800259 if (method == NULL ||
260 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
261 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700262 }
263 return method->GetDeclaringClass()->GetClassLoader();
264}
265
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
267 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700270 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700271 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700272 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700273
Brian Carlstromea46f952013-07-30 01:26:50 -0700274 ArtField* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700275 Class* field_type;
276 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
277 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800278 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700279 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700280 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700281 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700282 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 if (field_type == NULL) {
284 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800286 ThrowLocation throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700287 SirtRef<Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700288 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
290 "no type \"%s\" found and so no field \"%s\" could be found in class "
291 "\"%s\" or its superclasses", sig, name,
292 ClassHelper(c).GetDescriptor());
293 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294 return NULL;
295 }
296 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800297 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700298 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800299 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700301 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800302 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
303 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
304 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
305 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700306 return NULL;
307 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700309}
310
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700311static void PinPrimitiveArray(const ScopedObjectAccess& soa, Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700312 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700313 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700314 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700315 vm->pin_table.Add(array);
316}
317
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700318static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700321 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700322 vm->pin_table.Remove(array);
323}
324
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700325static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
326 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700328 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800329 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
330 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
331 "%s offset=%d length=%d %s.length=%d",
332 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700333}
Ian Rogers0571d352011-11-03 19:51:38 -0700334
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
336 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800338 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
339 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
340 "offset=%d length=%d string.length()=%d", start, length,
341 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700342}
Elliott Hughes814e4032011-08-23 12:07:56 -0700343
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700344int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700345 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700346 // Turn the const char* into a java.lang.String.
347 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
348 if (msg != NULL && s.get() == NULL) {
349 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700350 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700351
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 // Choose an appropriate constructor and set up the arguments.
353 jvalue args[2];
354 const char* signature;
355 if (msg == NULL && cause == NULL) {
356 signature = "()V";
357 } else if (msg != NULL && cause == NULL) {
358 signature = "(Ljava/lang/String;)V";
359 args[0].l = s.get();
360 } else if (msg == NULL && cause != NULL) {
361 signature = "(Ljava/lang/Throwable;)V";
362 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700363 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700364 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
365 args[0].l = s.get();
366 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700367 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
369 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800370 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700371 LOG(ERROR) << "No <init>" << signature << " in "
372 << PrettyClass(soa.Decode<Class*>(exception_class));
373 return JNI_ERR;
374 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700375
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700376 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
377 if (exception.get() == NULL) {
378 return JNI_ERR;
379 }
Ian Rogersef28b142012-11-30 14:22:18 -0800380 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800381 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
382 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700383 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700384}
385
Elliott Hughes462c9442012-03-23 18:47:50 -0700386static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700387 if (vm == NULL || p_env == NULL) {
388 return JNI_ERR;
389 }
390
Elliott Hughes462c9442012-03-23 18:47:50 -0700391 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700392 Thread* self = Thread::Current();
393 if (self != NULL) {
394 *p_env = self->GetJniEnv();
395 return JNI_OK;
396 }
397
398 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
399
400 // No threads allowed in zygote mode.
401 if (runtime->IsZygote()) {
402 LOG(ERROR) << "Attempt to attach a thread in the zygote";
403 return JNI_ERR;
404 }
405
Elliott Hughes462c9442012-03-23 18:47:50 -0700406 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
407 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700408 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700409 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700410 if (IsBadJniVersion(args->version)) {
411 LOG(ERROR) << "Bad JNI version passed to "
412 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
413 << args->version;
414 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700415 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700416 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700417 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700418 }
Elliott Hughes75770752011-08-24 17:52:38 -0700419
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800420 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700421 *p_env = NULL;
422 return JNI_ERR;
423 } else {
424 *p_env = Thread::Current()->GetJniEnv();
425 return JNI_OK;
426 }
Elliott Hughes75770752011-08-24 17:52:38 -0700427}
428
Elliott Hughes79082e32011-08-25 12:07:32 -0700429class SharedLibrary {
430 public:
431 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
432 : path_(path),
433 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700434 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700435 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700436 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700437 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700438 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700439 }
440
Elliott Hughes79082e32011-08-25 12:07:32 -0700441 Object* GetClassLoader() {
442 return class_loader_;
443 }
444
445 std::string GetPath() {
446 return path_;
447 }
448
449 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700450 * Check the result of an earlier call to JNI_OnLoad on this library.
451 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700452 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700453 bool CheckOnLoadResult()
454 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700455 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700456 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700457 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
458 bool okay;
459 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700460 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700461
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700462 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700463 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
464 // caller can continue.
465 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
466 okay = true;
467 } else {
468 while (jni_on_load_result_ == kPending) {
469 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700470 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700471 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700472
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700473 okay = (jni_on_load_result_ == kOkay);
474 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
475 << (okay ? "succeeded" : "failed") << "]";
476 }
477 }
478 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700479 return okay;
480 }
481
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700482 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700483 Thread* self = Thread::Current();
484 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700485
Elliott Hughes79082e32011-08-25 12:07:32 -0700486 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700487 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700488
489 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700490 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700491 }
492
493 void* FindSymbol(const std::string& symbol_name) {
494 return dlsym(handle_, symbol_name.c_str());
495 }
496
497 private:
498 enum JNI_OnLoadState {
499 kPending,
500 kFailed,
501 kOkay,
502 };
503
504 // Path to library "/system/lib/libjni.so".
505 std::string path_;
506
507 // The void* returned by dlopen(3).
508 void* handle_;
509
510 // The ClassLoader this library is associated with.
511 Object* class_loader_;
512
513 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700514 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700515 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700516 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700517 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700518 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700519 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700520 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700521};
522
Elliott Hughes79082e32011-08-25 12:07:32 -0700523// This exists mainly to keep implementation details out of the header file.
524class Libraries {
525 public:
526 Libraries() {
527 }
528
529 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700530 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700531 }
532
Elliott Hughesae80b492012-04-24 10:43:17 -0700533 void Dump(std::ostream& os) const {
534 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700535 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700536 if (!first) {
537 os << ' ';
538 }
539 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700540 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700541 }
542 }
543
544 size_t size() const {
545 return libraries_.size();
546 }
547
Elliott Hughes79082e32011-08-25 12:07:32 -0700548 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700549 auto it = libraries_.find(path);
Ian Rogers712462a2012-04-12 16:32:29 -0700550 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700551 }
552
553 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700554 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700555 }
556
557 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstromea46f952013-07-30 01:26:50 -0700558 void* FindNativeMethod(const ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700559 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700560 std::string jni_short_name(JniShortName(m));
561 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700562 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700563 for (const auto& lib : libraries_) {
564 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700565 if (library->GetClassLoader() != declaring_class_loader) {
566 // We only search libraries loaded by the appropriate ClassLoader.
567 continue;
568 }
569 // Try the short name then the long name...
570 void* fn = library->FindSymbol(jni_short_name);
571 if (fn == NULL) {
572 fn = library->FindSymbol(jni_long_name);
573 }
574 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800575 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
576 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700577 return fn;
578 }
579 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700580 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700581 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700582 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700584 return NULL;
585 }
586
587 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700588 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700589};
590
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700591JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
592 jvalue* args) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700593 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700594 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700595 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800596 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700597 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800598 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700599 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
600 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800601}
602
Ian Rogersbc939662013-08-15 10:26:54 -0700603#define CHECK_NON_NULL_ARGUMENT(fn, value) \
604 if (UNLIKELY(value == NULL)) { \
605 JniAbortF(#fn, #value " == null"); \
606 }
607
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700608#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
609 if (UNLIKELY(length != 0 && value == NULL)) { \
610 JniAbortF(#fn, #value " == null"); \
611 }
612
Elliott Hughescdf53122011-08-19 15:46:09 -0700613class JNI {
614 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700615 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700616 return JNI_VERSION_1_6;
617 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700618
Ian Rogers25e8b912012-09-07 11:31:36 -0700619 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700621 return NULL;
622 }
623
Elliott Hughescdf53122011-08-19 15:46:09 -0700624 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700625 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700626 Runtime* runtime = Runtime::Current();
627 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700628 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700629 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700630 Class* c = NULL;
631 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800632 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800633 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700634 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800635 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700636 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700637 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700638 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700639
Elliott Hughescdf53122011-08-19 15:46:09 -0700640 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700641 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700642 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700643 jobject art_method = env->GetObjectField(java_method,
644 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
645 ArtMethod* method = soa.Decode<ArtMethod*>(art_method);
646 DCHECK(method != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700648 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700649
Elliott Hughescdf53122011-08-19 15:46:09 -0700650 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700651 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700653 jobject art_field = env->GetObjectField(java_field,
654 WellKnownClasses::java_lang_reflect_Field_artField);
655 ArtField* field = soa.Decode<ArtField*>(art_field);
656 DCHECK(field != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700658 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700659
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700661 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700663 ArtMethod* m = soa.DecodeMethod(mid);
664 jobject art_method = soa.AddLocalReference<jobject>(m);
665 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
666 if (env->ExceptionCheck()) {
667 return NULL;
668 }
669 SetObjectField(env,
670 reflect_method,
671 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod,
672 art_method);
673 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700675
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700677 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700679 ArtField* f = soa.DecodeField(fid);
680 jobject art_field = soa.AddLocalReference<jobject>(f);
681 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
682 if (env->ExceptionCheck()) {
683 return NULL;
684 }
685 SetObjectField(env,
686 reflect_field,
687 WellKnownClasses::java_lang_reflect_Field_artField,
688 art_field);
689 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700691
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700693 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 ScopedObjectAccess soa(env);
695 Object* o = soa.Decode<Object*>(java_object);
696 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700697 }
698
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700699 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700700 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700701 ScopedObjectAccess soa(env);
702 Class* c = soa.Decode<Class*>(java_class);
703 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700704 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700705
Elliott Hughes37f7a402011-08-22 18:56:01 -0700706 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700707 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
708 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700709 ScopedObjectAccess soa(env);
710 Class* c1 = soa.Decode<Class*>(java_class1);
711 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700712 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700713 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700714
Elliott Hughese84278b2012-03-22 10:06:53 -0700715 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700716 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700717 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700718 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700719 return JNI_TRUE;
720 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700721 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700722 Object* obj = soa.Decode<Object*>(jobj);
723 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700724 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700725 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700727
Elliott Hughes37f7a402011-08-22 18:56:01 -0700728 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700729 ScopedObjectAccess soa(env);
730 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700731 if (exception == NULL) {
732 return JNI_ERR;
733 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800734 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
735 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700736 return JNI_OK;
737 }
738
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700739 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700740 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Elliott Hughesa4f94742012-05-29 16:28:38 -0700741 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700742 }
743
744 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700745 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700746 }
747
748 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700749 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700750 }
751
752 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700753 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700754
Brian Carlstromea46f952013-07-30 01:26:50 -0700755 SirtRef<Object> old_throw_this_object(soa.Self(), NULL);
756 SirtRef<ArtMethod> old_throw_method(soa.Self(), NULL);
757 SirtRef<Throwable> old_exception(soa.Self(), NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800758 uint32_t old_throw_dex_pc;
759 {
760 ThrowLocation old_throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700761 Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800762 old_throw_this_object.reset(old_throw_location.GetThis());
763 old_throw_method.reset(old_throw_location.GetMethod());
764 old_exception.reset(old_exception_obj);
765 old_throw_dex_pc = old_throw_location.GetDexPc();
766 soa.Self()->ClearException();
767 }
768 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700769 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
770 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
771 if (mid == NULL) {
772 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800773 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700774 } else {
775 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800776 if (soa.Self()->IsExceptionPending()) {
777 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700778 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800779 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700780 }
781 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800782 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
783 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700784
Ian Rogers62d6c772013-02-27 08:32:07 -0800785 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700786 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700787
Elliott Hughescdf53122011-08-19 15:46:09 -0700788 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700789 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800790 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700791 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700792 }
793
Ian Rogers25e8b912012-09-07 11:31:36 -0700794 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700795 LOG(FATAL) << "JNI FatalError called: " << msg;
796 }
797
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700798 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800799 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700800 return JNI_ERR;
801 }
Ian Rogersef28b142012-11-30 14:22:18 -0800802 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 return JNI_OK;
804 }
805
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700806 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700807 ScopedObjectAccess soa(env);
808 Object* survivor = soa.Decode<Object*>(java_survivor);
809 soa.Env()->PopFrame();
810 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 }
812
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700813 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800814 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700815 }
816
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700818 if (obj == NULL) {
819 return NULL;
820 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700821 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700822 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700824 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogersb8a0b942013-08-20 18:09:52 -0700825 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700826 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 return reinterpret_cast<jobject>(ref);
828 }
829
830 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 if (obj == NULL) {
832 return;
833 }
Ian Rogersef28b142012-11-30 14:22:18 -0800834 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800836 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700837 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700838
839 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
840 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
841 << "failed to find entry";
842 }
843 }
844
845 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 ScopedObjectAccess soa(env);
847 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 }
849
850 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700851 if (obj != nullptr) {
852 ScopedObjectAccess soa(env);
853 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 }
855 }
856
857 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700858 if (obj == NULL) {
859 return NULL;
860 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700861 ScopedObjectAccess soa(env);
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700862 return soa.AddLocalReference<jobject>(soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700863 }
864
865 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 if (obj == NULL) {
867 return;
868 }
Ian Rogersef28b142012-11-30 14:22:18 -0800869 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700870
Ian Rogersef28b142012-11-30 14:22:18 -0800871 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 if (!locals.Remove(cookie, obj)) {
873 // Attempting to delete a local reference that is not in the
874 // topmost local reference frame is a no-op. DeleteLocalRef returns
875 // void and doesn't throw any exceptions, but we should probably
876 // complain about it so the user will notice that things aren't
877 // going quite the way they expect.
878 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
879 << "failed to find entry";
880 }
881 }
882
883 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700884 if (obj1 == obj2) {
885 return JNI_TRUE;
886 } else {
887 ScopedObjectAccess soa(env);
888 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
889 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 }
891
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700892 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700893 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700894 ScopedObjectAccess soa(env);
895 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700896 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700897 return NULL;
898 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700899 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 }
901
Ian Rogersbc939662013-08-15 10:26:54 -0700902 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700905 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
906 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
907 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 va_end(args);
909 return result;
910 }
911
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700913 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
914 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700915 ScopedObjectAccess soa(env);
916 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700917 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700918 return NULL;
919 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700920 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700921 if (result == NULL) {
922 return NULL;
923 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700924 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700925 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700926 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700927 return local_result;
928 } else {
929 return NULL;
930 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 }
932
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700934 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
935 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936 ScopedObjectAccess soa(env);
937 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700938 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700939 return NULL;
940 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700941 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700942 if (result == NULL) {
943 return NULL;
944 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700948 return local_result;
949 } else {
950 return NULL;
951 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 }
953
Ian Rogersbc939662013-08-15 10:26:54 -0700954 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
955 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
956 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
957 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700958 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700959 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 }
961
Ian Rogersbc939662013-08-15 10:26:54 -0700962 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
963 const char* sig) {
964 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
965 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
966 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700967 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700968 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 }
970
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 va_list ap;
973 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700974 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
975 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700976 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700979 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 }
981
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700983 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
984 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700985 ScopedObjectAccess soa(env);
986 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
987 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700991 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
992 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700993 ScopedObjectAccess soa(env);
994 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
995 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 va_list ap;
1000 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001001 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1002 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001003 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001006 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001010 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1011 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001012 ScopedObjectAccess soa(env);
1013 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001017 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1018 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001019 ScopedObjectAccess soa(env);
1020 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 va_list ap;
1025 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001026 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1027 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1028 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001031 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001035 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1036 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 ScopedObjectAccess soa(env);
1038 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001042 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1043 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001044 ScopedObjectAccess soa(env);
1045 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_list ap;
1050 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001051 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1052 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001053 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001054 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001056 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001060 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1061 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062 ScopedObjectAccess soa(env);
1063 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001067 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1068 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001069 ScopedObjectAccess soa(env);
1070 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 va_list ap;
1075 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001076 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1077 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001078 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001081 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 }
1083
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001085 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1086 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001087 ScopedObjectAccess soa(env);
1088 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001092 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1093 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094 ScopedObjectAccess soa(env);
1095 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 va_list ap;
1100 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001101 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1102 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1103 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001104 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001106 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001110 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1111 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001112 ScopedObjectAccess soa(env);
1113 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001117 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1118 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 ScopedObjectAccess soa(env);
1120 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 va_list ap;
1125 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001126 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1127 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001128 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001129 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001131 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001135 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1136 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001137 ScopedObjectAccess soa(env);
1138 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001142 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1143 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001144 ScopedObjectAccess soa(env);
1145 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 }
1147
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 va_list ap;
1150 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001151 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1152 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001153 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001154 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001156 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001160 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1161 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162 ScopedObjectAccess soa(env);
1163 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001167 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1168 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001169 ScopedObjectAccess soa(env);
1170 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 va_list ap;
1175 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001176 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1177 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001178 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001179 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001181 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001185 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1186 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001187 ScopedObjectAccess soa(env);
1188 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001192 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1193 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001194 ScopedObjectAccess soa(env);
1195 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 va_list ap;
1200 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001201 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1202 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001203 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001204 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001209 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1210 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001211 ScopedObjectAccess soa(env);
1212 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001216 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1217 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001218 ScopedObjectAccess soa(env);
1219 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001222 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001225 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1226 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001227 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1229 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_end(ap);
1231 return local_result;
1232 }
1233
Ian Rogersbc939662013-08-15 10:26:54 -07001234 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1235 va_list args) {
1236 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1237 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 ScopedObjectAccess soa(env);
1239 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1240 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
Ian Rogersbc939662013-08-15 10:26:54 -07001243 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1244 jvalue* args) {
1245 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1246 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001247 ScopedObjectAccess soa(env);
1248 JValue result(InvokeWithJValues(soa, obj, mid, args));
1249 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
Ian Rogersbc939662013-08-15 10:26:54 -07001252 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1253 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001256 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1257 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001258 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001259 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001261 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
Ian Rogersbc939662013-08-15 10:26:54 -07001264 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1265 va_list args) {
1266 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1267 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001268 ScopedObjectAccess soa(env);
1269 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
Ian Rogersbc939662013-08-15 10:26:54 -07001272 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1273 jvalue* args) {
1274 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1275 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001276 ScopedObjectAccess soa(env);
1277 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001280 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001283 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1284 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001285 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001286 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001288 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
1290
Ian Rogersbc939662013-08-15 10:26:54 -07001291 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1292 va_list args) {
1293 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1294 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001295 ScopedObjectAccess soa(env);
1296 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
1298
Ian Rogersbc939662013-08-15 10:26:54 -07001299 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1300 jvalue* args) {
1301 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1302 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001303 ScopedObjectAccess soa(env);
1304 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 }
1306
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001307 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001310 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1311 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1312 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001313 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001315 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Ian Rogersbc939662013-08-15 10:26:54 -07001318 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1319 va_list args) {
1320 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1321 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001322 ScopedObjectAccess soa(env);
1323 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
Ian Rogersbc939662013-08-15 10:26:54 -07001326 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1327 jvalue* args) {
1328 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1329 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001330 ScopedObjectAccess soa(env);
1331 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
1333
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001334 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001336 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001337 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1338 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001339 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001340 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001342 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 }
1344
Ian Rogersbc939662013-08-15 10:26:54 -07001345 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1346 va_list args) {
1347 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1348 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001349 ScopedObjectAccess soa(env);
1350 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
Ian Rogersbc939662013-08-15 10:26:54 -07001353 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1354 jvalue* args) {
1355 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1356 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001357 ScopedObjectAccess soa(env);
1358 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 }
1360
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001361 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001363 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001364 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1365 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001366 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001367 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001369 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
Ian Rogersbc939662013-08-15 10:26:54 -07001372 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1373 va_list args) {
1374 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1375 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001376 ScopedObjectAccess soa(env);
1377 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
Ian Rogersbc939662013-08-15 10:26:54 -07001380 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1381 jvalue* args) {
1382 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1383 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001384 ScopedObjectAccess soa(env);
1385 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
1387
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001388 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001390 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001391 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1392 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001393 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001394 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001396 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Ian Rogersbc939662013-08-15 10:26:54 -07001399 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1400 va_list args) {
1401 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1402 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001403 ScopedObjectAccess soa(env);
1404 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
Ian Rogersbc939662013-08-15 10:26:54 -07001407 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1408 jvalue* args) {
1409 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1410 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001411 ScopedObjectAccess soa(env);
1412 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001415 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001417 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001418 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1419 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001420 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001421 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001423 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Ian Rogersbc939662013-08-15 10:26:54 -07001426 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1427 va_list args) {
1428 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1429 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001430 ScopedObjectAccess soa(env);
1431 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Ian Rogersbc939662013-08-15 10:26:54 -07001434 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1435 jvalue* args) {
1436 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1437 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001438 ScopedObjectAccess soa(env);
1439 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001442 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001444 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001445 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1446 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001447 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001448 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001450 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 }
1452
Ian Rogersbc939662013-08-15 10:26:54 -07001453 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1454 va_list args) {
1455 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1456 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001457 ScopedObjectAccess soa(env);
1458 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
1460
Ian Rogersbc939662013-08-15 10:26:54 -07001461 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1462 jvalue* args) {
1463 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1464 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001465 ScopedObjectAccess soa(env);
1466 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 }
1468
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001469 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001471 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001472 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1473 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001474 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001475 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 va_end(ap);
1477 }
1478
Brian Carlstromea46f952013-07-30 01:26:50 -07001479 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1480 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001481 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1482 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001483 ScopedObjectAccess soa(env);
1484 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Ian Rogersbc939662013-08-15 10:26:54 -07001487 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1488 jvalue* args) {
1489 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1490 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001491 ScopedObjectAccess soa(env);
1492 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Ian Rogersbc939662013-08-15 10:26:54 -07001495 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1496 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1497 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1498 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001499 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001500 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001502
Ian Rogersbc939662013-08-15 10:26:54 -07001503 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1504 const char* sig) {
1505 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1506 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1507 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001508 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001509 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001511
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001513 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1514 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001515 ScopedObjectAccess soa(env);
1516 Object* o = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -07001517 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001518 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001520
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001521 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001522 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001523 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -07001524 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001525 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001529 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1530 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001531 ScopedObjectAccess soa(env);
1532 Object* o = soa.Decode<Object*>(java_object);
1533 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001534 ArtField* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001538 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001539 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001540 ScopedObjectAccess soa(env);
1541 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001542 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001543 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001546#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001547 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1548 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001549 ScopedObjectAccess soa(env); \
1550 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001551 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001552 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001553
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001554#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001555 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001556 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001557 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001558 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001559
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001560#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001561 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1562 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001563 ScopedObjectAccess soa(env); \
1564 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001565 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001566 f->Set ##fn(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001567
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001568#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001569 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001570 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001571 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001572 f->Set ##fn(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001573
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001575 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 }
1577
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001579 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001582 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001583 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 }
1585
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001586 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001587 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 }
1589
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001591 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001594 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001595 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 }
1597
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001598 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001599 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 }
1601
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001602 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001603 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 }
1605
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001606 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001607 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 }
1609
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001610 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001611 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 }
1613
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001614 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001615 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001618 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001619 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001622 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001623 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001626 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001627 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001628 }
1629
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001630 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001631 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001632 }
1633
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001634 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001635 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001636 }
1637
1638 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001639 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001640 }
1641
1642 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001643 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001644 }
1645
1646 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001647 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001648 }
1649
1650 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001651 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001652 }
1653
1654 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001655 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001656 }
1657
1658 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001659 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001660 }
1661
1662 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001663 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001664 }
1665
1666 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001667 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001668 }
1669
1670 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001671 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001672 }
1673
1674 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001675 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001676 }
1677
1678 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001679 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001680 }
1681
1682 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001683 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001684 }
1685
1686 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001687 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001688 }
1689
1690 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001691 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001692 }
1693
1694 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001695 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001696 }
1697
1698 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001699 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 }
1701
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001702 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001704 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001705 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001706 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001707 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1708 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 va_end(ap);
1710 return local_result;
1711 }
1712
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001713 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001714 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001715 ScopedObjectAccess soa(env);
1716 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1717 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001720 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001721 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001722 ScopedObjectAccess soa(env);
1723 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1724 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001727 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001729 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001730 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001731 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001732 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001734 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 }
1736
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001737 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001738 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001739 ScopedObjectAccess soa(env);
1740 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001743 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001744 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001745 ScopedObjectAccess soa(env);
1746 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001749 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001751 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001752 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001753 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001754 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001756 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 }
1758
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001759 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001760 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001761 ScopedObjectAccess soa(env);
1762 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 }
1764
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001765 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001766 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001767 ScopedObjectAccess soa(env);
1768 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001771 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001773 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001774 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001775 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001776 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001778 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001781 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001782 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001783 ScopedObjectAccess soa(env);
1784 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001787 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001788 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 ScopedObjectAccess soa(env);
1790 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001793 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001795 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001796 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001797 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001798 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001800 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 }
1802
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001803 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001804 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001805 ScopedObjectAccess soa(env);
1806 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 }
1808
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001809 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001810 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001811 ScopedObjectAccess soa(env);
1812 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001815 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001817 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001818 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001819 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001820 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001822 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001825 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001826 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001827 ScopedObjectAccess soa(env);
1828 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001831 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001832 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001833 ScopedObjectAccess soa(env);
1834 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001837 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001839 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001840 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001841 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001842 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001844 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 }
1846
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001847 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001848 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001849 ScopedObjectAccess soa(env);
1850 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 }
1852
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001853 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001854 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001855 ScopedObjectAccess soa(env);
1856 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 }
1858
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001859 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001861 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001862 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001863 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001864 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001866 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 }
1868
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001869 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001870 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001871 ScopedObjectAccess soa(env);
1872 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 }
1874
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001875 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001876 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001877 ScopedObjectAccess soa(env);
1878 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 }
1880
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001881 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001883 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001884 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001885 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001888 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 }
1890
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001891 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001892 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001893 ScopedObjectAccess soa(env);
1894 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001895 }
1896
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001897 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001898 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001899 ScopedObjectAccess soa(env);
1900 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 }
1902
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001903 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001905 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001906 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001907 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001908 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001909 va_end(ap);
1910 }
1911
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001912 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001913 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001914 ScopedObjectAccess soa(env);
1915 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001916 }
1917
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001918 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001919 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001920 ScopedObjectAccess soa(env);
1921 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 }
1923
Elliott Hughes814e4032011-08-23 12:07:56 -07001924 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogersbc939662013-08-15 10:26:54 -07001925 if (UNLIKELY(chars == NULL && char_count > 0)) { \
1926 JniAbortF("NewString", "char == null && char_count > 0"); \
1927 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001928 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001929 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001930 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 }
1932
1933 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 if (utf == NULL) {
1935 return NULL;
1936 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001937 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001938 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001939 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
Elliott Hughes814e4032011-08-23 12:07:56 -07001942 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001943 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001944 ScopedObjectAccess soa(env);
1945 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001946 }
1947
1948 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001949 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001950 ScopedObjectAccess soa(env);
1951 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001952 }
1953
Ian Rogersbc939662013-08-15 10:26:54 -07001954 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1955 jchar* buf) {
1956 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001957 ScopedObjectAccess soa(env);
1958 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001959 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001960 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001961 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001962 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001963 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1964 memcpy(buf, chars + start, length * sizeof(jchar));
1965 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001966 }
1967
Ian Rogersbc939662013-08-15 10:26:54 -07001968 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1969 char* buf) {
1970 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001971 ScopedObjectAccess soa(env);
1972 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001973 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001974 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001975 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001976 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001977 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1978 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1979 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001980 }
1981
Elliott Hughes75770752011-08-24 17:52:38 -07001982 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07001983 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001984 ScopedObjectAccess soa(env);
1985 String* s = soa.Decode<String*>(java_string);
Mathieu Chartier423d2a32013-09-12 17:33:56 -07001986 CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001987 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001988 if (is_copy != NULL) {
1989 *is_copy = JNI_FALSE;
1990 }
1991 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001992 }
1993
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001994 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogersbc939662013-08-15 10:26:54 -07001995 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001996 ScopedObjectAccess soa(env);
1997 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Elliott Hughes75770752011-08-24 17:52:38 -07002000 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002001 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes75770752011-08-24 17:52:38 -07002004 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002005 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002009 if (java_string == NULL) {
2010 return NULL;
2011 }
2012 if (is_copy != NULL) {
2013 *is_copy = JNI_TRUE;
2014 }
Ian Rogersef28b142012-11-30 14:22:18 -08002015 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002016 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002017 size_t byte_count = s->GetUtfLength();
2018 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002019 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002020 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2021 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2022 bytes[byte_count] = '\0';
2023 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002024 }
2025
Elliott Hughes75770752011-08-24 17:52:38 -07002026 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002027 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002028 }
2029
Elliott Hughesbd935992011-08-22 11:59:34 -07002030 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002031 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002032 ScopedObjectAccess soa(env);
2033 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002034 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002035 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2036 }
Elliott Hughesbd935992011-08-22 11:59:34 -07002037 Array* array = obj->AsArray();
2038 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes814e4032011-08-23 12:07:56 -07002041 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002042 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002043 ScopedObjectAccess soa(env);
2044 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2045 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Ian Rogersbc939662013-08-15 10:26:54 -07002048 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2049 jobject java_value) {
2050 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002051 ScopedObjectAccess soa(env);
2052 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2053 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 array->Set(index, value);
2055 }
2056
2057 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002058 ScopedObjectAccess soa(env);
2059 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
2062 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002063 ScopedObjectAccess soa(env);
2064 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
2067 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002068 ScopedObjectAccess soa(env);
2069 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
2072 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002073 ScopedObjectAccess soa(env);
2074 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
2077 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002078 ScopedObjectAccess soa(env);
2079 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
2082 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002083 ScopedObjectAccess soa(env);
2084 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
2087 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002088 ScopedObjectAccess soa(env);
2089 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
2092 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002093 if (length < 0) {
2094 JniAbortF("NewObjectArray", "negative array length: %d", length);
2095 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002096
2097 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002098 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002099 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 std::string descriptor;
2101 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002102 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002103
2104 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002105 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2106 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 return NULL;
2108 }
2109
Elliott Hughes75770752011-08-24 17:52:38 -07002110 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002111 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07002112 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002113 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002114 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07002115 for (jsize i = 0; i < length; ++i) {
2116 result->Set(i, initial_object);
2117 }
2118 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002119 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
2122 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002123 ScopedObjectAccess soa(env);
2124 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
Ian Rogersa15e67d2012-02-28 13:51:55 -08002127 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002128 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002129 ScopedObjectAccess soa(env);
2130 Array* array = soa.Decode<Array*>(java_array);
2131 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002132 if (is_copy != NULL) {
2133 *is_copy = JNI_FALSE;
2134 }
2135 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002136 }
2137
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002138 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002139 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Ian Rogersef28b142012-11-30 14:22:18 -08002140 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002141 }
2142
Elliott Hughes75770752011-08-24 17:52:38 -07002143 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002144 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002145 ScopedObjectAccess soa(env);
2146 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
Elliott Hughes75770752011-08-24 17:52:38 -07002149 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002150 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002151 ScopedObjectAccess soa(env);
2152 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Elliott Hughes75770752011-08-24 17:52:38 -07002155 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002156 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002157 ScopedObjectAccess soa(env);
2158 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 }
2160
Elliott Hughes75770752011-08-24 17:52:38 -07002161 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002162 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002163 ScopedObjectAccess soa(env);
2164 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Elliott Hughes75770752011-08-24 17:52:38 -07002167 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002168 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002169 ScopedObjectAccess soa(env);
2170 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Elliott Hughes75770752011-08-24 17:52:38 -07002173 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002174 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002175 ScopedObjectAccess soa(env);
2176 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178
Elliott Hughes75770752011-08-24 17:52:38 -07002179 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002180 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002181 ScopedObjectAccess soa(env);
2182 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184
Elliott Hughes75770752011-08-24 17:52:38 -07002185 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002186 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002187 ScopedObjectAccess soa(env);
2188 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002191 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002192 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002195 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002196 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 }
2198
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002199 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002200 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002203 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002204 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 }
2206
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002207 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002208 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002211 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002212 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 }
2214
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002215 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002216 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002219 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002220 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 }
2222
Ian Rogersbc939662013-08-15 10:26:54 -07002223 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2224 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002225 ScopedObjectAccess soa(env);
2226 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 }
2228
Ian Rogersbc939662013-08-15 10:26:54 -07002229 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2230 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002231 ScopedObjectAccess soa(env);
2232 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Ian Rogersbc939662013-08-15 10:26:54 -07002235 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2236 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002237 ScopedObjectAccess soa(env);
2238 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Ian Rogersbc939662013-08-15 10:26:54 -07002241 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2242 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002243 ScopedObjectAccess soa(env);
2244 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Ian Rogersbc939662013-08-15 10:26:54 -07002247 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2248 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002249 ScopedObjectAccess soa(env);
2250 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 }
2252
Ian Rogersbc939662013-08-15 10:26:54 -07002253 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2254 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002255 ScopedObjectAccess soa(env);
2256 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 }
2258
Ian Rogersbc939662013-08-15 10:26:54 -07002259 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2260 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002261 ScopedObjectAccess soa(env);
2262 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
2264
Ian Rogersbc939662013-08-15 10:26:54 -07002265 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2266 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002267 ScopedObjectAccess soa(env);
2268 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 }
2270
Ian Rogersbc939662013-08-15 10:26:54 -07002271 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2272 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002273 ScopedObjectAccess soa(env);
2274 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 }
2276
Ian Rogersbc939662013-08-15 10:26:54 -07002277 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2278 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002279 ScopedObjectAccess soa(env);
2280 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 }
2282
Ian Rogersbc939662013-08-15 10:26:54 -07002283 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2284 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002285 ScopedObjectAccess soa(env);
2286 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002287 }
2288
Ian Rogersbc939662013-08-15 10:26:54 -07002289 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2290 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002291 ScopedObjectAccess soa(env);
2292 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002293 }
2294
Ian Rogersbc939662013-08-15 10:26:54 -07002295 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2296 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002297 ScopedObjectAccess soa(env);
2298 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002299 }
2300
Ian Rogersbc939662013-08-15 10:26:54 -07002301 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2302 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002303 ScopedObjectAccess soa(env);
2304 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002305 }
2306
Ian Rogersbc939662013-08-15 10:26:54 -07002307 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2308 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002309 ScopedObjectAccess soa(env);
2310 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 }
2312
Ian Rogersbc939662013-08-15 10:26:54 -07002313 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2314 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002315 ScopedObjectAccess soa(env);
2316 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002317 }
2318
Ian Rogersbc939662013-08-15 10:26:54 -07002319 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2320 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002321 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2322 }
2323
Ian Rogersbc939662013-08-15 10:26:54 -07002324 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2325 jint method_count, bool return_errors) {
2326 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002327 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002328 return JNI_ERR; // Not reached.
2329 }
2330 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002331 ScopedObjectAccess soa(env);
2332 Class* c = soa.Decode<Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002333 if (UNLIKELY(method_count == 0)) {
2334 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2335 << PrettyDescriptor(c);
2336 return JNI_OK;
2337 }
2338 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2339 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002340 const char* name = methods[i].name;
2341 const char* sig = methods[i].signature;
2342
2343 if (*sig == '!') {
2344 // TODO: fast jni. it's too noisy to log all these.
2345 ++sig;
2346 }
2347
Brian Carlstromea46f952013-07-30 01:26:50 -07002348 ArtMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002349 if (m == NULL) {
2350 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002351 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002352 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002353 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002354 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002355 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002356 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002357 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002358 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002359 << PrettyDescriptor(c) << "." << name << sig
2360 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002361 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002362 return JNI_ERR;
2363 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002364
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002365 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002366
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002367 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002368 }
2369 return JNI_OK;
2370 }
2371
Elliott Hughes5174fe62011-08-23 15:12:35 -07002372 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002373 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002374 ScopedObjectAccess soa(env);
2375 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002376
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002377 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002378
2379 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002380 ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002381 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002382 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002383 }
2384 }
2385 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002386 ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002387 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002388 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002389 }
2390 }
2391
2392 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002393 }
2394
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002395 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2396 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002397 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002398 ScopedObjectAccess soa(env);
2399 Object* o = soa.Decode<Object*>(java_object);
2400 o->MonitorEnter(soa.Self());
2401 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002402 return JNI_ERR;
2403 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002404 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002405 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002406 }
2407
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002408 static jint MonitorExit(JNIEnv* env, jobject java_object)
2409 UNLOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002410 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002411 ScopedObjectAccess soa(env);
2412 Object* o = soa.Decode<Object*>(java_object);
2413 o->MonitorExit(soa.Self());
2414 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002415 return JNI_ERR;
2416 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002417 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002418 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002419 }
2420
2421 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002422 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002423 Runtime* runtime = Runtime::Current();
2424 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002425 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002426 } else {
2427 *vm = NULL;
2428 }
2429 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2430 }
2431
Elliott Hughescdf53122011-08-19 15:46:09 -07002432 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002433 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002434 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002435 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002436 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002437 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002438 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002439
Elliott Hughes96a98872012-12-19 14:21:15 -08002440 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002441 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2442 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002443 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002444 jint capacity_arg = static_cast<jint>(capacity);
2445
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002446 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2447 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002448 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002449 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002450 }
2451
Elliott Hughesb465ab02011-08-24 11:21:21 -07002452 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002453 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002454 }
2455
Elliott Hughesb465ab02011-08-24 11:21:21 -07002456 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002457 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002458 }
2459
Elliott Hughesb465ab02011-08-24 11:21:21 -07002460 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002461 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002462
2463 // Do we definitely know what kind of reference this is?
2464 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2465 IndirectRefKind kind = GetIndirectRefKind(ref);
2466 switch (kind) {
2467 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002468 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002469 return JNILocalRefType;
2470 }
2471 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002472 case kGlobal:
2473 return JNIGlobalRefType;
2474 case kWeakGlobal:
2475 return JNIWeakGlobalRefType;
2476 case kSirtOrInvalid:
2477 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002478 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002479 return JNILocalRefType;
2480 }
2481
Ian Rogersef28b142012-11-30 14:22:18 -08002482 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002483 return JNIInvalidRefType;
2484 }
2485
Elliott Hughesb465ab02011-08-24 11:21:21 -07002486 // If we're handing out direct pointers, check whether it's a direct pointer
2487 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002488 {
2489 ScopedObjectAccess soa(env);
2490 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2491 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2492 return JNILocalRefType;
2493 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002494 }
2495 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002496 return JNIInvalidRefType;
2497 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002498 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2499 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002500 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002501
2502 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002503 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2504 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002505 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002506 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002507 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2508 return JNI_ERR;
2509 }
2510 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002511 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002512 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2513 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002514 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002515 soa.Self()->ThrowOutOfMemoryError(caller);
2516 }
2517 return okay ? JNI_OK : JNI_ERR;
2518 }
2519
2520 template<typename JniT, typename ArtT>
2521 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002522 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002523 if (length < 0) {
2524 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2525 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002526 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002527 return soa.AddLocalReference<JniT>(result);
2528 }
2529
2530 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2531 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2532 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002533 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002534 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2535 PinPrimitiveArray(soa, array);
2536 if (is_copy != NULL) {
2537 *is_copy = JNI_FALSE;
2538 }
2539 return array->GetData();
2540 }
2541
2542 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002543 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002544 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002545 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002546 Array* array = soa.Decode<Array*>(java_array);
2547 UnpinPrimitiveArray(soa, array);
2548 }
2549 }
2550
2551 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2552 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2553 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002554 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002555 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002556 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2557 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2558 ThrowAIOOBE(soa, array, start, length, "src");
2559 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002560 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002561 JavaT* data = array->GetData();
2562 memcpy(buf, data + start, length * sizeof(JavaT));
2563 }
2564 }
2565
2566 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2567 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2568 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002569 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002570 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002571 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2572 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2573 ThrowAIOOBE(soa, array, start, length, "dst");
2574 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002575 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002576 JavaT* data = array->GetData();
2577 memcpy(data + start, buf, length * sizeof(JavaT));
2578 }
2579 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002580};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002581
Elliott Hughes88c5c352012-03-15 18:49:48 -07002582const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002583 NULL, // reserved0.
2584 NULL, // reserved1.
2585 NULL, // reserved2.
2586 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002587 JNI::GetVersion,
2588 JNI::DefineClass,
2589 JNI::FindClass,
2590 JNI::FromReflectedMethod,
2591 JNI::FromReflectedField,
2592 JNI::ToReflectedMethod,
2593 JNI::GetSuperclass,
2594 JNI::IsAssignableFrom,
2595 JNI::ToReflectedField,
2596 JNI::Throw,
2597 JNI::ThrowNew,
2598 JNI::ExceptionOccurred,
2599 JNI::ExceptionDescribe,
2600 JNI::ExceptionClear,
2601 JNI::FatalError,
2602 JNI::PushLocalFrame,
2603 JNI::PopLocalFrame,
2604 JNI::NewGlobalRef,
2605 JNI::DeleteGlobalRef,
2606 JNI::DeleteLocalRef,
2607 JNI::IsSameObject,
2608 JNI::NewLocalRef,
2609 JNI::EnsureLocalCapacity,
2610 JNI::AllocObject,
2611 JNI::NewObject,
2612 JNI::NewObjectV,
2613 JNI::NewObjectA,
2614 JNI::GetObjectClass,
2615 JNI::IsInstanceOf,
2616 JNI::GetMethodID,
2617 JNI::CallObjectMethod,
2618 JNI::CallObjectMethodV,
2619 JNI::CallObjectMethodA,
2620 JNI::CallBooleanMethod,
2621 JNI::CallBooleanMethodV,
2622 JNI::CallBooleanMethodA,
2623 JNI::CallByteMethod,
2624 JNI::CallByteMethodV,
2625 JNI::CallByteMethodA,
2626 JNI::CallCharMethod,
2627 JNI::CallCharMethodV,
2628 JNI::CallCharMethodA,
2629 JNI::CallShortMethod,
2630 JNI::CallShortMethodV,
2631 JNI::CallShortMethodA,
2632 JNI::CallIntMethod,
2633 JNI::CallIntMethodV,
2634 JNI::CallIntMethodA,
2635 JNI::CallLongMethod,
2636 JNI::CallLongMethodV,
2637 JNI::CallLongMethodA,
2638 JNI::CallFloatMethod,
2639 JNI::CallFloatMethodV,
2640 JNI::CallFloatMethodA,
2641 JNI::CallDoubleMethod,
2642 JNI::CallDoubleMethodV,
2643 JNI::CallDoubleMethodA,
2644 JNI::CallVoidMethod,
2645 JNI::CallVoidMethodV,
2646 JNI::CallVoidMethodA,
2647 JNI::CallNonvirtualObjectMethod,
2648 JNI::CallNonvirtualObjectMethodV,
2649 JNI::CallNonvirtualObjectMethodA,
2650 JNI::CallNonvirtualBooleanMethod,
2651 JNI::CallNonvirtualBooleanMethodV,
2652 JNI::CallNonvirtualBooleanMethodA,
2653 JNI::CallNonvirtualByteMethod,
2654 JNI::CallNonvirtualByteMethodV,
2655 JNI::CallNonvirtualByteMethodA,
2656 JNI::CallNonvirtualCharMethod,
2657 JNI::CallNonvirtualCharMethodV,
2658 JNI::CallNonvirtualCharMethodA,
2659 JNI::CallNonvirtualShortMethod,
2660 JNI::CallNonvirtualShortMethodV,
2661 JNI::CallNonvirtualShortMethodA,
2662 JNI::CallNonvirtualIntMethod,
2663 JNI::CallNonvirtualIntMethodV,
2664 JNI::CallNonvirtualIntMethodA,
2665 JNI::CallNonvirtualLongMethod,
2666 JNI::CallNonvirtualLongMethodV,
2667 JNI::CallNonvirtualLongMethodA,
2668 JNI::CallNonvirtualFloatMethod,
2669 JNI::CallNonvirtualFloatMethodV,
2670 JNI::CallNonvirtualFloatMethodA,
2671 JNI::CallNonvirtualDoubleMethod,
2672 JNI::CallNonvirtualDoubleMethodV,
2673 JNI::CallNonvirtualDoubleMethodA,
2674 JNI::CallNonvirtualVoidMethod,
2675 JNI::CallNonvirtualVoidMethodV,
2676 JNI::CallNonvirtualVoidMethodA,
2677 JNI::GetFieldID,
2678 JNI::GetObjectField,
2679 JNI::GetBooleanField,
2680 JNI::GetByteField,
2681 JNI::GetCharField,
2682 JNI::GetShortField,
2683 JNI::GetIntField,
2684 JNI::GetLongField,
2685 JNI::GetFloatField,
2686 JNI::GetDoubleField,
2687 JNI::SetObjectField,
2688 JNI::SetBooleanField,
2689 JNI::SetByteField,
2690 JNI::SetCharField,
2691 JNI::SetShortField,
2692 JNI::SetIntField,
2693 JNI::SetLongField,
2694 JNI::SetFloatField,
2695 JNI::SetDoubleField,
2696 JNI::GetStaticMethodID,
2697 JNI::CallStaticObjectMethod,
2698 JNI::CallStaticObjectMethodV,
2699 JNI::CallStaticObjectMethodA,
2700 JNI::CallStaticBooleanMethod,
2701 JNI::CallStaticBooleanMethodV,
2702 JNI::CallStaticBooleanMethodA,
2703 JNI::CallStaticByteMethod,
2704 JNI::CallStaticByteMethodV,
2705 JNI::CallStaticByteMethodA,
2706 JNI::CallStaticCharMethod,
2707 JNI::CallStaticCharMethodV,
2708 JNI::CallStaticCharMethodA,
2709 JNI::CallStaticShortMethod,
2710 JNI::CallStaticShortMethodV,
2711 JNI::CallStaticShortMethodA,
2712 JNI::CallStaticIntMethod,
2713 JNI::CallStaticIntMethodV,
2714 JNI::CallStaticIntMethodA,
2715 JNI::CallStaticLongMethod,
2716 JNI::CallStaticLongMethodV,
2717 JNI::CallStaticLongMethodA,
2718 JNI::CallStaticFloatMethod,
2719 JNI::CallStaticFloatMethodV,
2720 JNI::CallStaticFloatMethodA,
2721 JNI::CallStaticDoubleMethod,
2722 JNI::CallStaticDoubleMethodV,
2723 JNI::CallStaticDoubleMethodA,
2724 JNI::CallStaticVoidMethod,
2725 JNI::CallStaticVoidMethodV,
2726 JNI::CallStaticVoidMethodA,
2727 JNI::GetStaticFieldID,
2728 JNI::GetStaticObjectField,
2729 JNI::GetStaticBooleanField,
2730 JNI::GetStaticByteField,
2731 JNI::GetStaticCharField,
2732 JNI::GetStaticShortField,
2733 JNI::GetStaticIntField,
2734 JNI::GetStaticLongField,
2735 JNI::GetStaticFloatField,
2736 JNI::GetStaticDoubleField,
2737 JNI::SetStaticObjectField,
2738 JNI::SetStaticBooleanField,
2739 JNI::SetStaticByteField,
2740 JNI::SetStaticCharField,
2741 JNI::SetStaticShortField,
2742 JNI::SetStaticIntField,
2743 JNI::SetStaticLongField,
2744 JNI::SetStaticFloatField,
2745 JNI::SetStaticDoubleField,
2746 JNI::NewString,
2747 JNI::GetStringLength,
2748 JNI::GetStringChars,
2749 JNI::ReleaseStringChars,
2750 JNI::NewStringUTF,
2751 JNI::GetStringUTFLength,
2752 JNI::GetStringUTFChars,
2753 JNI::ReleaseStringUTFChars,
2754 JNI::GetArrayLength,
2755 JNI::NewObjectArray,
2756 JNI::GetObjectArrayElement,
2757 JNI::SetObjectArrayElement,
2758 JNI::NewBooleanArray,
2759 JNI::NewByteArray,
2760 JNI::NewCharArray,
2761 JNI::NewShortArray,
2762 JNI::NewIntArray,
2763 JNI::NewLongArray,
2764 JNI::NewFloatArray,
2765 JNI::NewDoubleArray,
2766 JNI::GetBooleanArrayElements,
2767 JNI::GetByteArrayElements,
2768 JNI::GetCharArrayElements,
2769 JNI::GetShortArrayElements,
2770 JNI::GetIntArrayElements,
2771 JNI::GetLongArrayElements,
2772 JNI::GetFloatArrayElements,
2773 JNI::GetDoubleArrayElements,
2774 JNI::ReleaseBooleanArrayElements,
2775 JNI::ReleaseByteArrayElements,
2776 JNI::ReleaseCharArrayElements,
2777 JNI::ReleaseShortArrayElements,
2778 JNI::ReleaseIntArrayElements,
2779 JNI::ReleaseLongArrayElements,
2780 JNI::ReleaseFloatArrayElements,
2781 JNI::ReleaseDoubleArrayElements,
2782 JNI::GetBooleanArrayRegion,
2783 JNI::GetByteArrayRegion,
2784 JNI::GetCharArrayRegion,
2785 JNI::GetShortArrayRegion,
2786 JNI::GetIntArrayRegion,
2787 JNI::GetLongArrayRegion,
2788 JNI::GetFloatArrayRegion,
2789 JNI::GetDoubleArrayRegion,
2790 JNI::SetBooleanArrayRegion,
2791 JNI::SetByteArrayRegion,
2792 JNI::SetCharArrayRegion,
2793 JNI::SetShortArrayRegion,
2794 JNI::SetIntArrayRegion,
2795 JNI::SetLongArrayRegion,
2796 JNI::SetFloatArrayRegion,
2797 JNI::SetDoubleArrayRegion,
2798 JNI::RegisterNatives,
2799 JNI::UnregisterNatives,
2800 JNI::MonitorEnter,
2801 JNI::MonitorExit,
2802 JNI::GetJavaVM,
2803 JNI::GetStringRegion,
2804 JNI::GetStringUTFRegion,
2805 JNI::GetPrimitiveArrayCritical,
2806 JNI::ReleasePrimitiveArrayCritical,
2807 JNI::GetStringCritical,
2808 JNI::ReleaseStringCritical,
2809 JNI::NewWeakGlobalRef,
2810 JNI::DeleteWeakGlobalRef,
2811 JNI::ExceptionCheck,
2812 JNI::NewDirectByteBuffer,
2813 JNI::GetDirectBufferAddress,
2814 JNI::GetDirectBufferCapacity,
2815 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002816};
2817
Elliott Hughes75770752011-08-24 17:52:38 -07002818JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002819 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002820 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002821 local_ref_cookie(IRT_FIRST_SEGMENT),
2822 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002823 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002824 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002825 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002826 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002827 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002828 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002829 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002830 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2831 // errors will ensue.
2832 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2833 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002834}
2835
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002836JNIEnvExt::~JNIEnvExt() {
2837}
2838
Elliott Hughes88c5c352012-03-15 18:49:48 -07002839void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2840 check_jni = enabled;
2841 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002842}
2843
Elliott Hughes73e66f72012-05-09 09:34:45 -07002844void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2845 locals.Dump(os);
2846 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002847}
2848
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002849void JNIEnvExt::PushFrame(int /*capacity*/) {
2850 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002851 stacked_local_ref_cookies.push_back(local_ref_cookie);
2852 local_ref_cookie = locals.GetSegmentState();
2853}
2854
2855void JNIEnvExt::PopFrame() {
2856 locals.SetSegmentState(local_ref_cookie);
2857 local_ref_cookie = stacked_local_ref_cookies.back();
2858 stacked_local_ref_cookies.pop_back();
2859}
2860
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002861Offset JNIEnvExt::SegmentStateOffset() {
2862 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2863 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2864}
2865
Carl Shapiroea4dca82011-08-01 13:45:38 -07002866// JNI Invocation interface.
2867
Brian Carlstrombddf9762013-05-14 11:35:37 -07002868extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002869 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002870 if (IsBadJniVersion(args->version)) {
2871 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002872 return JNI_EVERSION;
2873 }
2874 Runtime::Options options;
2875 for (int i = 0; i < args->nOptions; ++i) {
2876 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002877 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002878 }
2879 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002880 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002881 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002882 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002883 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002884 bool started = runtime->Start();
2885 if (!started) {
2886 delete Thread::Current()->GetJniEnv();
2887 delete runtime->GetJavaVM();
2888 LOG(WARNING) << "CreateJavaVM failed";
2889 return JNI_ERR;
2890 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002891 *p_env = Thread::Current()->GetJniEnv();
2892 *p_vm = runtime->GetJavaVM();
2893 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002894}
2895
Elliott Hughesf2682d52011-08-15 16:37:04 -07002896extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002897 Runtime* runtime = Runtime::Current();
2898 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002899 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002900 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002901 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002902 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002903 }
2904 return JNI_OK;
2905}
2906
2907// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002908extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002909 return JNI_ERR;
2910}
2911
Elliott Hughescdf53122011-08-19 15:46:09 -07002912class JII {
2913 public:
2914 static jint DestroyJavaVM(JavaVM* vm) {
2915 if (vm == NULL) {
2916 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002917 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002918 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2919 delete raw_vm->runtime;
2920 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002921 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002922
Elliott Hughescdf53122011-08-19 15:46:09 -07002923 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002924 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002925 }
2926
2927 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002928 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002929 }
2930
2931 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002932 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002933 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002934 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002935 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2936 Runtime* runtime = raw_vm->runtime;
2937 runtime->DetachCurrentThread();
2938 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002939 }
2940
2941 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002942 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2943 // and unlike other calls that take a JNI version doesn't care if you supply
2944 // JNI_VERSION_1_1, which we don't otherwise support.
2945 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002946 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002947 return JNI_EVERSION;
2948 }
2949 if (vm == NULL || env == NULL) {
2950 return JNI_ERR;
2951 }
2952 Thread* thread = Thread::Current();
2953 if (thread == NULL) {
2954 *env = NULL;
2955 return JNI_EDETACHED;
2956 }
2957 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002958 return JNI_OK;
2959 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002960};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002961
Elliott Hughes88c5c352012-03-15 18:49:48 -07002962const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002963 NULL, // reserved0
2964 NULL, // reserved1
2965 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002966 JII::DestroyJavaVM,
2967 JII::AttachCurrentThread,
2968 JII::DetachCurrentThread,
2969 JII::GetEnv,
2970 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002971};
2972
Elliott Hughesa0957642011-09-02 14:27:33 -07002973JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002974 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002975 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002976 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002977 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002978 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002979 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002980 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08002981 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002982 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002983 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002984 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002985 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07002986 libraries(new Libraries),
2987 weak_globals_lock_("JNI weak global reference table lock"),
2988 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2989 allow_new_weak_globals_(true),
2990 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002991 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002992 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002993 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002994 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002995}
2996
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002997JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002998 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002999}
3000
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003001jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3002 if (obj == nullptr) {
3003 return nullptr;
3004 }
3005 MutexLock mu(self, weak_globals_lock_);
3006 while (UNLIKELY(!allow_new_weak_globals_)) {
3007 weak_globals_add_condition_.WaitHoldingLocks(self);
3008 }
3009 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3010 return reinterpret_cast<jweak>(ref);
3011}
3012
3013void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3014 MutexLock mu(self, weak_globals_lock_);
3015 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3016 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3017 << "failed to find entry";
3018 }
3019}
3020
Elliott Hughes88c5c352012-03-15 18:49:48 -07003021void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3022 check_jni = enabled;
3023 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003024}
3025
Elliott Hughesae80b492012-04-24 10:43:17 -07003026void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3027 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3028 if (force_copy) {
3029 os << " (with forcecopy)";
3030 }
3031 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003032 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003033 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003034 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003035 os << "; pins=" << pin_table.Size();
3036 }
3037 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003038 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003039 os << "; globals=" << globals.Capacity();
3040 }
3041 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003042 MutexLock mu(self, weak_globals_lock_);
3043 if (weak_globals_.Capacity() > 0) {
3044 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003045 }
3046 }
3047 os << '\n';
3048
3049 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003050 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003051 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3052 }
3053}
3054
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003055void JavaVMExt::DisallowNewWeakGlobals() {
3056 MutexLock mu(Thread::Current(), weak_globals_lock_);
3057 allow_new_weak_globals_ = false;
3058}
3059
3060void JavaVMExt::AllowNewWeakGlobals() {
3061 Thread* self = Thread::Current();
3062 MutexLock mu(self, weak_globals_lock_);
3063 allow_new_weak_globals_ = true;
3064 weak_globals_add_condition_.Broadcast(self);
3065}
3066
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003067mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3068 MutexLock mu(self, weak_globals_lock_);
3069 while (UNLIKELY(!allow_new_weak_globals_)) {
3070 weak_globals_add_condition_.WaitHoldingLocks(self);
3071 }
3072 return const_cast<mirror::Object*>(weak_globals_.Get(ref));
3073}
3074
Elliott Hughes73e66f72012-05-09 09:34:45 -07003075void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003076 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003077 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003078 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003079 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003080 }
3081 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003082 MutexLock mu(self, weak_globals_lock_);
3083 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003084 }
3085 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003086 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003087 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003088 }
3089}
3090
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003091bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
3092 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07003093 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003094
3095 // See if we've already loaded this library. If we have, and the class loader
3096 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003097 // TODO: for better results we should canonicalize the pathname (or even compare
3098 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003099 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003100 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003101 {
3102 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003103 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003104 library = libraries->Get(path);
3105 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003106 if (library != NULL) {
3107 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07003108 // The library will be associated with class_loader. The JNI
3109 // spec says we can't load the same library into more than one
3110 // class loader.
3111 StringAppendF(&detail, "Shared library \"%s\" already opened by "
3112 "ClassLoader %p; can't open in ClassLoader %p",
3113 path.c_str(), library->GetClassLoader(), class_loader);
3114 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003115 return false;
3116 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003117 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
3118 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003119 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003120 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
3121 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003122 return false;
3123 }
3124 return true;
3125 }
3126
3127 // Open the shared library. Because we're using a full path, the system
3128 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3129 // resolve this library's dependencies though.)
3130
3131 // Failures here are expected when java.library.path has several entries
3132 // and we have to hunt for the lib.
3133
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003134 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3135 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3136 // dlopen) becomes zero from dlclose.
3137
Elliott Hughescdf53122011-08-19 15:46:09 -07003138 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003139 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003140 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
3141 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
3142 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003143
Elliott Hughes84b2f142012-09-27 09:16:28 -07003144 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003145
3146 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07003147 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003148 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003149 return false;
3150 }
3151
3152 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003153 // TODO: move the locking (and more of this logic) into Libraries.
3154 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003155 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003156 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003157 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003158 if (library == NULL) { // We won race to get libraries_lock
3159 library = new SharedLibrary(path, handle, class_loader);
3160 libraries->Put(path, library);
3161 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003162 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003163 }
3164 if (!created_library) {
3165 LOG(INFO) << "WOW: we lost a race to add shared library: "
3166 << "\"" << path << "\" ClassLoader=" << class_loader;
3167 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003168 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003169
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003170 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003171
Elliott Hughes79353722013-08-02 16:52:18 -07003172 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003173 void* sym = dlsym(handle, "JNI_OnLoad");
3174 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003175 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003176 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003177 } else {
3178 // Call JNI_OnLoad. We have to override the current class
3179 // loader, which will always be "null" since the stuff at the
3180 // top of the stack is around Runtime.loadLibrary(). (See
3181 // the comments in the JNI FindClass function.)
3182 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3183 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07003184 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07003185 self->SetClassLoaderOverride(class_loader);
3186
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003187 int version = 0;
3188 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003189 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003190 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003191 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07003192 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003193
Brian Carlstromaded5f72011-10-07 17:15:04 -07003194 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07003195
Elliott Hughes79353722013-08-02 16:52:18 -07003196 if (version == JNI_ERR) {
3197 StringAppendF(&detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
3198 } else if (IsBadJniVersion(version)) {
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003199 StringAppendF(&detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
3200 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003201 // It's unwise to call dlclose() here, but we can mark it
3202 // as bad and ensure that future load attempts will fail.
3203 // We don't know how far JNI_OnLoad got, so there could
3204 // be some partially-initialized stuff accessible through
3205 // newly-registered native method calls. We could try to
3206 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003207 } else {
3208 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003209 }
Elliott Hughes79353722013-08-02 16:52:18 -07003210 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003211 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003212 }
3213
Elliott Hughes79353722013-08-02 16:52:18 -07003214 library->SetResult(was_successful);
3215 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003216}
3217
Brian Carlstromea46f952013-07-30 01:26:50 -07003218void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003219 CHECK(m->IsNative());
3220
3221 Class* c = m->GetDeclaringClass();
3222
3223 // If this is a static method, it could be called before the class
3224 // has been initialized.
3225 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07003226 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003227 return NULL;
3228 }
3229 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003230 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003231 }
3232
Brian Carlstrom16192862011-09-12 17:50:06 -07003233 std::string detail;
3234 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003235 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003236 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003237 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003238 native_method = libraries->FindNativeMethod(m, detail);
3239 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003240 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07003241 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003242 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3243 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003244 }
3245 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003246}
3247
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003248void JavaVMExt::SweepJniWeakGlobals(RootVisitor visitor, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003249 MutexLock mu(Thread::Current(), weak_globals_lock_);
3250 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003251 mirror::Object* obj = *entry;
3252 mirror::Object* new_obj = visitor(obj, arg);
3253 if (new_obj == nullptr) {
3254 new_obj = kClearedJniWeakGlobal;
3255 }
3256 *entry = new_obj;
3257 }
3258}
3259
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003260void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003261 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003262 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003263 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003264 globals.VisitRoots(visitor, arg);
3265 }
3266 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003267 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003268 pin_table.VisitRoots(visitor, arg);
3269 }
3270 // The weak_globals table is visited by the GC itself (because it mutates the table).
3271}
3272
Elliott Hughesc8fece32013-01-02 11:27:23 -08003273void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003274 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003275 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
3276 if (c.get() == NULL) {
3277 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3278 }
3279 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3280}
3281
Ian Rogersdf20fe02011-07-20 20:34:16 -07003282} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003283
3284std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3285 switch (rhs) {
3286 case JNIInvalidRefType:
3287 os << "JNIInvalidRefType";
3288 return os;
3289 case JNILocalRefType:
3290 os << "JNILocalRefType";
3291 return os;
3292 case JNIGlobalRefType:
3293 os << "JNIGlobalRefType";
3294 return os;
3295 case JNIWeakGlobalRefType:
3296 os << "JNIWeakGlobalRefType";
3297 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003298 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003299 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003300 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003301 }
3302}