blob: 6ab44551d0d2a987b864881994d29a73136f2cb2 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
Elliott Hughes0af55432011-08-17 18:37:28 -070023#include <utility>
24#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025
Mathieu Chartierc7853442015-03-27 14:35:38 -070026#include "art_field-inl.h"
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -070027#include "art_method-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080028#include "atomic.h"
Mathieu Chartierbad02672014-08-25 13:08:22 -070029#include "base/allocator.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080030#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080031#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080032#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080033#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070034#include "dex_file-inl.h"
Mathieu Chartierd0004802014-10-15 16:59:47 -070035#include "fault_handler.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070036#include "gc_root.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070037#include "gc/accounting/card_table-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070038#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070039#include "interpreter/interpreter.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070040#include "jni_env_ext.h"
41#include "java_vm_ext.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/class-inl.h"
43#include "mirror/class_loader.h"
Hiroshi Yamauchi02d2f292015-04-03 13:35:16 -070044#include "mirror/field-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070045#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070048#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "mirror/throwable.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080050#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070051#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070052#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070053#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070055#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070056#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070058#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070059
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070060namespace art {
61
Mathieu Chartier24555ad2014-10-06 13:41:33 -070062// Consider turning this on when there is errors which could be related to JNI array copies such as
63// things not rendering correctly. E.g. b/16858794
64static constexpr bool kWarnJniAbort = false;
65
Elliott Hughes6b436852011-08-12 10:16:44 -070066// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
67// separated with slashes but aren't wrapped with "L;" like regular descriptors
68// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
69// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
70// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070071static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070072 std::string result;
73 // Add the missing "L;" if necessary.
74 if (name[0] == '[') {
75 result = name;
76 } else {
77 result += 'L';
78 result += name;
79 result += ';';
80 }
81 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070082 if (result.find('.') != std::string::npos) {
83 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
84 << "\"" << name << "\"";
85 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -070086 }
87 return result;
88}
89
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080090static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 const char* name, const char* sig, const char* kind)
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 Chartier3d21bdf2015-04-22 13:56:20 -0700129 ArtMethod* method = nullptr;
130 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Elliott Hughescdf53122011-08-19 15:46:09 -0700131 if (is_static) {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700132 method = c->FindDirectMethod(name, sig, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700133 } else if (c->IsInterface()) {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700134 method = c->FindInterfaceMethod(name, sig, pointer_size);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700135 } else {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700136 method = c->FindVirtualMethod(name, sig, pointer_size);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800137 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700138 // No virtual method matching the signature. Search declared
139 // private methods and constructors.
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700140 method = c->FindDeclaredDirectMethod(name, sig, pointer_size);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700141 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700142 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800143 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800145 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700146 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700148}
149
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800150static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700152 ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700153 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
154 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700155 return soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700156 }
Brian Carlstromce888532013-10-10 00:32:58 -0700157 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800158 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700159 return method->GetDeclaringClass()->GetClassLoader();
160 }
161 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800162 mirror::ClassLoader* class_loader =
163 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
164 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700165 return class_loader;
166 }
167 // See if the override ClassLoader is set for gtests.
Ian Rogers68d8b422014-07-17 11:09:10 -0700168 class_loader = soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800169 if (class_loader != nullptr) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700170 // If so, CommonCompilerTest should have marked the runtime as a compiler not compiling an
171 // image.
172 CHECK(Runtime::Current()->IsAotCompiler());
Andreas Gampe4585f872015-03-27 23:45:15 -0700173 CHECK(!Runtime::Current()->IsCompilingBootImage());
Brian Carlstromce888532013-10-10 00:32:58 -0700174 return class_loader;
175 }
176 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800177 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700178}
179
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700180static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
181 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700183 StackHandleScope<2> hs(soa.Self());
184 Handle<mirror::Class> c(
185 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
186 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800187 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700188 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700189 ArtField* field = nullptr;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800190 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700191 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
192 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700193 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800194 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700195 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700196 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700197 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800198 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700199 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200 DCHECK(soa.Self()->IsExceptionPending());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800201 StackHandleScope<1> hs2(soa.Self());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000202 Handle<mirror::Throwable> cause(hs2.NewHandle(soa.Self()->GetException()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 soa.Self()->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700204 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000205 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800206 "no type \"%s\" found and so no field \"%s\" "
207 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700208 c->GetDescriptor(&temp));
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000209 soa.Self()->GetException()->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800210 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700212 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700213 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700214 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700215 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700217 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800219 if (field == nullptr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000220 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800221 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700222 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800223 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700224 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700226}
227
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800228static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700229 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700231 std::string type(PrettyTypeOf(array));
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000232 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800233 "%s offset=%d length=%d %s.length=%d",
234 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700235}
Ian Rogers0571d352011-11-03 19:51:38 -0700236
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
238 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700239 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000240 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800241 "offset=%d length=%d string.length()=%d", start, length,
242 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700243}
Elliott Hughes814e4032011-08-23 12:07:56 -0700244
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700246 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 // Turn the const char* into a java.lang.String.
248 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800249 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700250 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700251 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700252
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 // Choose an appropriate constructor and set up the arguments.
254 jvalue args[2];
255 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800256 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800258 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 signature = "(Ljava/lang/String;)V";
260 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800261 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262 signature = "(Ljava/lang/Throwable;)V";
263 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700264 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
266 args[0].l = s.get();
267 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700268 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800270 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800271 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800273 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274 return JNI_ERR;
275 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700276
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800277 ScopedLocalRef<jthrowable> exception(
278 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
279 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 return JNI_ERR;
281 }
Ian Rogersef28b142012-11-30 14:22:18 -0800282 ScopedObjectAccess soa(env);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000283 soa.Self()->SetException(soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700285}
286
Ian Rogers68d8b422014-07-17 11:09:10 -0700287static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
288 return reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughes75770752011-08-24 17:52:38 -0700289}
290
Ian Rogers2d10b202014-05-12 19:15:18 -0700291#define CHECK_NON_NULL_ARGUMENT(value) \
292 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700293
Ian Rogers2d10b202014-05-12 19:15:18 -0700294#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
295 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
296
297#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
298 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
299
300#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
301 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
302
303#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800304 if (UNLIKELY(value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700305 JavaVmExtFromEnv(env)->JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700306 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700307 }
308
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700309#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800310 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700311 JavaVmExtFromEnv(env)->JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700312 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700313 }
314
Andreas Gampe82566092015-05-18 15:52:22 -0700315template <bool kNative>
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700316static ArtMethod* FindMethod(mirror::Class* c, const StringPiece& name, const StringPiece& sig)
Andreas Gampe82566092015-05-18 15:52:22 -0700317 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700318 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
319 for (auto& method : c->GetDirectMethods(pointer_size)) {
320 if (kNative == method.IsNative() && name == method.GetName() && method.GetSignature() == sig) {
321 return &method;
Andreas Gampe82566092015-05-18 15:52:22 -0700322 }
323 }
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700324 for (auto& method : c->GetVirtualMethods(pointer_size)) {
325 if (kNative == method.IsNative() && name == method.GetName() && method.GetSignature() == sig) {
326 return &method;
Andreas Gampe82566092015-05-18 15:52:22 -0700327 }
328 }
Andreas Gampe82566092015-05-18 15:52:22 -0700329 return nullptr;
330}
331
Elliott Hughescdf53122011-08-19 15:46:09 -0700332class JNI {
333 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700334 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700335 return JNI_VERSION_1_6;
336 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700337
Ian Rogers25e8b912012-09-07 11:31:36 -0700338 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700339 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800340 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700341 }
342
Elliott Hughescdf53122011-08-19 15:46:09 -0700343 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700344 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700345 Runtime* runtime = Runtime::Current();
346 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700347 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700348 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800349 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700350 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700351 StackHandleScope<1> hs(soa.Self());
352 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800353 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700354 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800355 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700356 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700358 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700359
Ian Rogers62f05122014-03-21 11:21:29 -0700360 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700361 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700362 ScopedObjectAccess soa(env);
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700363 return soa.EncodeMethod(ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700364 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700365
Ian Rogers62f05122014-03-21 11:21:29 -0700366 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700367 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 ScopedObjectAccess soa(env);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700369 mirror::Object* obj_field = soa.Decode<mirror::Object*>(jlr_field);
370 if (obj_field->GetClass() != mirror::Field::StaticClass()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700371 // Not even a java.lang.reflect.Field, return null. TODO, is this check necessary?
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700372 return nullptr;
373 }
374 auto* field = static_cast<mirror::Field*>(obj_field);
375 return soa.EncodeField(field->GetArtField());
Elliott Hughescdf53122011-08-19 15:46:09 -0700376 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700377
Elliott Hughescdf53122011-08-19 15:46:09 -0700378 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700379 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 ScopedObjectAccess soa(env);
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700381 ArtMethod* m = soa.DecodeMethod(mid);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700382 mirror::AbstractMethod* method;
Sebastien Hertzd3333762014-06-26 14:45:07 +0200383 if (m->IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700384 method = mirror::Constructor::CreateFromArtMethod(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200385 } else {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700386 method = mirror::Method::CreateFromArtMethod(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200387 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700388 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700389 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700390
Elliott Hughescdf53122011-08-19 15:46:09 -0700391 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700392 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 ScopedObjectAccess soa(env);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700394 ArtField* f = soa.DecodeField(fid);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700395 return soa.AddLocalReference<jobject>(mirror::Field::CreateFromArtField(soa.Self(), f, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700396 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700397
Elliott Hughes37f7a402011-08-22 18:56:01 -0700398 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700399 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800401 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700402 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700403 }
404
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700405 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700406 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700407 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800408 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700409 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700410 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700411
Narayan Kamath1268b742014-07-11 19:15:11 +0100412 // Note: java_class1 should be safely castable to java_class2, and
413 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700414 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700415 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
416 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700417 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800418 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
419 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100420 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700421 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700422
Elliott Hughese84278b2012-03-22 10:06:53 -0700423 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700424 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800425 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700426 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700427 return JNI_TRUE;
428 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700429 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800430 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
431 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700432 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700433 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700434 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700435
Elliott Hughes37f7a402011-08-22 18:56:01 -0700436 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700437 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800438 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
439 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700440 return JNI_ERR;
441 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000442 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700443 return JNI_OK;
444 }
445
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700446 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700447 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800448 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700449 }
450
451 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700452 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700453 }
454
455 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700456 ScopedObjectAccess soa(env);
457 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700458 }
459
460 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700461 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700462
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700463 // If we have no exception to describe, pass through.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000464 if (!soa.Self()->GetException()) {
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700465 return;
466 }
467
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000468 StackHandleScope<1> hs(soa.Self());
469 Handle<mirror::Throwable> old_exception(
470 hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()));
471 soa.Self()->ClearException();
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800472 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700473 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700474 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
475 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800476 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700477 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700478 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700479 } else {
480 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800481 if (soa.Self()->IsExceptionPending()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000482 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700483 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800484 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700485 }
486 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000487 soa.Self()->SetException(old_exception.Get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700488 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700489
Elliott Hughescdf53122011-08-19 15:46:09 -0700490 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700491 ScopedObjectAccess soa(env);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000492 mirror::Object* exception = soa.Self()->GetException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700493 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700494 }
495
Ian Rogers25e8b912012-09-07 11:31:36 -0700496 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700497 LOG(FATAL) << "JNI FatalError called: " << msg;
498 }
499
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700500 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700501 // TODO: SOA may not be necessary but I do it to please lock annotations.
502 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700503 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700504 return JNI_ERR;
505 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700506 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700507 return JNI_OK;
508 }
509
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700510 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700511 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800512 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700513 soa.Env()->PopFrame();
514 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700515 }
516
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700517 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700518 // TODO: SOA may not be necessary but I do it to please lock annotations.
519 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700520 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700521 }
522
Elliott Hughescdf53122011-08-19 15:46:09 -0700523 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700524 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800525 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700526 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700527 }
528
529 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700530 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
531 Thread* self = down_cast<JNIEnvExt*>(env)->self;
532 vm->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700533 }
534
535 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700536 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700537 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
538 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700539 }
540
541 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700542 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
543 Thread* self = down_cast<JNIEnvExt*>(env)->self;
544 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700545 }
546
547 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700548 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800549 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800550 // Check for null after decoding the object to handle cleared weak globals.
551 if (decoded_obj == nullptr) {
552 return nullptr;
553 }
554 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700555 }
556
Stephen Hines95c51b32014-11-26 01:24:13 -0800557 static void DeleteLocalRef(JNIEnv* env, jobject obj)
558 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800559 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700560 return;
561 }
Ian Rogersef28b142012-11-30 14:22:18 -0800562 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700563
Ian Rogersef28b142012-11-30 14:22:18 -0800564 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700565 if (!locals.Remove(cookie, obj)) {
566 // Attempting to delete a local reference that is not in the
567 // topmost local reference frame is a no-op. DeleteLocalRef returns
568 // void and doesn't throw any exceptions, but we should probably
569 // complain about it so the user will notice that things aren't
570 // going quite the way they expect.
571 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
572 << "failed to find entry";
573 }
574 }
575
576 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700577 if (obj1 == obj2) {
578 return JNI_TRUE;
579 } else {
580 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800581 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
582 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700583 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700584 }
585
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700586 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700587 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700588 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800589 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800590 if (c == nullptr) {
591 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700592 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800593 if (c->IsStringClass()) {
594 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
595 mirror::SetStringCountVisitor visitor(0);
596 return soa.AddLocalReference<jobject>(mirror::String::Alloc<true>(soa.Self(), 0,
597 allocator_type, visitor));
598 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700599 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700600 }
601
Ian Rogersbc939662013-08-15 10:26:54 -0700602 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700603 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700604 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700605 CHECK_NON_NULL_ARGUMENT(java_class);
606 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700607 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700608 va_end(args);
609 return result;
610 }
611
Elliott Hughes72025e52011-08-23 17:50:30 -0700612 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700613 CHECK_NON_NULL_ARGUMENT(java_class);
614 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800616 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800617 if (c == nullptr) {
618 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700619 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800620 if (c->IsStringClass()) {
621 // Replace calls to String.<init> with equivalent StringFactory call.
622 jmethodID sf_mid = WellKnownClasses::StringInitToStringFactoryMethodID(mid);
623 return CallStaticObjectMethodV(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
624 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800625 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800626 if (result == nullptr) {
627 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700628 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700629 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700630 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800631 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800632 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700633 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800634 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 }
636
Elliott Hughes72025e52011-08-23 17:50:30 -0700637 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700638 CHECK_NON_NULL_ARGUMENT(java_class);
639 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700640 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800641 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800642 if (c == nullptr) {
643 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700644 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800645 if (c->IsStringClass()) {
646 // Replace calls to String.<init> with equivalent StringFactory call.
647 jmethodID sf_mid = WellKnownClasses::StringInitToStringFactoryMethodID(mid);
648 return CallStaticObjectMethodA(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
649 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800650 mirror::Object* result = c->AllocObject(soa.Self());
651 if (result == nullptr) {
652 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700653 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700655 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800656 if (soa.Self()->IsExceptionPending()) {
657 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700658 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800659 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 }
661
Ian Rogersbc939662013-08-15 10:26:54 -0700662 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700663 CHECK_NON_NULL_ARGUMENT(java_class);
664 CHECK_NON_NULL_ARGUMENT(name);
665 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700666 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700667 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700668 }
669
Ian Rogersbc939662013-08-15 10:26:54 -0700670 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
671 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700672 CHECK_NON_NULL_ARGUMENT(java_class);
673 CHECK_NON_NULL_ARGUMENT(name);
674 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700675 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700676 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700677 }
678
Elliott Hughes72025e52011-08-23 17:50:30 -0700679 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700680 va_list ap;
681 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700682 CHECK_NON_NULL_ARGUMENT(obj);
683 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700684 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700685 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700686 va_end(ap);
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 jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700691 CHECK_NON_NULL_ARGUMENT(obj);
692 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700693 ScopedObjectAccess soa(env);
694 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
695 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 }
697
Elliott Hughes72025e52011-08-23 17:50:30 -0700698 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700699 CHECK_NON_NULL_ARGUMENT(obj);
700 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700701 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -0700702 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700703 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700704 }
705
Elliott Hughes72025e52011-08-23 17:50:30 -0700706 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700707 va_list ap;
708 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700709 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
710 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700711 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700713 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700714 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700715 }
716
Elliott Hughes72025e52011-08-23 17:50:30 -0700717 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700718 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
719 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700720 ScopedObjectAccess soa(env);
721 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 }
723
Elliott Hughes72025e52011-08-23 17:50:30 -0700724 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700725 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
726 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700727 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -0700728 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700729 }
730
Elliott Hughes72025e52011-08-23 17:50:30 -0700731 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700732 va_list ap;
733 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700734 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
735 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700736 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700737 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700738 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700739 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700740 }
741
Elliott Hughes72025e52011-08-23 17:50:30 -0700742 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700743 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
744 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700745 ScopedObjectAccess soa(env);
746 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700747 }
748
Elliott Hughes72025e52011-08-23 17:50:30 -0700749 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700750 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
751 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700752 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -0700753 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 }
755
Elliott Hughes72025e52011-08-23 17:50:30 -0700756 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700757 va_list ap;
758 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700759 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
760 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700761 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700762 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700763 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700764 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 }
766
Elliott Hughes72025e52011-08-23 17:50:30 -0700767 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700768 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
769 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700770 ScopedObjectAccess soa(env);
771 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 }
773
Elliott Hughes72025e52011-08-23 17:50:30 -0700774 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700775 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
776 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700777 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -0700778 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 }
780
Elliott Hughes72025e52011-08-23 17:50:30 -0700781 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700782 va_list ap;
783 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700784 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
785 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700786 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700787 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700788 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700789 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700790 }
791
Elliott Hughes72025e52011-08-23 17:50:30 -0700792 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700793 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
794 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700795 ScopedObjectAccess soa(env);
796 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 }
798
Elliott Hughes72025e52011-08-23 17:50:30 -0700799 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700800 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
801 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700802 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -0700803 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 }
805
Elliott Hughes72025e52011-08-23 17:50:30 -0700806 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700807 va_list ap;
808 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700809 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
810 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700811 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700813 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700814 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 }
816
Elliott Hughes72025e52011-08-23 17:50:30 -0700817 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700818 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
819 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700820 ScopedObjectAccess soa(env);
821 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 }
823
Elliott Hughes72025e52011-08-23 17:50:30 -0700824 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700825 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
826 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700827 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -0700828 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 }
830
Elliott Hughes72025e52011-08-23 17:50:30 -0700831 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700832 va_list ap;
833 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700834 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
835 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700836 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700837 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700838 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700839 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 }
841
Elliott Hughes72025e52011-08-23 17:50:30 -0700842 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700843 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
844 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 ScopedObjectAccess soa(env);
846 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 }
848
Elliott Hughes72025e52011-08-23 17:50:30 -0700849 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700850 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
851 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700852 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -0700853 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 }
855
Elliott Hughes72025e52011-08-23 17:50:30 -0700856 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700857 va_list ap;
858 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700859 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
860 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700861 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700862 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700863 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700864 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 }
866
Elliott Hughes72025e52011-08-23 17:50:30 -0700867 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700868 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
869 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700870 ScopedObjectAccess soa(env);
871 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 }
873
Elliott Hughes72025e52011-08-23 17:50:30 -0700874 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700875 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
876 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700877 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -0700878 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 }
880
Elliott Hughes72025e52011-08-23 17:50:30 -0700881 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700882 va_list ap;
883 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700884 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
885 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700886 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700887 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700888 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700889 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 }
891
Elliott Hughes72025e52011-08-23 17:50:30 -0700892 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700893 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
894 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700895 ScopedObjectAccess soa(env);
896 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 }
898
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700900 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
901 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700902 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -0700903 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 }
905
Elliott Hughes72025e52011-08-23 17:50:30 -0700906 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 va_list ap;
908 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700909 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
910 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700911 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -0700912 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700913 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 }
915
Elliott Hughes72025e52011-08-23 17:50:30 -0700916 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700917 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
918 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700919 ScopedObjectAccess soa(env);
920 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 }
922
Elliott Hughes72025e52011-08-23 17:50:30 -0700923 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700924 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
925 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700926 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -0700927 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 }
929
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700930 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700933 CHECK_NON_NULL_ARGUMENT(obj);
934 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700935 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
937 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 va_end(ap);
939 return local_result;
940 }
941
Ian Rogersbc939662013-08-15 10:26:54 -0700942 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
943 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700944 CHECK_NON_NULL_ARGUMENT(obj);
945 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700946 ScopedObjectAccess soa(env);
947 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
948 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 }
950
Ian Rogersbc939662013-08-15 10:26:54 -0700951 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
952 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700953 CHECK_NON_NULL_ARGUMENT(obj);
954 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700955 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -0700956 JValue result(InvokeWithJValues(soa, obj, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700957 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Ian Rogersbc939662013-08-15 10:26:54 -0700960 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
961 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700964 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
965 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700966 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700967 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700969 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 }
971
Ian Rogersbc939662013-08-15 10:26:54 -0700972 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
973 va_list 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);
977 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 }
979
Ian Rogersbc939662013-08-15 10:26:54 -0700980 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
981 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700982 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
983 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700984 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -0700985 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700988 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700991 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
992 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700993 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700996 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Ian Rogersbc939662013-08-15 10:26:54 -0700999 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1000 va_list 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);
1004 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Ian Rogersbc939662013-08-15 10:26:54 -07001007 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1008 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001009 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1010 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -07001012 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001015 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001018 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1019 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001020 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001021 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001023 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Ian Rogersbc939662013-08-15 10:26:54 -07001026 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1027 va_list 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);
1031 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Ian Rogersbc939662013-08-15 10:26:54 -07001034 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1035 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001036 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1037 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -07001039 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001042 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001045 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1046 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001047 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001050 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Ian Rogersbc939662013-08-15 10:26:54 -07001053 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1054 va_list 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);
1058 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Ian Rogersbc939662013-08-15 10:26:54 -07001061 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1062 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001063 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1064 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001065 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -07001066 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 }
1068
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001069 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001072 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1073 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001074 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001075 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001077 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Ian Rogersbc939662013-08-15 10:26:54 -07001080 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1081 va_list 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);
1085 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Ian Rogersbc939662013-08-15 10:26:54 -07001088 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1089 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001090 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1091 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001092 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -07001093 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001096 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001099 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1100 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001101 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001102 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001104 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Ian Rogersbc939662013-08-15 10:26:54 -07001107 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1108 va_list 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);
1112 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Ian Rogersbc939662013-08-15 10:26:54 -07001115 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1116 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001117 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1118 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -07001120 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001123 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001126 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1127 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001128 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001129 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001131 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Ian Rogersbc939662013-08-15 10:26:54 -07001134 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1135 va_list 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);
1139 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Ian Rogersbc939662013-08-15 10:26:54 -07001142 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1143 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001144 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1145 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001146 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -07001147 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001150 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001153 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1154 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001155 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001158 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Ian Rogersbc939662013-08-15 10:26:54 -07001161 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1162 va_list 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);
1166 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
Ian Rogersbc939662013-08-15 10:26:54 -07001169 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1170 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001171 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1172 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001173 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -07001174 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001177 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001180 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1181 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001182 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001183 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 va_end(ap);
1185 }
1186
Brian Carlstromea46f952013-07-30 01:26:50 -07001187 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1188 va_list 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);
1192 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 }
1194
Ian Rogersbc939662013-08-15 10:26:54 -07001195 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1196 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001197 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1198 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001199 ScopedObjectAccess soa(env);
Jeff Hao15e9ad12015-05-19 20:30:23 -07001200 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Ian Rogersbc939662013-08-15 10:26:54 -07001203 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001204 CHECK_NON_NULL_ARGUMENT(java_class);
1205 CHECK_NON_NULL_ARGUMENT(name);
1206 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001207 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001208 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001210
Ian Rogersbc939662013-08-15 10:26:54 -07001211 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1212 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001213 CHECK_NON_NULL_ARGUMENT(java_class);
1214 CHECK_NON_NULL_ARGUMENT(name);
1215 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001216 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001217 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001219
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001220 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001221 CHECK_NON_NULL_ARGUMENT(obj);
1222 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001223 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001224 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001225 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001226 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001228
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001229 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001230 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001231 ScopedObjectAccess soa(env);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001232 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001233 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001236 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001237 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1238 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001239 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001240 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1241 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>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001246 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001247 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001248 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001249 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001250 ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001251 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001254#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001255 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1256 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001257 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001258 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001259 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001260 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001261
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001262#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001263 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001264 ScopedObjectAccess soa(env); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001265 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001266 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001267
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001268#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001269 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1270 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001271 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001272 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001273 ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001274 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001275
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001276#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001277 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001278 ScopedObjectAccess soa(env); \
Mathieu Chartierc7853442015-03-27 14:35:38 -07001279 ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001280 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001281
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001282 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001283 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001286 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001287 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001290 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001291 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001294 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001295 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 }
1297
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001298 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001299 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001302 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001303 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001306 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001307 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001310 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001311 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001314 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001315 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001318 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001319 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001322 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001323 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001326 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001327 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001331 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
1333
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001334 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001335 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001336 }
1337
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001338 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001339 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001340 }
1341
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001342 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001343 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001344 }
1345
1346 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001347 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001348 }
1349
1350 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001351 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001352 }
1353
1354 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001355 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001356 }
1357
1358 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001359 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001360 }
1361
1362 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001363 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001364 }
1365
1366 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001367 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001368 }
1369
1370 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001371 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001372 }
1373
1374 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001375 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001376 }
1377
1378 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001379 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001380 }
1381
1382 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001383 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001384 }
1385
1386 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001387 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 }
1389
1390 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001391 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 }
1393
1394 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001395 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 }
1397
1398 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001399 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 }
1401
1402 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001403 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404 }
1405
1406 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001407 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 }
1409
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001410 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001412 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001413 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001414 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001415 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001416 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 va_end(ap);
1418 return local_result;
1419 }
1420
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001421 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001422 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001423 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001424 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001425 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001428 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001429 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001430 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001431 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001432 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001435 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001437 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001438 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001439 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001440 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001442 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
1444
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001445 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001446 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001447 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001448 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001451 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001452 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001453 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001454 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 }
1456
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001457 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001459 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001460 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001461 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001462 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001464 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001467 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001468 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001469 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001470 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001471 }
1472
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001473 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001474 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001475 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001476 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001479 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001481 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001482 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001483 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001484 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001486 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 }
1488
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001489 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001490 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001491 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001492 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001495 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001496 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001497 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001498 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 }
1500
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001501 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001503 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001504 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001505 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001506 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001508 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
1510
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001511 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001512 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001513 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001514 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 }
1516
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001517 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001518 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001519 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001520 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 }
1522
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001523 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001525 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001526 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001527 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001528 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001530 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001531 }
1532
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001533 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001534 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001535 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001536 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001539 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001540 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001541 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001542 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001545 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001547 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001548 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001549 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001550 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001552 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001555 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001556 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001557 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001558 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001561 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001562 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001563 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001564 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 }
1566
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001567 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001569 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001570 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001571 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001572 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001574 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001577 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001578 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001579 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001580 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001583 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001584 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001585 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001586 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001589 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001591 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001592 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001593 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001594 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001596 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001599 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001600 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001601 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001602 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001605 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001606 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001607 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001608 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 }
1610
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001611 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001614 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001615 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001616 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 va_end(ap);
1618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001621 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001622 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001623 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001626 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001627 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001628 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001629 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes814e4032011-08-23 12:07:56 -07001632 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001633 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001634 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001635 return nullptr;
1636 }
1637 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001638 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001639 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001640 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001641 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001642 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001643 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
1645
1646 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001647 if (utf == nullptr) {
1648 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001650 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001651 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001652 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 }
1654
Elliott Hughes814e4032011-08-23 12:07:56 -07001655 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001656 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001657 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001658 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001659 }
1660
1661 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001662 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001663 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001664 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001665 }
1666
Ian Rogersbc939662013-08-15 10:26:54 -07001667 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1668 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001669 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001671 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001672 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001673 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001674 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001675 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Jeff Hao848f70a2014-01-15 13:49:50 -08001676 const jchar* chars = s->GetValue();
Elliott Hughesb465ab02011-08-24 11:21:21 -07001677 memcpy(buf, chars + start, length * sizeof(jchar));
1678 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001679 }
1680
Ian Rogersbc939662013-08-15 10:26:54 -07001681 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1682 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001683 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001684 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001685 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001686 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001688 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001689 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Jeff Hao848f70a2014-01-15 13:49:50 -08001690 const jchar* chars = s->GetValue();
Elliott Hughesb465ab02011-08-24 11:21:21 -07001691 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1692 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001693 }
1694
Elliott Hughes75770752011-08-24 17:52:38 -07001695 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001696 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001697 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001698 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001699 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Hao848f70a2014-01-15 13:49:50 -08001700 if (heap->IsMovableObject(s)) {
1701 jchar* chars = new jchar[s->GetLength()];
1702 memcpy(chars, s->GetValue(), sizeof(jchar) * s->GetLength());
Fred Shih56890e22014-06-02 11:11:52 -07001703 if (is_copy != nullptr) {
1704 *is_copy = JNI_TRUE;
1705 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001706 return chars;
Elliott Hughes75770752011-08-24 17:52:38 -07001707 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001708 if (is_copy != nullptr) {
1709 *is_copy = JNI_FALSE;
1710 }
1711 return static_cast<jchar*>(s->GetValue());
Elliott Hughes814e4032011-08-23 12:07:56 -07001712 }
1713
Mathieu Chartier590fee92013-09-13 13:46:47 -07001714 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001715 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001716 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001717 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08001718 if (chars != s->GetValue()) {
Fred Shih56890e22014-06-02 11:11:52 -07001719 delete[] chars;
1720 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 }
1722
Elliott Hughes75770752011-08-24 17:52:38 -07001723 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001724 CHECK_NON_NULL_ARGUMENT(java_string);
1725 ScopedObjectAccess soa(env);
1726 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001727 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Hao848f70a2014-01-15 13:49:50 -08001728 if (heap->IsMovableObject(s)) {
Fred Shih56890e22014-06-02 11:11:52 -07001729 StackHandleScope<1> hs(soa.Self());
Jeff Hao848f70a2014-01-15 13:49:50 -08001730 HandleWrapper<mirror::String> h(hs.NewHandleWrapper(&s));
Fred Shih56890e22014-06-02 11:11:52 -07001731 heap->IncrementDisableMovingGC(soa.Self());
1732 }
1733 if (is_copy != nullptr) {
1734 *is_copy = JNI_FALSE;
1735 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001736 return static_cast<jchar*>(s->GetValue());
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes75770752011-08-24 17:52:38 -07001739 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001740 UNUSED(chars);
Fred Shih56890e22014-06-02 11:11:52 -07001741 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1742 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001743 gc::Heap* heap = Runtime::Current()->GetHeap();
1744 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08001745 if (heap->IsMovableObject(s)) {
Fred Shih56890e22014-06-02 11:11:52 -07001746 heap->DecrementDisableMovingGC(soa.Self());
1747 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
Elliott Hughes75770752011-08-24 17:52:38 -07001750 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001751 if (java_string == nullptr) {
1752 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001753 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001754 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001755 *is_copy = JNI_TRUE;
1756 }
Ian Rogersef28b142012-11-30 14:22:18 -08001757 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001758 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001759 size_t byte_count = s->GetUtfLength();
1760 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001761 CHECK(bytes != nullptr); // bionic aborts anyway.
Jeff Hao848f70a2014-01-15 13:49:50 -08001762 const uint16_t* chars = s->GetValue();
Elliott Hughes75770752011-08-24 17:52:38 -07001763 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1764 bytes[byte_count] = '\0';
1765 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001766 }
1767
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001768 static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001769 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001770 }
1771
Elliott Hughesbd935992011-08-22 11:59:34 -07001772 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001773 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001774 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001775 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001776 if (UNLIKELY(!obj->IsArrayInstance())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001777 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1778 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001779 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001780 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001781 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 }
1783
Elliott Hughes814e4032011-08-23 12:07:56 -07001784 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001785 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001786 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001787 mirror::ObjectArray<mirror::Object>* array =
1788 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 }
1791
Ian Rogersbc939662013-08-15 10:26:54 -07001792 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1793 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001794 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001795 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001796 mirror::ObjectArray<mirror::Object>* array =
1797 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1798 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001799 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
1802 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001803 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
1806 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001807 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
1810 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001811 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
1814 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001815 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
1818 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001819 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
1822 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001823 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
1826 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001827 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
Ian Rogers1d99e452014-01-02 17:36:41 -08001830 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1831 jobject initial_element) {
1832 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001833 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001834 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001835 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001836 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001837
1838 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001839 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001840 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08001841 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001842 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08001843 if (UNLIKELY(element_class->IsPrimitive())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001844 soa.Vm()->JniAbortF("NewObjectArray", "not an object type: %s",
1845 PrettyDescriptor(element_class).c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08001846 return nullptr;
1847 }
Ian Rogers1d99e452014-01-02 17:36:41 -08001848 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07001849 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08001850 if (UNLIKELY(array_class == nullptr)) {
1851 return nullptr;
1852 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 }
1854
Elliott Hughes75770752011-08-24 17:52:38 -07001855 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001856 mirror::ObjectArray<mirror::Object>* result =
1857 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001858 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001859 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08001860 if (initial_object != nullptr) {
1861 mirror::Class* element_class = result->GetClass()->GetComponentType();
1862 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001863 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
1864 "element type of '%s'",
1865 PrettyDescriptor(initial_object->GetClass()).c_str(),
1866 PrettyDescriptor(element_class).c_str());
1867 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08001868 } else {
1869 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001870 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08001871 }
1872 }
Elliott Hughes75770752011-08-24 17:52:38 -07001873 }
1874 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001875 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 }
1877
1878 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001879 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 }
1881
Ian Rogersa15e67d2012-02-28 13:51:55 -08001882 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001883 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001885 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07001886 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001887 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
1888 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001889 return nullptr;
1890 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001891 gc::Heap* heap = Runtime::Current()->GetHeap();
1892 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08001893 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001894 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001895 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001896 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001897 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001898 *is_copy = JNI_FALSE;
1899 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001900 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001901 }
1902
Ian Rogers2d10b202014-05-12 19:15:18 -07001903 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
1904 jint mode) {
1905 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
1906 ScopedObjectAccess soa(env);
1907 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
1908 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001909 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
1910 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001911 return;
1912 }
1913 const size_t component_size = array->GetClass()->GetComponentSize();
1914 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001915 }
1916
Elliott Hughes75770752011-08-24 17:52:38 -07001917 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001918 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 }
1920
Elliott Hughes75770752011-08-24 17:52:38 -07001921 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001922 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 }
1924
Elliott Hughes75770752011-08-24 17:52:38 -07001925 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001926 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 }
1928
Elliott Hughes75770752011-08-24 17:52:38 -07001929 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001930 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 }
1932
Elliott Hughes75770752011-08-24 17:52:38 -07001933 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001934 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes75770752011-08-24 17:52:38 -07001937 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001938 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Elliott Hughes75770752011-08-24 17:52:38 -07001941 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001942 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
Elliott Hughes75770752011-08-24 17:52:38 -07001945 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001946 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 }
1948
Mathieu Chartier590fee92013-09-13 13:46:47 -07001949 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
1950 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001951 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
1952 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Mathieu Chartier590fee92013-09-13 13:46:47 -07001955 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001956 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 }
1958
Mathieu Chartier590fee92013-09-13 13:46:47 -07001959 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001960 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 }
1962
Mathieu Chartier590fee92013-09-13 13:46:47 -07001963 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
1964 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001965 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Mathieu Chartier590fee92013-09-13 13:46:47 -07001968 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
1969 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001970 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Mathieu Chartier590fee92013-09-13 13:46:47 -07001973 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001974 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
Mathieu Chartier590fee92013-09-13 13:46:47 -07001977 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001978 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Mathieu Chartier590fee92013-09-13 13:46:47 -07001981 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
1982 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001983 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Ian Rogersbc939662013-08-15 10:26:54 -07001986 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
1987 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001988 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001989 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Ian Rogersbc939662013-08-15 10:26:54 -07001992 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
1993 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001994 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
Ian Rogersbc939662013-08-15 10:26:54 -07001997 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
1998 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001999 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
Ian Rogersbc939662013-08-15 10:26:54 -07002002 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2003 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002004 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002005 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Ian Rogersbc939662013-08-15 10:26:54 -07002008 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2009 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002010 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002011 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Ian Rogersbc939662013-08-15 10:26:54 -07002014 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2015 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002016 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Ian Rogersbc939662013-08-15 10:26:54 -07002019 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2020 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002021 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Ian Rogersbc939662013-08-15 10:26:54 -07002024 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2025 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002026 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002027 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Ian Rogersbc939662013-08-15 10:26:54 -07002030 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2031 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002032 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002033 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Ian Rogersbc939662013-08-15 10:26:54 -07002036 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2037 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002038 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Ian Rogersbc939662013-08-15 10:26:54 -07002041 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2042 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002043 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Ian Rogersbc939662013-08-15 10:26:54 -07002046 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2047 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002048 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002049 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Ian Rogersbc939662013-08-15 10:26:54 -07002052 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2053 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002054 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002055 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Ian Rogersbc939662013-08-15 10:26:54 -07002058 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2059 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002060 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Ian Rogersbc939662013-08-15 10:26:54 -07002063 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2064 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002065 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Ian Rogersbc939662013-08-15 10:26:54 -07002068 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2069 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002070 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002071 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Ian Rogersbc939662013-08-15 10:26:54 -07002074 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2075 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002076 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2077 }
2078
Ian Rogersbc939662013-08-15 10:26:54 -07002079 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2080 jint method_count, bool return_errors) {
2081 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002082 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2083 method_count);
2084 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002085 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002086 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002087 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002088 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002089 if (UNLIKELY(method_count == 0)) {
2090 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2091 << PrettyDescriptor(c);
2092 return JNI_OK;
2093 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002094 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002095 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 const char* name = methods[i].name;
2097 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002098 const void* fnPtr = methods[i].fnPtr;
2099 if (UNLIKELY(name == nullptr)) {
2100 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2101 return JNI_ERR;
2102 } else if (UNLIKELY(sig == nullptr)) {
2103 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2104 return JNI_ERR;
2105 } else if (UNLIKELY(fnPtr == nullptr)) {
2106 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2107 return JNI_ERR;
2108 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002109 bool is_fast = false;
Hiroshi Yamauchi368010a2015-05-12 14:21:34 -07002110 // Notes about fast JNI calls:
2111 //
2112 // On a normal JNI call, the calling thread usually transitions
2113 // from the kRunnable state to the kNative state. But if the
2114 // called native function needs to access any Java object, it
2115 // will have to transition back to the kRunnable state.
2116 //
2117 // There is a cost to this double transition. For a JNI call
2118 // that should be quick, this cost may dominate the call cost.
2119 //
2120 // On a fast JNI call, the calling thread avoids this double
2121 // transition by not transitioning from kRunnable to kNative and
2122 // stays in the kRunnable state.
2123 //
2124 // There are risks to using a fast JNI call because it can delay
2125 // a response to a thread suspension request which is typically
2126 // used for a GC root scanning, etc. If a fast JNI call takes a
2127 // long time, it could cause longer thread suspension latency
2128 // and GC pauses.
2129 //
2130 // Thus, fast JNI should be used with care. It should be used
2131 // for a JNI call that takes a short amount of time (eg. no
2132 // long-running loop) and does not block (eg. no locks, I/O,
2133 // etc.)
2134 //
2135 // A '!' prefix in the signature in the JNINativeMethod
2136 // indicates that it's a fast JNI call and the runtime omits the
2137 // thread state transition from kRunnable to kNative at the
2138 // entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002140 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 ++sig;
2142 }
2143
Andreas Gampe82566092015-05-18 15:52:22 -07002144 // Note: the right order is to try to find the method locally
2145 // first, either as a direct or a virtual method. Then move to
2146 // the parent.
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07002147 ArtMethod* m = nullptr;
Andreas Gampe82566092015-05-18 15:52:22 -07002148 bool warn_on_going_to_parent = down_cast<JNIEnvExt*>(env)->vm->IsCheckJniEnabled();
2149 for (mirror::Class* current_class = c;
2150 current_class != nullptr;
2151 current_class = current_class->GetSuperClass()) {
2152 // Search first only comparing methods which are native.
2153 m = FindMethod<true>(current_class, name, sig);
2154 if (m != nullptr) {
2155 break;
2156 }
2157
2158 // Search again comparing to all methods, to find non-native methods that match.
2159 m = FindMethod<false>(current_class, name, sig);
2160 if (m != nullptr) {
2161 break;
2162 }
2163
2164 if (warn_on_going_to_parent) {
2165 LOG(WARNING) << "CheckJNI: method to register \"" << name << "\" not in the given class. "
2166 << "This is slow, consider changing your RegisterNatives calls.";
2167 warn_on_going_to_parent = false;
2168 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
Andreas Gampe82566092015-05-18 15:52:22 -07002170
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002171 if (m == nullptr) {
Mathieu Chartiera6ce5b22015-05-04 10:18:24 -07002172 LOG(return_errors ? ERROR : INTERNAL_FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002173 << PrettyDescriptor(c) << "." << name << sig << " in "
2174 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Mathieu Chartiera6ce5b22015-05-04 10:18:24 -07002175 // Safe to pass in LOG(FATAL) since the log object aborts in destructor and only goes
2176 // out of scope after the DumpClass is done executing.
2177 c->DumpClass(LOG(return_errors ? ERROR : FATAL), mirror::Class::kDumpClassFullDetail);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002178 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002180 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002181 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002182 << PrettyDescriptor(c) << "." << name << sig
2183 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002184 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 return JNI_ERR;
2186 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002187
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002188 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002189
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002190 m->RegisterNative(fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192 return JNI_OK;
2193 }
2194
Elliott Hughes5174fe62011-08-23 15:12:35 -07002195 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002196 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002197 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002198 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002199
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002200 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002201
Ian Rogers2d10b202014-05-12 19:15:18 -07002202 size_t unregistered_count = 0;
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07002203 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2204 for (auto& m : c->GetDirectMethods(pointer_size)) {
2205 if (m.IsNative()) {
2206 m.UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002207 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002208 }
2209 }
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07002210 for (auto& m : c->GetVirtualMethods(pointer_size)) {
2211 if (m.IsNative()) {
2212 m.UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002213 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002214 }
2215 }
2216
Ian Rogers2d10b202014-05-12 19:15:18 -07002217 if (unregistered_count == 0) {
2218 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2219 << PrettyDescriptor(c) << "' that contains no native methods";
2220 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002221 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002222 }
2223
Ian Rogers719d1a32014-03-06 12:13:39 -08002224 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002225 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002226 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002227 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2228 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002229 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002230 return JNI_ERR;
2231 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002232 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002233 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Ian Rogers719d1a32014-03-06 12:13:39 -08002236 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002237 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002238 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002239 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002240 o->MonitorExit(soa.Self());
2241 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002242 return JNI_ERR;
2243 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002244 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002245 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 }
2247
2248 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002249 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002251 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002252 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002254 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002255 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002256 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 }
2258
Elliott Hughescdf53122011-08-19 15:46:09 -07002259 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002260 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002261 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2262 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002263 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002264 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002265 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002266 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2267 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002268 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002269 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002270
Brian Carlstrom85a93362014-06-25 09:30:52 -07002271 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002272 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002273 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2274 "buffer capacity greater than maximum jint: %" PRId64,
2275 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002276 return nullptr;
2277 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002278 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002279 jint capacity_arg = static_cast<jint>(capacity);
2280
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002281 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2282 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002283 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002284 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 }
2286
Elliott Hughesb465ab02011-08-24 11:21:21 -07002287 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002288 return reinterpret_cast<void*>(env->GetLongField(
2289 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 }
2291
Elliott Hughesb465ab02011-08-24 11:21:21 -07002292 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002293 return static_cast<jlong>(env->GetIntField(
2294 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002295 }
2296
Andreas Gampea8763072014-12-20 00:08:35 -08002297 static jobjectRefType GetObjectRefType(JNIEnv* env ATTRIBUTE_UNUSED, jobject java_object) {
2298 if (java_object == nullptr) {
2299 return JNIInvalidRefType;
2300 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002301
2302 // Do we definitely know what kind of reference this is?
2303 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2304 IndirectRefKind kind = GetIndirectRefKind(ref);
2305 switch (kind) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002306 case kLocal:
2307 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002308 case kGlobal:
2309 return JNIGlobalRefType;
2310 case kWeakGlobal:
2311 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002312 case kHandleScopeOrInvalid:
Ian Rogersc0542af2014-09-03 16:16:56 -07002313 // Assume value is in a handle scope.
2314 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002315 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002316 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
Andreas Gampea8763072014-12-20 00:08:35 -08002317 UNREACHABLE();
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002319
2320 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002321 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2322 const char* caller)
2323 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002324 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002325 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002326 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2327 return JNI_ERR;
2328 }
2329 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002330 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002331 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2332 if (!okay) {
2333 soa.Self()->ThrowOutOfMemoryError(caller);
2334 }
2335 return okay ? JNI_OK : JNI_ERR;
2336 }
2337
2338 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002339 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002340 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002341 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002342 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002343 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002344 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002345 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002346 return soa.AddLocalReference<JniT>(result);
2347 }
2348
Ian Rogers2d10b202014-05-12 19:15:18 -07002349 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2350 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2351 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002352 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002353 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002354 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002355 soa.Vm()->JniAbortF(fn_name,
2356 "attempt to %s %s primitive array elements with an object of type %s",
2357 operation,
2358 PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2359 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002360 return nullptr;
2361 }
2362 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2363 return array;
2364 }
2365
2366 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2367 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2368 CHECK_NON_NULL_ARGUMENT(java_array);
2369 ScopedObjectAccess soa(env);
2370 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2371 "GetArrayElements",
2372 "get");
2373 if (UNLIKELY(array == nullptr)) {
2374 return nullptr;
2375 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002376 // Only make a copy if necessary.
2377 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2378 if (is_copy != nullptr) {
2379 *is_copy = JNI_TRUE;
2380 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002381 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002382 size_t size = array->GetLength() * component_size;
2383 void* data = new uint64_t[RoundUp(size, 8) / 8];
2384 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002385 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002386 } else {
2387 if (is_copy != nullptr) {
2388 *is_copy = JNI_FALSE;
2389 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002390 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002391 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002392 }
2393
Ian Rogers2d10b202014-05-12 19:15:18 -07002394 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002395 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002396 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002397 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002398 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2399 "ReleaseArrayElements",
2400 "release");
2401 if (array == nullptr) {
2402 return;
2403 }
2404 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2405 }
2406
2407 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2408 size_t component_size, void* elements, jint mode)
2409 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002410 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002411 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002412 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002413 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002414 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2415 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002416 if (is_copy) {
2417 // Sanity check: If elements is not the same as the java array's data, it better not be a
2418 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2419 // copies we make?
2420 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002421 soa.Vm()->JniAbortF("ReleaseArrayElements",
2422 "invalid element pointer %p, array elements are %p",
2423 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002424 return;
2425 }
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002426 if (mode != JNI_ABORT) {
2427 memcpy(array_data, elements, bytes);
2428 } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2429 // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2430 LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2431 soa.Self()->DumpJavaStack(LOG(WARNING));
2432 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002433 }
2434 if (mode != JNI_COMMIT) {
2435 if (is_copy) {
2436 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002437 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002438 // Non copy to a movable object must means that we had disabled the moving GC.
2439 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002440 }
2441 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002442 }
2443
Ian Rogers2d10b202014-05-12 19:15:18 -07002444 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2445 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2446 jsize start, jsize length, ElementT* buf) {
2447 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2448 ScopedObjectAccess soa(env);
2449 ArtArrayT* array =
2450 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2451 "GetPrimitiveArrayRegion",
2452 "get region of");
2453 if (array != nullptr) {
2454 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2455 ThrowAIOOBE(soa, array, start, length, "src");
2456 } else {
2457 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2458 ElementT* data = array->GetData();
2459 memcpy(buf, data + start, length * sizeof(ElementT));
2460 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002461 }
2462 }
2463
Ian Rogers2d10b202014-05-12 19:15:18 -07002464 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2465 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2466 jsize start, jsize length, const ElementT* buf) {
2467 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2468 ScopedObjectAccess soa(env);
2469 ArtArrayT* array =
2470 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2471 "SetPrimitiveArrayRegion",
2472 "set region of");
2473 if (array != nullptr) {
2474 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2475 ThrowAIOOBE(soa, array, start, length, "dst");
2476 } else {
2477 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2478 ElementT* data = array->GetData();
2479 memcpy(data + start, buf, length * sizeof(ElementT));
2480 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002481 }
2482 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002483};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002484
Elliott Hughes88c5c352012-03-15 18:49:48 -07002485const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002486 nullptr, // reserved0.
2487 nullptr, // reserved1.
2488 nullptr, // reserved2.
2489 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002490 JNI::GetVersion,
2491 JNI::DefineClass,
2492 JNI::FindClass,
2493 JNI::FromReflectedMethod,
2494 JNI::FromReflectedField,
2495 JNI::ToReflectedMethod,
2496 JNI::GetSuperclass,
2497 JNI::IsAssignableFrom,
2498 JNI::ToReflectedField,
2499 JNI::Throw,
2500 JNI::ThrowNew,
2501 JNI::ExceptionOccurred,
2502 JNI::ExceptionDescribe,
2503 JNI::ExceptionClear,
2504 JNI::FatalError,
2505 JNI::PushLocalFrame,
2506 JNI::PopLocalFrame,
2507 JNI::NewGlobalRef,
2508 JNI::DeleteGlobalRef,
2509 JNI::DeleteLocalRef,
2510 JNI::IsSameObject,
2511 JNI::NewLocalRef,
2512 JNI::EnsureLocalCapacity,
2513 JNI::AllocObject,
2514 JNI::NewObject,
2515 JNI::NewObjectV,
2516 JNI::NewObjectA,
2517 JNI::GetObjectClass,
2518 JNI::IsInstanceOf,
2519 JNI::GetMethodID,
2520 JNI::CallObjectMethod,
2521 JNI::CallObjectMethodV,
2522 JNI::CallObjectMethodA,
2523 JNI::CallBooleanMethod,
2524 JNI::CallBooleanMethodV,
2525 JNI::CallBooleanMethodA,
2526 JNI::CallByteMethod,
2527 JNI::CallByteMethodV,
2528 JNI::CallByteMethodA,
2529 JNI::CallCharMethod,
2530 JNI::CallCharMethodV,
2531 JNI::CallCharMethodA,
2532 JNI::CallShortMethod,
2533 JNI::CallShortMethodV,
2534 JNI::CallShortMethodA,
2535 JNI::CallIntMethod,
2536 JNI::CallIntMethodV,
2537 JNI::CallIntMethodA,
2538 JNI::CallLongMethod,
2539 JNI::CallLongMethodV,
2540 JNI::CallLongMethodA,
2541 JNI::CallFloatMethod,
2542 JNI::CallFloatMethodV,
2543 JNI::CallFloatMethodA,
2544 JNI::CallDoubleMethod,
2545 JNI::CallDoubleMethodV,
2546 JNI::CallDoubleMethodA,
2547 JNI::CallVoidMethod,
2548 JNI::CallVoidMethodV,
2549 JNI::CallVoidMethodA,
2550 JNI::CallNonvirtualObjectMethod,
2551 JNI::CallNonvirtualObjectMethodV,
2552 JNI::CallNonvirtualObjectMethodA,
2553 JNI::CallNonvirtualBooleanMethod,
2554 JNI::CallNonvirtualBooleanMethodV,
2555 JNI::CallNonvirtualBooleanMethodA,
2556 JNI::CallNonvirtualByteMethod,
2557 JNI::CallNonvirtualByteMethodV,
2558 JNI::CallNonvirtualByteMethodA,
2559 JNI::CallNonvirtualCharMethod,
2560 JNI::CallNonvirtualCharMethodV,
2561 JNI::CallNonvirtualCharMethodA,
2562 JNI::CallNonvirtualShortMethod,
2563 JNI::CallNonvirtualShortMethodV,
2564 JNI::CallNonvirtualShortMethodA,
2565 JNI::CallNonvirtualIntMethod,
2566 JNI::CallNonvirtualIntMethodV,
2567 JNI::CallNonvirtualIntMethodA,
2568 JNI::CallNonvirtualLongMethod,
2569 JNI::CallNonvirtualLongMethodV,
2570 JNI::CallNonvirtualLongMethodA,
2571 JNI::CallNonvirtualFloatMethod,
2572 JNI::CallNonvirtualFloatMethodV,
2573 JNI::CallNonvirtualFloatMethodA,
2574 JNI::CallNonvirtualDoubleMethod,
2575 JNI::CallNonvirtualDoubleMethodV,
2576 JNI::CallNonvirtualDoubleMethodA,
2577 JNI::CallNonvirtualVoidMethod,
2578 JNI::CallNonvirtualVoidMethodV,
2579 JNI::CallNonvirtualVoidMethodA,
2580 JNI::GetFieldID,
2581 JNI::GetObjectField,
2582 JNI::GetBooleanField,
2583 JNI::GetByteField,
2584 JNI::GetCharField,
2585 JNI::GetShortField,
2586 JNI::GetIntField,
2587 JNI::GetLongField,
2588 JNI::GetFloatField,
2589 JNI::GetDoubleField,
2590 JNI::SetObjectField,
2591 JNI::SetBooleanField,
2592 JNI::SetByteField,
2593 JNI::SetCharField,
2594 JNI::SetShortField,
2595 JNI::SetIntField,
2596 JNI::SetLongField,
2597 JNI::SetFloatField,
2598 JNI::SetDoubleField,
2599 JNI::GetStaticMethodID,
2600 JNI::CallStaticObjectMethod,
2601 JNI::CallStaticObjectMethodV,
2602 JNI::CallStaticObjectMethodA,
2603 JNI::CallStaticBooleanMethod,
2604 JNI::CallStaticBooleanMethodV,
2605 JNI::CallStaticBooleanMethodA,
2606 JNI::CallStaticByteMethod,
2607 JNI::CallStaticByteMethodV,
2608 JNI::CallStaticByteMethodA,
2609 JNI::CallStaticCharMethod,
2610 JNI::CallStaticCharMethodV,
2611 JNI::CallStaticCharMethodA,
2612 JNI::CallStaticShortMethod,
2613 JNI::CallStaticShortMethodV,
2614 JNI::CallStaticShortMethodA,
2615 JNI::CallStaticIntMethod,
2616 JNI::CallStaticIntMethodV,
2617 JNI::CallStaticIntMethodA,
2618 JNI::CallStaticLongMethod,
2619 JNI::CallStaticLongMethodV,
2620 JNI::CallStaticLongMethodA,
2621 JNI::CallStaticFloatMethod,
2622 JNI::CallStaticFloatMethodV,
2623 JNI::CallStaticFloatMethodA,
2624 JNI::CallStaticDoubleMethod,
2625 JNI::CallStaticDoubleMethodV,
2626 JNI::CallStaticDoubleMethodA,
2627 JNI::CallStaticVoidMethod,
2628 JNI::CallStaticVoidMethodV,
2629 JNI::CallStaticVoidMethodA,
2630 JNI::GetStaticFieldID,
2631 JNI::GetStaticObjectField,
2632 JNI::GetStaticBooleanField,
2633 JNI::GetStaticByteField,
2634 JNI::GetStaticCharField,
2635 JNI::GetStaticShortField,
2636 JNI::GetStaticIntField,
2637 JNI::GetStaticLongField,
2638 JNI::GetStaticFloatField,
2639 JNI::GetStaticDoubleField,
2640 JNI::SetStaticObjectField,
2641 JNI::SetStaticBooleanField,
2642 JNI::SetStaticByteField,
2643 JNI::SetStaticCharField,
2644 JNI::SetStaticShortField,
2645 JNI::SetStaticIntField,
2646 JNI::SetStaticLongField,
2647 JNI::SetStaticFloatField,
2648 JNI::SetStaticDoubleField,
2649 JNI::NewString,
2650 JNI::GetStringLength,
2651 JNI::GetStringChars,
2652 JNI::ReleaseStringChars,
2653 JNI::NewStringUTF,
2654 JNI::GetStringUTFLength,
2655 JNI::GetStringUTFChars,
2656 JNI::ReleaseStringUTFChars,
2657 JNI::GetArrayLength,
2658 JNI::NewObjectArray,
2659 JNI::GetObjectArrayElement,
2660 JNI::SetObjectArrayElement,
2661 JNI::NewBooleanArray,
2662 JNI::NewByteArray,
2663 JNI::NewCharArray,
2664 JNI::NewShortArray,
2665 JNI::NewIntArray,
2666 JNI::NewLongArray,
2667 JNI::NewFloatArray,
2668 JNI::NewDoubleArray,
2669 JNI::GetBooleanArrayElements,
2670 JNI::GetByteArrayElements,
2671 JNI::GetCharArrayElements,
2672 JNI::GetShortArrayElements,
2673 JNI::GetIntArrayElements,
2674 JNI::GetLongArrayElements,
2675 JNI::GetFloatArrayElements,
2676 JNI::GetDoubleArrayElements,
2677 JNI::ReleaseBooleanArrayElements,
2678 JNI::ReleaseByteArrayElements,
2679 JNI::ReleaseCharArrayElements,
2680 JNI::ReleaseShortArrayElements,
2681 JNI::ReleaseIntArrayElements,
2682 JNI::ReleaseLongArrayElements,
2683 JNI::ReleaseFloatArrayElements,
2684 JNI::ReleaseDoubleArrayElements,
2685 JNI::GetBooleanArrayRegion,
2686 JNI::GetByteArrayRegion,
2687 JNI::GetCharArrayRegion,
2688 JNI::GetShortArrayRegion,
2689 JNI::GetIntArrayRegion,
2690 JNI::GetLongArrayRegion,
2691 JNI::GetFloatArrayRegion,
2692 JNI::GetDoubleArrayRegion,
2693 JNI::SetBooleanArrayRegion,
2694 JNI::SetByteArrayRegion,
2695 JNI::SetCharArrayRegion,
2696 JNI::SetShortArrayRegion,
2697 JNI::SetIntArrayRegion,
2698 JNI::SetLongArrayRegion,
2699 JNI::SetFloatArrayRegion,
2700 JNI::SetDoubleArrayRegion,
2701 JNI::RegisterNatives,
2702 JNI::UnregisterNatives,
2703 JNI::MonitorEnter,
2704 JNI::MonitorExit,
2705 JNI::GetJavaVM,
2706 JNI::GetStringRegion,
2707 JNI::GetStringUTFRegion,
2708 JNI::GetPrimitiveArrayCritical,
2709 JNI::ReleasePrimitiveArrayCritical,
2710 JNI::GetStringCritical,
2711 JNI::ReleaseStringCritical,
2712 JNI::NewWeakGlobalRef,
2713 JNI::DeleteWeakGlobalRef,
2714 JNI::ExceptionCheck,
2715 JNI::NewDirectByteBuffer,
2716 JNI::GetDirectBufferAddress,
2717 JNI::GetDirectBufferCapacity,
2718 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002719};
2720
Ian Rogers68d8b422014-07-17 11:09:10 -07002721const JNINativeInterface* GetJniNativeInterface() {
2722 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002723}
2724
Elliott Hughesc8fece32013-01-02 11:27:23 -08002725void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002726 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002727 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002728 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002729 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2730 }
2731 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2732}
2733
Ian Rogersdf20fe02011-07-20 20:34:16 -07002734} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002735
2736std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2737 switch (rhs) {
2738 case JNIInvalidRefType:
2739 os << "JNIInvalidRefType";
2740 return os;
2741 case JNILocalRefType:
2742 os << "JNILocalRefType";
2743 return os;
2744 case JNIGlobalRefType:
2745 os << "JNIGlobalRefType";
2746 return os;
2747 case JNIWeakGlobalRefType:
2748 os << "JNIWeakGlobalRefType";
2749 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002750 default:
Ian Rogersc7dd2952014-10-21 23:31:19 -07002751 LOG(::art::FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
2752 UNREACHABLE();
Elliott Hughesb465ab02011-08-24 11:21:21 -07002753 }
2754}