blob: 234a733967d7ccf6b95241c1290ccd45496f637a [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"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "art_method-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080028#include "atomic.h"
Mathieu Chartierbad02672014-08-25 13:08:22 -070029#include "base/allocator.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080030#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080031#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080032#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080033#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070034#include "dex_file-inl.h"
Mathieu Chartierd0004802014-10-15 16:59:47 -070035#include "fault_handler.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070036#include "gc_root.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070037#include "gc/accounting/card_table-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070038#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070039#include "interpreter/interpreter.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070040#include "jni_env_ext.h"
41#include "java_vm_ext.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)
Mathieu Chartier90443472015-07-16 20:32:27 -070092 SHARED_REQUIRES(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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700101 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200102 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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700110 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800111 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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700124 SHARED_REQUIRES(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 Chartiere401d142015-04-22 13:56:20 -0700129 ArtMethod* method = nullptr;
130 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Elliott Hughescdf53122011-08-19 15:46:09 -0700131 if (is_static) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132 method = c->FindDirectMethod(name, sig, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700133 } else if (c->IsInterface()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134 method = c->FindInterfaceMethod(name, sig, pointer_size);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700135 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700136 method = c->FindVirtualMethod(name, sig, pointer_size);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800137 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700138 // No virtual method matching the signature. Search declared
139 // private methods and constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140 method = c->FindDeclaredDirectMethod(name, sig, pointer_size);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700141 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700142 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800143 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800145 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700146 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700148}
149
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800150static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Mathieu Chartier90443472015-07-16 20:32:27 -0700151 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700152 ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700153 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
154 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700155 return soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700156 }
Brian Carlstromce888532013-10-10 00:32:58 -0700157 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800158 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700159 return method->GetDeclaringClass()->GetClassLoader();
160 }
161 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800162 mirror::ClassLoader* class_loader =
163 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
164 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700165 return class_loader;
166 }
167 // See if the override ClassLoader is set for gtests.
Ian Rogers68d8b422014-07-17 11:09:10 -0700168 class_loader = soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800169 if (class_loader != nullptr) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700170 // If so, CommonCompilerTest should have marked the runtime as a compiler not compiling an
171 // image.
172 CHECK(Runtime::Current()->IsAotCompiler());
Andreas Gampe4585f872015-03-27 23:45:15 -0700173 CHECK(!Runtime::Current()->IsCompilingBootImage());
Brian Carlstromce888532013-10-10 00:32:58 -0700174 return class_loader;
175 }
176 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800177 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700178}
179
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700180static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
181 const char* sig, bool is_static)
Mathieu Chartier90443472015-07-16 20:32:27 -0700182 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700183 StackHandleScope<2> hs(soa.Self());
184 Handle<mirror::Class> c(
185 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
186 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800187 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700188 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700189 ArtField* field = nullptr;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800190 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700191 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
192 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700193 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800194 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700195 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700196 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700197 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800198 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700199 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200 DCHECK(soa.Self()->IsExceptionPending());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800201 StackHandleScope<1> hs2(soa.Self());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000202 Handle<mirror::Throwable> cause(hs2.NewHandle(soa.Self()->GetException()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 soa.Self()->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700204 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000205 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800206 "no type \"%s\" found and so no field \"%s\" "
207 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700208 c->GetDescriptor(&temp));
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000209 soa.Self()->GetException()->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800210 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700212 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700213 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700214 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700215 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700217 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800219 if (field == nullptr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000220 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800221 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700222 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800223 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700224 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700226}
227
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800228static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700229 jsize length, const char* identifier)
Mathieu Chartier90443472015-07-16 20:32:27 -0700230 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700231 std::string type(PrettyTypeOf(array));
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000232 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800233 "%s offset=%d length=%d %s.length=%d",
234 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700235}
Ian Rogers0571d352011-11-03 19:51:38 -0700236
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
238 jsize array_length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700239 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000240 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800241 "offset=%d length=%d string.length()=%d", start, length,
242 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700243}
Elliott Hughes814e4032011-08-23 12:07:56 -0700244
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Mathieu Chartier90443472015-07-16 20:32:27 -0700246 REQUIRES(!Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 // Turn the const char* into a java.lang.String.
248 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800249 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700250 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700251 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700252
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 // Choose an appropriate constructor and set up the arguments.
254 jvalue args[2];
255 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800256 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800258 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 signature = "(Ljava/lang/String;)V";
260 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800261 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262 signature = "(Ljava/lang/Throwable;)V";
263 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700264 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
266 args[0].l = s.get();
267 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700268 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800270 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800271 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800273 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274 return JNI_ERR;
275 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700276
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800277 ScopedLocalRef<jthrowable> exception(
278 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
279 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 return JNI_ERR;
281 }
Ian Rogersef28b142012-11-30 14:22:18 -0800282 ScopedObjectAccess soa(env);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000283 soa.Self()->SetException(soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700285}
286
Ian Rogers68d8b422014-07-17 11:09:10 -0700287static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
288 return reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughes75770752011-08-24 17:52:38 -0700289}
290
Ian Rogers2d10b202014-05-12 19:15:18 -0700291#define CHECK_NON_NULL_ARGUMENT(value) \
292 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700293
Ian Rogers2d10b202014-05-12 19:15:18 -0700294#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
295 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
296
297#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
298 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
299
300#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
301 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
302
303#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800304 if (UNLIKELY(value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700305 JavaVmExtFromEnv(env)->JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700306 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700307 }
308
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700309#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800310 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700311 JavaVmExtFromEnv(env)->JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700312 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700313 }
314
Andreas Gampe3f1dc562015-05-18 15:52:22 -0700315template <bool kNative>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316static ArtMethod* FindMethod(mirror::Class* c, const StringPiece& name, const StringPiece& sig)
Mathieu Chartier90443472015-07-16 20:32:27 -0700317 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
319 for (auto& method : c->GetDirectMethods(pointer_size)) {
320 if (kNative == method.IsNative() && name == method.GetName() && method.GetSignature() == sig) {
321 return &method;
Andreas Gampe3f1dc562015-05-18 15:52:22 -0700322 }
323 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700324 for (auto& method : c->GetVirtualMethods(pointer_size)) {
325 if (kNative == method.IsNative() && name == method.GetName() && method.GetSignature() == sig) {
326 return &method;
Andreas Gampe3f1dc562015-05-18 15:52:22 -0700327 }
328 }
Andreas Gampe3f1dc562015-05-18 15:52:22 -0700329 return nullptr;
330}
331
Elliott Hughescdf53122011-08-19 15:46:09 -0700332class JNI {
333 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700334 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700335 return JNI_VERSION_1_6;
336 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700337
Ian Rogers25e8b912012-09-07 11:31:36 -0700338 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700339 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800340 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700341 }
342
Elliott Hughescdf53122011-08-19 15:46:09 -0700343 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700344 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700345 Runtime* runtime = Runtime::Current();
346 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700347 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700348 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800349 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700350 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700351 StackHandleScope<1> hs(soa.Self());
352 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800353 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700354 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800355 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700356 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700358 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700359
Ian Rogers62f05122014-03-21 11:21:29 -0700360 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700361 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700362 ScopedObjectAccess soa(env);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700363 return soa.EncodeMethod(ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700364 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700365
Ian Rogers62f05122014-03-21 11:21:29 -0700366 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700367 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 ScopedObjectAccess soa(env);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700369 mirror::Object* obj_field = soa.Decode<mirror::Object*>(jlr_field);
370 if (obj_field->GetClass() != mirror::Field::StaticClass()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700371 // Not even a java.lang.reflect.Field, return null. TODO, is this check necessary?
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700372 return nullptr;
373 }
374 auto* field = static_cast<mirror::Field*>(obj_field);
375 return soa.EncodeField(field->GetArtField());
Elliott Hughescdf53122011-08-19 15:46:09 -0700376 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700377
Elliott Hughescdf53122011-08-19 15:46:09 -0700378 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700379 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 ScopedObjectAccess soa(env);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700381 ArtMethod* m = soa.DecodeMethod(mid);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700382 mirror::AbstractMethod* method;
Sebastien Hertzd3333762014-06-26 14:45:07 +0200383 if (m->IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700384 method = mirror::Constructor::CreateFromArtMethod(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200385 } else {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700386 method = mirror::Method::CreateFromArtMethod(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200387 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700388 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700389 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700390
Elliott Hughescdf53122011-08-19 15:46:09 -0700391 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700392 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 ScopedObjectAccess soa(env);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700394 ArtField* f = soa.DecodeField(fid);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700395 return soa.AddLocalReference<jobject>(mirror::Field::CreateFromArtField(soa.Self(), f, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700396 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700397
Elliott Hughes37f7a402011-08-22 18:56:01 -0700398 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700399 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800401 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700402 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700403 }
404
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700405 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700406 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700407 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800408 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Brian Carlstrom08ac9222015-05-22 13:43:00 -0700409 return soa.AddLocalReference<jclass>(c->IsInterface() ? nullptr : c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700410 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700411
Narayan Kamath1268b742014-07-11 19:15:11 +0100412 // Note: java_class1 should be safely castable to java_class2, and
413 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700414 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700415 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
416 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700417 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800418 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
419 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100420 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700421 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700422
Elliott Hughese84278b2012-03-22 10:06:53 -0700423 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700424 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800425 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700426 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700427 return JNI_TRUE;
428 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700429 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800430 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
431 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700432 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700433 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700434 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700435
Elliott Hughes37f7a402011-08-22 18:56:01 -0700436 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700437 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800438 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
439 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700440 return JNI_ERR;
441 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000442 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700443 return JNI_OK;
444 }
445
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700446 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700447 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800448 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700449 }
450
451 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700452 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700453 }
454
455 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700456 ScopedObjectAccess soa(env);
457 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700458 }
459
460 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700461 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700462
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700463 // If we have no exception to describe, pass through.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000464 if (!soa.Self()->GetException()) {
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700465 return;
466 }
467
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000468 StackHandleScope<1> hs(soa.Self());
469 Handle<mirror::Throwable> old_exception(
470 hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()));
471 soa.Self()->ClearException();
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800472 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700473 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700474 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
475 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800476 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700477 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700478 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700479 } else {
480 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800481 if (soa.Self()->IsExceptionPending()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000482 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700483 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800484 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700485 }
486 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000487 soa.Self()->SetException(old_exception.Get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700488 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700489
Elliott Hughescdf53122011-08-19 15:46:09 -0700490 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700491 ScopedObjectAccess soa(env);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000492 mirror::Object* exception = soa.Self()->GetException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700493 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700494 }
495
Ian Rogers25e8b912012-09-07 11:31:36 -0700496 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700497 LOG(FATAL) << "JNI FatalError called: " << msg;
498 }
499
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700500 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700501 // TODO: SOA may not be necessary but I do it to please lock annotations.
502 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700503 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700504 return JNI_ERR;
505 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700506 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700507 return JNI_OK;
508 }
509
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700510 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700511 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800512 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700513 soa.Env()->PopFrame();
514 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700515 }
516
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700517 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700518 // TODO: SOA may not be necessary but I do it to please lock annotations.
519 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700520 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700521 }
522
Elliott Hughescdf53122011-08-19 15:46:09 -0700523 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700524 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800525 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700526 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700527 }
528
529 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700530 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
531 Thread* self = down_cast<JNIEnvExt*>(env)->self;
532 vm->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700533 }
534
535 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700536 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700537 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
538 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700539 }
540
541 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700542 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
543 Thread* self = down_cast<JNIEnvExt*>(env)->self;
544 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700545 }
546
547 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700548 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800549 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800550 // Check for null after decoding the object to handle cleared weak globals.
551 if (decoded_obj == nullptr) {
552 return nullptr;
553 }
554 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700555 }
556
Mathieu Chartierdd06afe2015-06-26 10:47:08 -0700557 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800558 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700559 return;
560 }
Mathieu Chartierdd06afe2015-06-26 10:47:08 -0700561 // SOA is only necessary to have exclusion between GC root marking and removing.
562 // We don't want to have the GC attempt to mark a null root if we just removed
563 // it. b/22119403
564 ScopedObjectAccess soa(env);
565 auto* ext_env = down_cast<JNIEnvExt*>(env);
566 if (!ext_env->locals.Remove(ext_env->local_ref_cookie, obj)) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 // Attempting to delete a local reference that is not in the
568 // topmost local reference frame is a no-op. DeleteLocalRef returns
569 // void and doesn't throw any exceptions, but we should probably
570 // complain about it so the user will notice that things aren't
571 // going quite the way they expect.
572 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
573 << "failed to find entry";
574 }
575 }
576
577 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700578 if (obj1 == obj2) {
579 return JNI_TRUE;
580 } else {
581 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800582 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
583 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700584 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700585 }
586
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700587 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700588 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700589 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800590 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800591 if (c == nullptr) {
592 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700593 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800594 if (c->IsStringClass()) {
595 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
596 mirror::SetStringCountVisitor visitor(0);
597 return soa.AddLocalReference<jobject>(mirror::String::Alloc<true>(soa.Self(), 0,
598 allocator_type, visitor));
599 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700600 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700601 }
602
Ian Rogersbc939662013-08-15 10:26:54 -0700603 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700604 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700605 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700606 CHECK_NON_NULL_ARGUMENT(java_class);
607 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700608 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700609 va_end(args);
610 return result;
611 }
612
Elliott Hughes72025e52011-08-23 17:50:30 -0700613 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700614 CHECK_NON_NULL_ARGUMENT(java_class);
615 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700616 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800617 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800618 if (c == nullptr) {
619 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700620 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800621 if (c->IsStringClass()) {
622 // Replace calls to String.<init> with equivalent StringFactory call.
623 jmethodID sf_mid = WellKnownClasses::StringInitToStringFactoryMethodID(mid);
624 return CallStaticObjectMethodV(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
625 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800626 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800627 if (result == nullptr) {
628 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700629 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700630 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700631 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800632 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800633 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700634 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800635 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 }
637
Elliott Hughes72025e52011-08-23 17:50:30 -0700638 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700639 CHECK_NON_NULL_ARGUMENT(java_class);
640 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700641 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800642 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800643 if (c == nullptr) {
644 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700645 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800646 if (c->IsStringClass()) {
647 // Replace calls to String.<init> with equivalent StringFactory call.
648 jmethodID sf_mid = WellKnownClasses::StringInitToStringFactoryMethodID(mid);
649 return CallStaticObjectMethodA(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
650 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800651 mirror::Object* result = c->AllocObject(soa.Self());
652 if (result == nullptr) {
653 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700654 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700655 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700656 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800657 if (soa.Self()->IsExceptionPending()) {
658 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700659 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800660 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 }
662
Ian Rogersbc939662013-08-15 10:26:54 -0700663 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700664 CHECK_NON_NULL_ARGUMENT(java_class);
665 CHECK_NON_NULL_ARGUMENT(name);
666 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700667 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700668 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700669 }
670
Ian Rogersbc939662013-08-15 10:26:54 -0700671 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
672 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700673 CHECK_NON_NULL_ARGUMENT(java_class);
674 CHECK_NON_NULL_ARGUMENT(name);
675 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700676 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700677 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 }
679
Elliott Hughes72025e52011-08-23 17:50:30 -0700680 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700681 va_list ap;
682 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700683 CHECK_NON_NULL_ARGUMENT(obj);
684 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700685 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700686 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700687 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700688 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 }
690
Elliott Hughes72025e52011-08-23 17:50:30 -0700691 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700692 CHECK_NON_NULL_ARGUMENT(obj);
693 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 ScopedObjectAccess soa(env);
695 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
696 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700697 }
698
Elliott Hughes72025e52011-08-23 17:50:30 -0700699 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700700 CHECK_NON_NULL_ARGUMENT(obj);
701 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700702 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700703 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700704 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 }
706
Elliott Hughes72025e52011-08-23 17:50:30 -0700707 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700708 va_list ap;
709 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700710 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
711 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700712 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700713 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700714 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700715 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700716 }
717
Elliott Hughes72025e52011-08-23 17:50:30 -0700718 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700719 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
720 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721 ScopedObjectAccess soa(env);
722 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700723 }
724
Elliott Hughes72025e52011-08-23 17:50:30 -0700725 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700726 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
727 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700729 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 }
731
Elliott Hughes72025e52011-08-23 17:50:30 -0700732 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700733 va_list ap;
734 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700735 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
736 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700737 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700738 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700739 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700740 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700741 }
742
Elliott Hughes72025e52011-08-23 17:50:30 -0700743 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700744 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
745 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700746 ScopedObjectAccess soa(env);
747 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 }
749
Elliott Hughes72025e52011-08-23 17:50:30 -0700750 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700751 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
752 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700753 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700754 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 }
756
Elliott Hughes72025e52011-08-23 17:50:30 -0700757 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700758 va_list ap;
759 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700760 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
761 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700762 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700763 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700764 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700765 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 }
767
Elliott Hughes72025e52011-08-23 17:50:30 -0700768 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700769 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
770 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700771 ScopedObjectAccess soa(env);
772 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 }
774
Elliott Hughes72025e52011-08-23 17:50:30 -0700775 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700776 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
777 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700778 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700779 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 }
781
Elliott Hughes72025e52011-08-23 17:50:30 -0700782 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700783 va_list ap;
784 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700785 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
786 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700787 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700788 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700789 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700790 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 }
792
Elliott Hughes72025e52011-08-23 17:50:30 -0700793 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700794 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
795 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700796 ScopedObjectAccess soa(env);
797 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700798 }
799
Elliott Hughes72025e52011-08-23 17:50:30 -0700800 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700801 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
802 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700803 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700804 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 }
806
Elliott Hughes72025e52011-08-23 17:50:30 -0700807 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700808 va_list ap;
809 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700810 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
811 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700812 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700813 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700814 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700815 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 }
817
Elliott Hughes72025e52011-08-23 17:50:30 -0700818 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700819 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
820 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700821 ScopedObjectAccess soa(env);
822 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 }
824
Elliott Hughes72025e52011-08-23 17:50:30 -0700825 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700826 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
827 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700828 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700829 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 }
831
Elliott Hughes72025e52011-08-23 17:50:30 -0700832 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700833 va_list ap;
834 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700835 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
836 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700837 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700838 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700839 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700840 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 }
842
Elliott Hughes72025e52011-08-23 17:50:30 -0700843 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700844 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
845 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 ScopedObjectAccess soa(env);
847 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 }
849
Elliott Hughes72025e52011-08-23 17:50:30 -0700850 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700851 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
852 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700853 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700854 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 }
856
Elliott Hughes72025e52011-08-23 17:50:30 -0700857 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700858 va_list ap;
859 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700860 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
861 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700862 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700864 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700865 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 }
867
Elliott Hughes72025e52011-08-23 17:50:30 -0700868 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700869 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
870 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700871 ScopedObjectAccess soa(env);
872 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 }
874
Elliott Hughes72025e52011-08-23 17:50:30 -0700875 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700876 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
877 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700878 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700879 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700880 }
881
Elliott Hughes72025e52011-08-23 17:50:30 -0700882 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700883 va_list ap;
884 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700885 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
886 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700887 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700888 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700889 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700890 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700891 }
892
Elliott Hughes72025e52011-08-23 17:50:30 -0700893 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700894 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
895 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700896 ScopedObjectAccess soa(env);
897 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700898 }
899
Elliott Hughes72025e52011-08-23 17:50:30 -0700900 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700901 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
902 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700903 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700904 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700905 }
906
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 va_list ap;
909 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700910 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
911 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700912 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -0700913 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 }
916
Elliott Hughes72025e52011-08-23 17:50:30 -0700917 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700918 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
919 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700920 ScopedObjectAccess soa(env);
921 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 }
923
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700925 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
926 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700927 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700928 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 }
930
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700931 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700932 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700934 CHECK_NON_NULL_ARGUMENT(obj);
935 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700936 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700937 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
938 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 va_end(ap);
940 return local_result;
941 }
942
Ian Rogersbc939662013-08-15 10:26:54 -0700943 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
944 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700945 CHECK_NON_NULL_ARGUMENT(obj);
946 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 ScopedObjectAccess soa(env);
948 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
949 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 }
951
Ian Rogersbc939662013-08-15 10:26:54 -0700952 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
953 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700954 CHECK_NON_NULL_ARGUMENT(obj);
955 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700957 JValue result(InvokeWithJValues(soa, obj, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700958 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700959 }
960
Ian Rogersbc939662013-08-15 10:26:54 -0700961 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
962 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700965 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
966 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700967 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700970 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 }
972
Ian Rogersbc939662013-08-15 10:26:54 -0700973 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
974 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700975 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
976 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 ScopedObjectAccess soa(env);
978 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Ian Rogersbc939662013-08-15 10:26:54 -0700981 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
982 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700983 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
984 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700985 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700986 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 }
988
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700989 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700992 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
993 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700994 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700995 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700997 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 }
999
Ian Rogersbc939662013-08-15 10:26:54 -07001000 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1001 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001002 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1003 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 ScopedObjectAccess soa(env);
1005 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Ian Rogersbc939662013-08-15 10:26:54 -07001008 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1009 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001010 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1011 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001012 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001013 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001016 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001019 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1020 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001021 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001024 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Ian Rogersbc939662013-08-15 10:26:54 -07001027 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1028 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001029 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1030 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001031 ScopedObjectAccess soa(env);
1032 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Ian Rogersbc939662013-08-15 10:26:54 -07001035 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1036 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001037 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1038 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001039 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001040 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001043 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001046 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1047 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001048 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001049 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001051 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Ian Rogersbc939662013-08-15 10:26:54 -07001054 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1055 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001056 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1057 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001058 ScopedObjectAccess soa(env);
1059 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Ian Rogersbc939662013-08-15 10:26:54 -07001062 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1063 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001064 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1065 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001066 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001067 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001070 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001073 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1074 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001075 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001078 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Ian Rogersbc939662013-08-15 10:26:54 -07001081 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1082 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001083 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1084 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085 ScopedObjectAccess soa(env);
1086 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Ian Rogersbc939662013-08-15 10:26:54 -07001089 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1090 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001091 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1092 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001094 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001097 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001100 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1101 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001102 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001105 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Ian Rogersbc939662013-08-15 10:26:54 -07001108 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1109 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001110 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1111 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001112 ScopedObjectAccess soa(env);
1113 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Ian Rogersbc939662013-08-15 10:26:54 -07001116 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1117 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001118 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1119 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001120 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001121 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001124 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001127 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1128 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001129 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001130 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001132 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
Ian Rogersbc939662013-08-15 10:26:54 -07001135 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1136 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001137 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1138 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001139 ScopedObjectAccess soa(env);
1140 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 }
1142
Ian Rogersbc939662013-08-15 10:26:54 -07001143 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1144 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001145 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1146 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001147 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001148 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001151 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001154 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1155 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001156 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001157 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001159 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 }
1161
Ian Rogersbc939662013-08-15 10:26:54 -07001162 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1163 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001164 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1165 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001166 ScopedObjectAccess soa(env);
1167 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
Ian Rogersbc939662013-08-15 10:26:54 -07001170 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1171 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001172 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1173 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001174 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001175 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001178 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001181 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1182 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001183 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001184 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 va_end(ap);
1186 }
1187
Brian Carlstromea46f952013-07-30 01:26:50 -07001188 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1189 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001190 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1191 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001192 ScopedObjectAccess soa(env);
1193 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
Ian Rogersbc939662013-08-15 10:26:54 -07001196 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1197 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001198 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1199 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001200 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001201 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
Ian Rogersbc939662013-08-15 10:26:54 -07001204 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001205 CHECK_NON_NULL_ARGUMENT(java_class);
1206 CHECK_NON_NULL_ARGUMENT(name);
1207 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001208 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001209 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001211
Ian Rogersbc939662013-08-15 10:26:54 -07001212 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1213 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001214 CHECK_NON_NULL_ARGUMENT(java_class);
1215 CHECK_NON_NULL_ARGUMENT(name);
1216 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001218 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001220
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001221 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001222 CHECK_NON_NULL_ARGUMENT(obj);
1223 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001224 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001225 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001226 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001227 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001229
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001230 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001231 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001232 ScopedObjectAccess soa(env);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001233 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001234 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 }
1236
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001237 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001238 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1239 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001240 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001241 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1242 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001243 ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001244 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 }
1246
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001247 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001248 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001249 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001250 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001251 ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001252 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 }
1254
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001255#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001256 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1257 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001258 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001259 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001260 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001261 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001262
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001263#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001264 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001265 ScopedObjectAccess soa(env); \
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 (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001268
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001269#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001270 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1271 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001272 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001273 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001274 ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001275 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001276
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001277#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001278 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001279 ScopedObjectAccess soa(env); \
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>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001282
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001283 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001284 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001287 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001288 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
1290
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001291 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001292 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001295 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001296 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
1298
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001299 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001300 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001303 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001304 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 }
1306
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001307 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001308 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001311 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001312 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001315 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001316 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 }
1318
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001319 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001320 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001323 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001324 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001327 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001328 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001331 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001332 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001335 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001336 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001337 }
1338
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001339 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001340 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001341 }
1342
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001343 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001344 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001345 }
1346
1347 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001348 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001349 }
1350
1351 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001352 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001353 }
1354
1355 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001356 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001357 }
1358
1359 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001360 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001361 }
1362
1363 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001364 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001365 }
1366
1367 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001368 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001369 }
1370
1371 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001372 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001373 }
1374
1375 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001376 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001377 }
1378
1379 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001380 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381 }
1382
1383 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001384 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001385 }
1386
1387 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001388 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389 }
1390
1391 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001392 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001393 }
1394
1395 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001396 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001397 }
1398
1399 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001400 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001401 }
1402
1403 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001404 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001405 }
1406
1407 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001408 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 }
1410
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001411 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001413 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001414 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001415 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001416 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001417 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 va_end(ap);
1419 return local_result;
1420 }
1421
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001422 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001423 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001424 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001425 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001426 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 }
1428
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001429 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001430 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001431 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001432 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001433 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001436 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001438 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001439 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001440 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001441 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001443 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001446 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001447 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001448 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001449 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001452 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* 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 InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001458 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001460 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001461 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001462 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001463 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001465 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001468 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001469 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001470 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001471 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001474 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* 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 InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001480 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001482 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001483 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001484 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001485 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001487 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 }
1489
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001490 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001491 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001492 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001493 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001496 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* 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 InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001502 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001504 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001505 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001506 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001507 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001509 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
1511
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001512 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001513 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001514 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001515 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001518 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* 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 InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001524 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001526 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001527 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001528 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001529 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001531 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
1533
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001534 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001535 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001536 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001537 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001540 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* 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 InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001546 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001548 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001549 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001550 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001551 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001553 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001556 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001557 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001558 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001559 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 }
1561
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001562 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* 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 InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001568 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001570 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001571 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001572 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001573 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001575 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 }
1577
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001578 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001579 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001580 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001581 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001584 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* 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 InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 }
1589
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001590 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001592 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001593 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001594 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001595 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001597 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001600 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001601 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001602 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001603 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 }
1605
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001606 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* 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 InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001612 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001614 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001615 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001616 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001617 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 va_end(ap);
1619 }
1620
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001621 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001622 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001623 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001624 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 }
1626
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001627 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* 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 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 }
1632
Elliott Hughes814e4032011-08-23 12:07:56 -07001633 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001634 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001635 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001636 return nullptr;
1637 }
1638 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001639 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001640 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001641 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001642 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001643 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001644 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 }
1646
1647 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001648 if (utf == nullptr) {
1649 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001651 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001652 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001653 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 }
1655
Elliott Hughes814e4032011-08-23 12:07:56 -07001656 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001657 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001658 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001659 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001660 }
1661
1662 static jsize GetStringUTFLength(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)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001666 }
1667
Ian Rogersbc939662013-08-15 10:26:54 -07001668 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1669 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001670 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001671 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001672 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001673 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001674 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001675 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001676 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Jeff Hao848f70a2014-01-15 13:49:50 -08001677 const jchar* chars = s->GetValue();
Elliott Hughesb465ab02011-08-24 11:21:21 -07001678 memcpy(buf, chars + start, length * sizeof(jchar));
1679 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001680 }
1681
Ian Rogersbc939662013-08-15 10:26:54 -07001682 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1683 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001684 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001685 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001686 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001687 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001688 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001689 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001690 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Jeff Hao848f70a2014-01-15 13:49:50 -08001691 const jchar* chars = s->GetValue();
Elliott Hughesb465ab02011-08-24 11:21:21 -07001692 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1693 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001694 }
1695
Elliott Hughes75770752011-08-24 17:52:38 -07001696 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001697 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001698 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001699 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001700 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Hao848f70a2014-01-15 13:49:50 -08001701 if (heap->IsMovableObject(s)) {
1702 jchar* chars = new jchar[s->GetLength()];
1703 memcpy(chars, s->GetValue(), sizeof(jchar) * s->GetLength());
Fred Shih56890e22014-06-02 11:11:52 -07001704 if (is_copy != nullptr) {
1705 *is_copy = JNI_TRUE;
1706 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001707 return chars;
Elliott Hughes75770752011-08-24 17:52:38 -07001708 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001709 if (is_copy != nullptr) {
1710 *is_copy = JNI_FALSE;
1711 }
1712 return static_cast<jchar*>(s->GetValue());
Elliott Hughes814e4032011-08-23 12:07:56 -07001713 }
1714
Mathieu Chartier590fee92013-09-13 13:46:47 -07001715 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001716 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001717 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001718 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08001719 if (chars != s->GetValue()) {
Fred Shih56890e22014-06-02 11:11:52 -07001720 delete[] chars;
1721 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 }
1723
Elliott Hughes75770752011-08-24 17:52:38 -07001724 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001725 CHECK_NON_NULL_ARGUMENT(java_string);
1726 ScopedObjectAccess soa(env);
1727 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001728 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Hao848f70a2014-01-15 13:49:50 -08001729 if (heap->IsMovableObject(s)) {
Fred Shih56890e22014-06-02 11:11:52 -07001730 StackHandleScope<1> hs(soa.Self());
Jeff Hao848f70a2014-01-15 13:49:50 -08001731 HandleWrapper<mirror::String> h(hs.NewHandleWrapper(&s));
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07001732 if (!kUseReadBarrier) {
1733 heap->IncrementDisableMovingGC(soa.Self());
1734 } else {
1735 // For the CC collector, we only need to wait for the thread flip rather than the whole GC
1736 // to occur thanks to the to-space invariant.
1737 heap->IncrementDisableThreadFlip(soa.Self());
1738 }
Fred Shih56890e22014-06-02 11:11:52 -07001739 }
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
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001746 static void ReleaseStringCritical(JNIEnv* env,
1747 jstring java_string,
1748 const jchar* chars ATTRIBUTE_UNUSED) {
Fred Shih56890e22014-06-02 11:11:52 -07001749 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1750 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001751 gc::Heap* heap = Runtime::Current()->GetHeap();
1752 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08001753 if (heap->IsMovableObject(s)) {
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07001754 if (!kUseReadBarrier) {
1755 heap->DecrementDisableMovingGC(soa.Self());
1756 } else {
1757 heap->DecrementDisableThreadFlip(soa.Self());
1758 }
Fred Shih56890e22014-06-02 11:11:52 -07001759 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes75770752011-08-24 17:52:38 -07001762 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001763 if (java_string == nullptr) {
1764 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001765 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001766 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001767 *is_copy = JNI_TRUE;
1768 }
Ian Rogersef28b142012-11-30 14:22:18 -08001769 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001770 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001771 size_t byte_count = s->GetUtfLength();
1772 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001773 CHECK(bytes != nullptr); // bionic aborts anyway.
Jeff Hao848f70a2014-01-15 13:49:50 -08001774 const uint16_t* chars = s->GetValue();
Elliott Hughes75770752011-08-24 17:52:38 -07001775 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1776 bytes[byte_count] = '\0';
1777 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001778 }
1779
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001780 static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001781 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001782 }
1783
Elliott Hughesbd935992011-08-22 11:59:34 -07001784 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001785 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001786 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001787 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001788 if (UNLIKELY(!obj->IsArrayInstance())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001789 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1790 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001791 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001792 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001793 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes814e4032011-08-23 12:07:56 -07001796 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001797 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001798 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001799 mirror::ObjectArray<mirror::Object>* array =
1800 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 }
1803
Ian Rogersbc939662013-08-15 10:26:54 -07001804 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1805 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001806 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001807 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001808 mirror::ObjectArray<mirror::Object>* array =
1809 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1810 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001811 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
1814 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001815 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
1818 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001819 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
1822 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001823 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
1826 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001827 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
1830 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001831 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
1834 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001835 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
1838 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001839 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 }
1841
Ian Rogers1d99e452014-01-02 17:36:41 -08001842 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1843 jobject initial_element) {
1844 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001845 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001846 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001847 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001848 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001849
1850 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001851 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001852 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08001853 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001854 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08001855 if (UNLIKELY(element_class->IsPrimitive())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001856 soa.Vm()->JniAbortF("NewObjectArray", "not an object type: %s",
1857 PrettyDescriptor(element_class).c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08001858 return nullptr;
1859 }
Ian Rogers1d99e452014-01-02 17:36:41 -08001860 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07001861 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08001862 if (UNLIKELY(array_class == nullptr)) {
1863 return nullptr;
1864 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
Elliott Hughes75770752011-08-24 17:52:38 -07001867 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001868 mirror::ObjectArray<mirror::Object>* result =
1869 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001870 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001871 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08001872 if (initial_object != nullptr) {
1873 mirror::Class* element_class = result->GetClass()->GetComponentType();
1874 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001875 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
1876 "element type of '%s'",
1877 PrettyDescriptor(initial_object->GetClass()).c_str(),
1878 PrettyDescriptor(element_class).c_str());
1879 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08001880 } else {
1881 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001882 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08001883 }
1884 }
Elliott Hughes75770752011-08-24 17:52:38 -07001885 }
1886 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001887 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 }
1889
1890 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001891 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 }
1893
Ian Rogersa15e67d2012-02-28 13:51:55 -08001894 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001895 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001896 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001897 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07001898 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001899 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
1900 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001901 return nullptr;
1902 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001903 gc::Heap* heap = Runtime::Current()->GetHeap();
1904 if (heap->IsMovableObject(array)) {
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07001905 if (!kUseReadBarrier) {
1906 heap->IncrementDisableMovingGC(soa.Self());
1907 } else {
1908 // For the CC collector, we only need to wait for the thread flip rather than the whole GC
1909 // to occur thanks to the to-space invariant.
1910 heap->IncrementDisableThreadFlip(soa.Self());
1911 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001912 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001913 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001914 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001915 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001916 *is_copy = JNI_FALSE;
1917 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001918 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001919 }
1920
Ian Rogers2d10b202014-05-12 19:15:18 -07001921 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
1922 jint mode) {
1923 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
1924 ScopedObjectAccess soa(env);
1925 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
1926 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001927 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
1928 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001929 return;
1930 }
1931 const size_t component_size = array->GetClass()->GetComponentSize();
1932 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001933 }
1934
Elliott Hughes75770752011-08-24 17:52:38 -07001935 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001936 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001937 }
1938
Elliott Hughes75770752011-08-24 17:52:38 -07001939 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001940 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001944 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes75770752011-08-24 17:52:38 -07001947 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001948 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
Elliott Hughes75770752011-08-24 17:52:38 -07001951 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001952 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Elliott Hughes75770752011-08-24 17:52:38 -07001955 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001956 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 }
1958
Elliott Hughes75770752011-08-24 17:52:38 -07001959 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001960 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 }
1962
Elliott Hughes75770752011-08-24 17:52:38 -07001963 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001964 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 }
1966
Mathieu Chartier590fee92013-09-13 13:46:47 -07001967 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
1968 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001969 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
1970 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Mathieu Chartier590fee92013-09-13 13:46:47 -07001973 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001974 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
Mathieu Chartier590fee92013-09-13 13:46:47 -07001977 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001978 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Mathieu Chartier590fee92013-09-13 13:46:47 -07001981 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
1982 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001983 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Mathieu Chartier590fee92013-09-13 13:46:47 -07001986 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
1987 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001988 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Mathieu Chartier590fee92013-09-13 13:46:47 -07001991 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001992 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 }
1994
Mathieu Chartier590fee92013-09-13 13:46:47 -07001995 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001996 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Mathieu Chartier590fee92013-09-13 13:46:47 -07001999 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2000 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002001 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Ian Rogersbc939662013-08-15 10:26:54 -07002004 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2005 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002006 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002007 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Ian Rogersbc939662013-08-15 10:26:54 -07002010 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2011 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002012 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Ian Rogersbc939662013-08-15 10:26:54 -07002015 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2016 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002017 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Ian Rogersbc939662013-08-15 10:26:54 -07002020 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2021 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002022 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002023 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Ian Rogersbc939662013-08-15 10:26:54 -07002026 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2027 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002028 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002029 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Ian Rogersbc939662013-08-15 10:26:54 -07002032 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2033 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002034 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Ian Rogersbc939662013-08-15 10:26:54 -07002037 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2038 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002039 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Ian Rogersbc939662013-08-15 10:26:54 -07002042 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2043 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002044 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002045 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Ian Rogersbc939662013-08-15 10:26:54 -07002048 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2049 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002050 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002051 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Ian Rogersbc939662013-08-15 10:26:54 -07002054 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2055 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002056 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Ian Rogersbc939662013-08-15 10:26:54 -07002059 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2060 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002061 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Ian Rogersbc939662013-08-15 10:26:54 -07002064 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2065 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002066 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002067 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Ian Rogersbc939662013-08-15 10:26:54 -07002070 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2071 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002072 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002073 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Ian Rogersbc939662013-08-15 10:26:54 -07002076 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2077 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002078 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Ian Rogersbc939662013-08-15 10:26:54 -07002081 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2082 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002083 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Ian Rogersbc939662013-08-15 10:26:54 -07002086 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2087 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002088 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002089 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
Ian Rogersbc939662013-08-15 10:26:54 -07002092 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2093 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002094 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2095 }
2096
Ian Rogersbc939662013-08-15 10:26:54 -07002097 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2098 jint method_count, bool return_errors) {
2099 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002100 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2101 method_count);
2102 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002103 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002104 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002105 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002106 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002107 if (UNLIKELY(method_count == 0)) {
2108 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2109 << PrettyDescriptor(c);
2110 return JNI_OK;
2111 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002112 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002113 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 const char* name = methods[i].name;
2115 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002116 const void* fnPtr = methods[i].fnPtr;
2117 if (UNLIKELY(name == nullptr)) {
2118 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2119 return JNI_ERR;
2120 } else if (UNLIKELY(sig == nullptr)) {
2121 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2122 return JNI_ERR;
2123 } else if (UNLIKELY(fnPtr == nullptr)) {
2124 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2125 return JNI_ERR;
2126 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002127 bool is_fast = false;
Hiroshi Yamauchi36bce582015-05-12 12:16:10 -07002128 // Notes about fast JNI calls:
2129 //
2130 // On a normal JNI call, the calling thread usually transitions
2131 // from the kRunnable state to the kNative state. But if the
2132 // called native function needs to access any Java object, it
2133 // will have to transition back to the kRunnable state.
2134 //
2135 // There is a cost to this double transition. For a JNI call
2136 // that should be quick, this cost may dominate the call cost.
2137 //
2138 // On a fast JNI call, the calling thread avoids this double
2139 // transition by not transitioning from kRunnable to kNative and
2140 // stays in the kRunnable state.
2141 //
2142 // There are risks to using a fast JNI call because it can delay
2143 // a response to a thread suspension request which is typically
2144 // used for a GC root scanning, etc. If a fast JNI call takes a
2145 // long time, it could cause longer thread suspension latency
2146 // and GC pauses.
2147 //
2148 // Thus, fast JNI should be used with care. It should be used
2149 // for a JNI call that takes a short amount of time (eg. no
2150 // long-running loop) and does not block (eg. no locks, I/O,
2151 // etc.)
2152 //
2153 // A '!' prefix in the signature in the JNINativeMethod
2154 // indicates that it's a fast JNI call and the runtime omits the
2155 // thread state transition from kRunnable to kNative at the
2156 // entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002158 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 ++sig;
2160 }
2161
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002162 // Note: the right order is to try to find the method locally
2163 // first, either as a direct or a virtual method. Then move to
2164 // the parent.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002165 ArtMethod* m = nullptr;
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002166 bool warn_on_going_to_parent = down_cast<JNIEnvExt*>(env)->vm->IsCheckJniEnabled();
2167 for (mirror::Class* current_class = c;
2168 current_class != nullptr;
2169 current_class = current_class->GetSuperClass()) {
2170 // Search first only comparing methods which are native.
2171 m = FindMethod<true>(current_class, name, sig);
2172 if (m != nullptr) {
2173 break;
2174 }
2175
2176 // Search again comparing to all methods, to find non-native methods that match.
2177 m = FindMethod<false>(current_class, name, sig);
2178 if (m != nullptr) {
2179 break;
2180 }
2181
2182 if (warn_on_going_to_parent) {
2183 LOG(WARNING) << "CheckJNI: method to register \"" << name << "\" not in the given class. "
2184 << "This is slow, consider changing your RegisterNatives calls.";
2185 warn_on_going_to_parent = false;
2186 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 }
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002188
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002189 if (m == nullptr) {
Mathieu Chartier5c02d6c2015-05-04 10:18:24 -07002190 LOG(return_errors ? ERROR : INTERNAL_FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002191 << PrettyDescriptor(c) << "." << name << sig << " in "
2192 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Mathieu Chartier5c02d6c2015-05-04 10:18:24 -07002193 // Safe to pass in LOG(FATAL) since the log object aborts in destructor and only goes
2194 // out of scope after the DumpClass is done executing.
2195 c->DumpClass(LOG(return_errors ? ERROR : FATAL), mirror::Class::kDumpClassFullDetail);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002196 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002198 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002199 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002200 << PrettyDescriptor(c) << "." << name << sig
2201 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002202 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 return JNI_ERR;
2204 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002205
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002206 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002207
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002208 m->RegisterNative(fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210 return JNI_OK;
2211 }
2212
Elliott Hughes5174fe62011-08-23 15:12:35 -07002213 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002214 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002215 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002216 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002217
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002218 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002219
Ian Rogers2d10b202014-05-12 19:15:18 -07002220 size_t unregistered_count = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002221 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2222 for (auto& m : c->GetDirectMethods(pointer_size)) {
2223 if (m.IsNative()) {
2224 m.UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002225 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002226 }
2227 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002228 for (auto& m : c->GetVirtualMethods(pointer_size)) {
2229 if (m.IsNative()) {
2230 m.UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002231 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002232 }
2233 }
2234
Ian Rogers2d10b202014-05-12 19:15:18 -07002235 if (unregistered_count == 0) {
2236 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2237 << PrettyDescriptor(c) << "' that contains no native methods";
2238 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002239 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002240 }
2241
Ian Rogers719d1a32014-03-06 12:13:39 -08002242 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002243 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002244 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002245 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2246 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002247 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002248 return JNI_ERR;
2249 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002250 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002251 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002252 }
2253
Ian Rogers719d1a32014-03-06 12:13:39 -08002254 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002255 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002256 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002257 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002258 o->MonitorExit(soa.Self());
2259 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002260 return JNI_ERR;
2261 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002262 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002263 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002264 }
2265
2266 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002267 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002269 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002270 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002272 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002273 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002274 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 }
2276
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002278 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002279 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2280 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002281 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002282 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002283 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002284 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2285 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002286 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002287 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002288
Brian Carlstrom85a93362014-06-25 09:30:52 -07002289 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002290 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002291 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2292 "buffer capacity greater than maximum jint: %" PRId64,
2293 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002294 return nullptr;
2295 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002296 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002297 jint capacity_arg = static_cast<jint>(capacity);
2298
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002299 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2300 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002301 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002302 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002303 }
2304
Elliott Hughesb465ab02011-08-24 11:21:21 -07002305 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002306 return reinterpret_cast<void*>(env->GetLongField(
2307 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 }
2309
Elliott Hughesb465ab02011-08-24 11:21:21 -07002310 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002311 return static_cast<jlong>(env->GetIntField(
2312 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002313 }
2314
Andreas Gampea8763072014-12-20 00:08:35 -08002315 static jobjectRefType GetObjectRefType(JNIEnv* env ATTRIBUTE_UNUSED, jobject java_object) {
2316 if (java_object == nullptr) {
2317 return JNIInvalidRefType;
2318 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002319
2320 // Do we definitely know what kind of reference this is?
2321 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2322 IndirectRefKind kind = GetIndirectRefKind(ref);
2323 switch (kind) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002324 case kLocal:
2325 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002326 case kGlobal:
2327 return JNIGlobalRefType;
2328 case kWeakGlobal:
2329 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002330 case kHandleScopeOrInvalid:
Ian Rogersc0542af2014-09-03 16:16:56 -07002331 // Assume value is in a handle scope.
2332 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002333 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002334 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
Andreas Gampea8763072014-12-20 00:08:35 -08002335 UNREACHABLE();
Elliott Hughescdf53122011-08-19 15:46:09 -07002336 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002337
2338 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002339 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2340 const char* caller)
Mathieu Chartier90443472015-07-16 20:32:27 -07002341 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002342 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002343 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002344 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2345 return JNI_ERR;
2346 }
2347 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002348 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002349 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2350 if (!okay) {
2351 soa.Self()->ThrowOutOfMemoryError(caller);
2352 }
2353 return okay ? JNI_OK : JNI_ERR;
2354 }
2355
2356 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002357 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002358 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002359 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002360 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002361 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002362 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002363 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002364 return soa.AddLocalReference<JniT>(result);
2365 }
2366
Ian Rogers2d10b202014-05-12 19:15:18 -07002367 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2368 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2369 const char* fn_name, const char* operation)
Mathieu Chartier90443472015-07-16 20:32:27 -07002370 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002371 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002372 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002373 soa.Vm()->JniAbortF(fn_name,
2374 "attempt to %s %s primitive array elements with an object of type %s",
2375 operation,
2376 PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2377 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002378 return nullptr;
2379 }
2380 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2381 return array;
2382 }
2383
2384 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2385 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2386 CHECK_NON_NULL_ARGUMENT(java_array);
2387 ScopedObjectAccess soa(env);
2388 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2389 "GetArrayElements",
2390 "get");
2391 if (UNLIKELY(array == nullptr)) {
2392 return nullptr;
2393 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002394 // Only make a copy if necessary.
2395 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2396 if (is_copy != nullptr) {
2397 *is_copy = JNI_TRUE;
2398 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002399 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002400 size_t size = array->GetLength() * component_size;
2401 void* data = new uint64_t[RoundUp(size, 8) / 8];
2402 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002403 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002404 } else {
2405 if (is_copy != nullptr) {
2406 *is_copy = JNI_FALSE;
2407 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002408 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002409 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002410 }
2411
Ian Rogers2d10b202014-05-12 19:15:18 -07002412 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002413 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002414 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002415 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002416 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2417 "ReleaseArrayElements",
2418 "release");
2419 if (array == nullptr) {
2420 return;
2421 }
2422 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2423 }
2424
2425 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2426 size_t component_size, void* elements, jint mode)
Mathieu Chartier90443472015-07-16 20:32:27 -07002427 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002428 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002429 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002430 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002431 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002432 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2433 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002434 if (is_copy) {
2435 // Sanity check: If elements is not the same as the java array's data, it better not be a
2436 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2437 // copies we make?
2438 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002439 soa.Vm()->JniAbortF("ReleaseArrayElements",
2440 "invalid element pointer %p, array elements are %p",
2441 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002442 return;
2443 }
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002444 if (mode != JNI_ABORT) {
2445 memcpy(array_data, elements, bytes);
2446 } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2447 // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2448 LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2449 soa.Self()->DumpJavaStack(LOG(WARNING));
2450 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002451 }
2452 if (mode != JNI_COMMIT) {
2453 if (is_copy) {
2454 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002455 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002456 // Non copy to a movable object must means that we had disabled the moving GC.
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07002457 if (!kUseReadBarrier) {
2458 heap->DecrementDisableMovingGC(soa.Self());
2459 } else {
2460 heap->DecrementDisableThreadFlip(soa.Self());
2461 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002462 }
2463 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002464 }
2465
Ian Rogers2d10b202014-05-12 19:15:18 -07002466 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2467 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2468 jsize start, jsize length, ElementT* buf) {
2469 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2470 ScopedObjectAccess soa(env);
2471 ArtArrayT* array =
2472 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2473 "GetPrimitiveArrayRegion",
2474 "get region of");
2475 if (array != nullptr) {
2476 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2477 ThrowAIOOBE(soa, array, start, length, "src");
2478 } else {
2479 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2480 ElementT* data = array->GetData();
2481 memcpy(buf, data + start, length * sizeof(ElementT));
2482 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002483 }
2484 }
2485
Ian Rogers2d10b202014-05-12 19:15:18 -07002486 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2487 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2488 jsize start, jsize length, const ElementT* buf) {
2489 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2490 ScopedObjectAccess soa(env);
2491 ArtArrayT* array =
2492 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2493 "SetPrimitiveArrayRegion",
2494 "set region of");
2495 if (array != nullptr) {
2496 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2497 ThrowAIOOBE(soa, array, start, length, "dst");
2498 } else {
2499 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2500 ElementT* data = array->GetData();
2501 memcpy(data + start, buf, length * sizeof(ElementT));
2502 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002503 }
2504 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002505};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002506
Elliott Hughes88c5c352012-03-15 18:49:48 -07002507const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002508 nullptr, // reserved0.
2509 nullptr, // reserved1.
2510 nullptr, // reserved2.
2511 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002512 JNI::GetVersion,
2513 JNI::DefineClass,
2514 JNI::FindClass,
2515 JNI::FromReflectedMethod,
2516 JNI::FromReflectedField,
2517 JNI::ToReflectedMethod,
2518 JNI::GetSuperclass,
2519 JNI::IsAssignableFrom,
2520 JNI::ToReflectedField,
2521 JNI::Throw,
2522 JNI::ThrowNew,
2523 JNI::ExceptionOccurred,
2524 JNI::ExceptionDescribe,
2525 JNI::ExceptionClear,
2526 JNI::FatalError,
2527 JNI::PushLocalFrame,
2528 JNI::PopLocalFrame,
2529 JNI::NewGlobalRef,
2530 JNI::DeleteGlobalRef,
2531 JNI::DeleteLocalRef,
2532 JNI::IsSameObject,
2533 JNI::NewLocalRef,
2534 JNI::EnsureLocalCapacity,
2535 JNI::AllocObject,
2536 JNI::NewObject,
2537 JNI::NewObjectV,
2538 JNI::NewObjectA,
2539 JNI::GetObjectClass,
2540 JNI::IsInstanceOf,
2541 JNI::GetMethodID,
2542 JNI::CallObjectMethod,
2543 JNI::CallObjectMethodV,
2544 JNI::CallObjectMethodA,
2545 JNI::CallBooleanMethod,
2546 JNI::CallBooleanMethodV,
2547 JNI::CallBooleanMethodA,
2548 JNI::CallByteMethod,
2549 JNI::CallByteMethodV,
2550 JNI::CallByteMethodA,
2551 JNI::CallCharMethod,
2552 JNI::CallCharMethodV,
2553 JNI::CallCharMethodA,
2554 JNI::CallShortMethod,
2555 JNI::CallShortMethodV,
2556 JNI::CallShortMethodA,
2557 JNI::CallIntMethod,
2558 JNI::CallIntMethodV,
2559 JNI::CallIntMethodA,
2560 JNI::CallLongMethod,
2561 JNI::CallLongMethodV,
2562 JNI::CallLongMethodA,
2563 JNI::CallFloatMethod,
2564 JNI::CallFloatMethodV,
2565 JNI::CallFloatMethodA,
2566 JNI::CallDoubleMethod,
2567 JNI::CallDoubleMethodV,
2568 JNI::CallDoubleMethodA,
2569 JNI::CallVoidMethod,
2570 JNI::CallVoidMethodV,
2571 JNI::CallVoidMethodA,
2572 JNI::CallNonvirtualObjectMethod,
2573 JNI::CallNonvirtualObjectMethodV,
2574 JNI::CallNonvirtualObjectMethodA,
2575 JNI::CallNonvirtualBooleanMethod,
2576 JNI::CallNonvirtualBooleanMethodV,
2577 JNI::CallNonvirtualBooleanMethodA,
2578 JNI::CallNonvirtualByteMethod,
2579 JNI::CallNonvirtualByteMethodV,
2580 JNI::CallNonvirtualByteMethodA,
2581 JNI::CallNonvirtualCharMethod,
2582 JNI::CallNonvirtualCharMethodV,
2583 JNI::CallNonvirtualCharMethodA,
2584 JNI::CallNonvirtualShortMethod,
2585 JNI::CallNonvirtualShortMethodV,
2586 JNI::CallNonvirtualShortMethodA,
2587 JNI::CallNonvirtualIntMethod,
2588 JNI::CallNonvirtualIntMethodV,
2589 JNI::CallNonvirtualIntMethodA,
2590 JNI::CallNonvirtualLongMethod,
2591 JNI::CallNonvirtualLongMethodV,
2592 JNI::CallNonvirtualLongMethodA,
2593 JNI::CallNonvirtualFloatMethod,
2594 JNI::CallNonvirtualFloatMethodV,
2595 JNI::CallNonvirtualFloatMethodA,
2596 JNI::CallNonvirtualDoubleMethod,
2597 JNI::CallNonvirtualDoubleMethodV,
2598 JNI::CallNonvirtualDoubleMethodA,
2599 JNI::CallNonvirtualVoidMethod,
2600 JNI::CallNonvirtualVoidMethodV,
2601 JNI::CallNonvirtualVoidMethodA,
2602 JNI::GetFieldID,
2603 JNI::GetObjectField,
2604 JNI::GetBooleanField,
2605 JNI::GetByteField,
2606 JNI::GetCharField,
2607 JNI::GetShortField,
2608 JNI::GetIntField,
2609 JNI::GetLongField,
2610 JNI::GetFloatField,
2611 JNI::GetDoubleField,
2612 JNI::SetObjectField,
2613 JNI::SetBooleanField,
2614 JNI::SetByteField,
2615 JNI::SetCharField,
2616 JNI::SetShortField,
2617 JNI::SetIntField,
2618 JNI::SetLongField,
2619 JNI::SetFloatField,
2620 JNI::SetDoubleField,
2621 JNI::GetStaticMethodID,
2622 JNI::CallStaticObjectMethod,
2623 JNI::CallStaticObjectMethodV,
2624 JNI::CallStaticObjectMethodA,
2625 JNI::CallStaticBooleanMethod,
2626 JNI::CallStaticBooleanMethodV,
2627 JNI::CallStaticBooleanMethodA,
2628 JNI::CallStaticByteMethod,
2629 JNI::CallStaticByteMethodV,
2630 JNI::CallStaticByteMethodA,
2631 JNI::CallStaticCharMethod,
2632 JNI::CallStaticCharMethodV,
2633 JNI::CallStaticCharMethodA,
2634 JNI::CallStaticShortMethod,
2635 JNI::CallStaticShortMethodV,
2636 JNI::CallStaticShortMethodA,
2637 JNI::CallStaticIntMethod,
2638 JNI::CallStaticIntMethodV,
2639 JNI::CallStaticIntMethodA,
2640 JNI::CallStaticLongMethod,
2641 JNI::CallStaticLongMethodV,
2642 JNI::CallStaticLongMethodA,
2643 JNI::CallStaticFloatMethod,
2644 JNI::CallStaticFloatMethodV,
2645 JNI::CallStaticFloatMethodA,
2646 JNI::CallStaticDoubleMethod,
2647 JNI::CallStaticDoubleMethodV,
2648 JNI::CallStaticDoubleMethodA,
2649 JNI::CallStaticVoidMethod,
2650 JNI::CallStaticVoidMethodV,
2651 JNI::CallStaticVoidMethodA,
2652 JNI::GetStaticFieldID,
2653 JNI::GetStaticObjectField,
2654 JNI::GetStaticBooleanField,
2655 JNI::GetStaticByteField,
2656 JNI::GetStaticCharField,
2657 JNI::GetStaticShortField,
2658 JNI::GetStaticIntField,
2659 JNI::GetStaticLongField,
2660 JNI::GetStaticFloatField,
2661 JNI::GetStaticDoubleField,
2662 JNI::SetStaticObjectField,
2663 JNI::SetStaticBooleanField,
2664 JNI::SetStaticByteField,
2665 JNI::SetStaticCharField,
2666 JNI::SetStaticShortField,
2667 JNI::SetStaticIntField,
2668 JNI::SetStaticLongField,
2669 JNI::SetStaticFloatField,
2670 JNI::SetStaticDoubleField,
2671 JNI::NewString,
2672 JNI::GetStringLength,
2673 JNI::GetStringChars,
2674 JNI::ReleaseStringChars,
2675 JNI::NewStringUTF,
2676 JNI::GetStringUTFLength,
2677 JNI::GetStringUTFChars,
2678 JNI::ReleaseStringUTFChars,
2679 JNI::GetArrayLength,
2680 JNI::NewObjectArray,
2681 JNI::GetObjectArrayElement,
2682 JNI::SetObjectArrayElement,
2683 JNI::NewBooleanArray,
2684 JNI::NewByteArray,
2685 JNI::NewCharArray,
2686 JNI::NewShortArray,
2687 JNI::NewIntArray,
2688 JNI::NewLongArray,
2689 JNI::NewFloatArray,
2690 JNI::NewDoubleArray,
2691 JNI::GetBooleanArrayElements,
2692 JNI::GetByteArrayElements,
2693 JNI::GetCharArrayElements,
2694 JNI::GetShortArrayElements,
2695 JNI::GetIntArrayElements,
2696 JNI::GetLongArrayElements,
2697 JNI::GetFloatArrayElements,
2698 JNI::GetDoubleArrayElements,
2699 JNI::ReleaseBooleanArrayElements,
2700 JNI::ReleaseByteArrayElements,
2701 JNI::ReleaseCharArrayElements,
2702 JNI::ReleaseShortArrayElements,
2703 JNI::ReleaseIntArrayElements,
2704 JNI::ReleaseLongArrayElements,
2705 JNI::ReleaseFloatArrayElements,
2706 JNI::ReleaseDoubleArrayElements,
2707 JNI::GetBooleanArrayRegion,
2708 JNI::GetByteArrayRegion,
2709 JNI::GetCharArrayRegion,
2710 JNI::GetShortArrayRegion,
2711 JNI::GetIntArrayRegion,
2712 JNI::GetLongArrayRegion,
2713 JNI::GetFloatArrayRegion,
2714 JNI::GetDoubleArrayRegion,
2715 JNI::SetBooleanArrayRegion,
2716 JNI::SetByteArrayRegion,
2717 JNI::SetCharArrayRegion,
2718 JNI::SetShortArrayRegion,
2719 JNI::SetIntArrayRegion,
2720 JNI::SetLongArrayRegion,
2721 JNI::SetFloatArrayRegion,
2722 JNI::SetDoubleArrayRegion,
2723 JNI::RegisterNatives,
2724 JNI::UnregisterNatives,
2725 JNI::MonitorEnter,
2726 JNI::MonitorExit,
2727 JNI::GetJavaVM,
2728 JNI::GetStringRegion,
2729 JNI::GetStringUTFRegion,
2730 JNI::GetPrimitiveArrayCritical,
2731 JNI::ReleasePrimitiveArrayCritical,
2732 JNI::GetStringCritical,
2733 JNI::ReleaseStringCritical,
2734 JNI::NewWeakGlobalRef,
2735 JNI::DeleteWeakGlobalRef,
2736 JNI::ExceptionCheck,
2737 JNI::NewDirectByteBuffer,
2738 JNI::GetDirectBufferAddress,
2739 JNI::GetDirectBufferCapacity,
2740 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002741};
2742
Ian Rogers68d8b422014-07-17 11:09:10 -07002743const JNINativeInterface* GetJniNativeInterface() {
2744 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002745}
2746
Elliott Hughesc8fece32013-01-02 11:27:23 -08002747void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002748 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002749 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002750 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002751 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2752 }
2753 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2754}
2755
Ian Rogersdf20fe02011-07-20 20:34:16 -07002756} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002757
2758std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2759 switch (rhs) {
2760 case JNIInvalidRefType:
2761 os << "JNIInvalidRefType";
2762 return os;
2763 case JNILocalRefType:
2764 os << "JNILocalRefType";
2765 return os;
2766 case JNIGlobalRefType:
2767 os << "JNIGlobalRefType";
2768 return os;
2769 case JNIWeakGlobalRefType:
2770 os << "JNIWeakGlobalRefType";
2771 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002772 default:
Ian Rogersc7dd2952014-10-21 23:31:19 -07002773 LOG(::art::FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
2774 UNREACHABLE();
Elliott Hughesb465ab02011-08-24 11:21:21 -07002775 }
2776}