blob: e098e11e51154ee755391f7d7c089881233fdd6d [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>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
Elliott Hughes0af55432011-08-17 18:37:28 -070023#include <utility>
24#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025
Ian Rogersef7d42f2014-01-06 12:55:46 -080026#include "atomic.h"
Mathieu Chartierbad02672014-08-25 13:08:22 -070027#include "base/allocator.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080029#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080030#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080031#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "dex_file-inl.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070033#include "gc_root.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/accounting/card_table-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070035#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070036#include "interpreter/interpreter.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070037#include "jni_env_ext.h"
38#include "java_vm_ext.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070039#include "mirror/art_field-inl.h"
40#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/class-inl.h"
42#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/object-inl.h"
44#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070045#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "mirror/throwable.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080047#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070048#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070049#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070050#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070052#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070053#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070055#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070056
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070057namespace art {
58
Mathieu Chartier24555ad2014-10-06 13:41:33 -070059// Consider turning this on when there is errors which could be related to JNI array copies such as
60// things not rendering correctly. E.g. b/16858794
61static constexpr bool kWarnJniAbort = false;
62
Elliott Hughes6b436852011-08-12 10:16:44 -070063// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
64// separated with slashes but aren't wrapped with "L;" like regular descriptors
65// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
66// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
67// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070068static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070069 std::string result;
70 // Add the missing "L;" if necessary.
71 if (name[0] == '[') {
72 result = name;
73 } else {
74 result += 'L';
75 result += name;
76 result += ';';
77 }
78 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070079 if (result.find('.') != std::string::npos) {
80 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
81 << "\"" << name << "\"";
82 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -070083 }
84 return result;
85}
86
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080087static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -070089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080090 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Ian Rogers1ff3c982014-08-12 02:30:58 -070091 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -080092 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
93 "no %s method \"%s.%s%s\"",
Ian Rogers1ff3c982014-08-12 02:30:58 -070094 kind, c->GetDescriptor(&temp), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -070095}
96
Sebastien Hertzfa65e842014-07-03 09:39:53 +020097static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa, mirror::Class* c,
98 const char* kind, jint idx, bool return_errors)
99 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
100 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method in "
101 << PrettyDescriptor(c) << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
102 << ": " << kind << " is null at index " << idx;
103 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
104 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
105 "%s is null at index %d", kind, idx);
106}
107
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800108static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
110 if (LIKELY(klass->IsInitialized())) {
111 return klass;
112 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700113 StackHandleScope<1> hs(self);
114 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700115 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800116 return nullptr;
117 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700118 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800119}
120
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
122 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700123 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800124 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800125 if (c == nullptr) {
126 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700127 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800128 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700129 if (is_static) {
130 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700131 } else if (c->IsInterface()) {
132 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700133 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700134 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800135 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700136 // No virtual method matching the signature. Search declared
137 // private methods and constructors.
138 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700139 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700140 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800141 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700142 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800143 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700144 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700146}
147
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800148static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800150 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700151 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
152 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700153 return soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700154 }
Brian Carlstromce888532013-10-10 00:32:58 -0700155 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800156 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700157 return method->GetDeclaringClass()->GetClassLoader();
158 }
159 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800160 mirror::ClassLoader* class_loader =
161 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
162 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700163 return class_loader;
164 }
165 // See if the override ClassLoader is set for gtests.
Ian Rogers68d8b422014-07-17 11:09:10 -0700166 class_loader = soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800167 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800168 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700169 CHECK(Runtime::Current()->UseCompileTimeClassPath());
170 return class_loader;
171 }
172 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800173 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700174}
175
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700176static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
177 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700179 StackHandleScope<2> hs(soa.Self());
180 Handle<mirror::Class> c(
181 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
182 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800183 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700184 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800185 mirror::ArtField* field = nullptr;
186 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700187 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
188 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700189 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800190 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700191 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700193 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800194 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700196 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800197 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700198 StackHandleScope<1> hs(soa.Self());
199 Handle<mirror::Throwable> cause(hs.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200 soa.Self()->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700201 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800203 "no type \"%s\" found and so no field \"%s\" "
204 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700205 c->GetDescriptor(&temp));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700206 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800207 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700208 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700209 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700211 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700212 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700213 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700214 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800216 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800217 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
218 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
219 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700220 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800221 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700222 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700224}
225
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800226static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700228 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700229 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800230 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
231 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
232 "%s offset=%d length=%d %s.length=%d",
233 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700234}
Ian Rogers0571d352011-11-03 19:51:38 -0700235
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700236static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
237 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700238 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800239 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
240 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
241 "offset=%d length=%d string.length()=%d", start, length,
242 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700243}
Elliott Hughes814e4032011-08-23 12:07:56 -0700244
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700246 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 // Turn the const char* into a java.lang.String.
248 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800249 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700250 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700251 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700252
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 // Choose an appropriate constructor and set up the arguments.
254 jvalue args[2];
255 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800256 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800258 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 signature = "(Ljava/lang/String;)V";
260 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800261 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262 signature = "(Ljava/lang/Throwable;)V";
263 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700264 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
266 args[0].l = s.get();
267 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700268 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800270 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800271 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800273 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274 return JNI_ERR;
275 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700276
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800277 ScopedLocalRef<jthrowable> exception(
278 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
279 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 return JNI_ERR;
281 }
Ian Rogersef28b142012-11-30 14:22:18 -0800282 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800283 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800284 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700286}
287
Ian Rogers68d8b422014-07-17 11:09:10 -0700288static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
289 return reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughes75770752011-08-24 17:52:38 -0700290}
291
Ian Rogers2d10b202014-05-12 19:15:18 -0700292#define CHECK_NON_NULL_ARGUMENT(value) \
293 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700294
Ian Rogers2d10b202014-05-12 19:15:18 -0700295#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
296 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
297
298#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
299 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
300
301#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
302 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
303
304#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800305 if (UNLIKELY(value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700306 JavaVmExtFromEnv(env)->JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700307 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700308 }
309
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700310#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800311 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700312 JavaVmExtFromEnv(env)->JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700313 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700314 }
315
Elliott Hughescdf53122011-08-19 15:46:09 -0700316class JNI {
317 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700318 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700319 return JNI_VERSION_1_6;
320 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700321
Ian Rogers25e8b912012-09-07 11:31:36 -0700322 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700323 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800324 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700325 }
326
Elliott Hughescdf53122011-08-19 15:46:09 -0700327 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700328 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700329 Runtime* runtime = Runtime::Current();
330 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700331 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700332 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800333 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700334 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700335 StackHandleScope<1> hs(soa.Self());
336 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800337 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700338 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800339 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700340 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700342 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700343
Ian Rogers62f05122014-03-21 11:21:29 -0700344 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700345 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700346 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700347 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700348 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700349
Ian Rogers62f05122014-03-21 11:21:29 -0700350 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700351 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700353 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700354 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700355
Elliott Hughescdf53122011-08-19 15:46:09 -0700356 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700357 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800359 mirror::ArtMethod* m = soa.DecodeMethod(mid);
360 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700361 jobject art_method = soa.AddLocalReference<jobject>(m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200362 jobject reflect_method;
363 if (m->IsConstructor()) {
364 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
365 } else {
366 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
367 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700368 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800369 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700370 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800371 SetObjectField(env, reflect_method,
372 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700373 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700374 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700375
Elliott Hughescdf53122011-08-19 15:46:09 -0700376 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700377 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800379 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700380 jobject art_field = soa.AddLocalReference<jobject>(f);
381 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
382 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800383 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700384 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800385 SetObjectField(env, reflect_field,
386 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700387 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700388 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700389
Elliott Hughes37f7a402011-08-22 18:56:01 -0700390 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700391 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700392 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800393 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700395 }
396
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700397 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700398 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700399 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800400 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700402 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700403
Narayan Kamath1268b742014-07-11 19:15:11 +0100404 // Note: java_class1 should be safely castable to java_class2, and
405 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700406 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700407 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
408 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700409 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800410 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
411 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100412 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700413 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700414
Elliott Hughese84278b2012-03-22 10:06:53 -0700415 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700416 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800417 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700418 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700419 return JNI_TRUE;
420 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700421 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800422 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
423 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700424 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700425 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700426 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700427
Elliott Hughes37f7a402011-08-22 18:56:01 -0700428 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700429 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800430 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
431 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700432 return JNI_ERR;
433 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800434 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
435 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700436 return JNI_OK;
437 }
438
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700439 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700440 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800441 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700442 }
443
444 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700445 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700446 }
447
448 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700449 ScopedObjectAccess soa(env);
450 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700451 }
452
453 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700454 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700455
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700456 // If we have no exception to describe, pass through.
457 if (!soa.Self()->GetException(nullptr)) {
458 return;
459 }
460
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700461 StackHandleScope<3> hs(soa.Self());
462 // TODO: Use nullptr instead of null handles?
463 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
464 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
465 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800466 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200467 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800468 {
469 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800470 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700471 old_throw_this_object.Assign(old_throw_location.GetThis());
472 old_throw_method.Assign(old_throw_location.GetMethod());
473 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800474 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200475 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800476 soa.Self()->ClearException();
477 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800478 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700479 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700480 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
481 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800482 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700483 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700484 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700485 } else {
486 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800487 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800488 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700489 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800490 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700491 }
492 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700493 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800494 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700495
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700496 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200497 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700498 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700499
Elliott Hughescdf53122011-08-19 15:46:09 -0700500 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700501 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800502 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700503 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700504 }
505
Ian Rogers25e8b912012-09-07 11:31:36 -0700506 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700507 LOG(FATAL) << "JNI FatalError called: " << msg;
508 }
509
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700510 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700511 // TODO: SOA may not be necessary but I do it to please lock annotations.
512 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700513 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700514 return JNI_ERR;
515 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700516 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700517 return JNI_OK;
518 }
519
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700520 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700521 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800522 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700523 soa.Env()->PopFrame();
524 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700525 }
526
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700527 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700528 // TODO: SOA may not be necessary but I do it to please lock annotations.
529 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700530 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700531 }
532
Elliott Hughescdf53122011-08-19 15:46:09 -0700533 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700534 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800535 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700536 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700537 }
538
539 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700540 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
541 Thread* self = down_cast<JNIEnvExt*>(env)->self;
542 vm->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700543 }
544
545 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700546 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700547 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
548 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700549 }
550
551 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700552 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
553 Thread* self = down_cast<JNIEnvExt*>(env)->self;
554 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700555 }
556
557 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700558 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800559 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800560 // Check for null after decoding the object to handle cleared weak globals.
561 if (decoded_obj == nullptr) {
562 return nullptr;
563 }
564 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700565 }
566
567 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800568 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700569 return;
570 }
Ian Rogersef28b142012-11-30 14:22:18 -0800571 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700572
Ian Rogersef28b142012-11-30 14:22:18 -0800573 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700574 if (!locals.Remove(cookie, obj)) {
575 // Attempting to delete a local reference that is not in the
576 // topmost local reference frame is a no-op. DeleteLocalRef returns
577 // void and doesn't throw any exceptions, but we should probably
578 // complain about it so the user will notice that things aren't
579 // going quite the way they expect.
580 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
581 << "failed to find entry";
582 }
583 }
584
585 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700586 if (obj1 == obj2) {
587 return JNI_TRUE;
588 } else {
589 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800590 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
591 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700592 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700593 }
594
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700595 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700596 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700597 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800598 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800599 if (c == nullptr) {
600 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700601 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700602 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700603 }
604
Ian Rogersbc939662013-08-15 10:26:54 -0700605 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700606 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700607 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700608 CHECK_NON_NULL_ARGUMENT(java_class);
609 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700610 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700611 va_end(args);
612 return result;
613 }
614
Elliott Hughes72025e52011-08-23 17:50:30 -0700615 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700616 CHECK_NON_NULL_ARGUMENT(java_class);
617 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700618 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800619 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800620 if (c == nullptr) {
621 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700622 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800623 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800624 if (result == nullptr) {
625 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700626 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700627 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700628 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800629 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800630 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700631 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800632 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700633 }
634
Elliott Hughes72025e52011-08-23 17:50:30 -0700635 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700636 CHECK_NON_NULL_ARGUMENT(java_class);
637 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800639 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800640 if (c == nullptr) {
641 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700642 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800643 mirror::Object* result = c->AllocObject(soa.Self());
644 if (result == nullptr) {
645 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700646 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700648 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800649 if (soa.Self()->IsExceptionPending()) {
650 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700651 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800652 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 }
654
Ian Rogersbc939662013-08-15 10:26:54 -0700655 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700656 CHECK_NON_NULL_ARGUMENT(java_class);
657 CHECK_NON_NULL_ARGUMENT(name);
658 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700659 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700660 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 }
662
Ian Rogersbc939662013-08-15 10:26:54 -0700663 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
664 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700665 CHECK_NON_NULL_ARGUMENT(java_class);
666 CHECK_NON_NULL_ARGUMENT(name);
667 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700669 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 }
671
Elliott Hughes72025e52011-08-23 17:50:30 -0700672 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700673 va_list ap;
674 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700675 CHECK_NON_NULL_ARGUMENT(obj);
676 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700677 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700679 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700680 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 }
682
Elliott Hughes72025e52011-08-23 17:50:30 -0700683 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700684 CHECK_NON_NULL_ARGUMENT(obj);
685 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700686 ScopedObjectAccess soa(env);
687 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
688 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 }
690
Elliott Hughes72025e52011-08-23 17:50:30 -0700691 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700692 CHECK_NON_NULL_ARGUMENT(obj);
693 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700695 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
696 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700697 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700698 }
699
Elliott Hughes72025e52011-08-23 17:50:30 -0700700 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700701 va_list ap;
702 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700703 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
704 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700705 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700706 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700707 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700708 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 }
710
Elliott Hughes72025e52011-08-23 17:50:30 -0700711 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700712 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
713 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 ScopedObjectAccess soa(env);
715 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700716 }
717
Elliott Hughes72025e52011-08-23 17:50:30 -0700718 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700719 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
720 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700722 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
723 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700724 }
725
Elliott Hughes72025e52011-08-23 17:50:30 -0700726 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700727 va_list ap;
728 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700729 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
730 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700731 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700732 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700733 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700734 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 }
736
Elliott Hughes72025e52011-08-23 17:50:30 -0700737 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700738 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
739 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700740 ScopedObjectAccess soa(env);
741 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 }
743
Elliott Hughes72025e52011-08-23 17:50:30 -0700744 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700745 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
746 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700747 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700748 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
749 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700750 }
751
Elliott Hughes72025e52011-08-23 17:50:30 -0700752 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700753 va_list ap;
754 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700755 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
756 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700757 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700758 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700759 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700760 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 }
762
Elliott Hughes72025e52011-08-23 17:50:30 -0700763 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700764 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
765 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766 ScopedObjectAccess soa(env);
767 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700768 }
769
Elliott Hughes72025e52011-08-23 17:50:30 -0700770 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700771 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
772 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700773 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700774 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
775 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700776 }
777
Elliott Hughes72025e52011-08-23 17:50:30 -0700778 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700779 va_list ap;
780 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700781 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
782 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700783 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700784 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700785 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700786 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 }
788
Elliott Hughes72025e52011-08-23 17:50:30 -0700789 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700790 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
791 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700792 ScopedObjectAccess soa(env);
793 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700794 }
795
Elliott Hughes72025e52011-08-23 17:50:30 -0700796 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700797 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
798 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700799 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700800 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
801 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700802 }
803
Elliott Hughes72025e52011-08-23 17:50:30 -0700804 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700805 va_list ap;
806 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700807 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
808 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700809 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700810 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700811 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700812 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 }
814
Elliott Hughes72025e52011-08-23 17:50:30 -0700815 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700816 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
817 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700818 ScopedObjectAccess soa(env);
819 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 }
821
Elliott Hughes72025e52011-08-23 17:50:30 -0700822 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700823 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
824 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700825 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700826 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
827 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 }
829
Elliott Hughes72025e52011-08-23 17:50:30 -0700830 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700831 va_list ap;
832 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700833 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
834 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700835 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700836 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700837 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700838 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 }
840
Elliott Hughes72025e52011-08-23 17:50:30 -0700841 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700842 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
843 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700844 ScopedObjectAccess soa(env);
845 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700846 }
847
Elliott Hughes72025e52011-08-23 17:50:30 -0700848 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700849 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
850 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700851 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700852 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
853 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 }
855
Elliott Hughes72025e52011-08-23 17:50:30 -0700856 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700857 va_list ap;
858 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700859 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
860 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700861 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700862 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700863 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700864 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 }
866
Elliott Hughes72025e52011-08-23 17:50:30 -0700867 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700868 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
869 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700870 ScopedObjectAccess soa(env);
871 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 }
873
Elliott Hughes72025e52011-08-23 17:50:30 -0700874 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700875 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
876 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700877 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700878 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
879 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700880 }
881
Elliott Hughes72025e52011-08-23 17:50:30 -0700882 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700883 va_list ap;
884 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700885 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
886 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700887 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700888 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700889 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700890 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700891 }
892
Elliott Hughes72025e52011-08-23 17:50:30 -0700893 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700894 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
895 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700896 ScopedObjectAccess soa(env);
897 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700898 }
899
Elliott Hughes72025e52011-08-23 17:50:30 -0700900 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700901 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
902 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700903 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700904 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
905 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 }
907
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700909 va_list ap;
910 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700911 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
912 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700913 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -0700914 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 }
917
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700919 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
920 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700921 ScopedObjectAccess soa(env);
922 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 }
924
Elliott Hughes72025e52011-08-23 17:50:30 -0700925 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700926 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
927 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700928 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700929 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 }
931
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700932 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700935 CHECK_NON_NULL_ARGUMENT(obj);
936 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700937 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700938 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
939 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 va_end(ap);
941 return local_result;
942 }
943
Ian Rogersbc939662013-08-15 10:26:54 -0700944 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
945 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700946 CHECK_NON_NULL_ARGUMENT(obj);
947 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700948 ScopedObjectAccess soa(env);
949 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
950 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 }
952
Ian Rogersbc939662013-08-15 10:26:54 -0700953 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
954 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700955 CHECK_NON_NULL_ARGUMENT(obj);
956 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700957 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700958 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700959 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 }
961
Ian Rogersbc939662013-08-15 10:26:54 -0700962 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
963 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700966 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
967 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700968 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700969 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700971 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Ian Rogersbc939662013-08-15 10:26:54 -0700974 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
975 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700976 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
977 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700978 ScopedObjectAccess soa(env);
979 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 }
981
Ian Rogersbc939662013-08-15 10:26:54 -0700982 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
983 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700984 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
985 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700987 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700990 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700993 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
994 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700995 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700998 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Ian Rogersbc939662013-08-15 10:26:54 -07001001 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1002 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001003 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1004 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001005 ScopedObjectAccess soa(env);
1006 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Ian Rogersbc939662013-08-15 10:26:54 -07001009 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1010 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001011 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1012 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001014 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001017 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001020 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1021 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001022 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001023 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001025 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Ian Rogersbc939662013-08-15 10:26:54 -07001028 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1029 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001030 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1031 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001032 ScopedObjectAccess soa(env);
1033 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Ian Rogersbc939662013-08-15 10:26:54 -07001036 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1037 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001038 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1039 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001040 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001041 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 }
1043
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001044 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001047 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1048 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001049 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001050 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001052 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Ian Rogersbc939662013-08-15 10:26:54 -07001055 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1056 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001057 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1058 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001059 ScopedObjectAccess soa(env);
1060 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Ian Rogersbc939662013-08-15 10:26:54 -07001063 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1064 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001065 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1066 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001067 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001068 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001071 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001074 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1075 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001076 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001079 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Ian Rogersbc939662013-08-15 10:26:54 -07001082 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1083 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001084 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1085 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 ScopedObjectAccess soa(env);
1087 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Ian Rogersbc939662013-08-15 10:26:54 -07001090 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1091 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001092 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1093 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001095 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001098 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001101 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1102 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001103 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001104 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001106 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Ian Rogersbc939662013-08-15 10:26:54 -07001109 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1110 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001111 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1112 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001113 ScopedObjectAccess soa(env);
1114 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Ian Rogersbc939662013-08-15 10:26:54 -07001117 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1118 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001119 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1120 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001121 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001122 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001125 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001128 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1129 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001130 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001131 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001133 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Ian Rogersbc939662013-08-15 10:26:54 -07001136 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1137 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001138 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1139 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001140 ScopedObjectAccess soa(env);
1141 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Ian Rogersbc939662013-08-15 10:26:54 -07001144 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1145 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001146 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1147 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001149 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001152 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001155 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1156 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001157 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001158 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001160 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
Ian Rogersbc939662013-08-15 10:26:54 -07001163 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1164 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001165 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1166 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001167 ScopedObjectAccess soa(env);
1168 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
Ian Rogersbc939662013-08-15 10:26:54 -07001171 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1172 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001173 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1174 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001175 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001176 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001179 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001182 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1183 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001184 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001185 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 va_end(ap);
1187 }
1188
Brian Carlstromea46f952013-07-30 01:26:50 -07001189 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1190 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001191 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1192 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001193 ScopedObjectAccess soa(env);
1194 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
Ian Rogersbc939662013-08-15 10:26:54 -07001197 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1198 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001199 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1200 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001201 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001202 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
Ian Rogersbc939662013-08-15 10:26:54 -07001205 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001206 CHECK_NON_NULL_ARGUMENT(java_class);
1207 CHECK_NON_NULL_ARGUMENT(name);
1208 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001209 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001210 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001212
Ian Rogersbc939662013-08-15 10:26:54 -07001213 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1214 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001215 CHECK_NON_NULL_ARGUMENT(java_class);
1216 CHECK_NON_NULL_ARGUMENT(name);
1217 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001218 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001219 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001221
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001222 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001223 CHECK_NON_NULL_ARGUMENT(obj);
1224 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001225 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001226 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1227 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001230
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001231 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001232 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001233 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001234 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001235 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 }
1237
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001238 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001239 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1240 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001241 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001242 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1243 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1244 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001245 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001248 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001249 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001250 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001251 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1252 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001253 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 }
1255
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001256#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001257 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1258 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001259 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001260 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1261 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001262 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001263
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001264#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001265 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001266 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001267 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001268 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001269
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001270#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001271 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1272 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001273 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001274 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1275 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001276 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001277
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001278#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001279 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001280 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001281 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001282 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001283
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001284 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001285 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
1287
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001288 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001289 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001292 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001293 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001296 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001297 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001300 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001301 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001304 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001305 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001308 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001309 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001312 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001313 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001316 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001317 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001320 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001321 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001324 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001325 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001328 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001329 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 }
1331
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001332 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001333 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001336 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001337 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001338 }
1339
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001340 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001341 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001342 }
1343
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001344 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001345 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001346 }
1347
1348 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001349 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001350 }
1351
1352 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001353 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001354 }
1355
1356 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001357 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001358 }
1359
1360 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001361 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001362 }
1363
1364 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001365 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001366 }
1367
1368 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001369 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001370 }
1371
1372 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001373 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001374 }
1375
1376 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001377 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001378 }
1379
1380 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001381 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001382 }
1383
1384 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001385 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 }
1387
1388 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001389 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001390 }
1391
1392 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001393 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 }
1395
1396 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001397 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001398 }
1399
1400 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001401 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 }
1403
1404 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001405 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001406 }
1407
1408 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001409 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001412 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001414 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001415 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001416 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001417 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001418 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 va_end(ap);
1420 return local_result;
1421 }
1422
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001423 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001424 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001425 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001426 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001427 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001430 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001431 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001432 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001433 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001434 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
1436
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001437 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001439 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001440 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001441 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001442 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001444 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001447 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001448 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001449 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001450 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 }
1452
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001453 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001454 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001455 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001456 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001459 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001461 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001462 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001463 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001464 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001466 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 }
1468
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001469 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001470 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001471 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001472 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001475 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001476 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001477 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001478 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001479 }
1480
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001481 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001483 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001484 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001485 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001486 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001488 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001491 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001492 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001493 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001494 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 }
1496
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001497 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001498 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001499 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001500 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001503 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001505 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001506 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001507 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001508 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001510 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001511 }
1512
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001513 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001514 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001515 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001516 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
1518
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001519 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001520 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001521 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001522 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 }
1524
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001525 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001527 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001528 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001529 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001530 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001531 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001532 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001535 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001536 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001537 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001538 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001539 }
1540
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001541 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001542 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001543 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001544 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001547 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001549 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001550 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001551 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001552 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001554 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 }
1556
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001557 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001558 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001559 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001560 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001563 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001564 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001565 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001566 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001569 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001571 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001572 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001573 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001574 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001576 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001579 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001580 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001581 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001582 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001585 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001586 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001587 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001588 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001591 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001593 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001594 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001595 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001596 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001598 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001601 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001602 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001603 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001604 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001607 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001608 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001609 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001610 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001613 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001616 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001617 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001618 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 va_end(ap);
1620 }
1621
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001622 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001623 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001624 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001625 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 }
1627
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001628 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001629 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001630 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001631 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 }
1633
Elliott Hughes814e4032011-08-23 12:07:56 -07001634 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001635 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001636 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001637 return nullptr;
1638 }
1639 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001640 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001641 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001642 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001643 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001644 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001645 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 }
1647
1648 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001649 if (utf == nullptr) {
1650 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001652 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001653 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001654 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 }
1656
Elliott Hughes814e4032011-08-23 12:07:56 -07001657 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001658 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001659 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001660 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001661 }
1662
1663 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001664 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001665 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001666 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001667 }
1668
Ian Rogersbc939662013-08-15 10:26:54 -07001669 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1670 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001671 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001672 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001673 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001674 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001675 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001676 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001677 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001678 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1679 memcpy(buf, chars + start, length * sizeof(jchar));
1680 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001681 }
1682
Ian Rogersbc939662013-08-15 10:26:54 -07001683 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1684 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001685 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001686 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001687 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001688 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001689 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001690 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001691 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001692 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1693 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1694 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001695 }
1696
Elliott Hughes75770752011-08-24 17:52:38 -07001697 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001698 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001699 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001700 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1701 mirror::CharArray* chars = s->GetCharArray();
Fred Shih56890e22014-06-02 11:11:52 -07001702 gc::Heap* heap = Runtime::Current()->GetHeap();
1703 if (heap->IsMovableObject(chars)) {
1704 if (is_copy != nullptr) {
1705 *is_copy = JNI_TRUE;
1706 }
1707 int32_t char_count = s->GetLength();
1708 int32_t offset = s->GetOffset();
1709 jchar* bytes = new jchar[char_count];
1710 for (int32_t i = 0; i < char_count; i++) {
1711 bytes[i] = chars->Get(i + offset);
1712 }
1713 return bytes;
1714 } else {
1715 if (is_copy != nullptr) {
1716 *is_copy = JNI_FALSE;
1717 }
1718 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001719 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001720 }
1721
Mathieu Chartier590fee92013-09-13 13:46:47 -07001722 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001723 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001724 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001725 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1726 mirror::CharArray* s_chars = s->GetCharArray();
1727 if (chars != (s_chars->GetData() + s->GetOffset())) {
1728 delete[] chars;
1729 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 }
1731
Elliott Hughes75770752011-08-24 17:52:38 -07001732 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001733 CHECK_NON_NULL_ARGUMENT(java_string);
1734 ScopedObjectAccess soa(env);
1735 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1736 mirror::CharArray* chars = s->GetCharArray();
1737 int32_t offset = s->GetOffset();
Fred Shih56890e22014-06-02 11:11:52 -07001738 gc::Heap* heap = Runtime::Current()->GetHeap();
1739 if (heap->IsMovableObject(chars)) {
1740 StackHandleScope<1> hs(soa.Self());
1741 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
1742 heap->IncrementDisableMovingGC(soa.Self());
1743 }
1744 if (is_copy != nullptr) {
1745 *is_copy = JNI_FALSE;
1746 }
1747 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
Elliott Hughes75770752011-08-24 17:52:38 -07001750 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07001751 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1752 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001753 gc::Heap* heap = Runtime::Current()->GetHeap();
1754 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1755 mirror::CharArray* s_chars = s->GetCharArray();
1756 if (heap->IsMovableObject(s_chars)) {
1757 heap->DecrementDisableMovingGC(soa.Self());
1758 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes75770752011-08-24 17:52:38 -07001761 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001762 if (java_string == nullptr) {
1763 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001764 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001765 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001766 *is_copy = JNI_TRUE;
1767 }
Ian Rogersef28b142012-11-30 14:22:18 -08001768 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001769 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001770 size_t byte_count = s->GetUtfLength();
1771 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001772 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001773 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1774 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1775 bytes[byte_count] = '\0';
1776 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001777 }
1778
Elliott Hughes75770752011-08-24 17:52:38 -07001779 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001780 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001781 }
1782
Elliott Hughesbd935992011-08-22 11:59:34 -07001783 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001784 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001785 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001786 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001787 if (UNLIKELY(!obj->IsArrayInstance())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001788 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1789 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001790 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001791 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001792 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes814e4032011-08-23 12:07:56 -07001795 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001796 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001797 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001798 mirror::ObjectArray<mirror::Object>* array =
1799 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001800 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 }
1802
Ian Rogersbc939662013-08-15 10:26:54 -07001803 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1804 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001805 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001806 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001807 mirror::ObjectArray<mirror::Object>* array =
1808 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1809 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001810 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 }
1812
1813 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001814 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
1817 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001818 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
1821 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001822 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
1825 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001826 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 }
1828
1829 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001830 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
1833 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001834 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
1837 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001838 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 }
1840
Ian Rogers1d99e452014-01-02 17:36:41 -08001841 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1842 jobject initial_element) {
1843 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001844 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001845 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001846 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001847 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001848
1849 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001850 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001851 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08001852 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001853 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08001854 if (UNLIKELY(element_class->IsPrimitive())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001855 soa.Vm()->JniAbortF("NewObjectArray", "not an object type: %s",
1856 PrettyDescriptor(element_class).c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08001857 return nullptr;
1858 }
Ian Rogers1d99e452014-01-02 17:36:41 -08001859 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07001860 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08001861 if (UNLIKELY(array_class == nullptr)) {
1862 return nullptr;
1863 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 }
1865
Elliott Hughes75770752011-08-24 17:52:38 -07001866 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001867 mirror::ObjectArray<mirror::Object>* result =
1868 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001869 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001870 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08001871 if (initial_object != nullptr) {
1872 mirror::Class* element_class = result->GetClass()->GetComponentType();
1873 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001874 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
1875 "element type of '%s'",
1876 PrettyDescriptor(initial_object->GetClass()).c_str(),
1877 PrettyDescriptor(element_class).c_str());
1878 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08001879 } else {
1880 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001881 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08001882 }
1883 }
Elliott Hughes75770752011-08-24 17:52:38 -07001884 }
1885 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 }
1888
1889 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001890 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
Ian Rogersa15e67d2012-02-28 13:51:55 -08001893 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001894 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001895 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001896 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07001897 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001898 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
1899 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001900 return nullptr;
1901 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001902 gc::Heap* heap = Runtime::Current()->GetHeap();
1903 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08001904 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001905 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001906 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001907 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001908 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001909 *is_copy = JNI_FALSE;
1910 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001911 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001912 }
1913
Ian Rogers2d10b202014-05-12 19:15:18 -07001914 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
1915 jint mode) {
1916 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
1917 ScopedObjectAccess soa(env);
1918 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
1919 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001920 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
1921 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001922 return;
1923 }
1924 const size_t component_size = array->GetClass()->GetComponentSize();
1925 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001926 }
1927
Elliott Hughes75770752011-08-24 17:52:38 -07001928 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001929 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001930 }
1931
Elliott Hughes75770752011-08-24 17:52:38 -07001932 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001933 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 }
1935
Elliott Hughes75770752011-08-24 17:52:38 -07001936 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001937 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 }
1939
Elliott Hughes75770752011-08-24 17:52:38 -07001940 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001941 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001942 }
1943
Elliott Hughes75770752011-08-24 17:52:38 -07001944 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001945 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 }
1947
Elliott Hughes75770752011-08-24 17:52:38 -07001948 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001949 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 }
1951
Elliott Hughes75770752011-08-24 17:52:38 -07001952 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001953 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Elliott Hughes75770752011-08-24 17:52:38 -07001956 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001957 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Mathieu Chartier590fee92013-09-13 13:46:47 -07001960 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
1961 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001962 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
1963 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Mathieu Chartier590fee92013-09-13 13:46:47 -07001966 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001967 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Mathieu Chartier590fee92013-09-13 13:46:47 -07001970 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001971 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Mathieu Chartier590fee92013-09-13 13:46:47 -07001974 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
1975 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001976 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Mathieu Chartier590fee92013-09-13 13:46:47 -07001979 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
1980 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001981 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Mathieu Chartier590fee92013-09-13 13:46:47 -07001984 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001985 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Mathieu Chartier590fee92013-09-13 13:46:47 -07001988 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001989 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Mathieu Chartier590fee92013-09-13 13:46:47 -07001992 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
1993 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001994 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
Ian Rogersbc939662013-08-15 10:26:54 -07001997 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
1998 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001999 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002000 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Ian Rogersbc939662013-08-15 10:26:54 -07002003 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2004 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002005 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Ian Rogersbc939662013-08-15 10:26:54 -07002008 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2009 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002010 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Ian Rogersbc939662013-08-15 10:26:54 -07002013 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2014 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002015 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002016 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Ian Rogersbc939662013-08-15 10:26:54 -07002019 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2020 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002021 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002022 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Ian Rogersbc939662013-08-15 10:26:54 -07002025 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2026 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002027 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Ian Rogersbc939662013-08-15 10:26:54 -07002030 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2031 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002032 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
2034
Ian Rogersbc939662013-08-15 10:26:54 -07002035 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2036 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002037 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002038 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Ian Rogersbc939662013-08-15 10:26:54 -07002041 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2042 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002043 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002044 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Ian Rogersbc939662013-08-15 10:26:54 -07002047 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2048 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002049 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Ian Rogersbc939662013-08-15 10:26:54 -07002052 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2053 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002054 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Ian Rogersbc939662013-08-15 10:26:54 -07002057 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2058 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002059 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002060 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Ian Rogersbc939662013-08-15 10:26:54 -07002063 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2064 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002065 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002066 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Ian Rogersbc939662013-08-15 10:26:54 -07002069 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2070 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002071 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Ian Rogersbc939662013-08-15 10:26:54 -07002074 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2075 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002076 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Ian Rogersbc939662013-08-15 10:26:54 -07002079 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2080 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002081 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002082 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Ian Rogersbc939662013-08-15 10:26:54 -07002085 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2086 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002087 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2088 }
2089
Ian Rogersbc939662013-08-15 10:26:54 -07002090 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2091 jint method_count, bool return_errors) {
2092 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002093 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2094 method_count);
2095 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002096 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002097 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002098 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002099 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002100 if (UNLIKELY(method_count == 0)) {
2101 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2102 << PrettyDescriptor(c);
2103 return JNI_OK;
2104 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002105 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002106 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 const char* name = methods[i].name;
2108 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002109 const void* fnPtr = methods[i].fnPtr;
2110 if (UNLIKELY(name == nullptr)) {
2111 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2112 return JNI_ERR;
2113 } else if (UNLIKELY(sig == nullptr)) {
2114 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2115 return JNI_ERR;
2116 } else if (UNLIKELY(fnPtr == nullptr)) {
2117 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2118 return JNI_ERR;
2119 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002120 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002122 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 ++sig;
2124 }
2125
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002126 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2127 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002128 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002130 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002131 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002132 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002133 << PrettyDescriptor(c) << "." << name << sig << " in "
2134 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002135 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002137 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002138 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002139 << PrettyDescriptor(c) << "." << name << sig
2140 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002141 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 return JNI_ERR;
2143 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002144
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002145 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002146
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002147 m->RegisterNative(fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 }
2149 return JNI_OK;
2150 }
2151
Elliott Hughes5174fe62011-08-23 15:12:35 -07002152 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002153 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002154 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002155 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002156
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002157 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158
Ian Rogers2d10b202014-05-12 19:15:18 -07002159 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002160 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002161 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002163 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002164 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002165 }
2166 }
2167 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002168 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002169 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002170 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002171 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002172 }
2173 }
2174
Ian Rogers2d10b202014-05-12 19:15:18 -07002175 if (unregistered_count == 0) {
2176 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2177 << PrettyDescriptor(c) << "' that contains no native methods";
2178 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002179 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
Ian Rogers719d1a32014-03-06 12:13:39 -08002182 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002183 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002184 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002185 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2186 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002187 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002188 return JNI_ERR;
2189 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002190 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002191 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
2193
Ian Rogers719d1a32014-03-06 12:13:39 -08002194 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002195 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002196 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002197 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002198 o->MonitorExit(soa.Self());
2199 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002200 return JNI_ERR;
2201 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002202 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002203 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 }
2205
2206 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002207 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002209 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002210 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002212 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002214 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 }
2216
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002218 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002219 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2220 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002221 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002222 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002223 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002224 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2225 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002226 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002227 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002228
Brian Carlstrom85a93362014-06-25 09:30:52 -07002229 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002230 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002231 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2232 "buffer capacity greater than maximum jint: %" PRId64,
2233 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002234 return nullptr;
2235 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002236 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002237 jint capacity_arg = static_cast<jint>(capacity);
2238
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002239 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2240 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002241 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002242 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 }
2244
Elliott Hughesb465ab02011-08-24 11:21:21 -07002245 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002246 return reinterpret_cast<void*>(env->GetLongField(
2247 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 }
2249
Elliott Hughesb465ab02011-08-24 11:21:21 -07002250 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002251 return static_cast<jlong>(env->GetIntField(
2252 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 }
2254
Elliott Hughesb465ab02011-08-24 11:21:21 -07002255 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002256 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002257
2258 // Do we definitely know what kind of reference this is?
2259 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2260 IndirectRefKind kind = GetIndirectRefKind(ref);
2261 switch (kind) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002262 case kLocal:
2263 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002264 case kGlobal:
2265 return JNIGlobalRefType;
2266 case kWeakGlobal:
2267 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002268 case kHandleScopeOrInvalid:
Ian Rogersc0542af2014-09-03 16:16:56 -07002269 // Assume value is in a handle scope.
2270 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002271 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002272 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2273 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002274 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002275
2276 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002277 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2278 const char* caller)
2279 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002280 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002281 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002282 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2283 return JNI_ERR;
2284 }
2285 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002286 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002287 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2288 if (!okay) {
2289 soa.Self()->ThrowOutOfMemoryError(caller);
2290 }
2291 return okay ? JNI_OK : JNI_ERR;
2292 }
2293
2294 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002295 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002296 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002297 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002298 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002299 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002300 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002301 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002302 return soa.AddLocalReference<JniT>(result);
2303 }
2304
Ian Rogers2d10b202014-05-12 19:15:18 -07002305 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2306 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2307 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002309 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002310 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002311 soa.Vm()->JniAbortF(fn_name,
2312 "attempt to %s %s primitive array elements with an object of type %s",
2313 operation,
2314 PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2315 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002316 return nullptr;
2317 }
2318 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2319 return array;
2320 }
2321
2322 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2323 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2324 CHECK_NON_NULL_ARGUMENT(java_array);
2325 ScopedObjectAccess soa(env);
2326 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2327 "GetArrayElements",
2328 "get");
2329 if (UNLIKELY(array == nullptr)) {
2330 return nullptr;
2331 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002332 // Only make a copy if necessary.
2333 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2334 if (is_copy != nullptr) {
2335 *is_copy = JNI_TRUE;
2336 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002337 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002338 size_t size = array->GetLength() * component_size;
2339 void* data = new uint64_t[RoundUp(size, 8) / 8];
2340 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002341 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002342 } else {
2343 if (is_copy != nullptr) {
2344 *is_copy = JNI_FALSE;
2345 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002346 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002347 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002348 }
2349
Ian Rogers2d10b202014-05-12 19:15:18 -07002350 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002351 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002352 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002353 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002354 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2355 "ReleaseArrayElements",
2356 "release");
2357 if (array == nullptr) {
2358 return;
2359 }
2360 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2361 }
2362
2363 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2364 size_t component_size, void* elements, jint mode)
2365 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002366 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002367 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002368 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002369 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002370 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2371 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002372 if (is_copy) {
2373 // Sanity check: If elements is not the same as the java array's data, it better not be a
2374 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2375 // copies we make?
2376 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002377 soa.Vm()->JniAbortF("ReleaseArrayElements",
2378 "invalid element pointer %p, array elements are %p",
2379 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002380 return;
2381 }
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002382 if (mode != JNI_ABORT) {
2383 memcpy(array_data, elements, bytes);
2384 } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2385 // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2386 LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2387 soa.Self()->DumpJavaStack(LOG(WARNING));
2388 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002389 }
2390 if (mode != JNI_COMMIT) {
2391 if (is_copy) {
2392 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002393 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002394 // Non copy to a movable object must means that we had disabled the moving GC.
2395 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002396 }
2397 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002398 }
2399
Ian Rogers2d10b202014-05-12 19:15:18 -07002400 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2401 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2402 jsize start, jsize length, ElementT* buf) {
2403 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2404 ScopedObjectAccess soa(env);
2405 ArtArrayT* array =
2406 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2407 "GetPrimitiveArrayRegion",
2408 "get region of");
2409 if (array != nullptr) {
2410 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2411 ThrowAIOOBE(soa, array, start, length, "src");
2412 } else {
2413 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2414 ElementT* data = array->GetData();
2415 memcpy(buf, data + start, length * sizeof(ElementT));
2416 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002417 }
2418 }
2419
Ian Rogers2d10b202014-05-12 19:15:18 -07002420 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2421 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2422 jsize start, jsize length, const ElementT* buf) {
2423 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2424 ScopedObjectAccess soa(env);
2425 ArtArrayT* array =
2426 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2427 "SetPrimitiveArrayRegion",
2428 "set region of");
2429 if (array != nullptr) {
2430 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2431 ThrowAIOOBE(soa, array, start, length, "dst");
2432 } else {
2433 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2434 ElementT* data = array->GetData();
2435 memcpy(data + start, buf, length * sizeof(ElementT));
2436 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002437 }
2438 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002439};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002440
Elliott Hughes88c5c352012-03-15 18:49:48 -07002441const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002442 nullptr, // reserved0.
2443 nullptr, // reserved1.
2444 nullptr, // reserved2.
2445 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002446 JNI::GetVersion,
2447 JNI::DefineClass,
2448 JNI::FindClass,
2449 JNI::FromReflectedMethod,
2450 JNI::FromReflectedField,
2451 JNI::ToReflectedMethod,
2452 JNI::GetSuperclass,
2453 JNI::IsAssignableFrom,
2454 JNI::ToReflectedField,
2455 JNI::Throw,
2456 JNI::ThrowNew,
2457 JNI::ExceptionOccurred,
2458 JNI::ExceptionDescribe,
2459 JNI::ExceptionClear,
2460 JNI::FatalError,
2461 JNI::PushLocalFrame,
2462 JNI::PopLocalFrame,
2463 JNI::NewGlobalRef,
2464 JNI::DeleteGlobalRef,
2465 JNI::DeleteLocalRef,
2466 JNI::IsSameObject,
2467 JNI::NewLocalRef,
2468 JNI::EnsureLocalCapacity,
2469 JNI::AllocObject,
2470 JNI::NewObject,
2471 JNI::NewObjectV,
2472 JNI::NewObjectA,
2473 JNI::GetObjectClass,
2474 JNI::IsInstanceOf,
2475 JNI::GetMethodID,
2476 JNI::CallObjectMethod,
2477 JNI::CallObjectMethodV,
2478 JNI::CallObjectMethodA,
2479 JNI::CallBooleanMethod,
2480 JNI::CallBooleanMethodV,
2481 JNI::CallBooleanMethodA,
2482 JNI::CallByteMethod,
2483 JNI::CallByteMethodV,
2484 JNI::CallByteMethodA,
2485 JNI::CallCharMethod,
2486 JNI::CallCharMethodV,
2487 JNI::CallCharMethodA,
2488 JNI::CallShortMethod,
2489 JNI::CallShortMethodV,
2490 JNI::CallShortMethodA,
2491 JNI::CallIntMethod,
2492 JNI::CallIntMethodV,
2493 JNI::CallIntMethodA,
2494 JNI::CallLongMethod,
2495 JNI::CallLongMethodV,
2496 JNI::CallLongMethodA,
2497 JNI::CallFloatMethod,
2498 JNI::CallFloatMethodV,
2499 JNI::CallFloatMethodA,
2500 JNI::CallDoubleMethod,
2501 JNI::CallDoubleMethodV,
2502 JNI::CallDoubleMethodA,
2503 JNI::CallVoidMethod,
2504 JNI::CallVoidMethodV,
2505 JNI::CallVoidMethodA,
2506 JNI::CallNonvirtualObjectMethod,
2507 JNI::CallNonvirtualObjectMethodV,
2508 JNI::CallNonvirtualObjectMethodA,
2509 JNI::CallNonvirtualBooleanMethod,
2510 JNI::CallNonvirtualBooleanMethodV,
2511 JNI::CallNonvirtualBooleanMethodA,
2512 JNI::CallNonvirtualByteMethod,
2513 JNI::CallNonvirtualByteMethodV,
2514 JNI::CallNonvirtualByteMethodA,
2515 JNI::CallNonvirtualCharMethod,
2516 JNI::CallNonvirtualCharMethodV,
2517 JNI::CallNonvirtualCharMethodA,
2518 JNI::CallNonvirtualShortMethod,
2519 JNI::CallNonvirtualShortMethodV,
2520 JNI::CallNonvirtualShortMethodA,
2521 JNI::CallNonvirtualIntMethod,
2522 JNI::CallNonvirtualIntMethodV,
2523 JNI::CallNonvirtualIntMethodA,
2524 JNI::CallNonvirtualLongMethod,
2525 JNI::CallNonvirtualLongMethodV,
2526 JNI::CallNonvirtualLongMethodA,
2527 JNI::CallNonvirtualFloatMethod,
2528 JNI::CallNonvirtualFloatMethodV,
2529 JNI::CallNonvirtualFloatMethodA,
2530 JNI::CallNonvirtualDoubleMethod,
2531 JNI::CallNonvirtualDoubleMethodV,
2532 JNI::CallNonvirtualDoubleMethodA,
2533 JNI::CallNonvirtualVoidMethod,
2534 JNI::CallNonvirtualVoidMethodV,
2535 JNI::CallNonvirtualVoidMethodA,
2536 JNI::GetFieldID,
2537 JNI::GetObjectField,
2538 JNI::GetBooleanField,
2539 JNI::GetByteField,
2540 JNI::GetCharField,
2541 JNI::GetShortField,
2542 JNI::GetIntField,
2543 JNI::GetLongField,
2544 JNI::GetFloatField,
2545 JNI::GetDoubleField,
2546 JNI::SetObjectField,
2547 JNI::SetBooleanField,
2548 JNI::SetByteField,
2549 JNI::SetCharField,
2550 JNI::SetShortField,
2551 JNI::SetIntField,
2552 JNI::SetLongField,
2553 JNI::SetFloatField,
2554 JNI::SetDoubleField,
2555 JNI::GetStaticMethodID,
2556 JNI::CallStaticObjectMethod,
2557 JNI::CallStaticObjectMethodV,
2558 JNI::CallStaticObjectMethodA,
2559 JNI::CallStaticBooleanMethod,
2560 JNI::CallStaticBooleanMethodV,
2561 JNI::CallStaticBooleanMethodA,
2562 JNI::CallStaticByteMethod,
2563 JNI::CallStaticByteMethodV,
2564 JNI::CallStaticByteMethodA,
2565 JNI::CallStaticCharMethod,
2566 JNI::CallStaticCharMethodV,
2567 JNI::CallStaticCharMethodA,
2568 JNI::CallStaticShortMethod,
2569 JNI::CallStaticShortMethodV,
2570 JNI::CallStaticShortMethodA,
2571 JNI::CallStaticIntMethod,
2572 JNI::CallStaticIntMethodV,
2573 JNI::CallStaticIntMethodA,
2574 JNI::CallStaticLongMethod,
2575 JNI::CallStaticLongMethodV,
2576 JNI::CallStaticLongMethodA,
2577 JNI::CallStaticFloatMethod,
2578 JNI::CallStaticFloatMethodV,
2579 JNI::CallStaticFloatMethodA,
2580 JNI::CallStaticDoubleMethod,
2581 JNI::CallStaticDoubleMethodV,
2582 JNI::CallStaticDoubleMethodA,
2583 JNI::CallStaticVoidMethod,
2584 JNI::CallStaticVoidMethodV,
2585 JNI::CallStaticVoidMethodA,
2586 JNI::GetStaticFieldID,
2587 JNI::GetStaticObjectField,
2588 JNI::GetStaticBooleanField,
2589 JNI::GetStaticByteField,
2590 JNI::GetStaticCharField,
2591 JNI::GetStaticShortField,
2592 JNI::GetStaticIntField,
2593 JNI::GetStaticLongField,
2594 JNI::GetStaticFloatField,
2595 JNI::GetStaticDoubleField,
2596 JNI::SetStaticObjectField,
2597 JNI::SetStaticBooleanField,
2598 JNI::SetStaticByteField,
2599 JNI::SetStaticCharField,
2600 JNI::SetStaticShortField,
2601 JNI::SetStaticIntField,
2602 JNI::SetStaticLongField,
2603 JNI::SetStaticFloatField,
2604 JNI::SetStaticDoubleField,
2605 JNI::NewString,
2606 JNI::GetStringLength,
2607 JNI::GetStringChars,
2608 JNI::ReleaseStringChars,
2609 JNI::NewStringUTF,
2610 JNI::GetStringUTFLength,
2611 JNI::GetStringUTFChars,
2612 JNI::ReleaseStringUTFChars,
2613 JNI::GetArrayLength,
2614 JNI::NewObjectArray,
2615 JNI::GetObjectArrayElement,
2616 JNI::SetObjectArrayElement,
2617 JNI::NewBooleanArray,
2618 JNI::NewByteArray,
2619 JNI::NewCharArray,
2620 JNI::NewShortArray,
2621 JNI::NewIntArray,
2622 JNI::NewLongArray,
2623 JNI::NewFloatArray,
2624 JNI::NewDoubleArray,
2625 JNI::GetBooleanArrayElements,
2626 JNI::GetByteArrayElements,
2627 JNI::GetCharArrayElements,
2628 JNI::GetShortArrayElements,
2629 JNI::GetIntArrayElements,
2630 JNI::GetLongArrayElements,
2631 JNI::GetFloatArrayElements,
2632 JNI::GetDoubleArrayElements,
2633 JNI::ReleaseBooleanArrayElements,
2634 JNI::ReleaseByteArrayElements,
2635 JNI::ReleaseCharArrayElements,
2636 JNI::ReleaseShortArrayElements,
2637 JNI::ReleaseIntArrayElements,
2638 JNI::ReleaseLongArrayElements,
2639 JNI::ReleaseFloatArrayElements,
2640 JNI::ReleaseDoubleArrayElements,
2641 JNI::GetBooleanArrayRegion,
2642 JNI::GetByteArrayRegion,
2643 JNI::GetCharArrayRegion,
2644 JNI::GetShortArrayRegion,
2645 JNI::GetIntArrayRegion,
2646 JNI::GetLongArrayRegion,
2647 JNI::GetFloatArrayRegion,
2648 JNI::GetDoubleArrayRegion,
2649 JNI::SetBooleanArrayRegion,
2650 JNI::SetByteArrayRegion,
2651 JNI::SetCharArrayRegion,
2652 JNI::SetShortArrayRegion,
2653 JNI::SetIntArrayRegion,
2654 JNI::SetLongArrayRegion,
2655 JNI::SetFloatArrayRegion,
2656 JNI::SetDoubleArrayRegion,
2657 JNI::RegisterNatives,
2658 JNI::UnregisterNatives,
2659 JNI::MonitorEnter,
2660 JNI::MonitorExit,
2661 JNI::GetJavaVM,
2662 JNI::GetStringRegion,
2663 JNI::GetStringUTFRegion,
2664 JNI::GetPrimitiveArrayCritical,
2665 JNI::ReleasePrimitiveArrayCritical,
2666 JNI::GetStringCritical,
2667 JNI::ReleaseStringCritical,
2668 JNI::NewWeakGlobalRef,
2669 JNI::DeleteWeakGlobalRef,
2670 JNI::ExceptionCheck,
2671 JNI::NewDirectByteBuffer,
2672 JNI::GetDirectBufferAddress,
2673 JNI::GetDirectBufferCapacity,
2674 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002675};
2676
Ian Rogers68d8b422014-07-17 11:09:10 -07002677const JNINativeInterface* GetJniNativeInterface() {
2678 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002679}
2680
Elliott Hughesc8fece32013-01-02 11:27:23 -08002681void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002682 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002683 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002684 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002685 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2686 }
2687 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2688}
2689
Ian Rogersdf20fe02011-07-20 20:34:16 -07002690} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002691
2692std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2693 switch (rhs) {
2694 case JNIInvalidRefType:
2695 os << "JNIInvalidRefType";
2696 return os;
2697 case JNILocalRefType:
2698 os << "JNILocalRefType";
2699 return os;
2700 case JNIGlobalRefType:
2701 os << "JNIGlobalRefType";
2702 return os;
2703 case JNIWeakGlobalRefType:
2704 os << "JNIWeakGlobalRefType";
2705 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002706 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002707 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002708 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002709 }
2710}