blob: fc3826b0232721b63861acafb880809a825697f4 [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
Elliott Hughescdf53122011-08-19 15:46:09 -0700314class JNI {
315 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700316 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700317 return JNI_VERSION_1_6;
318 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700319
Ian Rogers25e8b912012-09-07 11:31:36 -0700320 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700321 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800322 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700323 }
324
Elliott Hughescdf53122011-08-19 15:46:09 -0700325 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700326 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700327 Runtime* runtime = Runtime::Current();
328 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700329 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700330 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800331 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700332 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700333 StackHandleScope<1> hs(soa.Self());
334 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800335 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700336 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800337 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700338 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700340 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700341
Ian Rogers62f05122014-03-21 11:21:29 -0700342 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700343 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700344 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700345 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700346 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700347
Ian Rogers62f05122014-03-21 11:21:29 -0700348 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700349 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350 ScopedObjectAccess soa(env);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700351 mirror::Object* obj_field = soa.Decode<mirror::Object*>(jlr_field);
352 if (obj_field->GetClass() != mirror::Field::StaticClass()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700353 // Not even a java.lang.reflect.Field, return null. TODO, is this check necessary?
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700354 return nullptr;
355 }
356 auto* field = static_cast<mirror::Field*>(obj_field);
357 return soa.EncodeField(field->GetArtField());
Elliott Hughescdf53122011-08-19 15:46:09 -0700358 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700359
Elliott Hughescdf53122011-08-19 15:46:09 -0700360 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700361 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700362 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800363 mirror::ArtMethod* m = soa.DecodeMethod(mid);
364 CHECK(!kMovingMethods);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700365 mirror::AbstractMethod* method;
Sebastien Hertzd3333762014-06-26 14:45:07 +0200366 if (m->IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700367 method = mirror::Constructor::CreateFromArtMethod(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200368 } else {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700369 method = mirror::Method::CreateFromArtMethod(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200370 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700371 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700372 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700373
Elliott Hughescdf53122011-08-19 15:46:09 -0700374 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700375 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700376 ScopedObjectAccess soa(env);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700377 ArtField* f = soa.DecodeField(fid);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700378 return soa.AddLocalReference<jobject>(mirror::Field::CreateFromArtField(soa.Self(), f, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700379 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700380
Elliott Hughes37f7a402011-08-22 18:56:01 -0700381 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700382 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700383 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800384 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700385 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700386 }
387
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700388 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700389 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800391 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700392 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700393 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700394
Narayan Kamath1268b742014-07-11 19:15:11 +0100395 // Note: java_class1 should be safely castable to java_class2, and
396 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700397 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700398 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
399 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800401 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
402 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100403 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700404 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700405
Elliott Hughese84278b2012-03-22 10:06:53 -0700406 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700407 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800408 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700409 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700410 return JNI_TRUE;
411 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700412 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800413 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
414 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700415 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700416 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700417 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700418
Elliott Hughes37f7a402011-08-22 18:56:01 -0700419 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700420 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800421 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
422 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700423 return JNI_ERR;
424 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000425 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700426 return JNI_OK;
427 }
428
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700429 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700430 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800431 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700432 }
433
434 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700435 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700436 }
437
438 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700439 ScopedObjectAccess soa(env);
440 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700441 }
442
443 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700444 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700445
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700446 // If we have no exception to describe, pass through.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000447 if (!soa.Self()->GetException()) {
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700448 return;
449 }
450
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000451 StackHandleScope<1> hs(soa.Self());
452 Handle<mirror::Throwable> old_exception(
453 hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()));
454 soa.Self()->ClearException();
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800455 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700456 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700457 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
458 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800459 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700460 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700461 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700462 } else {
463 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800464 if (soa.Self()->IsExceptionPending()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000465 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700466 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800467 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700468 }
469 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000470 soa.Self()->SetException(old_exception.Get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700471 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700472
Elliott Hughescdf53122011-08-19 15:46:09 -0700473 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700474 ScopedObjectAccess soa(env);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000475 mirror::Object* exception = soa.Self()->GetException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700476 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700477 }
478
Ian Rogers25e8b912012-09-07 11:31:36 -0700479 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700480 LOG(FATAL) << "JNI FatalError called: " << msg;
481 }
482
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700483 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700484 // TODO: SOA may not be necessary but I do it to please lock annotations.
485 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700486 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700487 return JNI_ERR;
488 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700489 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700490 return JNI_OK;
491 }
492
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700493 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700494 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800495 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700496 soa.Env()->PopFrame();
497 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700498 }
499
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700500 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700501 // TODO: SOA may not be necessary but I do it to please lock annotations.
502 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700503 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700504 }
505
Elliott Hughescdf53122011-08-19 15:46:09 -0700506 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700507 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800508 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700509 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700510 }
511
512 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700513 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
514 Thread* self = down_cast<JNIEnvExt*>(env)->self;
515 vm->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700516 }
517
518 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700519 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700520 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
521 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700522 }
523
524 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700525 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
526 Thread* self = down_cast<JNIEnvExt*>(env)->self;
527 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700528 }
529
530 static jobject NewLocalRef(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);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800533 // Check for null after decoding the object to handle cleared weak globals.
534 if (decoded_obj == nullptr) {
535 return nullptr;
536 }
537 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700538 }
539
Stephen Hines95c51b32014-11-26 01:24:13 -0800540 static void DeleteLocalRef(JNIEnv* env, jobject obj)
541 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800542 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700543 return;
544 }
Ian Rogersef28b142012-11-30 14:22:18 -0800545 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700546
Ian Rogersef28b142012-11-30 14:22:18 -0800547 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700548 if (!locals.Remove(cookie, obj)) {
549 // Attempting to delete a local reference that is not in the
550 // topmost local reference frame is a no-op. DeleteLocalRef returns
551 // void and doesn't throw any exceptions, but we should probably
552 // complain about it so the user will notice that things aren't
553 // going quite the way they expect.
554 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
555 << "failed to find entry";
556 }
557 }
558
559 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700560 if (obj1 == obj2) {
561 return JNI_TRUE;
562 } else {
563 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800564 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
565 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700566 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 }
568
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700569 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700570 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700571 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800572 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800573 if (c == nullptr) {
574 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700575 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800576 if (c->IsStringClass()) {
577 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
578 mirror::SetStringCountVisitor visitor(0);
579 return soa.AddLocalReference<jobject>(mirror::String::Alloc<true>(soa.Self(), 0,
580 allocator_type, visitor));
581 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700582 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700583 }
584
Ian Rogersbc939662013-08-15 10:26:54 -0700585 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700586 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700587 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700588 CHECK_NON_NULL_ARGUMENT(java_class);
589 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700590 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700591 va_end(args);
592 return result;
593 }
594
Elliott Hughes72025e52011-08-23 17:50:30 -0700595 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700596 CHECK_NON_NULL_ARGUMENT(java_class);
597 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700598 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800599 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800600 if (c == nullptr) {
601 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700602 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800603 if (c->IsStringClass()) {
604 // Replace calls to String.<init> with equivalent StringFactory call.
605 jmethodID sf_mid = WellKnownClasses::StringInitToStringFactoryMethodID(mid);
606 return CallStaticObjectMethodV(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
607 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800608 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800609 if (result == nullptr) {
610 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700611 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700612 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700613 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800614 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800615 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700616 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800617 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700618 }
619
Elliott Hughes72025e52011-08-23 17:50:30 -0700620 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700621 CHECK_NON_NULL_ARGUMENT(java_class);
622 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700623 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800624 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800625 if (c == nullptr) {
626 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700627 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800628 if (c->IsStringClass()) {
629 // Replace calls to String.<init> with equivalent StringFactory call.
630 jmethodID sf_mid = WellKnownClasses::StringInitToStringFactoryMethodID(mid);
631 return CallStaticObjectMethodA(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
632 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800633 mirror::Object* result = c->AllocObject(soa.Self());
634 if (result == nullptr) {
635 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700636 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700637 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700638 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800639 if (soa.Self()->IsExceptionPending()) {
640 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700641 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800642 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700643 }
644
Ian Rogersbc939662013-08-15 10:26:54 -0700645 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700646 CHECK_NON_NULL_ARGUMENT(java_class);
647 CHECK_NON_NULL_ARGUMENT(name);
648 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700649 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700650 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 }
652
Ian Rogersbc939662013-08-15 10:26:54 -0700653 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
654 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700655 CHECK_NON_NULL_ARGUMENT(java_class);
656 CHECK_NON_NULL_ARGUMENT(name);
657 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700658 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700659 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 }
661
Elliott Hughes72025e52011-08-23 17:50:30 -0700662 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700663 va_list ap;
664 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700665 CHECK_NON_NULL_ARGUMENT(obj);
666 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700667 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700669 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 }
672
Elliott Hughes72025e52011-08-23 17:50:30 -0700673 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700674 CHECK_NON_NULL_ARGUMENT(obj);
675 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700676 ScopedObjectAccess soa(env);
677 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
678 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700679 }
680
Elliott Hughes72025e52011-08-23 17:50:30 -0700681 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700682 CHECK_NON_NULL_ARGUMENT(obj);
683 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700684 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700685 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
686 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700687 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700688 }
689
Elliott Hughes72025e52011-08-23 17:50:30 -0700690 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700691 va_list ap;
692 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700693 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
694 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700695 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700697 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700698 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 }
700
Elliott Hughes72025e52011-08-23 17:50:30 -0700701 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700702 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
703 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700704 ScopedObjectAccess soa(env);
705 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700706 }
707
Elliott Hughes72025e52011-08-23 17:50:30 -0700708 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700709 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
710 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700711 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700712 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
713 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 }
715
Elliott Hughes72025e52011-08-23 17:50:30 -0700716 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700717 va_list ap;
718 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700719 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
720 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700721 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700722 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700723 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700724 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700725 }
726
Elliott Hughes72025e52011-08-23 17:50:30 -0700727 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700728 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
729 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730 ScopedObjectAccess soa(env);
731 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700732 }
733
Elliott Hughes72025e52011-08-23 17:50:30 -0700734 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700735 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
736 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700737 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700738 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
739 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700740 }
741
Elliott Hughes72025e52011-08-23 17:50:30 -0700742 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700743 va_list ap;
744 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700745 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
746 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700747 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700748 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700749 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700750 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 }
752
Elliott Hughes72025e52011-08-23 17:50:30 -0700753 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700754 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
755 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700756 ScopedObjectAccess soa(env);
757 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 }
759
Elliott Hughes72025e52011-08-23 17:50:30 -0700760 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700761 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
762 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700763 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700764 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
765 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 }
767
Elliott Hughes72025e52011-08-23 17:50:30 -0700768 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700769 va_list ap;
770 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700771 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
772 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700773 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700774 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700775 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700776 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 }
778
Elliott Hughes72025e52011-08-23 17:50:30 -0700779 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700780 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
781 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700782 ScopedObjectAccess soa(env);
783 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700784 }
785
Elliott Hughes72025e52011-08-23 17:50:30 -0700786 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700787 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
788 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700789 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700790 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
791 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700792 }
793
Elliott Hughes72025e52011-08-23 17:50:30 -0700794 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700795 va_list ap;
796 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700797 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
798 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700799 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700800 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700801 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700802 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 }
804
Elliott Hughes72025e52011-08-23 17:50:30 -0700805 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700806 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
807 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700808 ScopedObjectAccess soa(env);
809 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 }
811
Elliott Hughes72025e52011-08-23 17:50:30 -0700812 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700813 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
814 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700815 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700816 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
817 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700818 }
819
Elliott Hughes72025e52011-08-23 17:50:30 -0700820 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700821 va_list ap;
822 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700823 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
824 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700825 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700826 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700827 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700828 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 }
830
Elliott Hughes72025e52011-08-23 17:50:30 -0700831 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700832 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
833 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700834 ScopedObjectAccess soa(env);
835 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 }
837
Elliott Hughes72025e52011-08-23 17:50:30 -0700838 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700839 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
840 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700841 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700842 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
843 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 }
845
Elliott Hughes72025e52011-08-23 17:50:30 -0700846 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700847 va_list ap;
848 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700849 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
850 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700851 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700852 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700853 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700854 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 }
856
Elliott Hughes72025e52011-08-23 17:50:30 -0700857 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700858 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
859 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700860 ScopedObjectAccess soa(env);
861 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 }
863
Elliott Hughes72025e52011-08-23 17:50:30 -0700864 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700865 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
866 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700867 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700868 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
869 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 }
871
Elliott Hughes72025e52011-08-23 17:50:30 -0700872 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700873 va_list ap;
874 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700875 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
876 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700877 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700878 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700879 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700880 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700881 }
882
Elliott Hughes72025e52011-08-23 17:50:30 -0700883 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700884 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
885 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700886 ScopedObjectAccess soa(env);
887 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 }
889
Elliott Hughes72025e52011-08-23 17:50:30 -0700890 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700891 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
892 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700893 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700894 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
895 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 }
897
Elliott Hughes72025e52011-08-23 17:50:30 -0700898 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 va_list ap;
900 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700901 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
902 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700903 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -0700904 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 }
907
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700909 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
910 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700911 ScopedObjectAccess soa(env);
912 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 }
914
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700916 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
917 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700919 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700922 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700925 CHECK_NON_NULL_ARGUMENT(obj);
926 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700927 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700928 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
929 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 va_end(ap);
931 return local_result;
932 }
933
Ian Rogersbc939662013-08-15 10:26:54 -0700934 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
935 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700936 CHECK_NON_NULL_ARGUMENT(obj);
937 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700938 ScopedObjectAccess soa(env);
939 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
940 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 }
942
Ian Rogersbc939662013-08-15 10:26:54 -0700943 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
944 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700945 CHECK_NON_NULL_ARGUMENT(obj);
946 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700948 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700949 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 }
951
Ian Rogersbc939662013-08-15 10:26:54 -0700952 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
953 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700956 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
957 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700958 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700959 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700961 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Ian Rogersbc939662013-08-15 10:26:54 -0700964 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
965 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700966 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
967 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968 ScopedObjectAccess soa(env);
969 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 }
971
Ian Rogersbc939662013-08-15 10:26:54 -0700972 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
973 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700974 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
975 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700976 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700977 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 }
979
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700980 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700983 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
984 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700985 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700988 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 }
990
Ian Rogersbc939662013-08-15 10:26:54 -0700991 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
992 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700993 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
994 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700995 ScopedObjectAccess soa(env);
996 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Ian Rogersbc939662013-08-15 10:26:54 -0700999 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1000 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001001 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1002 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001004 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001007 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001010 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1011 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001012 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001015 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Ian Rogersbc939662013-08-15 10:26:54 -07001018 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1019 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001020 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1021 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 ScopedObjectAccess soa(env);
1023 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Ian Rogersbc939662013-08-15 10:26:54 -07001026 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1027 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001028 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1029 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001030 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001031 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001034 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001037 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1038 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001039 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001040 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001042 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Ian Rogersbc939662013-08-15 10:26:54 -07001045 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1046 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001047 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1048 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001049 ScopedObjectAccess soa(env);
1050 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Ian Rogersbc939662013-08-15 10:26:54 -07001053 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1054 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001055 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1056 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001057 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001058 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001061 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001064 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1065 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001066 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001067 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001069 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Ian Rogersbc939662013-08-15 10:26:54 -07001072 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1073 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001074 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1075 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 ScopedObjectAccess soa(env);
1077 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Ian Rogersbc939662013-08-15 10:26:54 -07001080 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1081 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001082 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1083 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001084 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001085 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001088 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001091 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1092 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001093 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001096 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Ian Rogersbc939662013-08-15 10:26:54 -07001099 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1100 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001101 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1102 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103 ScopedObjectAccess soa(env);
1104 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Ian Rogersbc939662013-08-15 10:26:54 -07001107 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1108 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001109 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1110 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001111 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001112 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001115 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001118 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1119 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001120 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001121 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001123 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Ian Rogersbc939662013-08-15 10:26:54 -07001126 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1127 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001128 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1129 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001130 ScopedObjectAccess soa(env);
1131 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Ian Rogersbc939662013-08-15 10:26:54 -07001134 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1135 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001136 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1137 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001138 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001139 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001142 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001145 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1146 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001147 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001150 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Ian Rogersbc939662013-08-15 10:26:54 -07001153 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1154 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001155 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1156 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001157 ScopedObjectAccess soa(env);
1158 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Ian Rogersbc939662013-08-15 10:26:54 -07001161 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1162 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001163 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1164 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001165 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001166 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001169 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001172 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1173 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001174 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001175 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 va_end(ap);
1177 }
1178
Brian Carlstromea46f952013-07-30 01:26:50 -07001179 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1180 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001181 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1182 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001183 ScopedObjectAccess soa(env);
1184 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
Ian Rogersbc939662013-08-15 10:26:54 -07001187 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1188 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001189 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1190 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001191 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001192 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 }
1194
Ian Rogersbc939662013-08-15 10:26:54 -07001195 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001196 CHECK_NON_NULL_ARGUMENT(java_class);
1197 CHECK_NON_NULL_ARGUMENT(name);
1198 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001199 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001200 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001202
Ian Rogersbc939662013-08-15 10:26:54 -07001203 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1204 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001205 CHECK_NON_NULL_ARGUMENT(java_class);
1206 CHECK_NON_NULL_ARGUMENT(name);
1207 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001208 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001209 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001211
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001212 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001213 CHECK_NON_NULL_ARGUMENT(obj);
1214 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001215 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001216 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001217 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001218 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001220
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001221 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001222 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001223 ScopedObjectAccess soa(env);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001224 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001225 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001228 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001229 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1230 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001231 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001232 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1233 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001234 ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001235 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 }
1237
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001238 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001239 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001240 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001241 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001242 ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001243 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001246#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001247 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1248 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001249 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001250 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001251 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001252 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001253
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001254#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001255 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001256 ScopedObjectAccess soa(env); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001257 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001258 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001259
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001260#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001261 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1262 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001263 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001264 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001265 ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001266 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001267
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001268#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001269 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001270 ScopedObjectAccess soa(env); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001271 ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001272 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001273
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001274 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001275 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001278 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001279 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
1281
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001282 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001283 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001286 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001287 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001290 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001291 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001294 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001295 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 }
1297
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001298 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001299 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001302 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001303 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001306 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001307 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001310 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001311 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001314 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001315 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001318 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001319 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001322 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001323 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001326 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001327 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001328 }
1329
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001331 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001332 }
1333
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001334 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001335 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001336 }
1337
1338 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001339 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001340 }
1341
1342 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001343 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001344 }
1345
1346 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001347 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001348 }
1349
1350 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001351 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001352 }
1353
1354 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001355 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001356 }
1357
1358 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001359 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001360 }
1361
1362 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001363 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001364 }
1365
1366 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001367 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001368 }
1369
1370 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001371 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001372 }
1373
1374 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001375 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001376 }
1377
1378 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001379 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001380 }
1381
1382 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001383 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001384 }
1385
1386 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001387 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 }
1389
1390 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001391 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 }
1393
1394 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001395 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 }
1397
1398 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001399 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001402 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001404 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001405 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001406 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001407 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001408 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 va_end(ap);
1410 return local_result;
1411 }
1412
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001413 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001414 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001415 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001416 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001417 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001420 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001421 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001422 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001423 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001424 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 }
1426
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001427 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001429 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001430 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001431 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001432 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001434 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
1436
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001437 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001438 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001440 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001443 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001444 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001445 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001446 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 }
1448
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001449 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001451 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001452 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001453 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001454 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001456 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001459 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001460 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001461 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001462 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 }
1464
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001465 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001466 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001467 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001468 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001471 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001473 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001474 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001475 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001476 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001478 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001479 }
1480
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001481 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001482 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001483 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001484 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001487 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001488 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001489 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001490 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 }
1492
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001493 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001495 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001496 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001497 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001498 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001500 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001503 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001504 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001505 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001506 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 }
1508
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001509 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001510 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001511 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001512 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 }
1514
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001515 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001517 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001518 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001519 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001520 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001522 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 }
1524
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001525 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001526 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001527 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001528 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
1530
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001531 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001532 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001533 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001534 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
1536
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001537 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001539 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001540 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001541 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001542 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001544 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001547 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001548 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001549 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001550 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001553 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001554 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001555 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001556 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001559 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001561 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001562 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001563 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001564 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001566 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001569 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001570 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001571 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001572 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001575 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001576 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001577 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001578 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 }
1580
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001581 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001583 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001584 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001585 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001586 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001588 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001591 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001592 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001593 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001594 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001597 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001598 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001599 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001600 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001603 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001606 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001607 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001608 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 va_end(ap);
1610 }
1611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001612 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001613 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001614 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001615 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001618 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001619 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001620 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001621 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes814e4032011-08-23 12:07:56 -07001624 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001625 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001626 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001627 return nullptr;
1628 }
1629 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001630 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001631 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001632 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001633 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001634 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001635 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 }
1637
1638 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001639 if (utf == nullptr) {
1640 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001642 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001643 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001644 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 }
1646
Elliott Hughes814e4032011-08-23 12:07:56 -07001647 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001648 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001649 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001650 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001651 }
1652
1653 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001654 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001655 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001656 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001657 }
1658
Ian Rogersbc939662013-08-15 10:26:54 -07001659 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1660 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001661 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001662 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001663 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001664 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001665 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001666 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001667 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Jeff Hao848f70a2014-01-15 13:49:50 -08001668 const jchar* chars = s->GetValue();
Elliott Hughesb465ab02011-08-24 11:21:21 -07001669 memcpy(buf, chars + start, length * sizeof(jchar));
1670 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001671 }
1672
Ian Rogersbc939662013-08-15 10:26:54 -07001673 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1674 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001675 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001676 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001677 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001678 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001679 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001680 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001681 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Jeff Hao848f70a2014-01-15 13:49:50 -08001682 const jchar* chars = s->GetValue();
Elliott Hughesb465ab02011-08-24 11:21:21 -07001683 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1684 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001685 }
1686
Elliott Hughes75770752011-08-24 17:52:38 -07001687 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001688 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001689 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001690 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001691 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Hao848f70a2014-01-15 13:49:50 -08001692 if (heap->IsMovableObject(s)) {
1693 jchar* chars = new jchar[s->GetLength()];
1694 memcpy(chars, s->GetValue(), sizeof(jchar) * s->GetLength());
Fred Shih56890e22014-06-02 11:11:52 -07001695 if (is_copy != nullptr) {
1696 *is_copy = JNI_TRUE;
1697 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001698 return chars;
Elliott Hughes75770752011-08-24 17:52:38 -07001699 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001700 if (is_copy != nullptr) {
1701 *is_copy = JNI_FALSE;
1702 }
1703 return static_cast<jchar*>(s->GetValue());
Elliott Hughes814e4032011-08-23 12:07:56 -07001704 }
1705
Mathieu Chartier590fee92013-09-13 13:46:47 -07001706 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001707 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001708 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001709 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08001710 if (chars != s->GetValue()) {
Fred Shih56890e22014-06-02 11:11:52 -07001711 delete[] chars;
1712 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 }
1714
Elliott Hughes75770752011-08-24 17:52:38 -07001715 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001716 CHECK_NON_NULL_ARGUMENT(java_string);
1717 ScopedObjectAccess soa(env);
1718 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001719 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Hao848f70a2014-01-15 13:49:50 -08001720 if (heap->IsMovableObject(s)) {
Fred Shih56890e22014-06-02 11:11:52 -07001721 StackHandleScope<1> hs(soa.Self());
Jeff Hao848f70a2014-01-15 13:49:50 -08001722 HandleWrapper<mirror::String> h(hs.NewHandleWrapper(&s));
Fred Shih56890e22014-06-02 11:11:52 -07001723 heap->IncrementDisableMovingGC(soa.Self());
1724 }
1725 if (is_copy != nullptr) {
1726 *is_copy = JNI_FALSE;
1727 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001728 return static_cast<jchar*>(s->GetValue());
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes75770752011-08-24 17:52:38 -07001731 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001732 UNUSED(chars);
Fred Shih56890e22014-06-02 11:11:52 -07001733 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1734 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001735 gc::Heap* heap = Runtime::Current()->GetHeap();
1736 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08001737 if (heap->IsMovableObject(s)) {
Fred Shih56890e22014-06-02 11:11:52 -07001738 heap->DecrementDisableMovingGC(soa.Self());
1739 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 }
1741
Elliott Hughes75770752011-08-24 17:52:38 -07001742 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001743 if (java_string == nullptr) {
1744 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001745 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001746 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001747 *is_copy = JNI_TRUE;
1748 }
Ian Rogersef28b142012-11-30 14:22:18 -08001749 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001750 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001751 size_t byte_count = s->GetUtfLength();
1752 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001753 CHECK(bytes != nullptr); // bionic aborts anyway.
Jeff Hao848f70a2014-01-15 13:49:50 -08001754 const uint16_t* chars = s->GetValue();
Elliott Hughes75770752011-08-24 17:52:38 -07001755 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1756 bytes[byte_count] = '\0';
1757 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001758 }
1759
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001760 static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001761 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001762 }
1763
Elliott Hughesbd935992011-08-22 11:59:34 -07001764 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001765 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001766 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001767 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001768 if (UNLIKELY(!obj->IsArrayInstance())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001769 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1770 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001771 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001772 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001773 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes814e4032011-08-23 12:07:56 -07001776 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001777 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001778 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001779 mirror::ObjectArray<mirror::Object>* array =
1780 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001781 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 }
1783
Ian Rogersbc939662013-08-15 10:26:54 -07001784 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1785 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001786 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001787 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001788 mirror::ObjectArray<mirror::Object>* array =
1789 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1790 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001791 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 }
1793
1794 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001795 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 }
1797
1798 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001799 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
1802 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001803 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
1806 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001807 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
1810 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001811 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
1814 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001815 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
1818 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001819 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
Ian Rogers1d99e452014-01-02 17:36:41 -08001822 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1823 jobject initial_element) {
1824 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001825 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001826 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001827 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001828 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001829
1830 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001831 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001832 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08001833 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001834 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08001835 if (UNLIKELY(element_class->IsPrimitive())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001836 soa.Vm()->JniAbortF("NewObjectArray", "not an object type: %s",
1837 PrettyDescriptor(element_class).c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08001838 return nullptr;
1839 }
Ian Rogers1d99e452014-01-02 17:36:41 -08001840 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07001841 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08001842 if (UNLIKELY(array_class == nullptr)) {
1843 return nullptr;
1844 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 }
1846
Elliott Hughes75770752011-08-24 17:52:38 -07001847 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001848 mirror::ObjectArray<mirror::Object>* result =
1849 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001850 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001851 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08001852 if (initial_object != nullptr) {
1853 mirror::Class* element_class = result->GetClass()->GetComponentType();
1854 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001855 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
1856 "element type of '%s'",
1857 PrettyDescriptor(initial_object->GetClass()).c_str(),
1858 PrettyDescriptor(element_class).c_str());
1859 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08001860 } else {
1861 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001862 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08001863 }
1864 }
Elliott Hughes75770752011-08-24 17:52:38 -07001865 }
1866 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001867 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 }
1869
1870 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001871 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 }
1873
Ian Rogersa15e67d2012-02-28 13:51:55 -08001874 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001875 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001876 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001877 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07001878 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001879 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
1880 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001881 return nullptr;
1882 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001883 gc::Heap* heap = Runtime::Current()->GetHeap();
1884 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08001885 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001886 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001887 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001888 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001889 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001890 *is_copy = JNI_FALSE;
1891 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001892 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001893 }
1894
Ian Rogers2d10b202014-05-12 19:15:18 -07001895 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
1896 jint mode) {
1897 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
1898 ScopedObjectAccess soa(env);
1899 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
1900 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001901 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
1902 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001903 return;
1904 }
1905 const size_t component_size = array->GetClass()->GetComponentSize();
1906 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001907 }
1908
Elliott Hughes75770752011-08-24 17:52:38 -07001909 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001910 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 }
1912
Elliott Hughes75770752011-08-24 17:52:38 -07001913 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001914 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001915 }
1916
Elliott Hughes75770752011-08-24 17:52:38 -07001917 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001918 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 }
1920
Elliott Hughes75770752011-08-24 17:52:38 -07001921 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001922 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 }
1924
Elliott Hughes75770752011-08-24 17:52:38 -07001925 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001926 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 }
1928
Elliott Hughes75770752011-08-24 17:52:38 -07001929 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001930 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 }
1932
Elliott Hughes75770752011-08-24 17:52:38 -07001933 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001934 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes75770752011-08-24 17:52:38 -07001937 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001938 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Mathieu Chartier590fee92013-09-13 13:46:47 -07001941 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
1942 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001943 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
1944 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Mathieu Chartier590fee92013-09-13 13:46:47 -07001947 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001948 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
Mathieu Chartier590fee92013-09-13 13:46:47 -07001951 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001952 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Mathieu Chartier590fee92013-09-13 13:46:47 -07001955 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
1956 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001957 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Mathieu Chartier590fee92013-09-13 13:46:47 -07001960 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
1961 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001962 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 }
1964
Mathieu Chartier590fee92013-09-13 13:46:47 -07001965 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001966 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 }
1968
Mathieu Chartier590fee92013-09-13 13:46:47 -07001969 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001970 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Mathieu Chartier590fee92013-09-13 13:46:47 -07001973 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
1974 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001975 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Ian Rogersbc939662013-08-15 10:26:54 -07001978 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
1979 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001980 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001981 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Ian Rogersbc939662013-08-15 10:26:54 -07001984 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
1985 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001986 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Ian Rogersbc939662013-08-15 10:26:54 -07001989 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
1990 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001991 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Ian Rogersbc939662013-08-15 10:26:54 -07001994 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
1995 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001996 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001997 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Ian Rogersbc939662013-08-15 10:26:54 -07002000 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2001 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002002 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002003 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Ian Rogersbc939662013-08-15 10:26:54 -07002006 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2007 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002008 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Ian Rogersbc939662013-08-15 10:26:54 -07002011 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2012 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002013 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Ian Rogersbc939662013-08-15 10:26:54 -07002016 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2017 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002018 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002019 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
Ian Rogersbc939662013-08-15 10:26:54 -07002022 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2023 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002024 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002025 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Ian Rogersbc939662013-08-15 10:26:54 -07002028 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2029 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002030 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Ian Rogersbc939662013-08-15 10:26:54 -07002033 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2034 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002035 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Ian Rogersbc939662013-08-15 10:26:54 -07002038 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2039 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002040 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002041 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Ian Rogersbc939662013-08-15 10:26:54 -07002044 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2045 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002046 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002047 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Ian Rogersbc939662013-08-15 10:26:54 -07002050 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2051 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002052 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Ian Rogersbc939662013-08-15 10:26:54 -07002055 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2056 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002057 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
Ian Rogersbc939662013-08-15 10:26:54 -07002060 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2061 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002062 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002063 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Ian Rogersbc939662013-08-15 10:26:54 -07002066 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2067 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002068 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2069 }
2070
Ian Rogersbc939662013-08-15 10:26:54 -07002071 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2072 jint method_count, bool return_errors) {
2073 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002074 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2075 method_count);
2076 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002077 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002078 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002079 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002080 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002081 if (UNLIKELY(method_count == 0)) {
2082 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2083 << PrettyDescriptor(c);
2084 return JNI_OK;
2085 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002086 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002087 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 const char* name = methods[i].name;
2089 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002090 const void* fnPtr = methods[i].fnPtr;
2091 if (UNLIKELY(name == nullptr)) {
2092 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2093 return JNI_ERR;
2094 } else if (UNLIKELY(sig == nullptr)) {
2095 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2096 return JNI_ERR;
2097 } else if (UNLIKELY(fnPtr == nullptr)) {
2098 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2099 return JNI_ERR;
2100 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002101 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002103 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ++sig;
2105 }
2106
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002107 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2108 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002109 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002111 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002112 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002113 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002114 << PrettyDescriptor(c) << "." << name << sig << " in "
2115 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002116 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002118 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002119 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002120 << PrettyDescriptor(c) << "." << name << sig
2121 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002122 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 return JNI_ERR;
2124 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002125
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002126 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002127
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002128 m->RegisterNative(fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130 return JNI_OK;
2131 }
2132
Elliott Hughes5174fe62011-08-23 15:12:35 -07002133 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002134 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002135 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002136 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002137
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002138 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002139
Ian Rogers2d10b202014-05-12 19:15:18 -07002140 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002141 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002142 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002144 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002145 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002146 }
2147 }
2148 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002149 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002151 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002152 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002153 }
2154 }
2155
Ian Rogers2d10b202014-05-12 19:15:18 -07002156 if (unregistered_count == 0) {
2157 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2158 << PrettyDescriptor(c) << "' that contains no native methods";
2159 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002160 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Ian Rogers719d1a32014-03-06 12:13:39 -08002163 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002164 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002165 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002166 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2167 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002168 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002169 return JNI_ERR;
2170 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002171 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002172 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 }
2174
Ian Rogers719d1a32014-03-06 12:13:39 -08002175 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002176 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002177 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002178 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002179 o->MonitorExit(soa.Self());
2180 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002181 return JNI_ERR;
2182 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002183 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002184 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
2187 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002188 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002190 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002191 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002193 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002195 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002199 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002200 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2201 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002202 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002203 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002204 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002205 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2206 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002207 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002208 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002209
Brian Carlstrom85a93362014-06-25 09:30:52 -07002210 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002211 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002212 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2213 "buffer capacity greater than maximum jint: %" PRId64,
2214 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002215 return nullptr;
2216 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002217 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002218 jint capacity_arg = static_cast<jint>(capacity);
2219
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002220 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2221 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002222 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002223 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225
Elliott Hughesb465ab02011-08-24 11:21:21 -07002226 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002227 return reinterpret_cast<void*>(env->GetLongField(
2228 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Elliott Hughesb465ab02011-08-24 11:21:21 -07002231 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002232 return static_cast<jlong>(env->GetIntField(
2233 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Andreas Gampea8763072014-12-20 00:08:35 -08002236 static jobjectRefType GetObjectRefType(JNIEnv* env ATTRIBUTE_UNUSED, jobject java_object) {
2237 if (java_object == nullptr) {
2238 return JNIInvalidRefType;
2239 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002240
2241 // Do we definitely know what kind of reference this is?
2242 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2243 IndirectRefKind kind = GetIndirectRefKind(ref);
2244 switch (kind) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002245 case kLocal:
2246 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247 case kGlobal:
2248 return JNIGlobalRefType;
2249 case kWeakGlobal:
2250 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002251 case kHandleScopeOrInvalid:
Ian Rogersc0542af2014-09-03 16:16:56 -07002252 // Assume value is in a handle scope.
2253 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002254 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002255 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
Andreas Gampea8763072014-12-20 00:08:35 -08002256 UNREACHABLE();
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002258
2259 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002260 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2261 const char* caller)
2262 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002263 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002264 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002265 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2266 return JNI_ERR;
2267 }
2268 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002269 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002270 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2271 if (!okay) {
2272 soa.Self()->ThrowOutOfMemoryError(caller);
2273 }
2274 return okay ? JNI_OK : JNI_ERR;
2275 }
2276
2277 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002278 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002279 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002280 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002281 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002282 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002283 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002284 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002285 return soa.AddLocalReference<JniT>(result);
2286 }
2287
Ian Rogers2d10b202014-05-12 19:15:18 -07002288 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2289 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2290 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002291 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002292 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002293 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002294 soa.Vm()->JniAbortF(fn_name,
2295 "attempt to %s %s primitive array elements with an object of type %s",
2296 operation,
2297 PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2298 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002299 return nullptr;
2300 }
2301 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2302 return array;
2303 }
2304
2305 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2306 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2307 CHECK_NON_NULL_ARGUMENT(java_array);
2308 ScopedObjectAccess soa(env);
2309 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2310 "GetArrayElements",
2311 "get");
2312 if (UNLIKELY(array == nullptr)) {
2313 return nullptr;
2314 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002315 // Only make a copy if necessary.
2316 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2317 if (is_copy != nullptr) {
2318 *is_copy = JNI_TRUE;
2319 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002320 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002321 size_t size = array->GetLength() * component_size;
2322 void* data = new uint64_t[RoundUp(size, 8) / 8];
2323 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002324 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002325 } else {
2326 if (is_copy != nullptr) {
2327 *is_copy = JNI_FALSE;
2328 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002329 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002330 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002331 }
2332
Ian Rogers2d10b202014-05-12 19:15:18 -07002333 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002334 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002335 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002336 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002337 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2338 "ReleaseArrayElements",
2339 "release");
2340 if (array == nullptr) {
2341 return;
2342 }
2343 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2344 }
2345
2346 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2347 size_t component_size, void* elements, jint mode)
2348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002349 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002350 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002351 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002352 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002353 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2354 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002355 if (is_copy) {
2356 // Sanity check: If elements is not the same as the java array's data, it better not be a
2357 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2358 // copies we make?
2359 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002360 soa.Vm()->JniAbortF("ReleaseArrayElements",
2361 "invalid element pointer %p, array elements are %p",
2362 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002363 return;
2364 }
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002365 if (mode != JNI_ABORT) {
2366 memcpy(array_data, elements, bytes);
2367 } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2368 // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2369 LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2370 soa.Self()->DumpJavaStack(LOG(WARNING));
2371 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002372 }
2373 if (mode != JNI_COMMIT) {
2374 if (is_copy) {
2375 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002376 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002377 // Non copy to a movable object must means that we had disabled the moving GC.
2378 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002379 }
2380 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002381 }
2382
Ian Rogers2d10b202014-05-12 19:15:18 -07002383 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2384 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2385 jsize start, jsize length, ElementT* buf) {
2386 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2387 ScopedObjectAccess soa(env);
2388 ArtArrayT* array =
2389 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2390 "GetPrimitiveArrayRegion",
2391 "get region of");
2392 if (array != nullptr) {
2393 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2394 ThrowAIOOBE(soa, array, start, length, "src");
2395 } else {
2396 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2397 ElementT* data = array->GetData();
2398 memcpy(buf, data + start, length * sizeof(ElementT));
2399 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002400 }
2401 }
2402
Ian Rogers2d10b202014-05-12 19:15:18 -07002403 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2404 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2405 jsize start, jsize length, const ElementT* buf) {
2406 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2407 ScopedObjectAccess soa(env);
2408 ArtArrayT* array =
2409 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2410 "SetPrimitiveArrayRegion",
2411 "set region of");
2412 if (array != nullptr) {
2413 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2414 ThrowAIOOBE(soa, array, start, length, "dst");
2415 } else {
2416 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2417 ElementT* data = array->GetData();
2418 memcpy(data + start, buf, length * sizeof(ElementT));
2419 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002420 }
2421 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002422};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002423
Elliott Hughes88c5c352012-03-15 18:49:48 -07002424const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002425 nullptr, // reserved0.
2426 nullptr, // reserved1.
2427 nullptr, // reserved2.
2428 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002429 JNI::GetVersion,
2430 JNI::DefineClass,
2431 JNI::FindClass,
2432 JNI::FromReflectedMethod,
2433 JNI::FromReflectedField,
2434 JNI::ToReflectedMethod,
2435 JNI::GetSuperclass,
2436 JNI::IsAssignableFrom,
2437 JNI::ToReflectedField,
2438 JNI::Throw,
2439 JNI::ThrowNew,
2440 JNI::ExceptionOccurred,
2441 JNI::ExceptionDescribe,
2442 JNI::ExceptionClear,
2443 JNI::FatalError,
2444 JNI::PushLocalFrame,
2445 JNI::PopLocalFrame,
2446 JNI::NewGlobalRef,
2447 JNI::DeleteGlobalRef,
2448 JNI::DeleteLocalRef,
2449 JNI::IsSameObject,
2450 JNI::NewLocalRef,
2451 JNI::EnsureLocalCapacity,
2452 JNI::AllocObject,
2453 JNI::NewObject,
2454 JNI::NewObjectV,
2455 JNI::NewObjectA,
2456 JNI::GetObjectClass,
2457 JNI::IsInstanceOf,
2458 JNI::GetMethodID,
2459 JNI::CallObjectMethod,
2460 JNI::CallObjectMethodV,
2461 JNI::CallObjectMethodA,
2462 JNI::CallBooleanMethod,
2463 JNI::CallBooleanMethodV,
2464 JNI::CallBooleanMethodA,
2465 JNI::CallByteMethod,
2466 JNI::CallByteMethodV,
2467 JNI::CallByteMethodA,
2468 JNI::CallCharMethod,
2469 JNI::CallCharMethodV,
2470 JNI::CallCharMethodA,
2471 JNI::CallShortMethod,
2472 JNI::CallShortMethodV,
2473 JNI::CallShortMethodA,
2474 JNI::CallIntMethod,
2475 JNI::CallIntMethodV,
2476 JNI::CallIntMethodA,
2477 JNI::CallLongMethod,
2478 JNI::CallLongMethodV,
2479 JNI::CallLongMethodA,
2480 JNI::CallFloatMethod,
2481 JNI::CallFloatMethodV,
2482 JNI::CallFloatMethodA,
2483 JNI::CallDoubleMethod,
2484 JNI::CallDoubleMethodV,
2485 JNI::CallDoubleMethodA,
2486 JNI::CallVoidMethod,
2487 JNI::CallVoidMethodV,
2488 JNI::CallVoidMethodA,
2489 JNI::CallNonvirtualObjectMethod,
2490 JNI::CallNonvirtualObjectMethodV,
2491 JNI::CallNonvirtualObjectMethodA,
2492 JNI::CallNonvirtualBooleanMethod,
2493 JNI::CallNonvirtualBooleanMethodV,
2494 JNI::CallNonvirtualBooleanMethodA,
2495 JNI::CallNonvirtualByteMethod,
2496 JNI::CallNonvirtualByteMethodV,
2497 JNI::CallNonvirtualByteMethodA,
2498 JNI::CallNonvirtualCharMethod,
2499 JNI::CallNonvirtualCharMethodV,
2500 JNI::CallNonvirtualCharMethodA,
2501 JNI::CallNonvirtualShortMethod,
2502 JNI::CallNonvirtualShortMethodV,
2503 JNI::CallNonvirtualShortMethodA,
2504 JNI::CallNonvirtualIntMethod,
2505 JNI::CallNonvirtualIntMethodV,
2506 JNI::CallNonvirtualIntMethodA,
2507 JNI::CallNonvirtualLongMethod,
2508 JNI::CallNonvirtualLongMethodV,
2509 JNI::CallNonvirtualLongMethodA,
2510 JNI::CallNonvirtualFloatMethod,
2511 JNI::CallNonvirtualFloatMethodV,
2512 JNI::CallNonvirtualFloatMethodA,
2513 JNI::CallNonvirtualDoubleMethod,
2514 JNI::CallNonvirtualDoubleMethodV,
2515 JNI::CallNonvirtualDoubleMethodA,
2516 JNI::CallNonvirtualVoidMethod,
2517 JNI::CallNonvirtualVoidMethodV,
2518 JNI::CallNonvirtualVoidMethodA,
2519 JNI::GetFieldID,
2520 JNI::GetObjectField,
2521 JNI::GetBooleanField,
2522 JNI::GetByteField,
2523 JNI::GetCharField,
2524 JNI::GetShortField,
2525 JNI::GetIntField,
2526 JNI::GetLongField,
2527 JNI::GetFloatField,
2528 JNI::GetDoubleField,
2529 JNI::SetObjectField,
2530 JNI::SetBooleanField,
2531 JNI::SetByteField,
2532 JNI::SetCharField,
2533 JNI::SetShortField,
2534 JNI::SetIntField,
2535 JNI::SetLongField,
2536 JNI::SetFloatField,
2537 JNI::SetDoubleField,
2538 JNI::GetStaticMethodID,
2539 JNI::CallStaticObjectMethod,
2540 JNI::CallStaticObjectMethodV,
2541 JNI::CallStaticObjectMethodA,
2542 JNI::CallStaticBooleanMethod,
2543 JNI::CallStaticBooleanMethodV,
2544 JNI::CallStaticBooleanMethodA,
2545 JNI::CallStaticByteMethod,
2546 JNI::CallStaticByteMethodV,
2547 JNI::CallStaticByteMethodA,
2548 JNI::CallStaticCharMethod,
2549 JNI::CallStaticCharMethodV,
2550 JNI::CallStaticCharMethodA,
2551 JNI::CallStaticShortMethod,
2552 JNI::CallStaticShortMethodV,
2553 JNI::CallStaticShortMethodA,
2554 JNI::CallStaticIntMethod,
2555 JNI::CallStaticIntMethodV,
2556 JNI::CallStaticIntMethodA,
2557 JNI::CallStaticLongMethod,
2558 JNI::CallStaticLongMethodV,
2559 JNI::CallStaticLongMethodA,
2560 JNI::CallStaticFloatMethod,
2561 JNI::CallStaticFloatMethodV,
2562 JNI::CallStaticFloatMethodA,
2563 JNI::CallStaticDoubleMethod,
2564 JNI::CallStaticDoubleMethodV,
2565 JNI::CallStaticDoubleMethodA,
2566 JNI::CallStaticVoidMethod,
2567 JNI::CallStaticVoidMethodV,
2568 JNI::CallStaticVoidMethodA,
2569 JNI::GetStaticFieldID,
2570 JNI::GetStaticObjectField,
2571 JNI::GetStaticBooleanField,
2572 JNI::GetStaticByteField,
2573 JNI::GetStaticCharField,
2574 JNI::GetStaticShortField,
2575 JNI::GetStaticIntField,
2576 JNI::GetStaticLongField,
2577 JNI::GetStaticFloatField,
2578 JNI::GetStaticDoubleField,
2579 JNI::SetStaticObjectField,
2580 JNI::SetStaticBooleanField,
2581 JNI::SetStaticByteField,
2582 JNI::SetStaticCharField,
2583 JNI::SetStaticShortField,
2584 JNI::SetStaticIntField,
2585 JNI::SetStaticLongField,
2586 JNI::SetStaticFloatField,
2587 JNI::SetStaticDoubleField,
2588 JNI::NewString,
2589 JNI::GetStringLength,
2590 JNI::GetStringChars,
2591 JNI::ReleaseStringChars,
2592 JNI::NewStringUTF,
2593 JNI::GetStringUTFLength,
2594 JNI::GetStringUTFChars,
2595 JNI::ReleaseStringUTFChars,
2596 JNI::GetArrayLength,
2597 JNI::NewObjectArray,
2598 JNI::GetObjectArrayElement,
2599 JNI::SetObjectArrayElement,
2600 JNI::NewBooleanArray,
2601 JNI::NewByteArray,
2602 JNI::NewCharArray,
2603 JNI::NewShortArray,
2604 JNI::NewIntArray,
2605 JNI::NewLongArray,
2606 JNI::NewFloatArray,
2607 JNI::NewDoubleArray,
2608 JNI::GetBooleanArrayElements,
2609 JNI::GetByteArrayElements,
2610 JNI::GetCharArrayElements,
2611 JNI::GetShortArrayElements,
2612 JNI::GetIntArrayElements,
2613 JNI::GetLongArrayElements,
2614 JNI::GetFloatArrayElements,
2615 JNI::GetDoubleArrayElements,
2616 JNI::ReleaseBooleanArrayElements,
2617 JNI::ReleaseByteArrayElements,
2618 JNI::ReleaseCharArrayElements,
2619 JNI::ReleaseShortArrayElements,
2620 JNI::ReleaseIntArrayElements,
2621 JNI::ReleaseLongArrayElements,
2622 JNI::ReleaseFloatArrayElements,
2623 JNI::ReleaseDoubleArrayElements,
2624 JNI::GetBooleanArrayRegion,
2625 JNI::GetByteArrayRegion,
2626 JNI::GetCharArrayRegion,
2627 JNI::GetShortArrayRegion,
2628 JNI::GetIntArrayRegion,
2629 JNI::GetLongArrayRegion,
2630 JNI::GetFloatArrayRegion,
2631 JNI::GetDoubleArrayRegion,
2632 JNI::SetBooleanArrayRegion,
2633 JNI::SetByteArrayRegion,
2634 JNI::SetCharArrayRegion,
2635 JNI::SetShortArrayRegion,
2636 JNI::SetIntArrayRegion,
2637 JNI::SetLongArrayRegion,
2638 JNI::SetFloatArrayRegion,
2639 JNI::SetDoubleArrayRegion,
2640 JNI::RegisterNatives,
2641 JNI::UnregisterNatives,
2642 JNI::MonitorEnter,
2643 JNI::MonitorExit,
2644 JNI::GetJavaVM,
2645 JNI::GetStringRegion,
2646 JNI::GetStringUTFRegion,
2647 JNI::GetPrimitiveArrayCritical,
2648 JNI::ReleasePrimitiveArrayCritical,
2649 JNI::GetStringCritical,
2650 JNI::ReleaseStringCritical,
2651 JNI::NewWeakGlobalRef,
2652 JNI::DeleteWeakGlobalRef,
2653 JNI::ExceptionCheck,
2654 JNI::NewDirectByteBuffer,
2655 JNI::GetDirectBufferAddress,
2656 JNI::GetDirectBufferCapacity,
2657 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002658};
2659
Ian Rogers68d8b422014-07-17 11:09:10 -07002660const JNINativeInterface* GetJniNativeInterface() {
2661 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002662}
2663
Elliott Hughesc8fece32013-01-02 11:27:23 -08002664void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002665 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002666 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002667 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002668 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2669 }
2670 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2671}
2672
Ian Rogersdf20fe02011-07-20 20:34:16 -07002673} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002674
2675std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2676 switch (rhs) {
2677 case JNIInvalidRefType:
2678 os << "JNIInvalidRefType";
2679 return os;
2680 case JNILocalRefType:
2681 os << "JNILocalRefType";
2682 return os;
2683 case JNIGlobalRefType:
2684 os << "JNIGlobalRefType";
2685 return os;
2686 case JNIWeakGlobalRefType:
2687 os << "JNIWeakGlobalRefType";
2688 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002689 default:
Ian Rogersc7dd2952014-10-21 23:31:19 -07002690 LOG(::art::FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
2691 UNREACHABLE();
Elliott Hughesb465ab02011-08-24 11:21:21 -07002692 }
2693}