blob: 466edebf5914ea7980474b2fd65e0c076411511e [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
Mathieu Chartier590fee92013-09-13 13:46:47 -070025#include "atomic_integer.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080027#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080028#include "base/stl_util.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080029#include "base/stringpiece.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070030#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070033#include "interpreter/interpreter.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070034#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070035#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070036#include "mirror/art_field-inl.h"
37#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/class-inl.h"
39#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
42#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080043#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070044#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070045#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070047#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070048#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070050#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070051#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070052
Brian Carlstromea46f952013-07-30 01:26:50 -070053using ::art::mirror::ArtField;
54using ::art::mirror::ArtMethod;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070055using ::art::mirror::Array;
56using ::art::mirror::BooleanArray;
57using ::art::mirror::ByteArray;
58using ::art::mirror::CharArray;
59using ::art::mirror::Class;
60using ::art::mirror::ClassLoader;
61using ::art::mirror::DoubleArray;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070062using ::art::mirror::FloatArray;
63using ::art::mirror::IntArray;
64using ::art::mirror::LongArray;
65using ::art::mirror::Object;
66using ::art::mirror::ObjectArray;
67using ::art::mirror::ShortArray;
68using ::art::mirror::String;
69using ::art::mirror::Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070071namespace art {
72
Brian Carlstrom7934ac22013-07-26 10:54:15 -070073static const size_t kMonitorsInitial = 32; // Arbitrary.
74static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070075
Brian Carlstrom7934ac22013-07-26 10:54:15 -070076static const size_t kLocalsInitial = 64; // Arbitrary.
77static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070078
Brian Carlstrom7934ac22013-07-26 10:54:15 -070079static const size_t kPinTableInitial = 16; // Arbitrary.
80static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070081
Brian Carlstrom7934ac22013-07-26 10:54:15 -070082static size_t gGlobalsInitial = 512; // Arbitrary.
83static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070084
Brian Carlstrom7934ac22013-07-26 10:54:15 -070085static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
86static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070087
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070090 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070091}
92
Jeff Hao19c5d372013-03-15 14:33:43 -070093static bool IsBadJniVersion(int version) {
94 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
95 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
96}
97
Brian Carlstromea46f952013-07-30 01:26:50 -070098static void CheckMethodArguments(ArtMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070099 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700100 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -0700101 const DexFile::TypeList* params = mh.GetParameterTypeList();
102 if (params == NULL) {
103 return; // No arguments so nothing to check.
104 }
Jeff Hao5d917302013-02-27 17:57:33 -0800105 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700106 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700107 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -0800108 if (!m->IsStatic()) {
109 offset = 1;
110 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700111 for (uint32_t i = 0; i < num_params; i++) {
112 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
113 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
114 if (param_type == NULL) {
115 Thread* self = Thread::Current();
116 CHECK(self->IsExceptionPending());
117 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
118 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800119 << self->GetException(NULL)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700120 self->ClearException();
121 ++error_count;
122 } else if (!param_type->IsPrimitive()) {
123 // TODO: check primitives are in range.
Jeff Hao5d917302013-02-27 17:57:33 -0800124 Object* argument = reinterpret_cast<Object*>(args[i + offset]);
Ian Rogers50b35e22012-10-04 10:09:15 -0700125 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700126 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
127 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
128 ++error_count;
129 }
Jeff Hao5d917302013-02-27 17:57:33 -0800130 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
131 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700132 }
133 }
134 if (error_count > 0) {
135 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
136 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700137 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700138 }
139}
Elliott Hughesb264f082012-04-06 17:10:10 -0700140
Brian Carlstromea46f952013-07-30 01:26:50 -0700141void InvokeWithArgArray(const ScopedObjectAccess& soa, ArtMethod* method,
Jeff Hao6474d192013-03-26 14:08:09 -0700142 ArgArray* arg_array, JValue* result, char result_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700144 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700146 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700147 }
Sebastien Hertz7d658cf2013-07-09 10:56:11 +0200148 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700149}
150
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
152 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700154 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700155 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700156 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800157 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700158 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800159 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700160 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
161 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700162}
163
Brian Carlstromea46f952013-07-30 01:26:50 -0700164static ArtMethod* FindVirtualMethod(Object* receiver, ArtMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700166 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700167}
168
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700169static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
170 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700172 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700173 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700174 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800175 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700176 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800177 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700178 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
179 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700180}
181
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700182static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
183 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700185 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700186 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700187 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800188 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700189 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800190 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700191 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
192 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700193}
194
Elliott Hughes6b436852011-08-12 10:16:44 -0700195// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
196// separated with slashes but aren't wrapped with "L;" like regular descriptors
197// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
198// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
199// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700200static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700201 std::string result;
202 // Add the missing "L;" if necessary.
203 if (name[0] == '[') {
204 result = name;
205 } else {
206 result += 'L';
207 result += name;
208 result += ';';
209 }
210 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700211 if (result.find('.') != std::string::npos) {
212 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
213 << "\"" << name << "\"";
214 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700215 }
216 return result;
217}
218
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
220 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800222 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
223 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
224 "no %s method \"%s.%s%s\"",
225 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700226}
227
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
229 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700232 DCHECK(c != nullptr);
Ian Rogers0045a292012-03-31 21:08:41 -0700233 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700234 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700235 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700236
Brian Carlstromea46f952013-07-30 01:26:50 -0700237 ArtMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700238 if (is_static) {
239 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700240 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700241 method = c->FindVirtualMethod(name, sig);
242 if (method == NULL) {
243 // No virtual method matching the signature. Search declared
244 // private methods and constructors.
245 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700246 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700247 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700248
Elliott Hughescdf53122011-08-19 15:46:09 -0700249 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700250 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700251 return NULL;
252 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700253
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700255}
256
Ian Rogersef28b142012-11-30 14:22:18 -0800257static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700258 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700259 ArtMethod* method = soa.Self()->GetCurrentMethod(NULL);
Brian Carlstromce888532013-10-10 00:32:58 -0700260 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
261 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800262 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700263 }
Brian Carlstromce888532013-10-10 00:32:58 -0700264 // If we have a method, use its ClassLoader for context.
265 if (method != NULL) {
266 return method->GetDeclaringClass()->GetClassLoader();
267 }
268 // We don't have a method, so try to use the system ClassLoader.
269 ClassLoader* class_loader = soa.Decode<ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
270 if (class_loader != NULL) {
271 return class_loader;
272 }
273 // See if the override ClassLoader is set for gtests.
274 class_loader = soa.Self()->GetClassLoaderOverride();
275 if (class_loader != NULL) {
276 // If so, CommonTest should have set UseCompileTimeClassPath.
277 CHECK(Runtime::Current()->UseCompileTimeClassPath());
278 return class_loader;
279 }
280 // Use the BOOTCLASSPATH.
281 return NULL;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700282}
283
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
285 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700286 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700288 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700289 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700290 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700291
Brian Carlstromea46f952013-07-30 01:26:50 -0700292 ArtField* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700293 Class* field_type;
294 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
295 if (sig[1] != '\0') {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700296 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
297 field_type = class_linker->FindClass(sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700298 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700299 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700300 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 if (field_type == NULL) {
302 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800304 ThrowLocation throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700305 SirtRef<Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700306 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800307 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
308 "no type \"%s\" found and so no field \"%s\" could be found in class "
309 "\"%s\" or its superclasses", sig, name,
310 ClassHelper(c).GetDescriptor());
311 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 return NULL;
313 }
314 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800315 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700316 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800317 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700318 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700319 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800320 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
321 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
322 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
323 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700324 return NULL;
325 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700327}
328
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700329static void PinPrimitiveArray(const ScopedObjectAccess& soa, Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700330 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700332 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700333 vm->pin_table.Add(array);
334}
335
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700336static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700338 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700339 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700340 vm->pin_table.Remove(array);
341}
342
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
344 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700346 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800347 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
348 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
349 "%s offset=%d length=%d %s.length=%d",
350 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700351}
Ian Rogers0571d352011-11-03 19:51:38 -0700352
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700353static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
354 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800356 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
357 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
358 "offset=%d length=%d string.length()=%d", start, length,
359 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700360}
Elliott Hughes814e4032011-08-23 12:07:56 -0700361
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700362int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700363 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700364 // Turn the const char* into a java.lang.String.
365 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
366 if (msg != NULL && s.get() == NULL) {
367 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700368 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700369
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 // Choose an appropriate constructor and set up the arguments.
371 jvalue args[2];
372 const char* signature;
373 if (msg == NULL && cause == NULL) {
374 signature = "()V";
375 } else if (msg != NULL && cause == NULL) {
376 signature = "(Ljava/lang/String;)V";
377 args[0].l = s.get();
378 } else if (msg == NULL && cause != NULL) {
379 signature = "(Ljava/lang/Throwable;)V";
380 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700381 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
383 args[0].l = s.get();
384 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700385 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700386 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
387 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800388 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389 LOG(ERROR) << "No <init>" << signature << " in "
390 << PrettyClass(soa.Decode<Class*>(exception_class));
391 return JNI_ERR;
392 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700393
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
395 if (exception.get() == NULL) {
396 return JNI_ERR;
397 }
Ian Rogersef28b142012-11-30 14:22:18 -0800398 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800399 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
400 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700402}
403
Elliott Hughes462c9442012-03-23 18:47:50 -0700404static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700405 if (vm == NULL || p_env == NULL) {
406 return JNI_ERR;
407 }
408
Elliott Hughes462c9442012-03-23 18:47:50 -0700409 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700410 Thread* self = Thread::Current();
411 if (self != NULL) {
412 *p_env = self->GetJniEnv();
413 return JNI_OK;
414 }
415
416 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
417
418 // No threads allowed in zygote mode.
419 if (runtime->IsZygote()) {
420 LOG(ERROR) << "Attempt to attach a thread in the zygote";
421 return JNI_ERR;
422 }
423
Elliott Hughes462c9442012-03-23 18:47:50 -0700424 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
425 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700426 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700427 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700428 if (IsBadJniVersion(args->version)) {
429 LOG(ERROR) << "Bad JNI version passed to "
430 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
431 << args->version;
432 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700433 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700434 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700435 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700436 }
Elliott Hughes75770752011-08-24 17:52:38 -0700437
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800438 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700439 *p_env = NULL;
440 return JNI_ERR;
441 } else {
442 *p_env = Thread::Current()->GetJniEnv();
443 return JNI_OK;
444 }
Elliott Hughes75770752011-08-24 17:52:38 -0700445}
446
Elliott Hughes79082e32011-08-25 12:07:32 -0700447class SharedLibrary {
448 public:
449 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
450 : path_(path),
451 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700452 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700453 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700454 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700455 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700456 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700457 }
458
Elliott Hughes79082e32011-08-25 12:07:32 -0700459 Object* GetClassLoader() {
460 return class_loader_;
461 }
462
463 std::string GetPath() {
464 return path_;
465 }
466
467 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700468 * Check the result of an earlier call to JNI_OnLoad on this library.
469 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700470 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700471 bool CheckOnLoadResult()
472 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700473 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700474 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700475 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
476 bool okay;
477 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700478 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700479
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700480 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700481 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
482 // caller can continue.
483 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
484 okay = true;
485 } else {
486 while (jni_on_load_result_ == kPending) {
487 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700488 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700489 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700490
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700491 okay = (jni_on_load_result_ == kOkay);
492 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
493 << (okay ? "succeeded" : "failed") << "]";
494 }
495 }
496 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 return okay;
498 }
499
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700500 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700501 Thread* self = Thread::Current();
502 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700503
Elliott Hughes79082e32011-08-25 12:07:32 -0700504 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700505 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700506
507 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700508 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700509 }
510
511 void* FindSymbol(const std::string& symbol_name) {
512 return dlsym(handle_, symbol_name.c_str());
513 }
514
515 private:
516 enum JNI_OnLoadState {
517 kPending,
518 kFailed,
519 kOkay,
520 };
521
522 // Path to library "/system/lib/libjni.so".
523 std::string path_;
524
525 // The void* returned by dlopen(3).
526 void* handle_;
527
528 // The ClassLoader this library is associated with.
529 Object* class_loader_;
530
531 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700532 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700533 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700534 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700535 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700536 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700537 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700538 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700539};
540
Elliott Hughes79082e32011-08-25 12:07:32 -0700541// This exists mainly to keep implementation details out of the header file.
542class Libraries {
543 public:
544 Libraries() {
545 }
546
547 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700548 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700549 }
550
Elliott Hughesae80b492012-04-24 10:43:17 -0700551 void Dump(std::ostream& os) const {
552 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700553 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700554 if (!first) {
555 os << ' ';
556 }
557 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700558 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700559 }
560 }
561
562 size_t size() const {
563 return libraries_.size();
564 }
565
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700567 auto it = libraries_.find(path);
Ian Rogers712462a2012-04-12 16:32:29 -0700568 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700569 }
570
571 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700572 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700573 }
574
575 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstromea46f952013-07-30 01:26:50 -0700576 void* FindNativeMethod(const ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700577 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700578 std::string jni_short_name(JniShortName(m));
579 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700580 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700581 for (const auto& lib : libraries_) {
582 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 if (library->GetClassLoader() != declaring_class_loader) {
584 // We only search libraries loaded by the appropriate ClassLoader.
585 continue;
586 }
587 // Try the short name then the long name...
588 void* fn = library->FindSymbol(jni_short_name);
589 if (fn == NULL) {
590 fn = library->FindSymbol(jni_long_name);
591 }
592 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800593 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
594 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700595 return fn;
596 }
597 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700598 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700599 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700600 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700601 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700602 return NULL;
603 }
604
605 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700606 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700607};
608
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700609JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
610 jvalue* args) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700611 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700612 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700613 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800614 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700615 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800616 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700617 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
618 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800619}
620
Ian Rogersbc939662013-08-15 10:26:54 -0700621#define CHECK_NON_NULL_ARGUMENT(fn, value) \
622 if (UNLIKELY(value == NULL)) { \
623 JniAbortF(#fn, #value " == null"); \
624 }
625
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700626#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
627 if (UNLIKELY(length != 0 && value == NULL)) { \
628 JniAbortF(#fn, #value " == null"); \
629 }
630
Elliott Hughescdf53122011-08-19 15:46:09 -0700631class JNI {
632 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700633 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700634 return JNI_VERSION_1_6;
635 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700636
Ian Rogers25e8b912012-09-07 11:31:36 -0700637 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700638 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700639 return NULL;
640 }
641
Elliott Hughescdf53122011-08-19 15:46:09 -0700642 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700643 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700644 Runtime* runtime = Runtime::Current();
645 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700646 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700647 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700648 Class* c = NULL;
649 if (runtime->IsStarted()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700650 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
651 c = class_linker->FindClass(descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700652 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800653 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700654 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700655 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700656 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700657
Elliott Hughescdf53122011-08-19 15:46:09 -0700658 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700659 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700660 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700661 jobject art_method = env->GetObjectField(java_method,
662 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
663 ArtMethod* method = soa.Decode<ArtMethod*>(art_method);
664 DCHECK(method != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700665 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700666 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700667
Elliott Hughescdf53122011-08-19 15:46:09 -0700668 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700669 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700671 jobject art_field = env->GetObjectField(java_field,
672 WellKnownClasses::java_lang_reflect_Field_artField);
673 ArtField* field = soa.Decode<ArtField*>(art_field);
674 DCHECK(field != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700675 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700677
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700679 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700680 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700681 ArtMethod* m = soa.DecodeMethod(mid);
682 jobject art_method = soa.AddLocalReference<jobject>(m);
683 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
684 if (env->ExceptionCheck()) {
685 return NULL;
686 }
687 SetObjectField(env,
688 reflect_method,
689 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod,
690 art_method);
691 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700692 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700693
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700695 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700697 ArtField* f = soa.DecodeField(fid);
698 jobject art_field = soa.AddLocalReference<jobject>(f);
699 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
700 if (env->ExceptionCheck()) {
701 return NULL;
702 }
703 SetObjectField(env,
704 reflect_field,
705 WellKnownClasses::java_lang_reflect_Field_artField,
706 art_field);
707 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700709
Elliott Hughes37f7a402011-08-22 18:56:01 -0700710 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700711 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 ScopedObjectAccess soa(env);
713 Object* o = soa.Decode<Object*>(java_object);
714 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700715 }
716
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700717 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700718 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700719 ScopedObjectAccess soa(env);
720 Class* c = soa.Decode<Class*>(java_class);
721 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700723
Elliott Hughes37f7a402011-08-22 18:56:01 -0700724 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700725 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
726 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700727 ScopedObjectAccess soa(env);
728 Class* c1 = soa.Decode<Class*>(java_class1);
729 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700730 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700732
Elliott Hughese84278b2012-03-22 10:06:53 -0700733 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700734 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700735 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700736 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700737 return JNI_TRUE;
738 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700739 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700740 Object* obj = soa.Decode<Object*>(jobj);
741 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700742 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700743 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700745
Elliott Hughes37f7a402011-08-22 18:56:01 -0700746 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700747 ScopedObjectAccess soa(env);
748 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700749 if (exception == NULL) {
750 return JNI_ERR;
751 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800752 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
753 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700754 return JNI_OK;
755 }
756
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700757 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700758 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Elliott Hughesa4f94742012-05-29 16:28:38 -0700759 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700760 }
761
762 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700763 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700764 }
765
766 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700767 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700768 }
769
770 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700771 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700772
Brian Carlstromea46f952013-07-30 01:26:50 -0700773 SirtRef<Object> old_throw_this_object(soa.Self(), NULL);
774 SirtRef<ArtMethod> old_throw_method(soa.Self(), NULL);
775 SirtRef<Throwable> old_exception(soa.Self(), NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800776 uint32_t old_throw_dex_pc;
777 {
778 ThrowLocation old_throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700779 Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800780 old_throw_this_object.reset(old_throw_location.GetThis());
781 old_throw_method.reset(old_throw_location.GetMethod());
782 old_exception.reset(old_exception_obj);
783 old_throw_dex_pc = old_throw_location.GetDexPc();
784 soa.Self()->ClearException();
785 }
786 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700787 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
788 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
789 if (mid == NULL) {
790 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800791 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700792 } else {
793 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800794 if (soa.Self()->IsExceptionPending()) {
795 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700796 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800797 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700798 }
799 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800800 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
801 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700802
Ian Rogers62d6c772013-02-27 08:32:07 -0800803 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700805
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700807 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800808 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700809 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 }
811
Ian Rogers25e8b912012-09-07 11:31:36 -0700812 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 LOG(FATAL) << "JNI FatalError called: " << msg;
814 }
815
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700816 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800817 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700818 return JNI_ERR;
819 }
Ian Rogersef28b142012-11-30 14:22:18 -0800820 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 return JNI_OK;
822 }
823
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700824 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700825 ScopedObjectAccess soa(env);
826 Object* survivor = soa.Decode<Object*>(java_survivor);
827 soa.Env()->PopFrame();
828 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 }
830
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700831 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800832 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700833 }
834
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 if (obj == NULL) {
837 return NULL;
838 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700839 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700840 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700842 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogersb8a0b942013-08-20 18:09:52 -0700843 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700844 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700845 return reinterpret_cast<jobject>(ref);
846 }
847
848 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 if (obj == NULL) {
850 return;
851 }
Ian Rogersef28b142012-11-30 14:22:18 -0800852 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700853 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800854 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700855 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700856
857 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
858 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
859 << "failed to find entry";
860 }
861 }
862
863 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700864 ScopedObjectAccess soa(env);
865 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 }
867
868 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700869 if (obj != nullptr) {
870 ScopedObjectAccess soa(env);
871 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 }
873 }
874
875 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700876 if (obj == NULL) {
877 return NULL;
878 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700879 ScopedObjectAccess soa(env);
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700880 return soa.AddLocalReference<jobject>(soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700881 }
882
883 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 if (obj == NULL) {
885 return;
886 }
Ian Rogersef28b142012-11-30 14:22:18 -0800887 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700888
Ian Rogersef28b142012-11-30 14:22:18 -0800889 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 if (!locals.Remove(cookie, obj)) {
891 // Attempting to delete a local reference that is not in the
892 // topmost local reference frame is a no-op. DeleteLocalRef returns
893 // void and doesn't throw any exceptions, but we should probably
894 // complain about it so the user will notice that things aren't
895 // going quite the way they expect.
896 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
897 << "failed to find entry";
898 }
899 }
900
901 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700902 if (obj1 == obj2) {
903 return JNI_TRUE;
904 } else {
905 ScopedObjectAccess soa(env);
906 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
907 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 }
909
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700910 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700911 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700912 ScopedObjectAccess soa(env);
913 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700914 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700915 return NULL;
916 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700917 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700918 }
919
Ian Rogersbc939662013-08-15 10:26:54 -0700920 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700923 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
924 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
925 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700926 va_end(args);
927 return result;
928 }
929
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700931 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
932 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933 ScopedObjectAccess soa(env);
934 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700935 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700936 return NULL;
937 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700938 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700939 if (result == NULL) {
940 return NULL;
941 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700942 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700945 return local_result;
946 } else {
947 return NULL;
948 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 }
950
Elliott Hughes72025e52011-08-23 17:50:30 -0700951 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700952 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
953 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954 ScopedObjectAccess soa(env);
955 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700956 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700957 return NULL;
958 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700959 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700960 if (result == NULL) {
961 return NULL;
962 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700963 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700965 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700966 return local_result;
967 } else {
968 return NULL;
969 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 }
971
Ian Rogersbc939662013-08-15 10:26:54 -0700972 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
973 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
974 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
975 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700976 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700977 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 }
979
Ian Rogersbc939662013-08-15 10:26:54 -0700980 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
981 const char* sig) {
982 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
983 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
984 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700985 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700986 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 va_list ap;
991 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700992 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
993 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700994 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700995 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700997 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 }
999
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001001 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
1002 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003 ScopedObjectAccess soa(env);
1004 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1005 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001009 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
1010 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 ScopedObjectAccess soa(env);
1012 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1013 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 va_list ap;
1018 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001019 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1020 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001021 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001024 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001028 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1029 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001030 ScopedObjectAccess soa(env);
1031 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001035 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1036 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 ScopedObjectAccess soa(env);
1038 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 va_list ap;
1043 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001044 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1045 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1046 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001047 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001049 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001053 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1054 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001055 ScopedObjectAccess soa(env);
1056 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001060 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1061 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062 ScopedObjectAccess soa(env);
1063 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 va_list ap;
1068 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001069 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1070 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001071 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001072 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001074 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001078 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1079 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 ScopedObjectAccess soa(env);
1081 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 }
1083
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001085 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1086 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001087 ScopedObjectAccess soa(env);
1088 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 va_list ap;
1093 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001094 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1095 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001096 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001097 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001099 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001103 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1104 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001105 ScopedObjectAccess soa(env);
1106 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001110 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1111 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001112 ScopedObjectAccess soa(env);
1113 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_list ap;
1118 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001119 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1120 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1121 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001122 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001124 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001128 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1129 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001130 ScopedObjectAccess soa(env);
1131 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001135 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1136 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001137 ScopedObjectAccess soa(env);
1138 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 va_list ap;
1143 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001144 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1145 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001146 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001147 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001149 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001153 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1154 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001155 ScopedObjectAccess soa(env);
1156 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001160 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1161 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162 ScopedObjectAccess soa(env);
1163 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 va_list ap;
1168 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001169 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1170 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001171 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001172 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001174 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001178 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1179 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001180 ScopedObjectAccess soa(env);
1181 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001185 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1186 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001187 ScopedObjectAccess soa(env);
1188 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 va_list ap;
1193 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001194 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1195 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001196 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001197 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001199 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001203 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1204 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001205 ScopedObjectAccess soa(env);
1206 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 }
1208
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001210 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1211 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001212 ScopedObjectAccess soa(env);
1213 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
1215
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 va_list ap;
1218 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001219 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1220 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001221 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001222 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 }
1225
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001227 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1228 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001229 ScopedObjectAccess soa(env);
1230 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 }
1232
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001234 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1235 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001236 ScopedObjectAccess soa(env);
1237 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001240 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001242 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001243 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1244 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001245 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1247 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 va_end(ap);
1249 return local_result;
1250 }
1251
Ian Rogersbc939662013-08-15 10:26:54 -07001252 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1253 va_list args) {
1254 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1255 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001256 ScopedObjectAccess soa(env);
1257 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1258 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 }
1260
Ian Rogersbc939662013-08-15 10:26:54 -07001261 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1262 jvalue* args) {
1263 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1264 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001265 ScopedObjectAccess soa(env);
1266 JValue result(InvokeWithJValues(soa, obj, mid, args));
1267 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
Ian Rogersbc939662013-08-15 10:26:54 -07001270 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1271 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001274 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1275 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001276 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001277 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001279 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
1281
Ian Rogersbc939662013-08-15 10:26:54 -07001282 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1283 va_list args) {
1284 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1285 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001286 ScopedObjectAccess soa(env);
1287 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Ian Rogersbc939662013-08-15 10:26:54 -07001290 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1291 jvalue* args) {
1292 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1293 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001294 ScopedObjectAccess soa(env);
1295 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 }
1297
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001298 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001300 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001301 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1302 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001303 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001304 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001306 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
Ian Rogersbc939662013-08-15 10:26:54 -07001309 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1310 va_list args) {
1311 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1312 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001313 ScopedObjectAccess soa(env);
1314 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
Ian Rogersbc939662013-08-15 10:26:54 -07001317 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1318 jvalue* args) {
1319 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1320 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001321 ScopedObjectAccess soa(env);
1322 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001325 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001327 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001328 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1329 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1330 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001331 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001333 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
Ian Rogersbc939662013-08-15 10:26:54 -07001336 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1337 va_list args) {
1338 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1339 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001340 ScopedObjectAccess soa(env);
1341 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
Ian Rogersbc939662013-08-15 10:26:54 -07001344 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1345 jvalue* args) {
1346 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1347 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001348 ScopedObjectAccess soa(env);
1349 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001352 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001355 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1356 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001357 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001358 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001360 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
Ian Rogersbc939662013-08-15 10:26:54 -07001363 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1364 va_list args) {
1365 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1366 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001367 ScopedObjectAccess soa(env);
1368 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
Ian Rogersbc939662013-08-15 10:26:54 -07001371 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1372 jvalue* args) {
1373 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1374 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001375 ScopedObjectAccess soa(env);
1376 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001379 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001381 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001382 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1383 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001384 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001385 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001387 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 }
1389
Ian Rogersbc939662013-08-15 10:26:54 -07001390 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1391 va_list args) {
1392 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1393 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001394 ScopedObjectAccess soa(env);
1395 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
Ian Rogersbc939662013-08-15 10:26:54 -07001398 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1399 jvalue* args) {
1400 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1401 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001402 ScopedObjectAccess soa(env);
1403 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001406 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001408 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001409 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1410 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001411 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001412 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001414 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
1416
Ian Rogersbc939662013-08-15 10:26:54 -07001417 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1418 va_list args) {
1419 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1420 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001421 ScopedObjectAccess soa(env);
1422 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
Ian Rogersbc939662013-08-15 10:26:54 -07001425 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1426 jvalue* args) {
1427 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1428 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001429 ScopedObjectAccess soa(env);
1430 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
1432
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001433 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001435 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001436 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1437 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001438 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001441 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Ian Rogersbc939662013-08-15 10:26:54 -07001444 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1445 va_list args) {
1446 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1447 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001448 ScopedObjectAccess soa(env);
1449 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Ian Rogersbc939662013-08-15 10:26:54 -07001452 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1453 jvalue* args) {
1454 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1455 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001456 ScopedObjectAccess soa(env);
1457 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001460 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001462 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001463 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1464 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001465 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001466 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001468 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Ian Rogersbc939662013-08-15 10:26:54 -07001471 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1472 va_list args) {
1473 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1474 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001475 ScopedObjectAccess soa(env);
1476 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Ian Rogersbc939662013-08-15 10:26:54 -07001479 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1480 jvalue* args) {
1481 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1482 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001483 ScopedObjectAccess soa(env);
1484 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001487 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001489 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001490 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1491 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001492 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001493 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 va_end(ap);
1495 }
1496
Brian Carlstromea46f952013-07-30 01:26:50 -07001497 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1498 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001499 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1500 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001501 ScopedObjectAccess soa(env);
1502 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 }
1504
Ian Rogersbc939662013-08-15 10:26:54 -07001505 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1506 jvalue* args) {
1507 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1508 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001509 ScopedObjectAccess soa(env);
1510 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001511 }
1512
Ian Rogersbc939662013-08-15 10:26:54 -07001513 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1514 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1515 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1516 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001517 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001518 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001520
Ian Rogersbc939662013-08-15 10:26:54 -07001521 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1522 const char* sig) {
1523 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1524 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1525 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001526 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001527 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001529
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001530 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001531 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1532 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001533 ScopedObjectAccess soa(env);
1534 Object* o = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -07001535 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001536 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001538
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001539 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001540 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001541 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -07001542 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001543 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001546 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001547 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1548 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001549 ScopedObjectAccess soa(env);
1550 Object* o = soa.Decode<Object*>(java_object);
1551 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001552 ArtField* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001553 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001556 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001557 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001558 ScopedObjectAccess soa(env);
1559 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001560 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001561 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001564#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001565 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1566 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001567 ScopedObjectAccess soa(env); \
1568 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001569 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001570 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001571
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001572#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001573 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001574 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001575 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001576 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001577
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001579 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1580 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001581 ScopedObjectAccess soa(env); \
1582 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001583 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001584 f->Set ##fn(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001585
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001586#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001587 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001588 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001589 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001590 f->Set ##fn(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001591
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001596 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001600 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001604 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001608 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001612 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001616 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001617 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001620 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001621 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001624 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001625 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 }
1627
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001628 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001629 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001632 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001633 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001637 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 }
1639
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001640 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001641 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 }
1643
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001644 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001645 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001646 }
1647
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001648 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001649 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001650 }
1651
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001652 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001653 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001654 }
1655
1656 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001657 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001658 }
1659
1660 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001661 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001662 }
1663
1664 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001665 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001666 }
1667
1668 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001669 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001670 }
1671
1672 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001673 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001674 }
1675
1676 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001677 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001678 }
1679
1680 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001681 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001682 }
1683
1684 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001685 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001686 }
1687
1688 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001689 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001690 }
1691
1692 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001693 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001694 }
1695
1696 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001697 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001698 }
1699
1700 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001701 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001702 }
1703
1704 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001705 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001706 }
1707
1708 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001709 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001710 }
1711
1712 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001713 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001714 }
1715
1716 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001717 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001720 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001723 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001724 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001725 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1726 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 va_end(ap);
1728 return local_result;
1729 }
1730
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001731 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001732 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001733 ScopedObjectAccess soa(env);
1734 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1735 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001738 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001739 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001740 ScopedObjectAccess soa(env);
1741 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1742 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001748 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001749 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001750 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001752 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001755 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001756 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001757 ScopedObjectAccess soa(env);
1758 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001761 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001762 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001763 ScopedObjectAccess soa(env);
1764 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001767 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001769 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001770 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001771 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001772 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001774 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001777 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001778 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001779 ScopedObjectAccess soa(env);
1780 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001783 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001784 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001785 ScopedObjectAccess soa(env);
1786 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 }
1788
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001789 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001791 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001792 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001793 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001794 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001796 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001799 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001800 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 ScopedObjectAccess soa(env);
1802 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001806 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001807 ScopedObjectAccess soa(env);
1808 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001811 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001813 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001814 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001815 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001816 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001818 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001821 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001822 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001823 ScopedObjectAccess soa(env);
1824 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001827 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001828 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001829 ScopedObjectAccess soa(env);
1830 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001833 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001835 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001836 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001837 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001838 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001840 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001843 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001844 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001845 ScopedObjectAccess soa(env);
1846 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 }
1848
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001849 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001850 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001851 ScopedObjectAccess soa(env);
1852 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 }
1854
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001855 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001856 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001857 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001858 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001859 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001860 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001862 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001865 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001866 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001867 ScopedObjectAccess soa(env);
1868 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001871 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001872 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001873 ScopedObjectAccess soa(env);
1874 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 }
1876
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001877 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001879 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001880 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001881 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001882 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001884 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 }
1886
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001887 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001888 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889 ScopedObjectAccess soa(env);
1890 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001893 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001894 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001895 ScopedObjectAccess soa(env);
1896 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001897 }
1898
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001899 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001900 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001901 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001902 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001903 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001904 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001906 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 }
1908
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001909 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001910 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001911 ScopedObjectAccess soa(env);
1912 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 }
1914
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001915 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001916 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001917 ScopedObjectAccess soa(env);
1918 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 }
1920
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001921 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001923 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001924 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001925 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001926 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 va_end(ap);
1928 }
1929
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001930 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001931 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001932 ScopedObjectAccess soa(env);
1933 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 }
1935
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001936 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001937 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001938 ScopedObjectAccess soa(env);
1939 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
Elliott Hughes814e4032011-08-23 12:07:56 -07001942 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogersbc939662013-08-15 10:26:54 -07001943 if (UNLIKELY(chars == NULL && char_count > 0)) { \
1944 JniAbortF("NewString", "char == null && char_count > 0"); \
1945 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001946 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001947 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001948 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
1951 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 if (utf == NULL) {
1953 return NULL;
1954 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001955 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001956 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001957 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Elliott Hughes814e4032011-08-23 12:07:56 -07001960 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001961 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001962 ScopedObjectAccess soa(env);
1963 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001964 }
1965
1966 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001967 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001968 ScopedObjectAccess soa(env);
1969 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001970 }
1971
Ian Rogersbc939662013-08-15 10:26:54 -07001972 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1973 jchar* buf) {
1974 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001975 ScopedObjectAccess soa(env);
1976 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001977 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001978 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001979 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001980 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001981 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1982 memcpy(buf, chars + start, length * sizeof(jchar));
1983 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001984 }
1985
Ian Rogersbc939662013-08-15 10:26:54 -07001986 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1987 char* buf) {
1988 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001989 ScopedObjectAccess soa(env);
1990 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001991 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001992 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001993 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001994 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001995 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1996 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1997 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001998 }
1999
Elliott Hughes75770752011-08-24 17:52:38 -07002000 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002001 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002002 ScopedObjectAccess soa(env);
2003 String* s = soa.Decode<String*>(java_string);
Mathieu Chartier423d2a32013-09-12 17:33:56 -07002004 CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002005 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002006 if (is_copy != nullptr) {
2007 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07002008 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002009 int32_t char_count = s->GetLength();
2010 int32_t offset = s->GetOffset();
2011 jchar* bytes = new jchar[char_count + 1];
2012 for (int32_t i = 0; i < char_count; i++) {
2013 bytes[i] = chars->Get(i + offset);
2014 }
2015 bytes[char_count] = '\0';
2016 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07002017 }
2018
Mathieu Chartier590fee92013-09-13 13:46:47 -07002019 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogersbc939662013-08-15 10:26:54 -07002020 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002021 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002022 ScopedObjectAccess soa(env);
2023 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes75770752011-08-24 17:52:38 -07002026 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002027 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes75770752011-08-24 17:52:38 -07002030 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002031 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes75770752011-08-24 17:52:38 -07002034 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002035 if (java_string == NULL) {
2036 return NULL;
2037 }
2038 if (is_copy != NULL) {
2039 *is_copy = JNI_TRUE;
2040 }
Ian Rogersef28b142012-11-30 14:22:18 -08002041 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002042 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002043 size_t byte_count = s->GetUtfLength();
2044 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002045 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002046 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2047 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2048 bytes[byte_count] = '\0';
2049 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002050 }
2051
Elliott Hughes75770752011-08-24 17:52:38 -07002052 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002053 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002054 }
2055
Elliott Hughesbd935992011-08-22 11:59:34 -07002056 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002057 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002058 ScopedObjectAccess soa(env);
2059 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002060 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002061 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2062 }
Elliott Hughesbd935992011-08-22 11:59:34 -07002063 Array* array = obj->AsArray();
2064 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes814e4032011-08-23 12:07:56 -07002067 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002068 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002069 ScopedObjectAccess soa(env);
2070 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2071 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Ian Rogersbc939662013-08-15 10:26:54 -07002074 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2075 jobject java_value) {
2076 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002077 ScopedObjectAccess soa(env);
2078 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2079 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 array->Set(index, value);
2081 }
2082
2083 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002084 ScopedObjectAccess soa(env);
2085 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
2088 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002089 ScopedObjectAccess soa(env);
2090 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
2093 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002094 ScopedObjectAccess soa(env);
2095 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
2098 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002099 ScopedObjectAccess soa(env);
2100 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
2103 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002104 ScopedObjectAccess soa(env);
2105 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
2108 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002109 ScopedObjectAccess soa(env);
2110 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
2113 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002114 ScopedObjectAccess soa(env);
2115 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
2118 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002119 if (length < 0) {
2120 JniAbortF("NewObjectArray", "negative array length: %d", length);
2121 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002122
2123 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002124 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002125 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 std::string descriptor;
2127 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002128 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002129
2130 // Find the class.
Brian Carlstromce888532013-10-10 00:32:58 -07002131 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002132 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), element_class->GetClassLoader());
2133 Class* array_class = class_linker->FindClass(descriptor.c_str(), class_loader);
Brian Carlstromce888532013-10-10 00:32:58 -07002134 if (array_class == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 return NULL;
2136 }
2137
Elliott Hughes75770752011-08-24 17:52:38 -07002138 // Allocate and initialize if necessary.
Ian Rogers50b35e22012-10-04 10:09:15 -07002139 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002140 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002141 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07002142 for (jsize i = 0; i < length; ++i) {
2143 result->Set(i, initial_object);
2144 }
2145 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002146 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
2149 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002150 ScopedObjectAccess soa(env);
2151 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
2153
Ian Rogersa15e67d2012-02-28 13:51:55 -08002154 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002155 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002156 ScopedObjectAccess soa(env);
2157 Array* array = soa.Decode<Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002158 gc::Heap* heap = Runtime::Current()->GetHeap();
2159 if (heap->IsMovableObject(array)) {
2160 heap->IncrementDisableGC(soa.Self());
2161 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
2162 array = soa.Decode<Array*>(java_array);
2163 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002164 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002165 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002166 *is_copy = JNI_FALSE;
2167 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002168 void* address = array->GetRawData(array->GetClass()->GetComponentSize());;
2169 return address;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002170 }
2171
Mathieu Chartier590fee92013-09-13 13:46:47 -07002172 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002173 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002174 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002175 }
2176
Elliott Hughes75770752011-08-24 17:52:38 -07002177 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002178 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002179 ScopedObjectAccess soa(env);
2180 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
Elliott Hughes75770752011-08-24 17:52:38 -07002183 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002184 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002185 ScopedObjectAccess soa(env);
2186 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 }
2188
Elliott Hughes75770752011-08-24 17:52:38 -07002189 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002190 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002191 ScopedObjectAccess soa(env);
2192 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
Elliott Hughes75770752011-08-24 17:52:38 -07002195 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002196 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002197 ScopedObjectAccess soa(env);
2198 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 }
2200
Elliott Hughes75770752011-08-24 17:52:38 -07002201 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002202 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002203 ScopedObjectAccess soa(env);
2204 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 }
2206
Elliott Hughes75770752011-08-24 17:52:38 -07002207 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002208 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002209 ScopedObjectAccess soa(env);
2210 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
Elliott Hughes75770752011-08-24 17:52:38 -07002213 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002214 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002215 ScopedObjectAccess soa(env);
2216 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218
Elliott Hughes75770752011-08-24 17:52:38 -07002219 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002220 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002221 ScopedObjectAccess soa(env);
2222 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Mathieu Chartier590fee92013-09-13 13:46:47 -07002225 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2226 jint mode) {
2227 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
Mathieu Chartier590fee92013-09-13 13:46:47 -07002230 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2231 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 }
2233
Mathieu Chartier590fee92013-09-13 13:46:47 -07002234 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2235 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 }
2237
Mathieu Chartier590fee92013-09-13 13:46:47 -07002238 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2239 jint mode) {
2240 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 }
2242
Mathieu Chartier590fee92013-09-13 13:46:47 -07002243 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2244 jint mode) {
2245 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 }
2247
Mathieu Chartier590fee92013-09-13 13:46:47 -07002248 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2249 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Mathieu Chartier590fee92013-09-13 13:46:47 -07002252 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2253 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
Mathieu Chartier590fee92013-09-13 13:46:47 -07002256 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2257 jint mode) {
2258 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002259 }
2260
Ian Rogersbc939662013-08-15 10:26:54 -07002261 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2262 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002263 ScopedObjectAccess soa(env);
2264 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002265 }
2266
Ian Rogersbc939662013-08-15 10:26:54 -07002267 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2268 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002269 ScopedObjectAccess soa(env);
2270 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 }
2272
Ian Rogersbc939662013-08-15 10:26:54 -07002273 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2274 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002275 ScopedObjectAccess soa(env);
2276 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 }
2278
Ian Rogersbc939662013-08-15 10:26:54 -07002279 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2280 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002281 ScopedObjectAccess soa(env);
2282 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002283 }
2284
Ian Rogersbc939662013-08-15 10:26:54 -07002285 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2286 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002287 ScopedObjectAccess soa(env);
2288 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002289 }
2290
Ian Rogersbc939662013-08-15 10:26:54 -07002291 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2292 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002293 ScopedObjectAccess soa(env);
2294 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002295 }
2296
Ian Rogersbc939662013-08-15 10:26:54 -07002297 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2298 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002299 ScopedObjectAccess soa(env);
2300 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002301 }
2302
Ian Rogersbc939662013-08-15 10:26:54 -07002303 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2304 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002305 ScopedObjectAccess soa(env);
2306 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002307 }
2308
Ian Rogersbc939662013-08-15 10:26:54 -07002309 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2310 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002311 ScopedObjectAccess soa(env);
2312 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002313 }
2314
Ian Rogersbc939662013-08-15 10:26:54 -07002315 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2316 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002317 ScopedObjectAccess soa(env);
2318 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002319 }
2320
Ian Rogersbc939662013-08-15 10:26:54 -07002321 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2322 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002323 ScopedObjectAccess soa(env);
2324 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002325 }
2326
Ian Rogersbc939662013-08-15 10:26:54 -07002327 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2328 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002329 ScopedObjectAccess soa(env);
2330 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002331 }
2332
Ian Rogersbc939662013-08-15 10:26:54 -07002333 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2334 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002335 ScopedObjectAccess soa(env);
2336 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002337 }
2338
Ian Rogersbc939662013-08-15 10:26:54 -07002339 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2340 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002341 ScopedObjectAccess soa(env);
2342 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002343 }
2344
Ian Rogersbc939662013-08-15 10:26:54 -07002345 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2346 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002347 ScopedObjectAccess soa(env);
2348 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002349 }
2350
Ian Rogersbc939662013-08-15 10:26:54 -07002351 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2352 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002353 ScopedObjectAccess soa(env);
2354 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002355 }
2356
Ian Rogersbc939662013-08-15 10:26:54 -07002357 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2358 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002359 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2360 }
2361
Ian Rogersbc939662013-08-15 10:26:54 -07002362 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2363 jint method_count, bool return_errors) {
2364 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002365 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002366 return JNI_ERR; // Not reached.
2367 }
2368 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002369 ScopedObjectAccess soa(env);
2370 Class* c = soa.Decode<Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002371 if (UNLIKELY(method_count == 0)) {
2372 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2373 << PrettyDescriptor(c);
2374 return JNI_OK;
2375 }
2376 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2377 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002378 const char* name = methods[i].name;
2379 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002380 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002381 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002382 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002383 ++sig;
2384 }
2385
Brian Carlstromea46f952013-07-30 01:26:50 -07002386 ArtMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002387 if (m == NULL) {
2388 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002389 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002390 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002391 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002392 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002393 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002394 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002395 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002396 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002397 << PrettyDescriptor(c) << "." << name << sig
2398 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002399 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002400 return JNI_ERR;
2401 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002402
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002403 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002404
Ian Rogers1eb512d2013-10-18 15:42:20 -07002405 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002406 }
2407 return JNI_OK;
2408 }
2409
Elliott Hughes5174fe62011-08-23 15:12:35 -07002410 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002411 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002412 ScopedObjectAccess soa(env);
2413 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002414
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002415 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002416
2417 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002418 ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002419 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002420 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002421 }
2422 }
2423 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002424 ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002425 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002426 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002427 }
2428 }
2429
2430 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002431 }
2432
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002433 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2434 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002435 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002436 ScopedObjectAccess soa(env);
2437 Object* o = soa.Decode<Object*>(java_object);
2438 o->MonitorEnter(soa.Self());
2439 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002440 return JNI_ERR;
2441 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002442 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002443 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002444 }
2445
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002446 static jint MonitorExit(JNIEnv* env, jobject java_object)
2447 UNLOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002448 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002449 ScopedObjectAccess soa(env);
2450 Object* o = soa.Decode<Object*>(java_object);
2451 o->MonitorExit(soa.Self());
2452 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002453 return JNI_ERR;
2454 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002455 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002456 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002457 }
2458
2459 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002460 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002461 Runtime* runtime = Runtime::Current();
2462 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002463 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002464 } else {
2465 *vm = NULL;
2466 }
2467 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2468 }
2469
Elliott Hughescdf53122011-08-19 15:46:09 -07002470 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002471 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002472 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002473 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002474 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002475 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002476 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002477
Elliott Hughes96a98872012-12-19 14:21:15 -08002478 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002479 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2480 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002481 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002482 jint capacity_arg = static_cast<jint>(capacity);
2483
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002484 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2485 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002486 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002487 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002488 }
2489
Elliott Hughesb465ab02011-08-24 11:21:21 -07002490 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002491 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002492 }
2493
Elliott Hughesb465ab02011-08-24 11:21:21 -07002494 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002495 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002496 }
2497
Elliott Hughesb465ab02011-08-24 11:21:21 -07002498 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002499 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002500
2501 // Do we definitely know what kind of reference this is?
2502 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2503 IndirectRefKind kind = GetIndirectRefKind(ref);
2504 switch (kind) {
2505 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002506 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002507 return JNILocalRefType;
2508 }
2509 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002510 case kGlobal:
2511 return JNIGlobalRefType;
2512 case kWeakGlobal:
2513 return JNIWeakGlobalRefType;
2514 case kSirtOrInvalid:
2515 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002516 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002517 return JNILocalRefType;
2518 }
2519
Ian Rogersef28b142012-11-30 14:22:18 -08002520 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002521 return JNIInvalidRefType;
2522 }
2523
Elliott Hughesb465ab02011-08-24 11:21:21 -07002524 // If we're handing out direct pointers, check whether it's a direct pointer
2525 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002526 {
2527 ScopedObjectAccess soa(env);
2528 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2529 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2530 return JNILocalRefType;
2531 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002532 }
2533 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002534 return JNIInvalidRefType;
2535 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002536 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2537 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002538 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002539
2540 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002541 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2542 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002543 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002544 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002545 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2546 return JNI_ERR;
2547 }
2548 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002549 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002550 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2551 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002552 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002553 soa.Self()->ThrowOutOfMemoryError(caller);
2554 }
2555 return okay ? JNI_OK : JNI_ERR;
2556 }
2557
2558 template<typename JniT, typename ArtT>
2559 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002560 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002561 if (length < 0) {
2562 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2563 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002564 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002565 return soa.AddLocalReference<JniT>(result);
2566 }
2567
2568 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2569 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2570 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002571 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002572 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2573 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002574 // Only make a copy if necessary.
2575 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2576 if (is_copy != nullptr) {
2577 *is_copy = JNI_TRUE;
2578 }
2579 static const size_t component_size = array->GetClass()->GetComponentSize();
2580 size_t size = array->GetLength() * component_size;
2581 void* data = new uint64_t[RoundUp(size, 8) / 8];
2582 memcpy(data, array->GetData(), size);
2583 return reinterpret_cast<CArrayT>(data);
2584 } else {
2585 if (is_copy != nullptr) {
2586 *is_copy = JNI_FALSE;
2587 }
2588 return reinterpret_cast<CArrayT>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002589 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002590 }
2591
Mathieu Chartier590fee92013-09-13 13:46:47 -07002592 template <typename ArrayT, typename ElementT>
2593 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2594 ScopedObjectAccess soa(env);
2595 Array* array = soa.Decode<Array*>(java_array);
2596 size_t component_size = array->GetClass()->GetComponentSize();
2597 void* array_data = array->GetRawData(component_size);
2598 gc::Heap* heap = Runtime::Current()->GetHeap();
2599 bool is_copy = array_data != reinterpret_cast<void*>(elements);
2600 size_t bytes = array->GetLength() * component_size;
2601 VLOG(heap) << "Release primitive array " << env << " array_data " << array_data
2602 << " elements " << reinterpret_cast<void*>(elements);
2603 if (!is_copy && heap->IsMovableObject(array)) {
2604 heap->DecrementDisableGC(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002605 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002606 // Don't need to copy if we had a direct pointer.
2607 if (mode != JNI_ABORT && is_copy) {
2608 memcpy(array_data, elements, bytes);
2609 }
2610 if (mode != JNI_COMMIT) {
2611 if (is_copy) {
2612 delete[] reinterpret_cast<uint64_t*>(elements);
2613 }
2614 }
2615 // TODO: Do we always unpin primitive array?
2616 UnpinPrimitiveArray(soa, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002617 }
2618
2619 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2620 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2621 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002622 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002623 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002624 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2625 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2626 ThrowAIOOBE(soa, array, start, length, "src");
2627 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002628 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002629 JavaT* data = array->GetData();
2630 memcpy(buf, data + start, length * sizeof(JavaT));
2631 }
2632 }
2633
2634 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2635 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2636 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002637 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002638 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002639 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2640 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2641 ThrowAIOOBE(soa, array, start, length, "dst");
2642 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002643 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002644 JavaT* data = array->GetData();
2645 memcpy(data + start, buf, length * sizeof(JavaT));
2646 }
2647 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002648};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002649
Elliott Hughes88c5c352012-03-15 18:49:48 -07002650const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002651 NULL, // reserved0.
2652 NULL, // reserved1.
2653 NULL, // reserved2.
2654 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002655 JNI::GetVersion,
2656 JNI::DefineClass,
2657 JNI::FindClass,
2658 JNI::FromReflectedMethod,
2659 JNI::FromReflectedField,
2660 JNI::ToReflectedMethod,
2661 JNI::GetSuperclass,
2662 JNI::IsAssignableFrom,
2663 JNI::ToReflectedField,
2664 JNI::Throw,
2665 JNI::ThrowNew,
2666 JNI::ExceptionOccurred,
2667 JNI::ExceptionDescribe,
2668 JNI::ExceptionClear,
2669 JNI::FatalError,
2670 JNI::PushLocalFrame,
2671 JNI::PopLocalFrame,
2672 JNI::NewGlobalRef,
2673 JNI::DeleteGlobalRef,
2674 JNI::DeleteLocalRef,
2675 JNI::IsSameObject,
2676 JNI::NewLocalRef,
2677 JNI::EnsureLocalCapacity,
2678 JNI::AllocObject,
2679 JNI::NewObject,
2680 JNI::NewObjectV,
2681 JNI::NewObjectA,
2682 JNI::GetObjectClass,
2683 JNI::IsInstanceOf,
2684 JNI::GetMethodID,
2685 JNI::CallObjectMethod,
2686 JNI::CallObjectMethodV,
2687 JNI::CallObjectMethodA,
2688 JNI::CallBooleanMethod,
2689 JNI::CallBooleanMethodV,
2690 JNI::CallBooleanMethodA,
2691 JNI::CallByteMethod,
2692 JNI::CallByteMethodV,
2693 JNI::CallByteMethodA,
2694 JNI::CallCharMethod,
2695 JNI::CallCharMethodV,
2696 JNI::CallCharMethodA,
2697 JNI::CallShortMethod,
2698 JNI::CallShortMethodV,
2699 JNI::CallShortMethodA,
2700 JNI::CallIntMethod,
2701 JNI::CallIntMethodV,
2702 JNI::CallIntMethodA,
2703 JNI::CallLongMethod,
2704 JNI::CallLongMethodV,
2705 JNI::CallLongMethodA,
2706 JNI::CallFloatMethod,
2707 JNI::CallFloatMethodV,
2708 JNI::CallFloatMethodA,
2709 JNI::CallDoubleMethod,
2710 JNI::CallDoubleMethodV,
2711 JNI::CallDoubleMethodA,
2712 JNI::CallVoidMethod,
2713 JNI::CallVoidMethodV,
2714 JNI::CallVoidMethodA,
2715 JNI::CallNonvirtualObjectMethod,
2716 JNI::CallNonvirtualObjectMethodV,
2717 JNI::CallNonvirtualObjectMethodA,
2718 JNI::CallNonvirtualBooleanMethod,
2719 JNI::CallNonvirtualBooleanMethodV,
2720 JNI::CallNonvirtualBooleanMethodA,
2721 JNI::CallNonvirtualByteMethod,
2722 JNI::CallNonvirtualByteMethodV,
2723 JNI::CallNonvirtualByteMethodA,
2724 JNI::CallNonvirtualCharMethod,
2725 JNI::CallNonvirtualCharMethodV,
2726 JNI::CallNonvirtualCharMethodA,
2727 JNI::CallNonvirtualShortMethod,
2728 JNI::CallNonvirtualShortMethodV,
2729 JNI::CallNonvirtualShortMethodA,
2730 JNI::CallNonvirtualIntMethod,
2731 JNI::CallNonvirtualIntMethodV,
2732 JNI::CallNonvirtualIntMethodA,
2733 JNI::CallNonvirtualLongMethod,
2734 JNI::CallNonvirtualLongMethodV,
2735 JNI::CallNonvirtualLongMethodA,
2736 JNI::CallNonvirtualFloatMethod,
2737 JNI::CallNonvirtualFloatMethodV,
2738 JNI::CallNonvirtualFloatMethodA,
2739 JNI::CallNonvirtualDoubleMethod,
2740 JNI::CallNonvirtualDoubleMethodV,
2741 JNI::CallNonvirtualDoubleMethodA,
2742 JNI::CallNonvirtualVoidMethod,
2743 JNI::CallNonvirtualVoidMethodV,
2744 JNI::CallNonvirtualVoidMethodA,
2745 JNI::GetFieldID,
2746 JNI::GetObjectField,
2747 JNI::GetBooleanField,
2748 JNI::GetByteField,
2749 JNI::GetCharField,
2750 JNI::GetShortField,
2751 JNI::GetIntField,
2752 JNI::GetLongField,
2753 JNI::GetFloatField,
2754 JNI::GetDoubleField,
2755 JNI::SetObjectField,
2756 JNI::SetBooleanField,
2757 JNI::SetByteField,
2758 JNI::SetCharField,
2759 JNI::SetShortField,
2760 JNI::SetIntField,
2761 JNI::SetLongField,
2762 JNI::SetFloatField,
2763 JNI::SetDoubleField,
2764 JNI::GetStaticMethodID,
2765 JNI::CallStaticObjectMethod,
2766 JNI::CallStaticObjectMethodV,
2767 JNI::CallStaticObjectMethodA,
2768 JNI::CallStaticBooleanMethod,
2769 JNI::CallStaticBooleanMethodV,
2770 JNI::CallStaticBooleanMethodA,
2771 JNI::CallStaticByteMethod,
2772 JNI::CallStaticByteMethodV,
2773 JNI::CallStaticByteMethodA,
2774 JNI::CallStaticCharMethod,
2775 JNI::CallStaticCharMethodV,
2776 JNI::CallStaticCharMethodA,
2777 JNI::CallStaticShortMethod,
2778 JNI::CallStaticShortMethodV,
2779 JNI::CallStaticShortMethodA,
2780 JNI::CallStaticIntMethod,
2781 JNI::CallStaticIntMethodV,
2782 JNI::CallStaticIntMethodA,
2783 JNI::CallStaticLongMethod,
2784 JNI::CallStaticLongMethodV,
2785 JNI::CallStaticLongMethodA,
2786 JNI::CallStaticFloatMethod,
2787 JNI::CallStaticFloatMethodV,
2788 JNI::CallStaticFloatMethodA,
2789 JNI::CallStaticDoubleMethod,
2790 JNI::CallStaticDoubleMethodV,
2791 JNI::CallStaticDoubleMethodA,
2792 JNI::CallStaticVoidMethod,
2793 JNI::CallStaticVoidMethodV,
2794 JNI::CallStaticVoidMethodA,
2795 JNI::GetStaticFieldID,
2796 JNI::GetStaticObjectField,
2797 JNI::GetStaticBooleanField,
2798 JNI::GetStaticByteField,
2799 JNI::GetStaticCharField,
2800 JNI::GetStaticShortField,
2801 JNI::GetStaticIntField,
2802 JNI::GetStaticLongField,
2803 JNI::GetStaticFloatField,
2804 JNI::GetStaticDoubleField,
2805 JNI::SetStaticObjectField,
2806 JNI::SetStaticBooleanField,
2807 JNI::SetStaticByteField,
2808 JNI::SetStaticCharField,
2809 JNI::SetStaticShortField,
2810 JNI::SetStaticIntField,
2811 JNI::SetStaticLongField,
2812 JNI::SetStaticFloatField,
2813 JNI::SetStaticDoubleField,
2814 JNI::NewString,
2815 JNI::GetStringLength,
2816 JNI::GetStringChars,
2817 JNI::ReleaseStringChars,
2818 JNI::NewStringUTF,
2819 JNI::GetStringUTFLength,
2820 JNI::GetStringUTFChars,
2821 JNI::ReleaseStringUTFChars,
2822 JNI::GetArrayLength,
2823 JNI::NewObjectArray,
2824 JNI::GetObjectArrayElement,
2825 JNI::SetObjectArrayElement,
2826 JNI::NewBooleanArray,
2827 JNI::NewByteArray,
2828 JNI::NewCharArray,
2829 JNI::NewShortArray,
2830 JNI::NewIntArray,
2831 JNI::NewLongArray,
2832 JNI::NewFloatArray,
2833 JNI::NewDoubleArray,
2834 JNI::GetBooleanArrayElements,
2835 JNI::GetByteArrayElements,
2836 JNI::GetCharArrayElements,
2837 JNI::GetShortArrayElements,
2838 JNI::GetIntArrayElements,
2839 JNI::GetLongArrayElements,
2840 JNI::GetFloatArrayElements,
2841 JNI::GetDoubleArrayElements,
2842 JNI::ReleaseBooleanArrayElements,
2843 JNI::ReleaseByteArrayElements,
2844 JNI::ReleaseCharArrayElements,
2845 JNI::ReleaseShortArrayElements,
2846 JNI::ReleaseIntArrayElements,
2847 JNI::ReleaseLongArrayElements,
2848 JNI::ReleaseFloatArrayElements,
2849 JNI::ReleaseDoubleArrayElements,
2850 JNI::GetBooleanArrayRegion,
2851 JNI::GetByteArrayRegion,
2852 JNI::GetCharArrayRegion,
2853 JNI::GetShortArrayRegion,
2854 JNI::GetIntArrayRegion,
2855 JNI::GetLongArrayRegion,
2856 JNI::GetFloatArrayRegion,
2857 JNI::GetDoubleArrayRegion,
2858 JNI::SetBooleanArrayRegion,
2859 JNI::SetByteArrayRegion,
2860 JNI::SetCharArrayRegion,
2861 JNI::SetShortArrayRegion,
2862 JNI::SetIntArrayRegion,
2863 JNI::SetLongArrayRegion,
2864 JNI::SetFloatArrayRegion,
2865 JNI::SetDoubleArrayRegion,
2866 JNI::RegisterNatives,
2867 JNI::UnregisterNatives,
2868 JNI::MonitorEnter,
2869 JNI::MonitorExit,
2870 JNI::GetJavaVM,
2871 JNI::GetStringRegion,
2872 JNI::GetStringUTFRegion,
2873 JNI::GetPrimitiveArrayCritical,
2874 JNI::ReleasePrimitiveArrayCritical,
2875 JNI::GetStringCritical,
2876 JNI::ReleaseStringCritical,
2877 JNI::NewWeakGlobalRef,
2878 JNI::DeleteWeakGlobalRef,
2879 JNI::ExceptionCheck,
2880 JNI::NewDirectByteBuffer,
2881 JNI::GetDirectBufferAddress,
2882 JNI::GetDirectBufferCapacity,
2883 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002884};
2885
Elliott Hughes75770752011-08-24 17:52:38 -07002886JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002887 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002888 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002889 local_ref_cookie(IRT_FIRST_SEGMENT),
2890 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002891 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002892 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002893 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002894 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002895 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002896 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002897 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002898 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2899 // errors will ensue.
2900 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2901 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002902}
2903
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002904JNIEnvExt::~JNIEnvExt() {
2905}
2906
Mathieu Chartier590fee92013-09-13 13:46:47 -07002907jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2908 if (obj == nullptr) {
2909 return nullptr;
2910 }
2911 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2912}
2913
2914void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2915 if (obj != nullptr) {
2916 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2917 }
2918}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002919void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2920 check_jni = enabled;
2921 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002922}
2923
Elliott Hughes73e66f72012-05-09 09:34:45 -07002924void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2925 locals.Dump(os);
2926 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002927}
2928
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002929void JNIEnvExt::PushFrame(int /*capacity*/) {
2930 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002931 stacked_local_ref_cookies.push_back(local_ref_cookie);
2932 local_ref_cookie = locals.GetSegmentState();
2933}
2934
2935void JNIEnvExt::PopFrame() {
2936 locals.SetSegmentState(local_ref_cookie);
2937 local_ref_cookie = stacked_local_ref_cookies.back();
2938 stacked_local_ref_cookies.pop_back();
2939}
2940
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002941Offset JNIEnvExt::SegmentStateOffset() {
2942 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2943 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2944}
2945
Carl Shapiroea4dca82011-08-01 13:45:38 -07002946// JNI Invocation interface.
2947
Brian Carlstrombddf9762013-05-14 11:35:37 -07002948extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002949 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002950 if (IsBadJniVersion(args->version)) {
2951 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002952 return JNI_EVERSION;
2953 }
2954 Runtime::Options options;
2955 for (int i = 0; i < args->nOptions; ++i) {
2956 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002957 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002958 }
2959 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002960 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002961 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002962 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002963 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002964 bool started = runtime->Start();
2965 if (!started) {
2966 delete Thread::Current()->GetJniEnv();
2967 delete runtime->GetJavaVM();
2968 LOG(WARNING) << "CreateJavaVM failed";
2969 return JNI_ERR;
2970 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002971 *p_env = Thread::Current()->GetJniEnv();
2972 *p_vm = runtime->GetJavaVM();
2973 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002974}
2975
Elliott Hughesf2682d52011-08-15 16:37:04 -07002976extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002977 Runtime* runtime = Runtime::Current();
2978 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002979 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002980 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002981 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002982 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002983 }
2984 return JNI_OK;
2985}
2986
2987// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002988extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002989 return JNI_ERR;
2990}
2991
Elliott Hughescdf53122011-08-19 15:46:09 -07002992class JII {
2993 public:
2994 static jint DestroyJavaVM(JavaVM* vm) {
2995 if (vm == NULL) {
2996 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002997 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002998 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2999 delete raw_vm->runtime;
3000 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003001 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003002
Elliott Hughescdf53122011-08-19 15:46:09 -07003003 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003004 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003005 }
3006
3007 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003008 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003009 }
3010
3011 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07003012 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003013 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003014 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003015 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3016 Runtime* runtime = raw_vm->runtime;
3017 runtime->DetachCurrentThread();
3018 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003019 }
3020
3021 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003022 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3023 // and unlike other calls that take a JNI version doesn't care if you supply
3024 // JNI_VERSION_1_1, which we don't otherwise support.
3025 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003026 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003027 return JNI_EVERSION;
3028 }
3029 if (vm == NULL || env == NULL) {
3030 return JNI_ERR;
3031 }
3032 Thread* thread = Thread::Current();
3033 if (thread == NULL) {
3034 *env = NULL;
3035 return JNI_EDETACHED;
3036 }
3037 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003038 return JNI_OK;
3039 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003040};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003041
Elliott Hughes88c5c352012-03-15 18:49:48 -07003042const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07003043 NULL, // reserved0
3044 NULL, // reserved1
3045 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003046 JII::DestroyJavaVM,
3047 JII::AttachCurrentThread,
3048 JII::DetachCurrentThread,
3049 JII::GetEnv,
3050 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003051};
3052
Elliott Hughesa0957642011-09-02 14:27:33 -07003053JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003054 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07003055 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07003056 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003057 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003058 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003059 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08003060 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08003061 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003062 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003063 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003064 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003065 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003066 libraries(new Libraries),
3067 weak_globals_lock_("JNI weak global reference table lock"),
3068 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3069 allow_new_weak_globals_(true),
3070 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003071 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003072 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003073 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003074 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003075}
3076
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003077JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003078 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003079}
3080
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003081jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3082 if (obj == nullptr) {
3083 return nullptr;
3084 }
3085 MutexLock mu(self, weak_globals_lock_);
3086 while (UNLIKELY(!allow_new_weak_globals_)) {
3087 weak_globals_add_condition_.WaitHoldingLocks(self);
3088 }
3089 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3090 return reinterpret_cast<jweak>(ref);
3091}
3092
3093void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3094 MutexLock mu(self, weak_globals_lock_);
3095 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3096 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3097 << "failed to find entry";
3098 }
3099}
3100
Elliott Hughes88c5c352012-03-15 18:49:48 -07003101void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3102 check_jni = enabled;
3103 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003104}
3105
Elliott Hughesae80b492012-04-24 10:43:17 -07003106void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3107 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3108 if (force_copy) {
3109 os << " (with forcecopy)";
3110 }
3111 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003112 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003113 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003114 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003115 os << "; pins=" << pin_table.Size();
3116 }
3117 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003118 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003119 os << "; globals=" << globals.Capacity();
3120 }
3121 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003122 MutexLock mu(self, weak_globals_lock_);
3123 if (weak_globals_.Capacity() > 0) {
3124 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003125 }
3126 }
3127 os << '\n';
3128
3129 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003130 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003131 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3132 }
3133}
3134
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003135void JavaVMExt::DisallowNewWeakGlobals() {
3136 MutexLock mu(Thread::Current(), weak_globals_lock_);
3137 allow_new_weak_globals_ = false;
3138}
3139
3140void JavaVMExt::AllowNewWeakGlobals() {
3141 Thread* self = Thread::Current();
3142 MutexLock mu(self, weak_globals_lock_);
3143 allow_new_weak_globals_ = true;
3144 weak_globals_add_condition_.Broadcast(self);
3145}
3146
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003147mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3148 MutexLock mu(self, weak_globals_lock_);
3149 while (UNLIKELY(!allow_new_weak_globals_)) {
3150 weak_globals_add_condition_.WaitHoldingLocks(self);
3151 }
3152 return const_cast<mirror::Object*>(weak_globals_.Get(ref));
3153}
3154
Elliott Hughes73e66f72012-05-09 09:34:45 -07003155void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003156 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003157 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003158 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003159 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003160 }
3161 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003162 MutexLock mu(self, weak_globals_lock_);
3163 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003164 }
3165 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003166 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003167 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003168 }
3169}
3170
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003171bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003172 std::string* detail) {
3173 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003174
3175 // See if we've already loaded this library. If we have, and the class loader
3176 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003177 // TODO: for better results we should canonicalize the pathname (or even compare
3178 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003179 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003180 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003181 {
3182 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003183 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003184 library = libraries->Get(path);
3185 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003186 if (library != NULL) {
3187 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07003188 // The library will be associated with class_loader. The JNI
3189 // spec says we can't load the same library into more than one
3190 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003191 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003192 "ClassLoader %p; can't open in ClassLoader %p",
3193 path.c_str(), library->GetClassLoader(), class_loader);
3194 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003195 return false;
3196 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003197 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
3198 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003199 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003200 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003201 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003202 return false;
3203 }
3204 return true;
3205 }
3206
3207 // Open the shared library. Because we're using a full path, the system
3208 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3209 // resolve this library's dependencies though.)
3210
3211 // Failures here are expected when java.library.path has several entries
3212 // and we have to hunt for the lib.
3213
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003214 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3215 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3216 // dlopen) becomes zero from dlclose.
3217
Elliott Hughescdf53122011-08-19 15:46:09 -07003218 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003219 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003220 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
3221 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
3222 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003223
Elliott Hughes84b2f142012-09-27 09:16:28 -07003224 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003225
3226 if (handle == NULL) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003227 *detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003228 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003229 return false;
3230 }
3231
3232 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003233 // TODO: move the locking (and more of this logic) into Libraries.
3234 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003235 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003236 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003237 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003238 if (library == NULL) { // We won race to get libraries_lock
3239 library = new SharedLibrary(path, handle, class_loader);
3240 libraries->Put(path, library);
3241 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003242 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003243 }
3244 if (!created_library) {
3245 LOG(INFO) << "WOW: we lost a race to add shared library: "
3246 << "\"" << path << "\" ClassLoader=" << class_loader;
3247 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003248 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003249
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003250 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003251
Elliott Hughes79353722013-08-02 16:52:18 -07003252 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003253 void* sym = dlsym(handle, "JNI_OnLoad");
3254 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003255 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003256 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003257 } else {
3258 // Call JNI_OnLoad. We have to override the current class
3259 // loader, which will always be "null" since the stuff at the
3260 // top of the stack is around Runtime.loadLibrary(). (See
3261 // the comments in the JNI FindClass function.)
3262 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3263 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003264 SirtRef<ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Elliott Hughes79082e32011-08-25 12:07:32 -07003265 self->SetClassLoaderOverride(class_loader);
3266
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003267 int version = 0;
3268 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003269 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003270 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003271 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07003272 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003273
Mathieu Chartier590fee92013-09-13 13:46:47 -07003274 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003275
Elliott Hughes79353722013-08-02 16:52:18 -07003276 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003277 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003278 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003279 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003280 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003281 // It's unwise to call dlclose() here, but we can mark it
3282 // as bad and ensure that future load attempts will fail.
3283 // We don't know how far JNI_OnLoad got, so there could
3284 // be some partially-initialized stuff accessible through
3285 // newly-registered native method calls. We could try to
3286 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003287 } else {
3288 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003289 }
Elliott Hughes79353722013-08-02 16:52:18 -07003290 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003291 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003292 }
3293
Elliott Hughes79353722013-08-02 16:52:18 -07003294 library->SetResult(was_successful);
3295 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003296}
3297
Brian Carlstromea46f952013-07-30 01:26:50 -07003298void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003299 CHECK(m->IsNative());
3300
3301 Class* c = m->GetDeclaringClass();
3302
3303 // If this is a static method, it could be called before the class
3304 // has been initialized.
3305 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07003306 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003307 return NULL;
3308 }
3309 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003310 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003311 }
3312
Brian Carlstrom16192862011-09-12 17:50:06 -07003313 std::string detail;
3314 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003315 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003316 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003317 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003318 native_method = libraries->FindNativeMethod(m, detail);
3319 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003320 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07003321 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003322 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3323 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003324 }
3325 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003326}
3327
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003328void JavaVMExt::SweepJniWeakGlobals(RootVisitor visitor, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003329 MutexLock mu(Thread::Current(), weak_globals_lock_);
3330 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003331 mirror::Object* obj = *entry;
3332 mirror::Object* new_obj = visitor(obj, arg);
3333 if (new_obj == nullptr) {
3334 new_obj = kClearedJniWeakGlobal;
3335 }
3336 *entry = new_obj;
3337 }
3338}
3339
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003340void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003341 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003342 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003343 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003344 globals.VisitRoots(visitor, arg);
3345 }
3346 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003347 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003348 pin_table.VisitRoots(visitor, arg);
3349 }
3350 // The weak_globals table is visited by the GC itself (because it mutates the table).
3351}
3352
Elliott Hughesc8fece32013-01-02 11:27:23 -08003353void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003354 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003355 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
3356 if (c.get() == NULL) {
3357 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3358 }
3359 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3360}
3361
Ian Rogersdf20fe02011-07-20 20:34:16 -07003362} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003363
3364std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3365 switch (rhs) {
3366 case JNIInvalidRefType:
3367 os << "JNIInvalidRefType";
3368 return os;
3369 case JNILocalRefType:
3370 os << "JNILocalRefType";
3371 return os;
3372 case JNIGlobalRefType:
3373 os << "JNIGlobalRefType";
3374 return os;
3375 case JNIWeakGlobalRefType:
3376 os << "JNIWeakGlobalRefType";
3377 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003378 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003379 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003380 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003381 }
3382}