blob: ca45f7652b46a00ddb058e47890eda13924228cd [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
Elliott Hughes0af55432011-08-17 18:37:28 -070023#include <utility>
24#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025
Mathieu Chartierc7853442015-03-27 14:35:38 -070026#include "art_field-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080027#include "atomic.h"
Mathieu Chartierbad02672014-08-25 13:08:22 -070028#include "base/allocator.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080029#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080030#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080031#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080032#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070033#include "dex_file-inl.h"
Mathieu Chartierd0004802014-10-15 16:59:47 -070034#include "fault_handler.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070035#include "gc_root.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036#include "gc/accounting/card_table-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070037#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070038#include "interpreter/interpreter.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070039#include "jni_env_ext.h"
40#include "java_vm_ext.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070041#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/class-inl.h"
43#include "mirror/class_loader.h"
Hiroshi Yamauchi02d2f292015-04-03 13:35:16 -070044#include "mirror/field-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070045#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070048#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "mirror/throwable.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080050#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070051#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070052#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070053#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070055#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070056#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070058#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070059
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070060namespace art {
61
Mathieu Chartier24555ad2014-10-06 13:41:33 -070062// Consider turning this on when there is errors which could be related to JNI array copies such as
63// things not rendering correctly. E.g. b/16858794
64static constexpr bool kWarnJniAbort = false;
65
Elliott Hughes6b436852011-08-12 10:16:44 -070066// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
67// separated with slashes but aren't wrapped with "L;" like regular descriptors
68// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
69// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
70// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070071static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070072 std::string result;
73 // Add the missing "L;" if necessary.
74 if (name[0] == '[') {
75 result = name;
76 } else {
77 result += 'L';
78 result += name;
79 result += ';';
80 }
81 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070082 if (result.find('.') != std::string::npos) {
83 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
84 << "\"" << name << "\"";
85 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -070086 }
87 return result;
88}
89
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080090static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -070092 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -070093 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000094 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Ian Rogers62d6c772013-02-27 08:32:07 -080095 "no %s method \"%s.%s%s\"",
Ian Rogers1ff3c982014-08-12 02:30:58 -070096 kind, c->GetDescriptor(&temp), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -070097}
98
Sebastien Hertzfa65e842014-07-03 09:39:53 +020099static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa, mirror::Class* c,
100 const char* kind, jint idx, bool return_errors)
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
102 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method in "
103 << PrettyDescriptor(c) << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
104 << ": " << kind << " is null at index " << idx;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000105 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200106 "%s is null at index %d", kind, idx);
107}
108
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800109static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
111 if (LIKELY(klass->IsInitialized())) {
112 return klass;
113 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700114 StackHandleScope<1> hs(self);
115 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700116 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800117 return nullptr;
118 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800120}
121
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
123 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800125 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800126 if (c == nullptr) {
127 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700128 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800129 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700130 if (is_static) {
131 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700132 } else if (c->IsInterface()) {
133 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700134 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700135 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800136 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700137 // No virtual method matching the signature. Search declared
138 // private methods and constructors.
139 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700140 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700141 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800142 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800144 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700145 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700147}
148
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800149static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800151 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700152 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
153 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700154 return soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700155 }
Brian Carlstromce888532013-10-10 00:32:58 -0700156 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800157 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700158 return method->GetDeclaringClass()->GetClassLoader();
159 }
160 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800161 mirror::ClassLoader* class_loader =
162 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
163 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700164 return class_loader;
165 }
166 // See if the override ClassLoader is set for gtests.
Ian Rogers68d8b422014-07-17 11:09:10 -0700167 class_loader = soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800168 if (class_loader != nullptr) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700169 // If so, CommonCompilerTest should have marked the runtime as a compiler not compiling an
170 // image.
171 CHECK(Runtime::Current()->IsAotCompiler());
Andreas Gampe4585f872015-03-27 23:45:15 -0700172 CHECK(!Runtime::Current()->IsCompilingBootImage());
Brian Carlstromce888532013-10-10 00:32:58 -0700173 return class_loader;
174 }
175 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800176 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700177}
178
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700179static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
180 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700182 StackHandleScope<2> hs(soa.Self());
183 Handle<mirror::Class> c(
184 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
185 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800186 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700187 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700188 ArtField* field = nullptr;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800189 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700190 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
191 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700192 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800193 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700194 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700196 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800197 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199 DCHECK(soa.Self()->IsExceptionPending());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800200 StackHandleScope<1> hs2(soa.Self());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000201 Handle<mirror::Throwable> cause(hs2.NewHandle(soa.Self()->GetException()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700202 soa.Self()->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700203 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000204 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800205 "no type \"%s\" found and so no field \"%s\" "
206 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700207 c->GetDescriptor(&temp));
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000208 soa.Self()->GetException()->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800209 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700211 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700213 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700214 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700216 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700217 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800218 if (field == nullptr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000219 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800220 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700221 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800222 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700223 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700224 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700225}
226
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800227static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700230 std::string type(PrettyTypeOf(array));
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000231 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800232 "%s offset=%d length=%d %s.length=%d",
233 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700234}
Ian Rogers0571d352011-11-03 19:51:38 -0700235
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700236static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
237 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700238 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000239 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800240 "offset=%d length=%d string.length()=%d", start, length,
241 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700242}
Elliott Hughes814e4032011-08-23 12:07:56 -0700243
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700245 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 // Turn the const char* into a java.lang.String.
247 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800248 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700250 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700251
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252 // Choose an appropriate constructor and set up the arguments.
253 jvalue args[2];
254 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800255 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800257 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 signature = "(Ljava/lang/String;)V";
259 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800260 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 signature = "(Ljava/lang/Throwable;)V";
262 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700263 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
265 args[0].l = s.get();
266 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700267 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800269 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800270 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800272 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273 return JNI_ERR;
274 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700275
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800276 ScopedLocalRef<jthrowable> exception(
277 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
278 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279 return JNI_ERR;
280 }
Ian Rogersef28b142012-11-30 14:22:18 -0800281 ScopedObjectAccess soa(env);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000282 soa.Self()->SetException(soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700284}
285
Ian Rogers68d8b422014-07-17 11:09:10 -0700286static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
287 return reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughes75770752011-08-24 17:52:38 -0700288}
289
Ian Rogers2d10b202014-05-12 19:15:18 -0700290#define CHECK_NON_NULL_ARGUMENT(value) \
291 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700292
Ian Rogers2d10b202014-05-12 19:15:18 -0700293#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
294 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
295
296#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
297 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
298
299#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
300 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
301
302#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800303 if (UNLIKELY(value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700304 JavaVmExtFromEnv(env)->JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700305 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700306 }
307
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700308#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800309 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700310 JavaVmExtFromEnv(env)->JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700311 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700312 }
313
Andreas Gampe3f1dc562015-05-18 15:52:22 -0700314template <bool kNative>
315static mirror::ArtMethod* FindMethod(mirror::Class* c,
316 const StringPiece& name,
317 const StringPiece& sig)
318 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
319 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
320 mirror::ArtMethod* method = c->GetDirectMethod(i);
321 if (kNative == method->IsNative() &&
322 name == method->GetName() && method->GetSignature() == sig) {
323 return method;
324 }
325 }
326
327 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
328 mirror::ArtMethod* method = c->GetVirtualMethod(i);
329 if (kNative == method->IsNative() &&
330 name == method->GetName() && method->GetSignature() == sig) {
331 return method;
332 }
333 }
334
335 return nullptr;
336}
337
Elliott Hughescdf53122011-08-19 15:46:09 -0700338class JNI {
339 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700340 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700341 return JNI_VERSION_1_6;
342 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700343
Ian Rogers25e8b912012-09-07 11:31:36 -0700344 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700345 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800346 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700347 }
348
Elliott Hughescdf53122011-08-19 15:46:09 -0700349 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700350 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700351 Runtime* runtime = Runtime::Current();
352 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700353 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700354 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800355 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700356 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700357 StackHandleScope<1> hs(soa.Self());
358 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800359 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700360 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800361 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700362 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700363 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700364 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700365
Ian Rogers62f05122014-03-21 11:21:29 -0700366 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700367 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700369 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700370 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700371
Ian Rogers62f05122014-03-21 11:21:29 -0700372 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700373 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 ScopedObjectAccess soa(env);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700375 mirror::Object* obj_field = soa.Decode<mirror::Object*>(jlr_field);
376 if (obj_field->GetClass() != mirror::Field::StaticClass()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700377 // Not even a java.lang.reflect.Field, return null. TODO, is this check necessary?
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700378 return nullptr;
379 }
380 auto* field = static_cast<mirror::Field*>(obj_field);
381 return soa.EncodeField(field->GetArtField());
Elliott Hughescdf53122011-08-19 15:46:09 -0700382 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700383
Elliott Hughescdf53122011-08-19 15:46:09 -0700384 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700385 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700386 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800387 mirror::ArtMethod* m = soa.DecodeMethod(mid);
388 CHECK(!kMovingMethods);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700389 mirror::AbstractMethod* method;
Sebastien Hertzd3333762014-06-26 14:45:07 +0200390 if (m->IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700391 method = mirror::Constructor::CreateFromArtMethod(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200392 } else {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700393 method = mirror::Method::CreateFromArtMethod(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200394 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700395 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700396 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700397
Elliott Hughescdf53122011-08-19 15:46:09 -0700398 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700399 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 ScopedObjectAccess soa(env);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700401 ArtField* f = soa.DecodeField(fid);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700402 return soa.AddLocalReference<jobject>(mirror::Field::CreateFromArtField(soa.Self(), f, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700403 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700404
Elliott Hughes37f7a402011-08-22 18:56:01 -0700405 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700406 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700407 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800408 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700409 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700410 }
411
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700412 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700413 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700414 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800415 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700416 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700417 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700418
Narayan Kamath1268b742014-07-11 19:15:11 +0100419 // Note: java_class1 should be safely castable to java_class2, and
420 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700421 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700422 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
423 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700424 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800425 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
426 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100427 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700428 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700429
Elliott Hughese84278b2012-03-22 10:06:53 -0700430 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700431 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800432 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700433 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700434 return JNI_TRUE;
435 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700436 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800437 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
438 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700439 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700440 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700441 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700442
Elliott Hughes37f7a402011-08-22 18:56:01 -0700443 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700444 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800445 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
446 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700447 return JNI_ERR;
448 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000449 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700450 return JNI_OK;
451 }
452
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700453 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700454 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800455 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700456 }
457
458 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700459 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700460 }
461
462 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700463 ScopedObjectAccess soa(env);
464 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700465 }
466
467 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700468 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700469
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700470 // If we have no exception to describe, pass through.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000471 if (!soa.Self()->GetException()) {
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700472 return;
473 }
474
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000475 StackHandleScope<1> hs(soa.Self());
476 Handle<mirror::Throwable> old_exception(
477 hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()));
478 soa.Self()->ClearException();
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800479 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700480 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700481 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
482 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800483 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700484 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700485 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700486 } else {
487 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800488 if (soa.Self()->IsExceptionPending()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000489 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700490 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800491 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700492 }
493 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000494 soa.Self()->SetException(old_exception.Get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700495 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700496
Elliott Hughescdf53122011-08-19 15:46:09 -0700497 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700498 ScopedObjectAccess soa(env);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000499 mirror::Object* exception = soa.Self()->GetException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700500 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700501 }
502
Ian Rogers25e8b912012-09-07 11:31:36 -0700503 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700504 LOG(FATAL) << "JNI FatalError called: " << msg;
505 }
506
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700507 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700508 // TODO: SOA may not be necessary but I do it to please lock annotations.
509 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700510 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700511 return JNI_ERR;
512 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700513 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700514 return JNI_OK;
515 }
516
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700517 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700518 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800519 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700520 soa.Env()->PopFrame();
521 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700522 }
523
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700524 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700525 // TODO: SOA may not be necessary but I do it to please lock annotations.
526 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700527 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700528 }
529
Elliott Hughescdf53122011-08-19 15:46:09 -0700530 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700531 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800532 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700533 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700534 }
535
536 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700537 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
538 Thread* self = down_cast<JNIEnvExt*>(env)->self;
539 vm->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700540 }
541
542 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700543 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700544 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
545 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700546 }
547
548 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700549 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
550 Thread* self = down_cast<JNIEnvExt*>(env)->self;
551 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700552 }
553
554 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700555 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800556 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800557 // Check for null after decoding the object to handle cleared weak globals.
558 if (decoded_obj == nullptr) {
559 return nullptr;
560 }
561 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700562 }
563
Stephen Hines95c51b32014-11-26 01:24:13 -0800564 static void DeleteLocalRef(JNIEnv* env, jobject obj)
565 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800566 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 return;
568 }
Ian Rogersef28b142012-11-30 14:22:18 -0800569 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700570
Ian Rogersef28b142012-11-30 14:22:18 -0800571 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700572 if (!locals.Remove(cookie, obj)) {
573 // Attempting to delete a local reference that is not in the
574 // topmost local reference frame is a no-op. DeleteLocalRef returns
575 // void and doesn't throw any exceptions, but we should probably
576 // complain about it so the user will notice that things aren't
577 // going quite the way they expect.
578 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
579 << "failed to find entry";
580 }
581 }
582
583 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700584 if (obj1 == obj2) {
585 return JNI_TRUE;
586 } else {
587 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800588 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
589 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700590 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700591 }
592
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700593 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700594 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700595 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800596 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800597 if (c == nullptr) {
598 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700599 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800600 if (c->IsStringClass()) {
601 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
602 mirror::SetStringCountVisitor visitor(0);
603 return soa.AddLocalReference<jobject>(mirror::String::Alloc<true>(soa.Self(), 0,
604 allocator_type, visitor));
605 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700606 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700607 }
608
Ian Rogersbc939662013-08-15 10:26:54 -0700609 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700610 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700611 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700612 CHECK_NON_NULL_ARGUMENT(java_class);
613 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700614 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700615 va_end(args);
616 return result;
617 }
618
Elliott Hughes72025e52011-08-23 17:50:30 -0700619 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700620 CHECK_NON_NULL_ARGUMENT(java_class);
621 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800623 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800624 if (c == nullptr) {
625 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700626 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800627 if (c->IsStringClass()) {
628 // Replace calls to String.<init> with equivalent StringFactory call.
629 jmethodID sf_mid = WellKnownClasses::StringInitToStringFactoryMethodID(mid);
630 return CallStaticObjectMethodV(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
631 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800632 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800633 if (result == nullptr) {
634 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700635 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700637 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800638 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800639 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700640 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800641 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700642 }
643
Elliott Hughes72025e52011-08-23 17:50:30 -0700644 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700645 CHECK_NON_NULL_ARGUMENT(java_class);
646 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800648 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800649 if (c == nullptr) {
650 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700651 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800652 if (c->IsStringClass()) {
653 // Replace calls to String.<init> with equivalent StringFactory call.
654 jmethodID sf_mid = WellKnownClasses::StringInitToStringFactoryMethodID(mid);
655 return CallStaticObjectMethodA(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
656 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800657 mirror::Object* result = c->AllocObject(soa.Self());
658 if (result == nullptr) {
659 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700660 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700662 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800663 if (soa.Self()->IsExceptionPending()) {
664 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700665 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800666 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 }
668
Ian Rogersbc939662013-08-15 10:26:54 -0700669 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700670 CHECK_NON_NULL_ARGUMENT(java_class);
671 CHECK_NON_NULL_ARGUMENT(name);
672 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700674 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700675 }
676
Ian Rogersbc939662013-08-15 10:26:54 -0700677 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
678 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700679 CHECK_NON_NULL_ARGUMENT(java_class);
680 CHECK_NON_NULL_ARGUMENT(name);
681 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700682 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700683 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 }
685
Elliott Hughes72025e52011-08-23 17:50:30 -0700686 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700687 va_list ap;
688 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700689 CHECK_NON_NULL_ARGUMENT(obj);
690 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700691 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700692 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700693 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 }
696
Elliott Hughes72025e52011-08-23 17:50:30 -0700697 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700698 CHECK_NON_NULL_ARGUMENT(obj);
699 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 ScopedObjectAccess soa(env);
701 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
702 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 }
704
Elliott Hughes72025e52011-08-23 17:50:30 -0700705 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700706 CHECK_NON_NULL_ARGUMENT(obj);
707 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700709 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
710 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700711 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700712 }
713
Elliott Hughes72025e52011-08-23 17:50:30 -0700714 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700715 va_list ap;
716 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700717 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
718 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700719 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700720 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700721 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700722 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700723 }
724
Elliott Hughes72025e52011-08-23 17:50:30 -0700725 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list 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);
729 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 }
731
Elliott Hughes72025e52011-08-23 17:50:30 -0700732 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700733 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
734 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700735 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700736 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
737 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 }
739
Elliott Hughes72025e52011-08-23 17:50:30 -0700740 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700741 va_list ap;
742 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700743 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
744 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700745 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700746 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700747 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700748 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700749 }
750
Elliott Hughes72025e52011-08-23 17:50:30 -0700751 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700752 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
753 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700754 ScopedObjectAccess soa(env);
755 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700756 }
757
Elliott Hughes72025e52011-08-23 17:50:30 -0700758 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700759 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
760 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700761 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700762 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
763 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700764 }
765
Elliott Hughes72025e52011-08-23 17:50:30 -0700766 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700767 va_list ap;
768 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700769 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
770 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700771 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700772 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700773 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700774 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 }
776
Elliott Hughes72025e52011-08-23 17:50:30 -0700777 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700778 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
779 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700780 ScopedObjectAccess soa(env);
781 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 }
783
Elliott Hughes72025e52011-08-23 17:50:30 -0700784 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700785 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
786 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700787 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700788 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
789 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700790 }
791
Elliott Hughes72025e52011-08-23 17:50:30 -0700792 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700793 va_list ap;
794 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700795 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
796 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700797 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700798 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700799 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700800 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 }
802
Elliott Hughes72025e52011-08-23 17:50:30 -0700803 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700804 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
805 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700806 ScopedObjectAccess soa(env);
807 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700808 }
809
Elliott Hughes72025e52011-08-23 17:50:30 -0700810 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700811 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
812 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700813 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700814 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
815 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 }
817
Elliott Hughes72025e52011-08-23 17:50:30 -0700818 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700819 va_list ap;
820 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700821 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
822 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700823 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700824 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700825 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700826 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 }
828
Elliott Hughes72025e52011-08-23 17:50:30 -0700829 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700830 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
831 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700832 ScopedObjectAccess soa(env);
833 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 }
835
Elliott Hughes72025e52011-08-23 17:50:30 -0700836 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700837 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
838 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700839 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700840 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
841 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 }
843
Elliott Hughes72025e52011-08-23 17:50:30 -0700844 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700845 va_list ap;
846 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700847 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
848 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700849 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700850 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700851 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700852 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700853 }
854
Elliott Hughes72025e52011-08-23 17:50:30 -0700855 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700856 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
857 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700858 ScopedObjectAccess soa(env);
859 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 }
861
Elliott Hughes72025e52011-08-23 17:50:30 -0700862 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700863 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
864 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700866 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
867 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 }
869
Elliott Hughes72025e52011-08-23 17:50:30 -0700870 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700871 va_list ap;
872 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700873 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
874 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700875 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700876 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700877 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700878 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 }
880
Elliott Hughes72025e52011-08-23 17:50:30 -0700881 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700882 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
883 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700884 ScopedObjectAccess soa(env);
885 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700886 }
887
Elliott Hughes72025e52011-08-23 17:50:30 -0700888 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700889 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
890 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700891 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700892 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
893 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700894 }
895
Elliott Hughes72025e52011-08-23 17:50:30 -0700896 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700897 va_list ap;
898 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700899 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
900 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700901 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700902 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700903 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700904 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700905 }
906
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700908 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
909 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700910 ScopedObjectAccess soa(env);
911 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700912 }
913
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700915 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
916 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700917 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700918 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
919 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700923 va_list ap;
924 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700925 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
926 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700927 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -0700928 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700933 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
934 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935 ScopedObjectAccess soa(env);
936 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 }
938
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700940 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
941 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700942 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700943 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 }
945
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700946 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700948 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700949 CHECK_NON_NULL_ARGUMENT(obj);
950 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700951 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700952 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
953 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 va_end(ap);
955 return local_result;
956 }
957
Ian Rogersbc939662013-08-15 10:26:54 -0700958 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
959 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700960 CHECK_NON_NULL_ARGUMENT(obj);
961 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700962 ScopedObjectAccess soa(env);
963 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
964 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Ian Rogersbc939662013-08-15 10:26:54 -0700967 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
968 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700969 CHECK_NON_NULL_ARGUMENT(obj);
970 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700971 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700972 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700973 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Ian Rogersbc939662013-08-15 10:26:54 -0700976 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
977 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700980 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
981 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700982 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700983 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700985 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Ian Rogersbc939662013-08-15 10:26:54 -0700988 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
989 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700990 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
991 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700992 ScopedObjectAccess soa(env);
993 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Ian Rogersbc939662013-08-15 10:26:54 -0700996 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
997 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700998 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
999 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001000 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001001 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001004 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001007 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1008 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001009 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001010 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001012 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Ian Rogersbc939662013-08-15 10:26:54 -07001015 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1016 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001017 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1018 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001019 ScopedObjectAccess soa(env);
1020 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Ian Rogersbc939662013-08-15 10:26:54 -07001023 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1024 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001025 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1026 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001027 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001028 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001031 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001034 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1035 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001036 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001039 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Ian Rogersbc939662013-08-15 10:26:54 -07001042 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1043 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001044 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1045 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001046 ScopedObjectAccess soa(env);
1047 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Ian Rogersbc939662013-08-15 10:26:54 -07001050 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1051 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001052 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1053 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001054 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001055 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001058 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001061 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1062 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001063 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001064 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001066 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 }
1068
Ian Rogersbc939662013-08-15 10:26:54 -07001069 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1070 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001071 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1072 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 ScopedObjectAccess soa(env);
1074 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Ian Rogersbc939662013-08-15 10:26:54 -07001077 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1078 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001079 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1080 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001082 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001085 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001088 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1089 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001090 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001091 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001093 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Ian Rogersbc939662013-08-15 10:26:54 -07001096 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1097 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001098 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1099 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 ScopedObjectAccess soa(env);
1101 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Ian Rogersbc939662013-08-15 10:26:54 -07001104 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1105 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001106 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1107 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001108 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001109 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001112 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001115 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1116 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001117 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001120 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Ian Rogersbc939662013-08-15 10:26:54 -07001123 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1124 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001125 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1126 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001127 ScopedObjectAccess soa(env);
1128 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 }
1130
Ian Rogersbc939662013-08-15 10:26:54 -07001131 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1132 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001133 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1134 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001135 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001136 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001139 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001142 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1143 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001144 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001145 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001147 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Ian Rogersbc939662013-08-15 10:26:54 -07001150 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1151 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001152 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1153 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001154 ScopedObjectAccess soa(env);
1155 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
Ian Rogersbc939662013-08-15 10:26:54 -07001158 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1159 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001160 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1161 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001163 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001166 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001169 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1170 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001171 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001172 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001174 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
Ian Rogersbc939662013-08-15 10:26:54 -07001177 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1178 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001179 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1180 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001181 ScopedObjectAccess soa(env);
1182 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Ian Rogersbc939662013-08-15 10:26:54 -07001185 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1186 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001187 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1188 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001189 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001190 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001193 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001196 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1197 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001198 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001199 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 va_end(ap);
1201 }
1202
Brian Carlstromea46f952013-07-30 01:26:50 -07001203 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1204 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001205 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1206 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001207 ScopedObjectAccess soa(env);
1208 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 }
1210
Ian Rogersbc939662013-08-15 10:26:54 -07001211 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1212 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001213 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1214 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001215 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001216 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 }
1218
Ian Rogersbc939662013-08-15 10:26:54 -07001219 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001220 CHECK_NON_NULL_ARGUMENT(java_class);
1221 CHECK_NON_NULL_ARGUMENT(name);
1222 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001223 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001224 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001226
Ian Rogersbc939662013-08-15 10:26:54 -07001227 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1228 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001229 CHECK_NON_NULL_ARGUMENT(java_class);
1230 CHECK_NON_NULL_ARGUMENT(name);
1231 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001232 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001233 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001235
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001236 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001237 CHECK_NON_NULL_ARGUMENT(obj);
1238 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001239 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001240 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001241 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001242 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001244
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001245 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001246 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001247 ScopedObjectAccess soa(env);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001248 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001249 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001252 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001253 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1254 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001255 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001256 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1257 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001258 ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001259 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001262 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001263 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001264 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001265 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001266 ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001267 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001270#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001271 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1272 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001273 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001274 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001275 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001276 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001277
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001278#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001279 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001280 ScopedObjectAccess soa(env); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001281 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001282 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001283
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001284#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001285 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1286 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001287 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001288 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001289 ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001290 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001291
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001292#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001293 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001294 ScopedObjectAccess soa(env); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001295 ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001296 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001297
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001298 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001299 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001302 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001303 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001306 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001307 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001310 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001311 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001314 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001315 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001318 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001319 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001322 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001323 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001326 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001327 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001331 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
1333
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001334 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001335 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 }
1337
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001338 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001339 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 }
1341
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001342 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001343 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 }
1345
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001346 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001347 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001350 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001351 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001352 }
1353
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001354 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001355 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001356 }
1357
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001358 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001359 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001360 }
1361
1362 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001363 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001364 }
1365
1366 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001367 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001368 }
1369
1370 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001371 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001372 }
1373
1374 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001375 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001376 }
1377
1378 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001379 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001380 }
1381
1382 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001383 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001384 }
1385
1386 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001387 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 }
1389
1390 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001391 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 }
1393
1394 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001395 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 }
1397
1398 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001399 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 }
1401
1402 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001403 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404 }
1405
1406 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001407 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001408 }
1409
1410 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001411 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001412 }
1413
1414 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001415 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001416 }
1417
1418 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001419 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001420 }
1421
1422 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001423 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001426 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001428 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001429 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001430 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001431 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001432 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 va_end(ap);
1434 return local_result;
1435 }
1436
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001437 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001438 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001440 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001441 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001444 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001445 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001446 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001447 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001448 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001451 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001453 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001454 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001455 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001456 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001458 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
1460
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001461 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001462 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001463 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001464 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001467 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001468 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001469 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001470 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001471 }
1472
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001473 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001475 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001476 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001477 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001478 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001479 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001480 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001483 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001484 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001485 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001486 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 }
1488
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001489 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001490 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001491 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001492 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001495 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001496 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001497 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001498 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001499 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001500 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001502 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 }
1504
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001505 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001506 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001507 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001508 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
1510
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001511 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001512 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001513 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001514 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 }
1516
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001517 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001519 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001520 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001521 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001522 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001524 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
1526
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001527 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001528 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001529 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001530 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001531 }
1532
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001533 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001534 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001535 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001536 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001539 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001541 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001542 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001543 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001544 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001546 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001549 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001550 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001551 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001552 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001555 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001556 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001557 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001558 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001561 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001563 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001564 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001565 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001566 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001568 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001571 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001572 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001573 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001574 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001577 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001578 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001579 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001580 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001583 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001585 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001586 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001587 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001588 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001590 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001593 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001594 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001595 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001596 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001599 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001600 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001601 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001602 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001605 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001607 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001608 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001609 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001610 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001612 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001615 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001616 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001617 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001618 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001621 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001622 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001623 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001624 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 }
1626
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001627 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001629 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001630 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001631 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001632 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 va_end(ap);
1634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001637 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001638 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001639 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001642 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001643 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001644 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001645 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 }
1647
Elliott Hughes814e4032011-08-23 12:07:56 -07001648 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001649 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001650 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001651 return nullptr;
1652 }
1653 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001654 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001655 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001656 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001657 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001658 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001659 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
1662 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001663 if (utf == nullptr) {
1664 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001666 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001667 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001668 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 }
1670
Elliott Hughes814e4032011-08-23 12:07:56 -07001671 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001672 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001673 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001674 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001675 }
1676
1677 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001678 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001679 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001680 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001681 }
1682
Ian Rogersbc939662013-08-15 10:26:54 -07001683 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1684 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001685 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001686 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001687 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001688 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001689 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001690 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001691 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Jeff Hao848f70a2014-01-15 13:49:50 -08001692 const jchar* chars = s->GetValue();
Elliott Hughesb465ab02011-08-24 11:21:21 -07001693 memcpy(buf, chars + start, length * sizeof(jchar));
1694 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001695 }
1696
Ian Rogersbc939662013-08-15 10:26:54 -07001697 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1698 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001699 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001700 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001701 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001702 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001703 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001704 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001705 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Jeff Hao848f70a2014-01-15 13:49:50 -08001706 const jchar* chars = s->GetValue();
Elliott Hughesb465ab02011-08-24 11:21:21 -07001707 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1708 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001709 }
1710
Elliott Hughes75770752011-08-24 17:52:38 -07001711 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001712 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001713 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001714 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001715 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Hao848f70a2014-01-15 13:49:50 -08001716 if (heap->IsMovableObject(s)) {
1717 jchar* chars = new jchar[s->GetLength()];
1718 memcpy(chars, s->GetValue(), sizeof(jchar) * s->GetLength());
Fred Shih56890e22014-06-02 11:11:52 -07001719 if (is_copy != nullptr) {
1720 *is_copy = JNI_TRUE;
1721 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001722 return chars;
Elliott Hughes75770752011-08-24 17:52:38 -07001723 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001724 if (is_copy != nullptr) {
1725 *is_copy = JNI_FALSE;
1726 }
1727 return static_cast<jchar*>(s->GetValue());
Elliott Hughes814e4032011-08-23 12:07:56 -07001728 }
1729
Mathieu Chartier590fee92013-09-13 13:46:47 -07001730 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001731 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001732 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001733 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08001734 if (chars != s->GetValue()) {
Fred Shih56890e22014-06-02 11:11:52 -07001735 delete[] chars;
1736 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes75770752011-08-24 17:52:38 -07001739 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001740 CHECK_NON_NULL_ARGUMENT(java_string);
1741 ScopedObjectAccess soa(env);
1742 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001743 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Hao848f70a2014-01-15 13:49:50 -08001744 if (heap->IsMovableObject(s)) {
Fred Shih56890e22014-06-02 11:11:52 -07001745 StackHandleScope<1> hs(soa.Self());
Jeff Hao848f70a2014-01-15 13:49:50 -08001746 HandleWrapper<mirror::String> h(hs.NewHandleWrapper(&s));
Fred Shih56890e22014-06-02 11:11:52 -07001747 heap->IncrementDisableMovingGC(soa.Self());
1748 }
1749 if (is_copy != nullptr) {
1750 *is_copy = JNI_FALSE;
1751 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001752 return static_cast<jchar*>(s->GetValue());
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes75770752011-08-24 17:52:38 -07001755 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001756 UNUSED(chars);
Fred Shih56890e22014-06-02 11:11:52 -07001757 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1758 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001759 gc::Heap* heap = Runtime::Current()->GetHeap();
1760 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08001761 if (heap->IsMovableObject(s)) {
Fred Shih56890e22014-06-02 11:11:52 -07001762 heap->DecrementDisableMovingGC(soa.Self());
1763 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 }
1765
Elliott Hughes75770752011-08-24 17:52:38 -07001766 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001767 if (java_string == nullptr) {
1768 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001769 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001770 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001771 *is_copy = JNI_TRUE;
1772 }
Ian Rogersef28b142012-11-30 14:22:18 -08001773 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001774 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001775 size_t byte_count = s->GetUtfLength();
1776 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001777 CHECK(bytes != nullptr); // bionic aborts anyway.
Jeff Hao848f70a2014-01-15 13:49:50 -08001778 const uint16_t* chars = s->GetValue();
Elliott Hughes75770752011-08-24 17:52:38 -07001779 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1780 bytes[byte_count] = '\0';
1781 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001782 }
1783
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001784 static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001785 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001786 }
1787
Elliott Hughesbd935992011-08-22 11:59:34 -07001788 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001789 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001790 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001791 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001792 if (UNLIKELY(!obj->IsArrayInstance())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001793 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1794 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001795 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001796 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001797 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
Elliott Hughes814e4032011-08-23 12:07:56 -07001800 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001801 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001802 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001803 mirror::ObjectArray<mirror::Object>* array =
1804 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001805 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 }
1807
Ian Rogersbc939662013-08-15 10:26:54 -07001808 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1809 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001810 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001811 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001812 mirror::ObjectArray<mirror::Object>* array =
1813 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1814 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001815 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
1818 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001819 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
1822 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001823 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
1826 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001827 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
1830 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001831 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
1834 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001835 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
1838 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001839 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 }
1841
1842 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001843 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 }
1845
Ian Rogers1d99e452014-01-02 17:36:41 -08001846 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1847 jobject initial_element) {
1848 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001849 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001850 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001851 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001852 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001853
1854 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001855 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001856 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08001857 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001858 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08001859 if (UNLIKELY(element_class->IsPrimitive())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001860 soa.Vm()->JniAbortF("NewObjectArray", "not an object type: %s",
1861 PrettyDescriptor(element_class).c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08001862 return nullptr;
1863 }
Ian Rogers1d99e452014-01-02 17:36:41 -08001864 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07001865 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08001866 if (UNLIKELY(array_class == nullptr)) {
1867 return nullptr;
1868 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
Elliott Hughes75770752011-08-24 17:52:38 -07001871 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001872 mirror::ObjectArray<mirror::Object>* result =
1873 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001874 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001875 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08001876 if (initial_object != nullptr) {
1877 mirror::Class* element_class = result->GetClass()->GetComponentType();
1878 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001879 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
1880 "element type of '%s'",
1881 PrettyDescriptor(initial_object->GetClass()).c_str(),
1882 PrettyDescriptor(element_class).c_str());
1883 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08001884 } else {
1885 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001886 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08001887 }
1888 }
Elliott Hughes75770752011-08-24 17:52:38 -07001889 }
1890 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001891 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 }
1893
1894 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001895 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001896 }
1897
Ian Rogersa15e67d2012-02-28 13:51:55 -08001898 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001899 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001900 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001901 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07001902 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001903 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
1904 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001905 return nullptr;
1906 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001907 gc::Heap* heap = Runtime::Current()->GetHeap();
1908 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08001909 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001910 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001911 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001912 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001913 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001914 *is_copy = JNI_FALSE;
1915 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001916 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001917 }
1918
Ian Rogers2d10b202014-05-12 19:15:18 -07001919 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
1920 jint mode) {
1921 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
1922 ScopedObjectAccess soa(env);
1923 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
1924 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001925 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
1926 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001927 return;
1928 }
1929 const size_t component_size = array->GetClass()->GetComponentSize();
1930 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001931 }
1932
Elliott Hughes75770752011-08-24 17:52:38 -07001933 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001934 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes75770752011-08-24 17:52:38 -07001937 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001938 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Elliott Hughes75770752011-08-24 17:52:38 -07001941 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001942 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
Elliott Hughes75770752011-08-24 17:52:38 -07001945 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001946 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 }
1948
Elliott Hughes75770752011-08-24 17:52:38 -07001949 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001950 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 }
1952
Elliott Hughes75770752011-08-24 17:52:38 -07001953 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001954 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001955 }
1956
Elliott Hughes75770752011-08-24 17:52:38 -07001957 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001958 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 }
1960
Elliott Hughes75770752011-08-24 17:52:38 -07001961 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001962 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 }
1964
Mathieu Chartier590fee92013-09-13 13:46:47 -07001965 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
1966 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001967 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
1968 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 }
1970
Mathieu Chartier590fee92013-09-13 13:46:47 -07001971 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001972 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 }
1974
Mathieu Chartier590fee92013-09-13 13:46:47 -07001975 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001976 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Mathieu Chartier590fee92013-09-13 13:46:47 -07001979 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
1980 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001981 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Mathieu Chartier590fee92013-09-13 13:46:47 -07001984 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
1985 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001986 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Mathieu Chartier590fee92013-09-13 13:46:47 -07001989 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001990 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Mathieu Chartier590fee92013-09-13 13:46:47 -07001993 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001994 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
Mathieu Chartier590fee92013-09-13 13:46:47 -07001997 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
1998 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001999 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
Ian Rogersbc939662013-08-15 10:26:54 -07002002 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2003 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002004 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002005 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Ian Rogersbc939662013-08-15 10:26:54 -07002008 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2009 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002010 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Ian Rogersbc939662013-08-15 10:26:54 -07002013 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2014 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002015 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Ian Rogersbc939662013-08-15 10:26:54 -07002018 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2019 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002020 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002021 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Ian Rogersbc939662013-08-15 10:26:54 -07002024 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2025 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002026 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002027 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Ian Rogersbc939662013-08-15 10:26:54 -07002030 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2031 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002032 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
2034
Ian Rogersbc939662013-08-15 10:26:54 -07002035 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2036 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002037 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Ian Rogersbc939662013-08-15 10:26:54 -07002040 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2041 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002042 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002043 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Ian Rogersbc939662013-08-15 10:26:54 -07002046 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2047 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002048 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002049 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Ian Rogersbc939662013-08-15 10:26:54 -07002052 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2053 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002054 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Ian Rogersbc939662013-08-15 10:26:54 -07002057 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2058 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002059 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Ian Rogersbc939662013-08-15 10:26:54 -07002062 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2063 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002064 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002065 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Ian Rogersbc939662013-08-15 10:26:54 -07002068 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2069 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002070 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002071 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Ian Rogersbc939662013-08-15 10:26:54 -07002074 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2075 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002076 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Ian Rogersbc939662013-08-15 10:26:54 -07002079 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2080 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002081 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Ian Rogersbc939662013-08-15 10:26:54 -07002084 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2085 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002086 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002087 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
Ian Rogersbc939662013-08-15 10:26:54 -07002090 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2091 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002092 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2093 }
2094
Ian Rogersbc939662013-08-15 10:26:54 -07002095 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2096 jint method_count, bool return_errors) {
2097 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002098 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2099 method_count);
2100 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002101 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002102 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002103 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002104 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002105 if (UNLIKELY(method_count == 0)) {
2106 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2107 << PrettyDescriptor(c);
2108 return JNI_OK;
2109 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002110 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002111 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 const char* name = methods[i].name;
2113 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002114 const void* fnPtr = methods[i].fnPtr;
2115 if (UNLIKELY(name == nullptr)) {
2116 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2117 return JNI_ERR;
2118 } else if (UNLIKELY(sig == nullptr)) {
2119 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2120 return JNI_ERR;
2121 } else if (UNLIKELY(fnPtr == nullptr)) {
2122 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2123 return JNI_ERR;
2124 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002125 bool is_fast = false;
Hiroshi Yamauchi36bce582015-05-12 12:16:10 -07002126 // Notes about fast JNI calls:
2127 //
2128 // On a normal JNI call, the calling thread usually transitions
2129 // from the kRunnable state to the kNative state. But if the
2130 // called native function needs to access any Java object, it
2131 // will have to transition back to the kRunnable state.
2132 //
2133 // There is a cost to this double transition. For a JNI call
2134 // that should be quick, this cost may dominate the call cost.
2135 //
2136 // On a fast JNI call, the calling thread avoids this double
2137 // transition by not transitioning from kRunnable to kNative and
2138 // stays in the kRunnable state.
2139 //
2140 // There are risks to using a fast JNI call because it can delay
2141 // a response to a thread suspension request which is typically
2142 // used for a GC root scanning, etc. If a fast JNI call takes a
2143 // long time, it could cause longer thread suspension latency
2144 // and GC pauses.
2145 //
2146 // Thus, fast JNI should be used with care. It should be used
2147 // for a JNI call that takes a short amount of time (eg. no
2148 // long-running loop) and does not block (eg. no locks, I/O,
2149 // etc.)
2150 //
2151 // A '!' prefix in the signature in the JNINativeMethod
2152 // indicates that it's a fast JNI call and the runtime omits the
2153 // thread state transition from kRunnable to kNative at the
2154 // entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002156 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 ++sig;
2158 }
2159
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002160 // Note: the right order is to try to find the method locally
2161 // first, either as a direct or a virtual method. Then move to
2162 // the parent.
2163 mirror::ArtMethod* m = nullptr;
2164 bool warn_on_going_to_parent = down_cast<JNIEnvExt*>(env)->vm->IsCheckJniEnabled();
2165 for (mirror::Class* current_class = c;
2166 current_class != nullptr;
2167 current_class = current_class->GetSuperClass()) {
2168 // Search first only comparing methods which are native.
2169 m = FindMethod<true>(current_class, name, sig);
2170 if (m != nullptr) {
2171 break;
2172 }
2173
2174 // Search again comparing to all methods, to find non-native methods that match.
2175 m = FindMethod<false>(current_class, name, sig);
2176 if (m != nullptr) {
2177 break;
2178 }
2179
2180 if (warn_on_going_to_parent) {
2181 LOG(WARNING) << "CheckJNI: method to register \"" << name << "\" not in the given class. "
2182 << "This is slow, consider changing your RegisterNatives calls.";
2183 warn_on_going_to_parent = false;
2184 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002186
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002187 if (m == nullptr) {
Mathieu Chartier5c02d6c2015-05-04 10:18:24 -07002188 LOG(return_errors ? ERROR : INTERNAL_FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002189 << PrettyDescriptor(c) << "." << name << sig << " in "
2190 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Mathieu Chartier5c02d6c2015-05-04 10:18:24 -07002191 // Safe to pass in LOG(FATAL) since the log object aborts in destructor and only goes
2192 // out of scope after the DumpClass is done executing.
2193 c->DumpClass(LOG(return_errors ? ERROR : FATAL), mirror::Class::kDumpClassFullDetail);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002194 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002196 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002197 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002198 << PrettyDescriptor(c) << "." << name << sig
2199 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002200 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 return JNI_ERR;
2202 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002203
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002204 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002205
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002206 m->RegisterNative(fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208 return JNI_OK;
2209 }
2210
Elliott Hughes5174fe62011-08-23 15:12:35 -07002211 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002212 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002213 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002214 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002215
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002216 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002217
Ian Rogers2d10b202014-05-12 19:15:18 -07002218 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002219 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002220 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002221 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002222 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002223 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002224 }
2225 }
2226 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002227 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002228 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002229 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002230 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002231 }
2232 }
2233
Ian Rogers2d10b202014-05-12 19:15:18 -07002234 if (unregistered_count == 0) {
2235 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2236 << PrettyDescriptor(c) << "' that contains no native methods";
2237 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002238 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Ian Rogers719d1a32014-03-06 12:13:39 -08002241 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002242 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002243 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002244 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2245 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002246 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002247 return JNI_ERR;
2248 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002249 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002250 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 }
2252
Ian Rogers719d1a32014-03-06 12:13:39 -08002253 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002254 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002255 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002256 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002257 o->MonitorExit(soa.Self());
2258 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002259 return JNI_ERR;
2260 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002261 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002262 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
2264
2265 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002266 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002268 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002269 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002270 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002271 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002273 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002274 }
2275
Elliott Hughescdf53122011-08-19 15:46:09 -07002276 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002277 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002278 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2279 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002280 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002281 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002282 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002283 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2284 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002285 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002286 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002287
Brian Carlstrom85a93362014-06-25 09:30:52 -07002288 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002289 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002290 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2291 "buffer capacity greater than maximum jint: %" PRId64,
2292 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002293 return nullptr;
2294 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002295 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002296 jint capacity_arg = static_cast<jint>(capacity);
2297
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002298 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2299 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002300 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002301 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 }
2303
Elliott Hughesb465ab02011-08-24 11:21:21 -07002304 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002305 return reinterpret_cast<void*>(env->GetLongField(
2306 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002307 }
2308
Elliott Hughesb465ab02011-08-24 11:21:21 -07002309 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002310 return static_cast<jlong>(env->GetIntField(
2311 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002312 }
2313
Andreas Gampea8763072014-12-20 00:08:35 -08002314 static jobjectRefType GetObjectRefType(JNIEnv* env ATTRIBUTE_UNUSED, jobject java_object) {
2315 if (java_object == nullptr) {
2316 return JNIInvalidRefType;
2317 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002318
2319 // Do we definitely know what kind of reference this is?
2320 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2321 IndirectRefKind kind = GetIndirectRefKind(ref);
2322 switch (kind) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002323 case kLocal:
2324 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002325 case kGlobal:
2326 return JNIGlobalRefType;
2327 case kWeakGlobal:
2328 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002329 case kHandleScopeOrInvalid:
Ian Rogersc0542af2014-09-03 16:16:56 -07002330 // Assume value is in a handle scope.
2331 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002332 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002333 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
Andreas Gampea8763072014-12-20 00:08:35 -08002334 UNREACHABLE();
Elliott Hughescdf53122011-08-19 15:46:09 -07002335 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002336
2337 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002338 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2339 const char* caller)
2340 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002341 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002342 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002343 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2344 return JNI_ERR;
2345 }
2346 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002347 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002348 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2349 if (!okay) {
2350 soa.Self()->ThrowOutOfMemoryError(caller);
2351 }
2352 return okay ? JNI_OK : JNI_ERR;
2353 }
2354
2355 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002356 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002357 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002358 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002359 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002360 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002361 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002362 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002363 return soa.AddLocalReference<JniT>(result);
2364 }
2365
Ian Rogers2d10b202014-05-12 19:15:18 -07002366 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2367 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2368 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002369 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002370 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002371 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002372 soa.Vm()->JniAbortF(fn_name,
2373 "attempt to %s %s primitive array elements with an object of type %s",
2374 operation,
2375 PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2376 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002377 return nullptr;
2378 }
2379 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2380 return array;
2381 }
2382
2383 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2384 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2385 CHECK_NON_NULL_ARGUMENT(java_array);
2386 ScopedObjectAccess soa(env);
2387 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2388 "GetArrayElements",
2389 "get");
2390 if (UNLIKELY(array == nullptr)) {
2391 return nullptr;
2392 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002393 // Only make a copy if necessary.
2394 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2395 if (is_copy != nullptr) {
2396 *is_copy = JNI_TRUE;
2397 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002398 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002399 size_t size = array->GetLength() * component_size;
2400 void* data = new uint64_t[RoundUp(size, 8) / 8];
2401 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002402 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002403 } else {
2404 if (is_copy != nullptr) {
2405 *is_copy = JNI_FALSE;
2406 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002407 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002408 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002409 }
2410
Ian Rogers2d10b202014-05-12 19:15:18 -07002411 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002412 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002413 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002414 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002415 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2416 "ReleaseArrayElements",
2417 "release");
2418 if (array == nullptr) {
2419 return;
2420 }
2421 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2422 }
2423
2424 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2425 size_t component_size, void* elements, jint mode)
2426 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002427 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002428 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002429 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002430 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002431 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2432 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002433 if (is_copy) {
2434 // Sanity check: If elements is not the same as the java array's data, it better not be a
2435 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2436 // copies we make?
2437 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002438 soa.Vm()->JniAbortF("ReleaseArrayElements",
2439 "invalid element pointer %p, array elements are %p",
2440 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002441 return;
2442 }
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002443 if (mode != JNI_ABORT) {
2444 memcpy(array_data, elements, bytes);
2445 } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2446 // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2447 LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2448 soa.Self()->DumpJavaStack(LOG(WARNING));
2449 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002450 }
2451 if (mode != JNI_COMMIT) {
2452 if (is_copy) {
2453 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002454 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002455 // Non copy to a movable object must means that we had disabled the moving GC.
2456 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002457 }
2458 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002459 }
2460
Ian Rogers2d10b202014-05-12 19:15:18 -07002461 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2462 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2463 jsize start, jsize length, ElementT* buf) {
2464 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2465 ScopedObjectAccess soa(env);
2466 ArtArrayT* array =
2467 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2468 "GetPrimitiveArrayRegion",
2469 "get region of");
2470 if (array != nullptr) {
2471 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2472 ThrowAIOOBE(soa, array, start, length, "src");
2473 } else {
2474 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2475 ElementT* data = array->GetData();
2476 memcpy(buf, data + start, length * sizeof(ElementT));
2477 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002478 }
2479 }
2480
Ian Rogers2d10b202014-05-12 19:15:18 -07002481 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2482 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2483 jsize start, jsize length, const ElementT* buf) {
2484 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2485 ScopedObjectAccess soa(env);
2486 ArtArrayT* array =
2487 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2488 "SetPrimitiveArrayRegion",
2489 "set region of");
2490 if (array != nullptr) {
2491 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2492 ThrowAIOOBE(soa, array, start, length, "dst");
2493 } else {
2494 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2495 ElementT* data = array->GetData();
2496 memcpy(data + start, buf, length * sizeof(ElementT));
2497 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002498 }
2499 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002500};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002501
Elliott Hughes88c5c352012-03-15 18:49:48 -07002502const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002503 nullptr, // reserved0.
2504 nullptr, // reserved1.
2505 nullptr, // reserved2.
2506 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002507 JNI::GetVersion,
2508 JNI::DefineClass,
2509 JNI::FindClass,
2510 JNI::FromReflectedMethod,
2511 JNI::FromReflectedField,
2512 JNI::ToReflectedMethod,
2513 JNI::GetSuperclass,
2514 JNI::IsAssignableFrom,
2515 JNI::ToReflectedField,
2516 JNI::Throw,
2517 JNI::ThrowNew,
2518 JNI::ExceptionOccurred,
2519 JNI::ExceptionDescribe,
2520 JNI::ExceptionClear,
2521 JNI::FatalError,
2522 JNI::PushLocalFrame,
2523 JNI::PopLocalFrame,
2524 JNI::NewGlobalRef,
2525 JNI::DeleteGlobalRef,
2526 JNI::DeleteLocalRef,
2527 JNI::IsSameObject,
2528 JNI::NewLocalRef,
2529 JNI::EnsureLocalCapacity,
2530 JNI::AllocObject,
2531 JNI::NewObject,
2532 JNI::NewObjectV,
2533 JNI::NewObjectA,
2534 JNI::GetObjectClass,
2535 JNI::IsInstanceOf,
2536 JNI::GetMethodID,
2537 JNI::CallObjectMethod,
2538 JNI::CallObjectMethodV,
2539 JNI::CallObjectMethodA,
2540 JNI::CallBooleanMethod,
2541 JNI::CallBooleanMethodV,
2542 JNI::CallBooleanMethodA,
2543 JNI::CallByteMethod,
2544 JNI::CallByteMethodV,
2545 JNI::CallByteMethodA,
2546 JNI::CallCharMethod,
2547 JNI::CallCharMethodV,
2548 JNI::CallCharMethodA,
2549 JNI::CallShortMethod,
2550 JNI::CallShortMethodV,
2551 JNI::CallShortMethodA,
2552 JNI::CallIntMethod,
2553 JNI::CallIntMethodV,
2554 JNI::CallIntMethodA,
2555 JNI::CallLongMethod,
2556 JNI::CallLongMethodV,
2557 JNI::CallLongMethodA,
2558 JNI::CallFloatMethod,
2559 JNI::CallFloatMethodV,
2560 JNI::CallFloatMethodA,
2561 JNI::CallDoubleMethod,
2562 JNI::CallDoubleMethodV,
2563 JNI::CallDoubleMethodA,
2564 JNI::CallVoidMethod,
2565 JNI::CallVoidMethodV,
2566 JNI::CallVoidMethodA,
2567 JNI::CallNonvirtualObjectMethod,
2568 JNI::CallNonvirtualObjectMethodV,
2569 JNI::CallNonvirtualObjectMethodA,
2570 JNI::CallNonvirtualBooleanMethod,
2571 JNI::CallNonvirtualBooleanMethodV,
2572 JNI::CallNonvirtualBooleanMethodA,
2573 JNI::CallNonvirtualByteMethod,
2574 JNI::CallNonvirtualByteMethodV,
2575 JNI::CallNonvirtualByteMethodA,
2576 JNI::CallNonvirtualCharMethod,
2577 JNI::CallNonvirtualCharMethodV,
2578 JNI::CallNonvirtualCharMethodA,
2579 JNI::CallNonvirtualShortMethod,
2580 JNI::CallNonvirtualShortMethodV,
2581 JNI::CallNonvirtualShortMethodA,
2582 JNI::CallNonvirtualIntMethod,
2583 JNI::CallNonvirtualIntMethodV,
2584 JNI::CallNonvirtualIntMethodA,
2585 JNI::CallNonvirtualLongMethod,
2586 JNI::CallNonvirtualLongMethodV,
2587 JNI::CallNonvirtualLongMethodA,
2588 JNI::CallNonvirtualFloatMethod,
2589 JNI::CallNonvirtualFloatMethodV,
2590 JNI::CallNonvirtualFloatMethodA,
2591 JNI::CallNonvirtualDoubleMethod,
2592 JNI::CallNonvirtualDoubleMethodV,
2593 JNI::CallNonvirtualDoubleMethodA,
2594 JNI::CallNonvirtualVoidMethod,
2595 JNI::CallNonvirtualVoidMethodV,
2596 JNI::CallNonvirtualVoidMethodA,
2597 JNI::GetFieldID,
2598 JNI::GetObjectField,
2599 JNI::GetBooleanField,
2600 JNI::GetByteField,
2601 JNI::GetCharField,
2602 JNI::GetShortField,
2603 JNI::GetIntField,
2604 JNI::GetLongField,
2605 JNI::GetFloatField,
2606 JNI::GetDoubleField,
2607 JNI::SetObjectField,
2608 JNI::SetBooleanField,
2609 JNI::SetByteField,
2610 JNI::SetCharField,
2611 JNI::SetShortField,
2612 JNI::SetIntField,
2613 JNI::SetLongField,
2614 JNI::SetFloatField,
2615 JNI::SetDoubleField,
2616 JNI::GetStaticMethodID,
2617 JNI::CallStaticObjectMethod,
2618 JNI::CallStaticObjectMethodV,
2619 JNI::CallStaticObjectMethodA,
2620 JNI::CallStaticBooleanMethod,
2621 JNI::CallStaticBooleanMethodV,
2622 JNI::CallStaticBooleanMethodA,
2623 JNI::CallStaticByteMethod,
2624 JNI::CallStaticByteMethodV,
2625 JNI::CallStaticByteMethodA,
2626 JNI::CallStaticCharMethod,
2627 JNI::CallStaticCharMethodV,
2628 JNI::CallStaticCharMethodA,
2629 JNI::CallStaticShortMethod,
2630 JNI::CallStaticShortMethodV,
2631 JNI::CallStaticShortMethodA,
2632 JNI::CallStaticIntMethod,
2633 JNI::CallStaticIntMethodV,
2634 JNI::CallStaticIntMethodA,
2635 JNI::CallStaticLongMethod,
2636 JNI::CallStaticLongMethodV,
2637 JNI::CallStaticLongMethodA,
2638 JNI::CallStaticFloatMethod,
2639 JNI::CallStaticFloatMethodV,
2640 JNI::CallStaticFloatMethodA,
2641 JNI::CallStaticDoubleMethod,
2642 JNI::CallStaticDoubleMethodV,
2643 JNI::CallStaticDoubleMethodA,
2644 JNI::CallStaticVoidMethod,
2645 JNI::CallStaticVoidMethodV,
2646 JNI::CallStaticVoidMethodA,
2647 JNI::GetStaticFieldID,
2648 JNI::GetStaticObjectField,
2649 JNI::GetStaticBooleanField,
2650 JNI::GetStaticByteField,
2651 JNI::GetStaticCharField,
2652 JNI::GetStaticShortField,
2653 JNI::GetStaticIntField,
2654 JNI::GetStaticLongField,
2655 JNI::GetStaticFloatField,
2656 JNI::GetStaticDoubleField,
2657 JNI::SetStaticObjectField,
2658 JNI::SetStaticBooleanField,
2659 JNI::SetStaticByteField,
2660 JNI::SetStaticCharField,
2661 JNI::SetStaticShortField,
2662 JNI::SetStaticIntField,
2663 JNI::SetStaticLongField,
2664 JNI::SetStaticFloatField,
2665 JNI::SetStaticDoubleField,
2666 JNI::NewString,
2667 JNI::GetStringLength,
2668 JNI::GetStringChars,
2669 JNI::ReleaseStringChars,
2670 JNI::NewStringUTF,
2671 JNI::GetStringUTFLength,
2672 JNI::GetStringUTFChars,
2673 JNI::ReleaseStringUTFChars,
2674 JNI::GetArrayLength,
2675 JNI::NewObjectArray,
2676 JNI::GetObjectArrayElement,
2677 JNI::SetObjectArrayElement,
2678 JNI::NewBooleanArray,
2679 JNI::NewByteArray,
2680 JNI::NewCharArray,
2681 JNI::NewShortArray,
2682 JNI::NewIntArray,
2683 JNI::NewLongArray,
2684 JNI::NewFloatArray,
2685 JNI::NewDoubleArray,
2686 JNI::GetBooleanArrayElements,
2687 JNI::GetByteArrayElements,
2688 JNI::GetCharArrayElements,
2689 JNI::GetShortArrayElements,
2690 JNI::GetIntArrayElements,
2691 JNI::GetLongArrayElements,
2692 JNI::GetFloatArrayElements,
2693 JNI::GetDoubleArrayElements,
2694 JNI::ReleaseBooleanArrayElements,
2695 JNI::ReleaseByteArrayElements,
2696 JNI::ReleaseCharArrayElements,
2697 JNI::ReleaseShortArrayElements,
2698 JNI::ReleaseIntArrayElements,
2699 JNI::ReleaseLongArrayElements,
2700 JNI::ReleaseFloatArrayElements,
2701 JNI::ReleaseDoubleArrayElements,
2702 JNI::GetBooleanArrayRegion,
2703 JNI::GetByteArrayRegion,
2704 JNI::GetCharArrayRegion,
2705 JNI::GetShortArrayRegion,
2706 JNI::GetIntArrayRegion,
2707 JNI::GetLongArrayRegion,
2708 JNI::GetFloatArrayRegion,
2709 JNI::GetDoubleArrayRegion,
2710 JNI::SetBooleanArrayRegion,
2711 JNI::SetByteArrayRegion,
2712 JNI::SetCharArrayRegion,
2713 JNI::SetShortArrayRegion,
2714 JNI::SetIntArrayRegion,
2715 JNI::SetLongArrayRegion,
2716 JNI::SetFloatArrayRegion,
2717 JNI::SetDoubleArrayRegion,
2718 JNI::RegisterNatives,
2719 JNI::UnregisterNatives,
2720 JNI::MonitorEnter,
2721 JNI::MonitorExit,
2722 JNI::GetJavaVM,
2723 JNI::GetStringRegion,
2724 JNI::GetStringUTFRegion,
2725 JNI::GetPrimitiveArrayCritical,
2726 JNI::ReleasePrimitiveArrayCritical,
2727 JNI::GetStringCritical,
2728 JNI::ReleaseStringCritical,
2729 JNI::NewWeakGlobalRef,
2730 JNI::DeleteWeakGlobalRef,
2731 JNI::ExceptionCheck,
2732 JNI::NewDirectByteBuffer,
2733 JNI::GetDirectBufferAddress,
2734 JNI::GetDirectBufferCapacity,
2735 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002736};
2737
Ian Rogers68d8b422014-07-17 11:09:10 -07002738const JNINativeInterface* GetJniNativeInterface() {
2739 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002740}
2741
Elliott Hughesc8fece32013-01-02 11:27:23 -08002742void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002743 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002744 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002745 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002746 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2747 }
2748 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2749}
2750
Ian Rogersdf20fe02011-07-20 20:34:16 -07002751} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002752
2753std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2754 switch (rhs) {
2755 case JNIInvalidRefType:
2756 os << "JNIInvalidRefType";
2757 return os;
2758 case JNILocalRefType:
2759 os << "JNILocalRefType";
2760 return os;
2761 case JNIGlobalRefType:
2762 os << "JNIGlobalRefType";
2763 return os;
2764 case JNIWeakGlobalRefType:
2765 os << "JNIWeakGlobalRefType";
2766 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002767 default:
Ian Rogersc7dd2952014-10-21 23:31:19 -07002768 LOG(::art::FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
2769 UNREACHABLE();
Elliott Hughesb465ab02011-08-24 11:21:21 -07002770 }
2771}