blob: f435467e95bc4ad9d5622962acadfc096eeb73b6 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070026#include "art_field-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080027#include "atomic.h"
Mathieu Chartierbad02672014-08-25 13:08:22 -070028#include "base/allocator.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080029#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080030#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080031#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080032#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070033#include "dex_file-inl.h"
Mathieu Chartierd0004802014-10-15 16:59:47 -070034#include "fault_handler.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070035#include "gc_root.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036#include "gc/accounting/card_table-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070037#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070038#include "interpreter/interpreter.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070039#include "jni_env_ext.h"
40#include "java_vm_ext.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070041#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/class-inl.h"
43#include "mirror/class_loader.h"
Hiroshi Yamauchi02d2f292015-04-03 13:35:16 -070044#include "mirror/field-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070045#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070048#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "mirror/throwable.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080050#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070051#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070052#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070053#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070055#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070056#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070058#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070059
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070060namespace art {
61
Mathieu Chartier24555ad2014-10-06 13:41:33 -070062// Consider turning this on when there is errors which could be related to JNI array copies such as
63// things not rendering correctly. E.g. b/16858794
64static constexpr bool kWarnJniAbort = false;
65
Elliott Hughes6b436852011-08-12 10:16:44 -070066// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
67// separated with slashes but aren't wrapped with "L;" like regular descriptors
68// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
69// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
70// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070071static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070072 std::string result;
73 // Add the missing "L;" if necessary.
74 if (name[0] == '[') {
75 result = name;
76 } else {
77 result += 'L';
78 result += name;
79 result += ';';
80 }
81 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070082 if (result.find('.') != std::string::npos) {
83 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
84 << "\"" << name << "\"";
85 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -070086 }
87 return result;
88}
89
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080090static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -070092 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -070093 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000094 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Ian Rogers62d6c772013-02-27 08:32:07 -080095 "no %s method \"%s.%s%s\"",
Ian Rogers1ff3c982014-08-12 02:30:58 -070096 kind, c->GetDescriptor(&temp), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -070097}
98
Sebastien Hertzfa65e842014-07-03 09:39:53 +020099static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa, mirror::Class* c,
100 const char* kind, jint idx, bool return_errors)
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
102 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method in "
103 << PrettyDescriptor(c) << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
104 << ": " << kind << " is null at index " << idx;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000105 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200106 "%s is null at index %d", kind, idx);
107}
108
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800109static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
111 if (LIKELY(klass->IsInitialized())) {
112 return klass;
113 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700114 StackHandleScope<1> hs(self);
115 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700116 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800117 return nullptr;
118 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800120}
121
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
123 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800125 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800126 if (c == nullptr) {
127 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700128 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800129 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700130 if (is_static) {
131 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700132 } else if (c->IsInterface()) {
133 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700134 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700135 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800136 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700137 // No virtual method matching the signature. Search declared
138 // private methods and constructors.
139 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700140 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700141 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800142 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800144 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700145 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700147}
148
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800149static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800151 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700152 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
153 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700154 return soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700155 }
Brian Carlstromce888532013-10-10 00:32:58 -0700156 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800157 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700158 return method->GetDeclaringClass()->GetClassLoader();
159 }
160 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800161 mirror::ClassLoader* class_loader =
162 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
163 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700164 return class_loader;
165 }
166 // See if the override ClassLoader is set for gtests.
Ian Rogers68d8b422014-07-17 11:09:10 -0700167 class_loader = soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800168 if (class_loader != nullptr) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700169 // If so, CommonCompilerTest should have marked the runtime as a compiler not compiling an
170 // image.
171 CHECK(Runtime::Current()->IsAotCompiler());
Andreas Gampe4585f872015-03-27 23:45:15 -0700172 CHECK(!Runtime::Current()->IsCompilingBootImage());
Brian Carlstromce888532013-10-10 00:32:58 -0700173 return class_loader;
174 }
175 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800176 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700177}
178
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700179static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
180 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700182 StackHandleScope<2> hs(soa.Self());
183 Handle<mirror::Class> c(
184 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
185 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800186 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700187 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700188 ArtField* field = nullptr;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800189 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700190 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
191 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700192 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800193 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700194 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700196 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800197 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199 DCHECK(soa.Self()->IsExceptionPending());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800200 StackHandleScope<1> hs2(soa.Self());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000201 Handle<mirror::Throwable> cause(hs2.NewHandle(soa.Self()->GetException()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700202 soa.Self()->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700203 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000204 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800205 "no type \"%s\" found and so no field \"%s\" "
206 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700207 c->GetDescriptor(&temp));
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000208 soa.Self()->GetException()->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800209 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700211 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700213 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700214 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700216 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700217 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800218 if (field == nullptr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000219 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800220 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700221 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800222 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700223 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700224 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700225}
226
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800227static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700230 std::string type(PrettyTypeOf(array));
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000231 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800232 "%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_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000239 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800240 "offset=%d length=%d string.length()=%d", start, length,
241 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700242}
Elliott Hughes814e4032011-08-23 12:07:56 -0700243
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700245 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 // Turn the const char* into a java.lang.String.
247 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800248 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700250 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700251
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252 // Choose an appropriate constructor and set up the arguments.
253 jvalue args[2];
254 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800255 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800257 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 signature = "(Ljava/lang/String;)V";
259 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800260 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 signature = "(Ljava/lang/Throwable;)V";
262 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700263 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
265 args[0].l = s.get();
266 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700267 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800269 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800270 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800272 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273 return JNI_ERR;
274 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700275
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800276 ScopedLocalRef<jthrowable> exception(
277 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
278 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279 return JNI_ERR;
280 }
Ian Rogersef28b142012-11-30 14:22:18 -0800281 ScopedObjectAccess soa(env);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000282 soa.Self()->SetException(soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700284}
285
Ian Rogers68d8b422014-07-17 11:09:10 -0700286static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
287 return reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughes75770752011-08-24 17:52:38 -0700288}
289
Ian Rogers2d10b202014-05-12 19:15:18 -0700290#define CHECK_NON_NULL_ARGUMENT(value) \
291 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700292
Ian Rogers2d10b202014-05-12 19:15:18 -0700293#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
294 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
295
296#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
297 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
298
299#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
300 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
301
302#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800303 if (UNLIKELY(value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700304 JavaVmExtFromEnv(env)->JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700305 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700306 }
307
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700308#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800309 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700310 JavaVmExtFromEnv(env)->JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700311 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700312 }
313
Andreas Gampe3f1dc562015-05-18 15:52:22 -0700314template <bool kNative>
315static mirror::ArtMethod* FindMethod(mirror::Class* c,
316 const StringPiece& name,
317 const StringPiece& sig)
318 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
319 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
320 mirror::ArtMethod* method = c->GetDirectMethod(i);
321 if (kNative == method->IsNative() &&
322 name == method->GetName() && method->GetSignature() == sig) {
323 return method;
324 }
325 }
326
327 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
328 mirror::ArtMethod* method = c->GetVirtualMethod(i);
329 if (kNative == method->IsNative() &&
330 name == method->GetName() && method->GetSignature() == sig) {
331 return method;
332 }
333 }
334
335 return nullptr;
336}
337
Elliott Hughescdf53122011-08-19 15:46:09 -0700338class JNI {
339 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700340 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700341 return JNI_VERSION_1_6;
342 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700343
Ian Rogers25e8b912012-09-07 11:31:36 -0700344 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700345 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800346 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700347 }
348
Elliott Hughescdf53122011-08-19 15:46:09 -0700349 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700350 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700351 Runtime* runtime = Runtime::Current();
352 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700353 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700354 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800355 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700356 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700357 StackHandleScope<1> hs(soa.Self());
358 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800359 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700360 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800361 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700362 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700363 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700364 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700365
Ian Rogers62f05122014-03-21 11:21:29 -0700366 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700367 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700369 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700370 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700371
Ian Rogers62f05122014-03-21 11:21:29 -0700372 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700373 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 ScopedObjectAccess soa(env);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700375 mirror::Object* obj_field = soa.Decode<mirror::Object*>(jlr_field);
376 if (obj_field->GetClass() != mirror::Field::StaticClass()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700377 // Not even a java.lang.reflect.Field, return null. TODO, is this check necessary?
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700378 return nullptr;
379 }
380 auto* field = static_cast<mirror::Field*>(obj_field);
381 return soa.EncodeField(field->GetArtField());
Elliott Hughescdf53122011-08-19 15:46:09 -0700382 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700383
Elliott Hughescdf53122011-08-19 15:46:09 -0700384 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700385 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700386 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800387 mirror::ArtMethod* m = soa.DecodeMethod(mid);
388 CHECK(!kMovingMethods);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700389 mirror::AbstractMethod* method;
Sebastien Hertzd3333762014-06-26 14:45:07 +0200390 if (m->IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700391 method = mirror::Constructor::CreateFromArtMethod(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200392 } else {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700393 method = mirror::Method::CreateFromArtMethod(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200394 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700395 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700396 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700397
Elliott Hughescdf53122011-08-19 15:46:09 -0700398 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700399 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 ScopedObjectAccess soa(env);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700401 ArtField* f = soa.DecodeField(fid);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700402 return soa.AddLocalReference<jobject>(mirror::Field::CreateFromArtField(soa.Self(), f, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700403 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700404
Elliott Hughes37f7a402011-08-22 18:56:01 -0700405 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700406 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700407 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800408 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700409 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700410 }
411
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700412 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700413 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700414 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800415 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700416 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700417 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700418
Narayan Kamath1268b742014-07-11 19:15:11 +0100419 // Note: java_class1 should be safely castable to java_class2, and
420 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700421 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700422 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
423 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700424 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800425 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
426 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100427 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700428 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700429
Elliott Hughese84278b2012-03-22 10:06:53 -0700430 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700431 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800432 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700433 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700434 return JNI_TRUE;
435 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700436 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800437 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
438 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700439 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700440 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700441 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700442
Elliott Hughes37f7a402011-08-22 18:56:01 -0700443 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700444 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800445 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
446 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700447 return JNI_ERR;
448 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000449 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700450 return JNI_OK;
451 }
452
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700453 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700454 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800455 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700456 }
457
458 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700459 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700460 }
461
462 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700463 ScopedObjectAccess soa(env);
464 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700465 }
466
467 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700468 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700469
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700470 // If we have no exception to describe, pass through.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000471 if (!soa.Self()->GetException()) {
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700472 return;
473 }
474
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000475 StackHandleScope<1> hs(soa.Self());
476 Handle<mirror::Throwable> old_exception(
477 hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()));
478 soa.Self()->ClearException();
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800479 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700480 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700481 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
482 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800483 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700484 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700485 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700486 } else {
487 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800488 if (soa.Self()->IsExceptionPending()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000489 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700490 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800491 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700492 }
493 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000494 soa.Self()->SetException(old_exception.Get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700495 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700496
Elliott Hughescdf53122011-08-19 15:46:09 -0700497 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700498 ScopedObjectAccess soa(env);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000499 mirror::Object* exception = soa.Self()->GetException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700500 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700501 }
502
Ian Rogers25e8b912012-09-07 11:31:36 -0700503 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700504 LOG(FATAL) << "JNI FatalError called: " << msg;
505 }
506
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700507 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700508 // TODO: SOA may not be necessary but I do it to please lock annotations.
509 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700510 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700511 return JNI_ERR;
512 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700513 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700514 return JNI_OK;
515 }
516
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700517 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700518 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800519 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700520 soa.Env()->PopFrame();
521 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700522 }
523
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700524 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700525 // TODO: SOA may not be necessary but I do it to please lock annotations.
526 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700527 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700528 }
529
Elliott Hughescdf53122011-08-19 15:46:09 -0700530 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700531 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800532 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700533 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700534 }
535
536 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700537 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
538 Thread* self = down_cast<JNIEnvExt*>(env)->self;
539 vm->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700540 }
541
542 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700543 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700544 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
545 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700546 }
547
548 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700549 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
550 Thread* self = down_cast<JNIEnvExt*>(env)->self;
551 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700552 }
553
554 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700555 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800556 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800557 // Check for null after decoding the object to handle cleared weak globals.
558 if (decoded_obj == nullptr) {
559 return nullptr;
560 }
561 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700562 }
563
Stephen Hines95c51b32014-11-26 01:24:13 -0800564 static void DeleteLocalRef(JNIEnv* env, jobject obj)
565 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800566 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 return;
568 }
Ian Rogersef28b142012-11-30 14:22:18 -0800569 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700570
Ian Rogersef28b142012-11-30 14:22:18 -0800571 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700572 if (!locals.Remove(cookie, obj)) {
573 // Attempting to delete a local reference that is not in the
574 // topmost local reference frame is a no-op. DeleteLocalRef returns
575 // void and doesn't throw any exceptions, but we should probably
576 // complain about it so the user will notice that things aren't
577 // going quite the way they expect.
578 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
579 << "failed to find entry";
580 }
581 }
582
583 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700584 if (obj1 == obj2) {
585 return JNI_TRUE;
586 } else {
587 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800588 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
589 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700590 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700591 }
592
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700593 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700594 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700595 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800596 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800597 if (c == nullptr) {
598 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700599 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800600 if (c->IsStringClass()) {
601 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
602 mirror::SetStringCountVisitor visitor(0);
603 return soa.AddLocalReference<jobject>(mirror::String::Alloc<true>(soa.Self(), 0,
604 allocator_type, visitor));
605 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700606 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700607 }
608
Ian Rogersbc939662013-08-15 10:26:54 -0700609 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700610 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700611 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700612 CHECK_NON_NULL_ARGUMENT(java_class);
613 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700614 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700615 va_end(args);
616 return result;
617 }
618
Elliott Hughes72025e52011-08-23 17:50:30 -0700619 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700620 CHECK_NON_NULL_ARGUMENT(java_class);
621 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800623 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800624 if (c == nullptr) {
625 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700626 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800627 if (c->IsStringClass()) {
628 // Replace calls to String.<init> with equivalent StringFactory call.
629 jmethodID sf_mid = WellKnownClasses::StringInitToStringFactoryMethodID(mid);
630 return CallStaticObjectMethodV(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
631 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800632 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800633 if (result == nullptr) {
634 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700635 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700637 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800638 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800639 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700640 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800641 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700642 }
643
Elliott Hughes72025e52011-08-23 17:50:30 -0700644 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700645 CHECK_NON_NULL_ARGUMENT(java_class);
646 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800648 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800649 if (c == nullptr) {
650 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700651 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800652 if (c->IsStringClass()) {
653 // Replace calls to String.<init> with equivalent StringFactory call.
654 jmethodID sf_mid = WellKnownClasses::StringInitToStringFactoryMethodID(mid);
655 return CallStaticObjectMethodA(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
656 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800657 mirror::Object* result = c->AllocObject(soa.Self());
658 if (result == nullptr) {
659 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700660 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700662 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800663 if (soa.Self()->IsExceptionPending()) {
664 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700665 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800666 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 }
668
Ian Rogersbc939662013-08-15 10:26:54 -0700669 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700670 CHECK_NON_NULL_ARGUMENT(java_class);
671 CHECK_NON_NULL_ARGUMENT(name);
672 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700674 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700675 }
676
Ian Rogersbc939662013-08-15 10:26:54 -0700677 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
678 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700679 CHECK_NON_NULL_ARGUMENT(java_class);
680 CHECK_NON_NULL_ARGUMENT(name);
681 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700682 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700683 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 }
685
Elliott Hughes72025e52011-08-23 17:50:30 -0700686 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700687 va_list ap;
688 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700689 CHECK_NON_NULL_ARGUMENT(obj);
690 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700691 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700692 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700693 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 }
696
Elliott Hughes72025e52011-08-23 17:50:30 -0700697 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700698 CHECK_NON_NULL_ARGUMENT(obj);
699 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 ScopedObjectAccess soa(env);
701 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
702 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 }
704
Elliott Hughes72025e52011-08-23 17:50:30 -0700705 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700706 CHECK_NON_NULL_ARGUMENT(obj);
707 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700709 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 }
712
Elliott Hughes72025e52011-08-23 17:50:30 -0700713 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700714 va_list ap;
715 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700716 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
717 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700718 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700719 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700720 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700721 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 }
723
Elliott Hughes72025e52011-08-23 17:50:30 -0700724 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700725 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
726 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700727 ScopedObjectAccess soa(env);
728 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700729 }
730
Elliott Hughes72025e52011-08-23 17:50:30 -0700731 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700732 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
733 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700735 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 }
737
Elliott Hughes72025e52011-08-23 17:50:30 -0700738 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700739 va_list ap;
740 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700741 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
742 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700743 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700744 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700745 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700746 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700747 }
748
Elliott Hughes72025e52011-08-23 17:50:30 -0700749 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700750 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
751 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700752 ScopedObjectAccess soa(env);
753 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 }
755
Elliott Hughes72025e52011-08-23 17:50:30 -0700756 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700757 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
758 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700759 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700760 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 }
762
Elliott Hughes72025e52011-08-23 17:50:30 -0700763 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700764 va_list ap;
765 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700766 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
767 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700768 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700769 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700770 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700771 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 }
773
Elliott Hughes72025e52011-08-23 17:50:30 -0700774 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700775 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
776 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700777 ScopedObjectAccess soa(env);
778 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 }
780
Elliott Hughes72025e52011-08-23 17:50:30 -0700781 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700782 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
783 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700784 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700785 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700786 }
787
Elliott Hughes72025e52011-08-23 17:50:30 -0700788 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700789 va_list ap;
790 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700791 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
792 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700793 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700794 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700795 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700796 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 }
798
Elliott Hughes72025e52011-08-23 17:50:30 -0700799 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700800 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
801 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700802 ScopedObjectAccess soa(env);
803 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 }
805
Elliott Hughes72025e52011-08-23 17:50:30 -0700806 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700807 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
808 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700809 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700810 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 }
812
Elliott Hughes72025e52011-08-23 17:50:30 -0700813 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700814 va_list ap;
815 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700816 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
817 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700818 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700819 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700820 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700821 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 }
823
Elliott Hughes72025e52011-08-23 17:50:30 -0700824 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700825 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
826 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700827 ScopedObjectAccess soa(env);
828 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 }
830
Elliott Hughes72025e52011-08-23 17:50:30 -0700831 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700832 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
833 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700834 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700835 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 }
837
Elliott Hughes72025e52011-08-23 17:50:30 -0700838 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700839 va_list ap;
840 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700841 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
842 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700843 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700844 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700845 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700846 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 }
848
Elliott Hughes72025e52011-08-23 17:50:30 -0700849 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700850 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
851 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700852 ScopedObjectAccess soa(env);
853 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 }
855
Elliott Hughes72025e52011-08-23 17:50:30 -0700856 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700857 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
858 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700859 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700860 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 }
862
Elliott Hughes72025e52011-08-23 17:50:30 -0700863 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700864 va_list ap;
865 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700866 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
867 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700868 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700869 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700870 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700871 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 }
873
Elliott Hughes72025e52011-08-23 17:50:30 -0700874 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list 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);
878 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 }
880
Elliott Hughes72025e52011-08-23 17:50:30 -0700881 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700882 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
883 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700884 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700885 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700886 }
887
Elliott Hughes72025e52011-08-23 17:50:30 -0700888 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700889 va_list ap;
890 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700891 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
892 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700893 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700894 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700895 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700896 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 }
898
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700900 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
901 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700902 ScopedObjectAccess soa(env);
903 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 }
905
Elliott Hughes72025e52011-08-23 17:50:30 -0700906 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700907 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
908 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700909 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700910 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700911 }
912
Elliott Hughes72025e52011-08-23 17:50:30 -0700913 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 va_list ap;
915 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700916 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
917 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700918 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -0700919 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 }
922
Elliott Hughes72025e52011-08-23 17:50:30 -0700923 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700924 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
925 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700926 ScopedObjectAccess soa(env);
927 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 }
929
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700931 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
932 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700934 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 }
936
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700937 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700940 CHECK_NON_NULL_ARGUMENT(obj);
941 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700942 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700943 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
944 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 va_end(ap);
946 return local_result;
947 }
948
Ian Rogersbc939662013-08-15 10:26:54 -0700949 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
950 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700951 CHECK_NON_NULL_ARGUMENT(obj);
952 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700953 ScopedObjectAccess soa(env);
954 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
955 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 }
957
Ian Rogersbc939662013-08-15 10:26:54 -0700958 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
959 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700960 CHECK_NON_NULL_ARGUMENT(obj);
961 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700962 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700963 JValue result(InvokeWithJValues(soa, obj, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700964 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Ian Rogersbc939662013-08-15 10:26:54 -0700967 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
968 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700971 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
972 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700973 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700976 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Ian Rogersbc939662013-08-15 10:26:54 -0700979 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
980 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700981 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
982 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700983 ScopedObjectAccess soa(env);
984 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 }
986
Ian Rogersbc939662013-08-15 10:26:54 -0700987 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
988 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700989 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
990 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700991 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700992 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 }
994
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700995 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700998 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
999 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001000 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001001 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001003 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 }
1005
Ian Rogersbc939662013-08-15 10:26:54 -07001006 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1007 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001008 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1009 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001010 ScopedObjectAccess soa(env);
1011 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Ian Rogersbc939662013-08-15 10:26:54 -07001014 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1015 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001016 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1017 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001018 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001019 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001022 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001025 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1026 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001027 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001028 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001030 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Ian Rogersbc939662013-08-15 10:26:54 -07001033 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1034 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001035 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1036 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 ScopedObjectAccess soa(env);
1038 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Ian Rogersbc939662013-08-15 10:26:54 -07001041 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1042 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001043 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1044 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001045 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001046 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 }
1048
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001049 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001052 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1053 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001054 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001055 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001057 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Ian Rogersbc939662013-08-15 10:26:54 -07001060 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1061 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001062 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1063 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001064 ScopedObjectAccess soa(env);
1065 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Ian Rogersbc939662013-08-15 10:26:54 -07001068 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1069 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001070 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1071 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001072 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001073 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001076 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001079 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1080 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001081 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001084 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Ian Rogersbc939662013-08-15 10:26:54 -07001087 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1088 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001089 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1090 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001091 ScopedObjectAccess soa(env);
1092 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Ian Rogersbc939662013-08-15 10:26:54 -07001095 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1096 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001097 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1098 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001099 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001100 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001103 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001106 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1107 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001108 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001109 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001111 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Ian Rogersbc939662013-08-15 10:26:54 -07001114 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1115 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001116 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1117 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118 ScopedObjectAccess soa(env);
1119 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Ian Rogersbc939662013-08-15 10:26:54 -07001122 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1123 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001124 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1125 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001126 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001127 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001130 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001133 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1134 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001135 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001136 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001138 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Ian Rogersbc939662013-08-15 10:26:54 -07001141 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1142 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001143 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1144 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001145 ScopedObjectAccess soa(env);
1146 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
Ian Rogersbc939662013-08-15 10:26:54 -07001149 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1150 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001151 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1152 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001153 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001154 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001157 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001160 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1161 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001162 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001165 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
Ian Rogersbc939662013-08-15 10:26:54 -07001168 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1169 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001170 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1171 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001172 ScopedObjectAccess soa(env);
1173 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 }
1175
Ian Rogersbc939662013-08-15 10:26:54 -07001176 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1177 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001178 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1179 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001180 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001181 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001184 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001187 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1188 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001189 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 va_end(ap);
1192 }
1193
Brian Carlstromea46f952013-07-30 01:26:50 -07001194 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1195 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001196 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1197 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 ScopedObjectAccess soa(env);
1199 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
Ian Rogersbc939662013-08-15 10:26:54 -07001202 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1203 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001204 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1205 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001206 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001207 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
Ian Rogersbc939662013-08-15 10:26:54 -07001210 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001211 CHECK_NON_NULL_ARGUMENT(java_class);
1212 CHECK_NON_NULL_ARGUMENT(name);
1213 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001214 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001215 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001217
Ian Rogersbc939662013-08-15 10:26:54 -07001218 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1219 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001220 CHECK_NON_NULL_ARGUMENT(java_class);
1221 CHECK_NON_NULL_ARGUMENT(name);
1222 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001223 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001224 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001226
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001227 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001228 CHECK_NON_NULL_ARGUMENT(obj);
1229 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001230 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001231 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001232 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001233 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001235
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001236 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001237 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 ScopedObjectAccess soa(env);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001239 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001240 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001243 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001244 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1245 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001247 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1248 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001249 ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001250 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 }
1252
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001253 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001254 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001255 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001256 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001257 ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001258 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 }
1260
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001261#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001262 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1263 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001264 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001265 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001266 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001267 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001268
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001269#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001270 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001271 ScopedObjectAccess soa(env); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001272 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001273 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001274
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001275#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001276 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1277 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001278 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001279 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001280 ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001281 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001282
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001283#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001284 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001285 ScopedObjectAccess soa(env); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001286 ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001287 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001288
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001289 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001290 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001293 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001294 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001297 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001298 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001301 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001302 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001305 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001306 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001309 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001310 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 }
1312
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001313 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001314 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001317 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001318 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001321 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001322 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001325 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001326 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001329 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001330 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001333 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001334 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001337 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001338 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001341 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001342 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001343 }
1344
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001345 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001346 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001347 }
1348
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001349 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001350 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001351 }
1352
1353 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001354 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001355 }
1356
1357 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001358 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001359 }
1360
1361 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001362 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001363 }
1364
1365 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001366 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001367 }
1368
1369 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001370 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001371 }
1372
1373 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001374 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001375 }
1376
1377 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001378 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001379 }
1380
1381 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001382 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001383 }
1384
1385 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001386 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001387 }
1388
1389 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001390 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001391 }
1392
1393 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001394 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001395 }
1396
1397 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001398 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399 }
1400
1401 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001402 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001403 }
1404
1405 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001406 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407 }
1408
1409 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001410 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001411 }
1412
1413 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001414 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
1416
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001417 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001419 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001420 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001421 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001422 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001423 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 va_end(ap);
1425 return local_result;
1426 }
1427
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001428 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001429 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001430 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001431 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001432 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001435 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001436 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001437 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001438 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001442 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001444 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001445 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001446 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001447 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001449 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001452 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001453 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001454 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001455 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001458 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001459 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001460 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001461 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001464 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001466 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001467 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001468 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001469 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001471 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001474 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001475 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001476 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001477 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001480 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001481 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001482 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001483 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001486 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001488 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001489 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001490 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001491 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001493 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001496 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001497 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001498 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001499 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001502 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001503 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001504 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001505 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001508 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001510 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001511 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001512 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001513 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001515 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001518 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001519 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001520 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001521 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001524 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001525 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001526 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001527 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
1529
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001530 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001531 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001532 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001533 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001534 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001535 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001537 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001540 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001541 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001542 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001543 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001546 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001547 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001548 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001549 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 }
1551
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001552 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001554 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001555 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001556 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001557 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001559 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 }
1561
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001562 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001563 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001564 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001565 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001568 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001569 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001570 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001571 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001572 }
1573
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001574 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001576 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001577 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001578 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001579 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001581 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001584 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001585 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001586 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001587 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 }
1589
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001590 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001591 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001592 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001593 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001596 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001598 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001599 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001600 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001601 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001603 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 }
1605
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001606 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001607 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001608 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001609 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001612 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001613 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001614 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001615 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001618 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001620 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001621 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001622 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001623 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 va_end(ap);
1625 }
1626
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001627 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001628 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001629 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001630 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 }
1632
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001633 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001634 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001635 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001636 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 }
1638
Elliott Hughes814e4032011-08-23 12:07:56 -07001639 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001640 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001641 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001642 return nullptr;
1643 }
1644 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001645 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001646 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001647 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001648 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001649 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001650 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
1652
1653 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001654 if (utf == nullptr) {
1655 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001657 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001658 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001659 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
Elliott Hughes814e4032011-08-23 12:07:56 -07001662 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001663 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001664 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001665 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001666 }
1667
1668 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001669 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001671 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001672 }
1673
Ian Rogersbc939662013-08-15 10:26:54 -07001674 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1675 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001676 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001677 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001678 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001679 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001680 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001681 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001682 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Jeff Hao848f70a2014-01-15 13:49:50 -08001683 const jchar* chars = s->GetValue();
Elliott Hughesb465ab02011-08-24 11:21:21 -07001684 memcpy(buf, chars + start, length * sizeof(jchar));
1685 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001686 }
1687
Ian Rogersbc939662013-08-15 10:26:54 -07001688 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1689 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001690 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001691 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001692 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001693 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001694 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001695 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001696 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Jeff Hao848f70a2014-01-15 13:49:50 -08001697 const jchar* chars = s->GetValue();
Elliott Hughesb465ab02011-08-24 11:21:21 -07001698 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1699 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001700 }
1701
Elliott Hughes75770752011-08-24 17:52:38 -07001702 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001703 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001704 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001705 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001706 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Hao848f70a2014-01-15 13:49:50 -08001707 if (heap->IsMovableObject(s)) {
1708 jchar* chars = new jchar[s->GetLength()];
1709 memcpy(chars, s->GetValue(), sizeof(jchar) * s->GetLength());
Fred Shih56890e22014-06-02 11:11:52 -07001710 if (is_copy != nullptr) {
1711 *is_copy = JNI_TRUE;
1712 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001713 return chars;
Elliott Hughes75770752011-08-24 17:52:38 -07001714 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001715 if (is_copy != nullptr) {
1716 *is_copy = JNI_FALSE;
1717 }
1718 return static_cast<jchar*>(s->GetValue());
Elliott Hughes814e4032011-08-23 12:07:56 -07001719 }
1720
Mathieu Chartier590fee92013-09-13 13:46:47 -07001721 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001722 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001723 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001724 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08001725 if (chars != s->GetValue()) {
Fred Shih56890e22014-06-02 11:11:52 -07001726 delete[] chars;
1727 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes75770752011-08-24 17:52:38 -07001730 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001731 CHECK_NON_NULL_ARGUMENT(java_string);
1732 ScopedObjectAccess soa(env);
1733 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001734 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Hao848f70a2014-01-15 13:49:50 -08001735 if (heap->IsMovableObject(s)) {
Fred Shih56890e22014-06-02 11:11:52 -07001736 StackHandleScope<1> hs(soa.Self());
Jeff Hao848f70a2014-01-15 13:49:50 -08001737 HandleWrapper<mirror::String> h(hs.NewHandleWrapper(&s));
Fred Shih56890e22014-06-02 11:11:52 -07001738 heap->IncrementDisableMovingGC(soa.Self());
1739 }
1740 if (is_copy != nullptr) {
1741 *is_copy = JNI_FALSE;
1742 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001743 return static_cast<jchar*>(s->GetValue());
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
Elliott Hughes75770752011-08-24 17:52:38 -07001746 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001747 UNUSED(chars);
Fred Shih56890e22014-06-02 11:11:52 -07001748 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1749 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001750 gc::Heap* heap = Runtime::Current()->GetHeap();
1751 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08001752 if (heap->IsMovableObject(s)) {
Fred Shih56890e22014-06-02 11:11:52 -07001753 heap->DecrementDisableMovingGC(soa.Self());
1754 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes75770752011-08-24 17:52:38 -07001757 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001758 if (java_string == nullptr) {
1759 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001760 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001761 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001762 *is_copy = JNI_TRUE;
1763 }
Ian Rogersef28b142012-11-30 14:22:18 -08001764 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001765 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001766 size_t byte_count = s->GetUtfLength();
1767 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001768 CHECK(bytes != nullptr); // bionic aborts anyway.
Jeff Hao848f70a2014-01-15 13:49:50 -08001769 const uint16_t* chars = s->GetValue();
Elliott Hughes75770752011-08-24 17:52:38 -07001770 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1771 bytes[byte_count] = '\0';
1772 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001773 }
1774
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001775 static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001776 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001777 }
1778
Elliott Hughesbd935992011-08-22 11:59:34 -07001779 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001780 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001781 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001782 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001783 if (UNLIKELY(!obj->IsArrayInstance())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001784 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1785 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001786 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001787 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001788 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 }
1790
Elliott Hughes814e4032011-08-23 12:07:56 -07001791 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001792 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001793 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001794 mirror::ObjectArray<mirror::Object>* array =
1795 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001796 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Ian Rogersbc939662013-08-15 10:26:54 -07001799 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1800 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001801 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001802 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001803 mirror::ObjectArray<mirror::Object>* array =
1804 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1805 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001806 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 }
1808
1809 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001810 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 }
1812
1813 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001814 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
1817 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001818 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
1821 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001822 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
1825 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001826 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 }
1828
1829 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001830 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
1833 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001834 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
Ian Rogers1d99e452014-01-02 17:36:41 -08001837 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1838 jobject initial_element) {
1839 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001840 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001841 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001842 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001843 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001844
1845 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001846 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001847 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08001848 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001849 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08001850 if (UNLIKELY(element_class->IsPrimitive())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001851 soa.Vm()->JniAbortF("NewObjectArray", "not an object type: %s",
1852 PrettyDescriptor(element_class).c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08001853 return nullptr;
1854 }
Ian Rogers1d99e452014-01-02 17:36:41 -08001855 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07001856 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08001857 if (UNLIKELY(array_class == nullptr)) {
1858 return nullptr;
1859 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 }
1861
Elliott Hughes75770752011-08-24 17:52:38 -07001862 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001863 mirror::ObjectArray<mirror::Object>* result =
1864 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001865 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001866 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08001867 if (initial_object != nullptr) {
1868 mirror::Class* element_class = result->GetClass()->GetComponentType();
1869 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001870 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
1871 "element type of '%s'",
1872 PrettyDescriptor(initial_object->GetClass()).c_str(),
1873 PrettyDescriptor(element_class).c_str());
1874 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08001875 } else {
1876 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001877 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08001878 }
1879 }
Elliott Hughes75770752011-08-24 17:52:38 -07001880 }
1881 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001882 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
1884
1885 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001886 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 }
1888
Ian Rogersa15e67d2012-02-28 13:51:55 -08001889 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001890 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001891 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001892 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07001893 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001894 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
1895 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001896 return nullptr;
1897 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001898 gc::Heap* heap = Runtime::Current()->GetHeap();
1899 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08001900 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001901 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001902 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001903 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001904 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001905 *is_copy = JNI_FALSE;
1906 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001907 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001908 }
1909
Ian Rogers2d10b202014-05-12 19:15:18 -07001910 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
1911 jint mode) {
1912 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
1913 ScopedObjectAccess soa(env);
1914 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
1915 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001916 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
1917 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001918 return;
1919 }
1920 const size_t component_size = array->GetClass()->GetComponentSize();
1921 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001922 }
1923
Elliott Hughes75770752011-08-24 17:52:38 -07001924 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001925 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 }
1927
Elliott Hughes75770752011-08-24 17:52:38 -07001928 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001929 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001930 }
1931
Elliott Hughes75770752011-08-24 17:52:38 -07001932 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001933 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 }
1935
Elliott Hughes75770752011-08-24 17:52:38 -07001936 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001937 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 }
1939
Elliott Hughes75770752011-08-24 17:52:38 -07001940 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001941 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001942 }
1943
Elliott Hughes75770752011-08-24 17:52:38 -07001944 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001945 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 }
1947
Elliott Hughes75770752011-08-24 17:52:38 -07001948 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001949 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 }
1951
Elliott Hughes75770752011-08-24 17:52:38 -07001952 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001953 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Mathieu Chartier590fee92013-09-13 13:46:47 -07001956 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
1957 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001958 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
1959 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
Mathieu Chartier590fee92013-09-13 13:46:47 -07001962 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001963 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Mathieu Chartier590fee92013-09-13 13:46:47 -07001966 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001967 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Mathieu Chartier590fee92013-09-13 13:46:47 -07001970 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
1971 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001972 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 }
1974
Mathieu Chartier590fee92013-09-13 13:46:47 -07001975 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
1976 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001977 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
Mathieu Chartier590fee92013-09-13 13:46:47 -07001980 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001981 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Mathieu Chartier590fee92013-09-13 13:46:47 -07001984 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001985 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Mathieu Chartier590fee92013-09-13 13:46:47 -07001988 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
1989 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001990 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Ian Rogersbc939662013-08-15 10:26:54 -07001993 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
1994 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001995 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001996 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Ian Rogersbc939662013-08-15 10:26:54 -07001999 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2000 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002001 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Ian Rogersbc939662013-08-15 10:26:54 -07002004 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2005 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002006 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Ian Rogersbc939662013-08-15 10:26:54 -07002009 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2010 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002011 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002012 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Ian Rogersbc939662013-08-15 10:26:54 -07002015 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2016 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002017 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002018 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Ian Rogersbc939662013-08-15 10:26:54 -07002021 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2022 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002023 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Ian Rogersbc939662013-08-15 10:26:54 -07002026 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2027 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002028 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Ian Rogersbc939662013-08-15 10:26:54 -07002031 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2032 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002033 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002034 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Ian Rogersbc939662013-08-15 10:26:54 -07002037 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2038 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002039 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002040 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Ian Rogersbc939662013-08-15 10:26:54 -07002043 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2044 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002045 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Ian Rogersbc939662013-08-15 10:26:54 -07002048 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2049 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002050 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Ian Rogersbc939662013-08-15 10:26:54 -07002053 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2054 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002055 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002056 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Ian Rogersbc939662013-08-15 10:26:54 -07002059 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2060 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002061 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002062 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Ian Rogersbc939662013-08-15 10:26:54 -07002065 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2066 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002067 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Ian Rogersbc939662013-08-15 10:26:54 -07002070 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2071 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002072 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Ian Rogersbc939662013-08-15 10:26:54 -07002075 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2076 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002077 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002078 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Ian Rogersbc939662013-08-15 10:26:54 -07002081 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2082 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002083 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2084 }
2085
Ian Rogersbc939662013-08-15 10:26:54 -07002086 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2087 jint method_count, bool return_errors) {
2088 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002089 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2090 method_count);
2091 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002092 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002093 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002094 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002095 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002096 if (UNLIKELY(method_count == 0)) {
2097 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2098 << PrettyDescriptor(c);
2099 return JNI_OK;
2100 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002101 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002102 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 const char* name = methods[i].name;
2104 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002105 const void* fnPtr = methods[i].fnPtr;
2106 if (UNLIKELY(name == nullptr)) {
2107 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2108 return JNI_ERR;
2109 } else if (UNLIKELY(sig == nullptr)) {
2110 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2111 return JNI_ERR;
2112 } else if (UNLIKELY(fnPtr == nullptr)) {
2113 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2114 return JNI_ERR;
2115 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002116 bool is_fast = false;
Hiroshi Yamauchi36bce582015-05-12 12:16:10 -07002117 // Notes about fast JNI calls:
2118 //
2119 // On a normal JNI call, the calling thread usually transitions
2120 // from the kRunnable state to the kNative state. But if the
2121 // called native function needs to access any Java object, it
2122 // will have to transition back to the kRunnable state.
2123 //
2124 // There is a cost to this double transition. For a JNI call
2125 // that should be quick, this cost may dominate the call cost.
2126 //
2127 // On a fast JNI call, the calling thread avoids this double
2128 // transition by not transitioning from kRunnable to kNative and
2129 // stays in the kRunnable state.
2130 //
2131 // There are risks to using a fast JNI call because it can delay
2132 // a response to a thread suspension request which is typically
2133 // used for a GC root scanning, etc. If a fast JNI call takes a
2134 // long time, it could cause longer thread suspension latency
2135 // and GC pauses.
2136 //
2137 // Thus, fast JNI should be used with care. It should be used
2138 // for a JNI call that takes a short amount of time (eg. no
2139 // long-running loop) and does not block (eg. no locks, I/O,
2140 // etc.)
2141 //
2142 // A '!' prefix in the signature in the JNINativeMethod
2143 // indicates that it's a fast JNI call and the runtime omits the
2144 // thread state transition from kRunnable to kNative at the
2145 // entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002147 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 ++sig;
2149 }
2150
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002151 // Note: the right order is to try to find the method locally
2152 // first, either as a direct or a virtual method. Then move to
2153 // the parent.
2154 mirror::ArtMethod* m = nullptr;
2155 bool warn_on_going_to_parent = down_cast<JNIEnvExt*>(env)->vm->IsCheckJniEnabled();
2156 for (mirror::Class* current_class = c;
2157 current_class != nullptr;
2158 current_class = current_class->GetSuperClass()) {
2159 // Search first only comparing methods which are native.
2160 m = FindMethod<true>(current_class, name, sig);
2161 if (m != nullptr) {
2162 break;
2163 }
2164
2165 // Search again comparing to all methods, to find non-native methods that match.
2166 m = FindMethod<false>(current_class, name, sig);
2167 if (m != nullptr) {
2168 break;
2169 }
2170
2171 if (warn_on_going_to_parent) {
2172 LOG(WARNING) << "CheckJNI: method to register \"" << name << "\" not in the given class. "
2173 << "This is slow, consider changing your RegisterNatives calls.";
2174 warn_on_going_to_parent = false;
2175 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002177
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002178 if (m == nullptr) {
Mathieu Chartier5c02d6c2015-05-04 10:18:24 -07002179 LOG(return_errors ? ERROR : INTERNAL_FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002180 << PrettyDescriptor(c) << "." << name << sig << " in "
2181 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Mathieu Chartier5c02d6c2015-05-04 10:18:24 -07002182 // Safe to pass in LOG(FATAL) since the log object aborts in destructor and only goes
2183 // out of scope after the DumpClass is done executing.
2184 c->DumpClass(LOG(return_errors ? ERROR : FATAL), mirror::Class::kDumpClassFullDetail);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002185 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002187 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002188 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002189 << PrettyDescriptor(c) << "." << name << sig
2190 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002191 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 return JNI_ERR;
2193 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002194
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002195 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002196
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002197 m->RegisterNative(fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 }
2199 return JNI_OK;
2200 }
2201
Elliott Hughes5174fe62011-08-23 15:12:35 -07002202 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002203 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002204 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002205 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002206
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002207 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002208
Ian Rogers2d10b202014-05-12 19:15:18 -07002209 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002210 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002211 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002212 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002213 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002214 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002215 }
2216 }
2217 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002218 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002219 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002220 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002221 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002222 }
2223 }
2224
Ian Rogers2d10b202014-05-12 19:15:18 -07002225 if (unregistered_count == 0) {
2226 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2227 << PrettyDescriptor(c) << "' that contains no native methods";
2228 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002229 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
2231
Ian Rogers719d1a32014-03-06 12:13:39 -08002232 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002233 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002234 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002235 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2236 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002237 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002238 return JNI_ERR;
2239 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002240 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002241 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Ian Rogers719d1a32014-03-06 12:13:39 -08002244 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002245 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002246 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002247 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002248 o->MonitorExit(soa.Self());
2249 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002250 return JNI_ERR;
2251 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002252 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002253 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
2256 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002257 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002258 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002259 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002260 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002262 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002264 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002265 }
2266
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002268 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002269 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2270 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002271 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002272 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002273 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002274 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2275 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002276 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002277 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002278
Brian Carlstrom85a93362014-06-25 09:30:52 -07002279 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002280 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002281 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2282 "buffer capacity greater than maximum jint: %" PRId64,
2283 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002284 return nullptr;
2285 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002286 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002287 jint capacity_arg = static_cast<jint>(capacity);
2288
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002289 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2290 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002291 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002292 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002293 }
2294
Elliott Hughesb465ab02011-08-24 11:21:21 -07002295 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002296 return reinterpret_cast<void*>(env->GetLongField(
2297 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002298 }
2299
Elliott Hughesb465ab02011-08-24 11:21:21 -07002300 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002301 return static_cast<jlong>(env->GetIntField(
2302 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002303 }
2304
Andreas Gampea8763072014-12-20 00:08:35 -08002305 static jobjectRefType GetObjectRefType(JNIEnv* env ATTRIBUTE_UNUSED, jobject java_object) {
2306 if (java_object == nullptr) {
2307 return JNIInvalidRefType;
2308 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002309
2310 // Do we definitely know what kind of reference this is?
2311 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2312 IndirectRefKind kind = GetIndirectRefKind(ref);
2313 switch (kind) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002314 case kLocal:
2315 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002316 case kGlobal:
2317 return JNIGlobalRefType;
2318 case kWeakGlobal:
2319 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002320 case kHandleScopeOrInvalid:
Ian Rogersc0542af2014-09-03 16:16:56 -07002321 // Assume value is in a handle scope.
2322 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002323 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002324 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
Andreas Gampea8763072014-12-20 00:08:35 -08002325 UNREACHABLE();
Elliott Hughescdf53122011-08-19 15:46:09 -07002326 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002327
2328 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002329 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2330 const char* caller)
2331 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002332 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002333 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002334 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2335 return JNI_ERR;
2336 }
2337 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002338 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002339 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2340 if (!okay) {
2341 soa.Self()->ThrowOutOfMemoryError(caller);
2342 }
2343 return okay ? JNI_OK : JNI_ERR;
2344 }
2345
2346 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002347 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002348 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002349 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002350 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002351 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002352 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002353 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002354 return soa.AddLocalReference<JniT>(result);
2355 }
2356
Ian Rogers2d10b202014-05-12 19:15:18 -07002357 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2358 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2359 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002360 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002361 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002362 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002363 soa.Vm()->JniAbortF(fn_name,
2364 "attempt to %s %s primitive array elements with an object of type %s",
2365 operation,
2366 PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2367 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002368 return nullptr;
2369 }
2370 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2371 return array;
2372 }
2373
2374 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2375 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2376 CHECK_NON_NULL_ARGUMENT(java_array);
2377 ScopedObjectAccess soa(env);
2378 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2379 "GetArrayElements",
2380 "get");
2381 if (UNLIKELY(array == nullptr)) {
2382 return nullptr;
2383 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002384 // Only make a copy if necessary.
2385 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2386 if (is_copy != nullptr) {
2387 *is_copy = JNI_TRUE;
2388 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002389 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002390 size_t size = array->GetLength() * component_size;
2391 void* data = new uint64_t[RoundUp(size, 8) / 8];
2392 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002393 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002394 } else {
2395 if (is_copy != nullptr) {
2396 *is_copy = JNI_FALSE;
2397 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002398 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002399 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002400 }
2401
Ian Rogers2d10b202014-05-12 19:15:18 -07002402 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002403 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002404 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002405 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002406 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2407 "ReleaseArrayElements",
2408 "release");
2409 if (array == nullptr) {
2410 return;
2411 }
2412 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2413 }
2414
2415 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2416 size_t component_size, void* elements, jint mode)
2417 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002418 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002419 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002420 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002421 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002422 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2423 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002424 if (is_copy) {
2425 // Sanity check: If elements is not the same as the java array's data, it better not be a
2426 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2427 // copies we make?
2428 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002429 soa.Vm()->JniAbortF("ReleaseArrayElements",
2430 "invalid element pointer %p, array elements are %p",
2431 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002432 return;
2433 }
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002434 if (mode != JNI_ABORT) {
2435 memcpy(array_data, elements, bytes);
2436 } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2437 // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2438 LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2439 soa.Self()->DumpJavaStack(LOG(WARNING));
2440 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002441 }
2442 if (mode != JNI_COMMIT) {
2443 if (is_copy) {
2444 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002445 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002446 // Non copy to a movable object must means that we had disabled the moving GC.
2447 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002448 }
2449 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002450 }
2451
Ian Rogers2d10b202014-05-12 19:15:18 -07002452 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2453 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2454 jsize start, jsize length, ElementT* buf) {
2455 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2456 ScopedObjectAccess soa(env);
2457 ArtArrayT* array =
2458 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2459 "GetPrimitiveArrayRegion",
2460 "get region of");
2461 if (array != nullptr) {
2462 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2463 ThrowAIOOBE(soa, array, start, length, "src");
2464 } else {
2465 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2466 ElementT* data = array->GetData();
2467 memcpy(buf, data + start, length * sizeof(ElementT));
2468 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002469 }
2470 }
2471
Ian Rogers2d10b202014-05-12 19:15:18 -07002472 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2473 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2474 jsize start, jsize length, const ElementT* buf) {
2475 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2476 ScopedObjectAccess soa(env);
2477 ArtArrayT* array =
2478 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2479 "SetPrimitiveArrayRegion",
2480 "set region of");
2481 if (array != nullptr) {
2482 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2483 ThrowAIOOBE(soa, array, start, length, "dst");
2484 } else {
2485 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2486 ElementT* data = array->GetData();
2487 memcpy(data + start, buf, length * sizeof(ElementT));
2488 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002489 }
2490 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002491};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002492
Elliott Hughes88c5c352012-03-15 18:49:48 -07002493const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002494 nullptr, // reserved0.
2495 nullptr, // reserved1.
2496 nullptr, // reserved2.
2497 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002498 JNI::GetVersion,
2499 JNI::DefineClass,
2500 JNI::FindClass,
2501 JNI::FromReflectedMethod,
2502 JNI::FromReflectedField,
2503 JNI::ToReflectedMethod,
2504 JNI::GetSuperclass,
2505 JNI::IsAssignableFrom,
2506 JNI::ToReflectedField,
2507 JNI::Throw,
2508 JNI::ThrowNew,
2509 JNI::ExceptionOccurred,
2510 JNI::ExceptionDescribe,
2511 JNI::ExceptionClear,
2512 JNI::FatalError,
2513 JNI::PushLocalFrame,
2514 JNI::PopLocalFrame,
2515 JNI::NewGlobalRef,
2516 JNI::DeleteGlobalRef,
2517 JNI::DeleteLocalRef,
2518 JNI::IsSameObject,
2519 JNI::NewLocalRef,
2520 JNI::EnsureLocalCapacity,
2521 JNI::AllocObject,
2522 JNI::NewObject,
2523 JNI::NewObjectV,
2524 JNI::NewObjectA,
2525 JNI::GetObjectClass,
2526 JNI::IsInstanceOf,
2527 JNI::GetMethodID,
2528 JNI::CallObjectMethod,
2529 JNI::CallObjectMethodV,
2530 JNI::CallObjectMethodA,
2531 JNI::CallBooleanMethod,
2532 JNI::CallBooleanMethodV,
2533 JNI::CallBooleanMethodA,
2534 JNI::CallByteMethod,
2535 JNI::CallByteMethodV,
2536 JNI::CallByteMethodA,
2537 JNI::CallCharMethod,
2538 JNI::CallCharMethodV,
2539 JNI::CallCharMethodA,
2540 JNI::CallShortMethod,
2541 JNI::CallShortMethodV,
2542 JNI::CallShortMethodA,
2543 JNI::CallIntMethod,
2544 JNI::CallIntMethodV,
2545 JNI::CallIntMethodA,
2546 JNI::CallLongMethod,
2547 JNI::CallLongMethodV,
2548 JNI::CallLongMethodA,
2549 JNI::CallFloatMethod,
2550 JNI::CallFloatMethodV,
2551 JNI::CallFloatMethodA,
2552 JNI::CallDoubleMethod,
2553 JNI::CallDoubleMethodV,
2554 JNI::CallDoubleMethodA,
2555 JNI::CallVoidMethod,
2556 JNI::CallVoidMethodV,
2557 JNI::CallVoidMethodA,
2558 JNI::CallNonvirtualObjectMethod,
2559 JNI::CallNonvirtualObjectMethodV,
2560 JNI::CallNonvirtualObjectMethodA,
2561 JNI::CallNonvirtualBooleanMethod,
2562 JNI::CallNonvirtualBooleanMethodV,
2563 JNI::CallNonvirtualBooleanMethodA,
2564 JNI::CallNonvirtualByteMethod,
2565 JNI::CallNonvirtualByteMethodV,
2566 JNI::CallNonvirtualByteMethodA,
2567 JNI::CallNonvirtualCharMethod,
2568 JNI::CallNonvirtualCharMethodV,
2569 JNI::CallNonvirtualCharMethodA,
2570 JNI::CallNonvirtualShortMethod,
2571 JNI::CallNonvirtualShortMethodV,
2572 JNI::CallNonvirtualShortMethodA,
2573 JNI::CallNonvirtualIntMethod,
2574 JNI::CallNonvirtualIntMethodV,
2575 JNI::CallNonvirtualIntMethodA,
2576 JNI::CallNonvirtualLongMethod,
2577 JNI::CallNonvirtualLongMethodV,
2578 JNI::CallNonvirtualLongMethodA,
2579 JNI::CallNonvirtualFloatMethod,
2580 JNI::CallNonvirtualFloatMethodV,
2581 JNI::CallNonvirtualFloatMethodA,
2582 JNI::CallNonvirtualDoubleMethod,
2583 JNI::CallNonvirtualDoubleMethodV,
2584 JNI::CallNonvirtualDoubleMethodA,
2585 JNI::CallNonvirtualVoidMethod,
2586 JNI::CallNonvirtualVoidMethodV,
2587 JNI::CallNonvirtualVoidMethodA,
2588 JNI::GetFieldID,
2589 JNI::GetObjectField,
2590 JNI::GetBooleanField,
2591 JNI::GetByteField,
2592 JNI::GetCharField,
2593 JNI::GetShortField,
2594 JNI::GetIntField,
2595 JNI::GetLongField,
2596 JNI::GetFloatField,
2597 JNI::GetDoubleField,
2598 JNI::SetObjectField,
2599 JNI::SetBooleanField,
2600 JNI::SetByteField,
2601 JNI::SetCharField,
2602 JNI::SetShortField,
2603 JNI::SetIntField,
2604 JNI::SetLongField,
2605 JNI::SetFloatField,
2606 JNI::SetDoubleField,
2607 JNI::GetStaticMethodID,
2608 JNI::CallStaticObjectMethod,
2609 JNI::CallStaticObjectMethodV,
2610 JNI::CallStaticObjectMethodA,
2611 JNI::CallStaticBooleanMethod,
2612 JNI::CallStaticBooleanMethodV,
2613 JNI::CallStaticBooleanMethodA,
2614 JNI::CallStaticByteMethod,
2615 JNI::CallStaticByteMethodV,
2616 JNI::CallStaticByteMethodA,
2617 JNI::CallStaticCharMethod,
2618 JNI::CallStaticCharMethodV,
2619 JNI::CallStaticCharMethodA,
2620 JNI::CallStaticShortMethod,
2621 JNI::CallStaticShortMethodV,
2622 JNI::CallStaticShortMethodA,
2623 JNI::CallStaticIntMethod,
2624 JNI::CallStaticIntMethodV,
2625 JNI::CallStaticIntMethodA,
2626 JNI::CallStaticLongMethod,
2627 JNI::CallStaticLongMethodV,
2628 JNI::CallStaticLongMethodA,
2629 JNI::CallStaticFloatMethod,
2630 JNI::CallStaticFloatMethodV,
2631 JNI::CallStaticFloatMethodA,
2632 JNI::CallStaticDoubleMethod,
2633 JNI::CallStaticDoubleMethodV,
2634 JNI::CallStaticDoubleMethodA,
2635 JNI::CallStaticVoidMethod,
2636 JNI::CallStaticVoidMethodV,
2637 JNI::CallStaticVoidMethodA,
2638 JNI::GetStaticFieldID,
2639 JNI::GetStaticObjectField,
2640 JNI::GetStaticBooleanField,
2641 JNI::GetStaticByteField,
2642 JNI::GetStaticCharField,
2643 JNI::GetStaticShortField,
2644 JNI::GetStaticIntField,
2645 JNI::GetStaticLongField,
2646 JNI::GetStaticFloatField,
2647 JNI::GetStaticDoubleField,
2648 JNI::SetStaticObjectField,
2649 JNI::SetStaticBooleanField,
2650 JNI::SetStaticByteField,
2651 JNI::SetStaticCharField,
2652 JNI::SetStaticShortField,
2653 JNI::SetStaticIntField,
2654 JNI::SetStaticLongField,
2655 JNI::SetStaticFloatField,
2656 JNI::SetStaticDoubleField,
2657 JNI::NewString,
2658 JNI::GetStringLength,
2659 JNI::GetStringChars,
2660 JNI::ReleaseStringChars,
2661 JNI::NewStringUTF,
2662 JNI::GetStringUTFLength,
2663 JNI::GetStringUTFChars,
2664 JNI::ReleaseStringUTFChars,
2665 JNI::GetArrayLength,
2666 JNI::NewObjectArray,
2667 JNI::GetObjectArrayElement,
2668 JNI::SetObjectArrayElement,
2669 JNI::NewBooleanArray,
2670 JNI::NewByteArray,
2671 JNI::NewCharArray,
2672 JNI::NewShortArray,
2673 JNI::NewIntArray,
2674 JNI::NewLongArray,
2675 JNI::NewFloatArray,
2676 JNI::NewDoubleArray,
2677 JNI::GetBooleanArrayElements,
2678 JNI::GetByteArrayElements,
2679 JNI::GetCharArrayElements,
2680 JNI::GetShortArrayElements,
2681 JNI::GetIntArrayElements,
2682 JNI::GetLongArrayElements,
2683 JNI::GetFloatArrayElements,
2684 JNI::GetDoubleArrayElements,
2685 JNI::ReleaseBooleanArrayElements,
2686 JNI::ReleaseByteArrayElements,
2687 JNI::ReleaseCharArrayElements,
2688 JNI::ReleaseShortArrayElements,
2689 JNI::ReleaseIntArrayElements,
2690 JNI::ReleaseLongArrayElements,
2691 JNI::ReleaseFloatArrayElements,
2692 JNI::ReleaseDoubleArrayElements,
2693 JNI::GetBooleanArrayRegion,
2694 JNI::GetByteArrayRegion,
2695 JNI::GetCharArrayRegion,
2696 JNI::GetShortArrayRegion,
2697 JNI::GetIntArrayRegion,
2698 JNI::GetLongArrayRegion,
2699 JNI::GetFloatArrayRegion,
2700 JNI::GetDoubleArrayRegion,
2701 JNI::SetBooleanArrayRegion,
2702 JNI::SetByteArrayRegion,
2703 JNI::SetCharArrayRegion,
2704 JNI::SetShortArrayRegion,
2705 JNI::SetIntArrayRegion,
2706 JNI::SetLongArrayRegion,
2707 JNI::SetFloatArrayRegion,
2708 JNI::SetDoubleArrayRegion,
2709 JNI::RegisterNatives,
2710 JNI::UnregisterNatives,
2711 JNI::MonitorEnter,
2712 JNI::MonitorExit,
2713 JNI::GetJavaVM,
2714 JNI::GetStringRegion,
2715 JNI::GetStringUTFRegion,
2716 JNI::GetPrimitiveArrayCritical,
2717 JNI::ReleasePrimitiveArrayCritical,
2718 JNI::GetStringCritical,
2719 JNI::ReleaseStringCritical,
2720 JNI::NewWeakGlobalRef,
2721 JNI::DeleteWeakGlobalRef,
2722 JNI::ExceptionCheck,
2723 JNI::NewDirectByteBuffer,
2724 JNI::GetDirectBufferAddress,
2725 JNI::GetDirectBufferCapacity,
2726 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002727};
2728
Ian Rogers68d8b422014-07-17 11:09:10 -07002729const JNINativeInterface* GetJniNativeInterface() {
2730 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002731}
2732
Elliott Hughesc8fece32013-01-02 11:27:23 -08002733void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002734 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002735 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002736 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002737 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2738 }
2739 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2740}
2741
Ian Rogersdf20fe02011-07-20 20:34:16 -07002742} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002743
2744std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2745 switch (rhs) {
2746 case JNIInvalidRefType:
2747 os << "JNIInvalidRefType";
2748 return os;
2749 case JNILocalRefType:
2750 os << "JNILocalRefType";
2751 return os;
2752 case JNIGlobalRefType:
2753 os << "JNIGlobalRefType";
2754 return os;
2755 case JNIWeakGlobalRefType:
2756 os << "JNIWeakGlobalRefType";
2757 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002758 default:
Ian Rogersc7dd2952014-10-21 23:31:19 -07002759 LOG(::art::FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
2760 UNREACHABLE();
Elliott Hughesb465ab02011-08-24 11:21:21 -07002761 }
2762}