blob: 37ad46e20969f1c63bfc766d8c625b012216c679 [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
Ian Rogersef7d42f2014-01-06 12:55:46 -080026#include "atomic.h"
Mathieu Chartierbad02672014-08-25 13:08:22 -070027#include "base/allocator.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080029#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080030#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080031#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "dex_file-inl.h"
Mathieu Chartierd0004802014-10-15 16:59:47 -070033#include "fault_handler.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070034#include "gc_root.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "gc/accounting/card_table-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070036#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070037#include "interpreter/interpreter.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070038#include "jni_env_ext.h"
39#include "java_vm_ext.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070040#include "mirror/art_field-inl.h"
41#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/class-inl.h"
43#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/object-inl.h"
45#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070046#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/throwable.h"
jgu216d031042014-09-10 06:57:17 -040048#include "nativebridge/native_bridge.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080049#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070050#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070051#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070052#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070054#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070055#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070057#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070058
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070059namespace art {
60
Mathieu Chartier24555ad2014-10-06 13:41:33 -070061// Consider turning this on when there is errors which could be related to JNI array copies such as
62// things not rendering correctly. E.g. b/16858794
63static constexpr bool kWarnJniAbort = false;
64
Elliott Hughes6b436852011-08-12 10:16:44 -070065// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
66// separated with slashes but aren't wrapped with "L;" like regular descriptors
67// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
68// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
69// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070070static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070071 std::string result;
72 // Add the missing "L;" if necessary.
73 if (name[0] == '[') {
74 result = name;
75 } else {
76 result += 'L';
77 result += name;
78 result += ';';
79 }
80 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070081 if (result.find('.') != std::string::npos) {
82 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
83 << "\"" << name << "\"";
84 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -070085 }
86 return result;
87}
88
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080089static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070090 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -070091 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080092 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Ian Rogers1ff3c982014-08-12 02:30:58 -070093 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -080094 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
95 "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;
105 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
106 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
107 "%s is null at index %d", kind, idx);
108}
109
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800110static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
112 if (LIKELY(klass->IsInitialized())) {
113 return klass;
114 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700115 StackHandleScope<1> hs(self);
116 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700117 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800118 return nullptr;
119 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700120 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800121}
122
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700123static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
124 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800126 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800127 if (c == nullptr) {
128 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700129 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800130 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700131 if (is_static) {
132 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700133 } else if (c->IsInterface()) {
134 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700135 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700136 method = c->FindVirtualMethod(name, sig);
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.
140 method = c->FindDeclaredDirectMethod(name, sig);
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 Chartiere7e8a5f2014-02-14 16:59:41 -0800152 mirror::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) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800170 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700171 CHECK(Runtime::Current()->UseCompileTimeClassPath());
172 return class_loader;
173 }
174 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800175 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700176}
177
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700178static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
179 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700181 StackHandleScope<2> hs(soa.Self());
182 Handle<mirror::Class> c(
183 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
184 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800185 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700186 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800187 mirror::ArtField* field = nullptr;
188 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700189 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
190 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700191 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800192 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700193 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700194 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700195 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800196 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700197 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700198 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800199 ThrowLocation throw_location;
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800200 StackHandleScope<1> hs2(soa.Self());
201 Handle<mirror::Throwable> cause(hs2.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700202 soa.Self()->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700203 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800204 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800205 "no type \"%s\" found and so no field \"%s\" "
206 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700207 c->GetDescriptor(&temp));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700208 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800209 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700211 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700213 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700214 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700216 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700217 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800218 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800219 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
220 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
221 "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));
Ian Rogers62d6c772013-02-27 08:32:07 -0800232 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
233 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
234 "%s offset=%d length=%d %s.length=%d",
235 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700236}
Ian Rogers0571d352011-11-03 19:51:38 -0700237
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700238static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
239 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800241 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
242 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
243 "offset=%d length=%d string.length()=%d", start, length,
244 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700245}
Elliott Hughes814e4032011-08-23 12:07:56 -0700246
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700248 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 // Turn the const char* into a java.lang.String.
250 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800251 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700253 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700254
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 // Choose an appropriate constructor and set up the arguments.
256 jvalue args[2];
257 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800258 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800260 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 signature = "(Ljava/lang/String;)V";
262 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800263 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264 signature = "(Ljava/lang/Throwable;)V";
265 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700266 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
268 args[0].l = s.get();
269 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700270 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800272 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800273 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800275 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276 return JNI_ERR;
277 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700278
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800279 ScopedLocalRef<jthrowable> exception(
280 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
281 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 return JNI_ERR;
283 }
Ian Rogersef28b142012-11-30 14:22:18 -0800284 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800285 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800286 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700288}
289
Ian Rogers68d8b422014-07-17 11:09:10 -0700290static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
291 return reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughes75770752011-08-24 17:52:38 -0700292}
293
Ian Rogers2d10b202014-05-12 19:15:18 -0700294#define CHECK_NON_NULL_ARGUMENT(value) \
295 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700296
Ian Rogers2d10b202014-05-12 19:15:18 -0700297#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
298 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
299
300#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
301 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
302
303#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
304 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
305
306#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800307 if (UNLIKELY(value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700308 JavaVmExtFromEnv(env)->JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700309 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700310 }
311
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700312#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800313 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700314 JavaVmExtFromEnv(env)->JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700315 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700316 }
317
Elliott Hughescdf53122011-08-19 15:46:09 -0700318class JNI {
319 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700320 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700321 return JNI_VERSION_1_6;
322 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700323
Ian Rogers25e8b912012-09-07 11:31:36 -0700324 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700325 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800326 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700327 }
328
Elliott Hughescdf53122011-08-19 15:46:09 -0700329 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700330 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700331 Runtime* runtime = Runtime::Current();
332 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700333 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700334 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800335 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700336 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700337 StackHandleScope<1> hs(soa.Self());
338 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800339 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700340 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800341 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700342 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700344 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700345
Ian Rogers62f05122014-03-21 11:21:29 -0700346 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700347 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700348 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700349 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700350 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700351
Ian Rogers62f05122014-03-21 11:21:29 -0700352 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700353 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700355 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700356 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700357
Elliott Hughescdf53122011-08-19 15:46:09 -0700358 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700359 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700360 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800361 mirror::ArtMethod* m = soa.DecodeMethod(mid);
362 CHECK(!kMovingMethods);
Mathieu Chartier41da5962014-11-15 13:07:39 -0800363 ScopedLocalRef<jobject> art_method(env, soa.AddLocalReference<jobject>(m));
Sebastien Hertzd3333762014-06-26 14:45:07 +0200364 jobject reflect_method;
365 if (m->IsConstructor()) {
366 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
367 } else {
368 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
369 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700370 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800371 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700372 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800373 SetObjectField(env, reflect_method,
Mathieu Chartier41da5962014-11-15 13:07:39 -0800374 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method.get());
Brian Carlstromea46f952013-07-30 01:26:50 -0700375 return reflect_method;
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 ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700379 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800381 mirror::ArtField* f = soa.DecodeField(fid);
Mathieu Chartier41da5962014-11-15 13:07:39 -0800382 ScopedLocalRef<jobject> art_field(env, soa.AddLocalReference<jobject>(f));
Brian Carlstromea46f952013-07-30 01:26:50 -0700383 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
384 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800385 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700386 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800387 SetObjectField(env, reflect_field,
Mathieu Chartier41da5962014-11-15 13:07:39 -0800388 WellKnownClasses::java_lang_reflect_Field_artField, art_field.get());
Brian Carlstromea46f952013-07-30 01:26:50 -0700389 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700390 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700391
Elliott Hughes37f7a402011-08-22 18:56:01 -0700392 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700393 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800395 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700397 }
398
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700399 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700400 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800402 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700403 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700404 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700405
Narayan Kamath1268b742014-07-11 19:15:11 +0100406 // Note: java_class1 should be safely castable to java_class2, and
407 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700408 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700409 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
410 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700411 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800412 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
413 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100414 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700415 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700416
Elliott Hughese84278b2012-03-22 10:06:53 -0700417 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700418 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800419 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700420 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700421 return JNI_TRUE;
422 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700423 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800424 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
425 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700426 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700427 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700428 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700429
Elliott Hughes37f7a402011-08-22 18:56:01 -0700430 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700431 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800432 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
433 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700434 return JNI_ERR;
435 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800436 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
437 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700438 return JNI_OK;
439 }
440
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700441 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700442 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800443 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700444 }
445
446 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700447 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700448 }
449
450 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700451 ScopedObjectAccess soa(env);
452 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700453 }
454
455 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700456 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700457
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700458 // If we have no exception to describe, pass through.
459 if (!soa.Self()->GetException(nullptr)) {
460 return;
461 }
462
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700463 StackHandleScope<3> hs(soa.Self());
464 // TODO: Use nullptr instead of null handles?
465 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
466 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
467 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800468 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200469 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800470 {
471 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800472 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700473 old_throw_this_object.Assign(old_throw_location.GetThis());
474 old_throw_method.Assign(old_throw_location.GetMethod());
475 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800476 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200477 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800478 soa.Self()->ClearException();
479 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800480 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700481 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700482 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
483 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800484 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700485 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700486 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700487 } else {
488 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800489 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800490 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700491 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800492 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700493 }
494 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700495 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800496 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700497
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700498 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200499 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700500 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700501
Elliott Hughescdf53122011-08-19 15:46:09 -0700502 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700503 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800504 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700505 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700506 }
507
Ian Rogers25e8b912012-09-07 11:31:36 -0700508 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700509 LOG(FATAL) << "JNI FatalError called: " << msg;
510 }
511
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700512 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700513 // TODO: SOA may not be necessary but I do it to please lock annotations.
514 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700515 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700516 return JNI_ERR;
517 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700518 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700519 return JNI_OK;
520 }
521
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700522 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700523 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800524 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700525 soa.Env()->PopFrame();
526 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700527 }
528
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700529 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700530 // TODO: SOA may not be necessary but I do it to please lock annotations.
531 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700532 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700533 }
534
Elliott Hughescdf53122011-08-19 15:46:09 -0700535 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700536 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800537 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700538 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700539 }
540
541 static void DeleteGlobalRef(JNIEnv* env, jobject 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->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700545 }
546
547 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700548 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700549 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
550 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700551 }
552
553 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700554 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
555 Thread* self = down_cast<JNIEnvExt*>(env)->self;
556 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700557 }
558
559 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700560 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800561 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800562 // Check for null after decoding the object to handle cleared weak globals.
563 if (decoded_obj == nullptr) {
564 return nullptr;
565 }
566 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 }
568
Stephen Hines95c51b32014-11-26 01:24:13 -0800569 static void DeleteLocalRef(JNIEnv* env, jobject obj)
570 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800571 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700572 return;
573 }
Ian Rogersef28b142012-11-30 14:22:18 -0800574 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700575
Ian Rogersef28b142012-11-30 14:22:18 -0800576 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700577 if (!locals.Remove(cookie, obj)) {
578 // Attempting to delete a local reference that is not in the
579 // topmost local reference frame is a no-op. DeleteLocalRef returns
580 // void and doesn't throw any exceptions, but we should probably
581 // complain about it so the user will notice that things aren't
582 // going quite the way they expect.
583 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
584 << "failed to find entry";
585 }
586 }
587
588 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700589 if (obj1 == obj2) {
590 return JNI_TRUE;
591 } else {
592 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800593 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
594 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700595 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700596 }
597
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700598 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700599 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700600 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800601 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800602 if (c == nullptr) {
603 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700604 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700605 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700606 }
607
Ian Rogersbc939662013-08-15 10:26:54 -0700608 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700609 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700610 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700611 CHECK_NON_NULL_ARGUMENT(java_class);
612 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700613 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 va_end(args);
615 return result;
616 }
617
Elliott Hughes72025e52011-08-23 17:50:30 -0700618 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700619 CHECK_NON_NULL_ARGUMENT(java_class);
620 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700621 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800622 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800623 if (c == nullptr) {
624 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700625 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800626 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800627 if (result == nullptr) {
628 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700629 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700630 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700631 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800632 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800633 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700634 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800635 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 }
637
Elliott Hughes72025e52011-08-23 17:50:30 -0700638 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700639 CHECK_NON_NULL_ARGUMENT(java_class);
640 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700641 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800642 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800643 if (c == nullptr) {
644 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700645 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800646 mirror::Object* result = c->AllocObject(soa.Self());
647 if (result == nullptr) {
648 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700649 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700650 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700651 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800652 if (soa.Self()->IsExceptionPending()) {
653 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700654 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800655 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700656 }
657
Ian Rogersbc939662013-08-15 10:26:54 -0700658 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700659 CHECK_NON_NULL_ARGUMENT(java_class);
660 CHECK_NON_NULL_ARGUMENT(name);
661 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700663 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700664 }
665
Ian Rogersbc939662013-08-15 10:26:54 -0700666 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
667 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700668 CHECK_NON_NULL_ARGUMENT(java_class);
669 CHECK_NON_NULL_ARGUMENT(name);
670 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700671 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700672 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 }
674
Elliott Hughes72025e52011-08-23 17:50:30 -0700675 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700676 va_list ap;
677 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700678 CHECK_NON_NULL_ARGUMENT(obj);
679 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700680 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700681 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700682 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 }
685
Elliott Hughes72025e52011-08-23 17:50:30 -0700686 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700687 CHECK_NON_NULL_ARGUMENT(obj);
688 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700689 ScopedObjectAccess soa(env);
690 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
691 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700692 }
693
Elliott Hughes72025e52011-08-23 17:50:30 -0700694 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700695 CHECK_NON_NULL_ARGUMENT(obj);
696 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700697 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700698 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
699 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700701 }
702
Elliott Hughes72025e52011-08-23 17:50:30 -0700703 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700704 va_list ap;
705 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700706 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
707 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700708 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700709 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700710 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700711 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700712 }
713
Elliott Hughes72025e52011-08-23 17:50:30 -0700714 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700715 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
716 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700717 ScopedObjectAccess soa(env);
718 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 }
720
Elliott Hughes72025e52011-08-23 17:50:30 -0700721 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700722 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
723 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700724 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700725 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
726 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700727 }
728
Elliott Hughes72025e52011-08-23 17:50:30 -0700729 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700730 va_list ap;
731 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700732 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
733 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700734 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700735 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700736 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700737 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 }
739
Elliott Hughes72025e52011-08-23 17:50:30 -0700740 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700741 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
742 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700743 ScopedObjectAccess soa(env);
744 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 }
746
Elliott Hughes72025e52011-08-23 17:50:30 -0700747 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700748 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
749 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700750 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700751 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
752 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700753 }
754
Elliott Hughes72025e52011-08-23 17:50:30 -0700755 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700756 va_list ap;
757 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700758 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
759 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700760 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700761 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700762 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700763 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700764 }
765
Elliott Hughes72025e52011-08-23 17:50:30 -0700766 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700767 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
768 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700769 ScopedObjectAccess soa(env);
770 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700771 }
772
Elliott Hughes72025e52011-08-23 17:50:30 -0700773 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700774 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
775 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700776 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700777 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
778 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);
Ian Rogers53b8b092014-03-13 23:45:53 -0700803 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
804 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 }
806
Elliott Hughes72025e52011-08-23 17:50:30 -0700807 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700808 va_list ap;
809 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700810 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
811 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700812 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700813 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700814 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700815 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 }
817
Elliott Hughes72025e52011-08-23 17:50:30 -0700818 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700819 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
820 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700821 ScopedObjectAccess soa(env);
822 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 }
824
Elliott Hughes72025e52011-08-23 17:50:30 -0700825 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700826 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
827 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700828 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700829 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
830 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 }
832
Elliott Hughes72025e52011-08-23 17:50:30 -0700833 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700834 va_list ap;
835 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700836 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
837 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700838 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700839 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700840 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700841 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 }
843
Elliott Hughes72025e52011-08-23 17:50:30 -0700844 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700845 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
846 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700847 ScopedObjectAccess soa(env);
848 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 }
850
Elliott Hughes72025e52011-08-23 17:50:30 -0700851 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700852 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
853 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700854 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700855 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
856 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 }
858
Elliott Hughes72025e52011-08-23 17:50:30 -0700859 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700860 va_list ap;
861 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700862 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
863 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700864 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700866 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700867 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 }
869
Elliott Hughes72025e52011-08-23 17:50:30 -0700870 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700871 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
872 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700873 ScopedObjectAccess soa(env);
874 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 }
876
Elliott Hughes72025e52011-08-23 17:50:30 -0700877 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700878 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
879 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700880 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700881 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
882 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700883 }
884
Elliott Hughes72025e52011-08-23 17:50:30 -0700885 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700886 va_list ap;
887 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700888 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
889 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700890 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700891 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700892 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700893 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700894 }
895
Elliott Hughes72025e52011-08-23 17:50:30 -0700896 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700897 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
898 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700899 ScopedObjectAccess soa(env);
900 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 }
902
Elliott Hughes72025e52011-08-23 17:50:30 -0700903 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700904 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
905 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700906 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700907 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
908 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700909 }
910
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 va_list ap;
913 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700914 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
915 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700916 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -0700917 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 }
920
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700922 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
923 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700924 ScopedObjectAccess soa(env);
925 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700926 }
927
Elliott Hughes72025e52011-08-23 17:50:30 -0700928 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700929 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
930 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700931 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700932 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 }
934
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700935 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700938 CHECK_NON_NULL_ARGUMENT(obj);
939 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700940 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700941 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
942 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 va_end(ap);
944 return local_result;
945 }
946
Ian Rogersbc939662013-08-15 10:26:54 -0700947 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
948 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700949 CHECK_NON_NULL_ARGUMENT(obj);
950 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700951 ScopedObjectAccess soa(env);
952 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
953 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 }
955
Ian Rogersbc939662013-08-15 10:26:54 -0700956 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
957 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700958 CHECK_NON_NULL_ARGUMENT(obj);
959 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700961 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700962 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 }
964
Ian Rogersbc939662013-08-15 10:26:54 -0700965 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
966 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700968 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700969 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
970 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700971 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700972 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700974 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 }
976
Ian Rogersbc939662013-08-15 10:26:54 -0700977 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
978 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700979 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
980 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700981 ScopedObjectAccess soa(env);
982 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 }
984
Ian Rogersbc939662013-08-15 10:26:54 -0700985 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
986 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700987 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
988 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700990 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700993 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700996 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
997 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700998 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700999 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001001 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Ian Rogersbc939662013-08-15 10:26:54 -07001004 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1005 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001006 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1007 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001008 ScopedObjectAccess soa(env);
1009 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Ian Rogersbc939662013-08-15 10:26:54 -07001012 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1013 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001014 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1015 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001016 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001017 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 }
1019
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001020 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001023 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1024 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001025 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001026 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001028 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Ian Rogersbc939662013-08-15 10:26:54 -07001031 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1032 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001033 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1034 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001035 ScopedObjectAccess soa(env);
1036 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Ian Rogersbc939662013-08-15 10:26:54 -07001039 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1040 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001041 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1042 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001044 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001047 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001050 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1051 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001052 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001053 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001055 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Ian Rogersbc939662013-08-15 10:26:54 -07001058 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1059 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001060 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1061 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062 ScopedObjectAccess soa(env);
1063 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Ian Rogersbc939662013-08-15 10:26:54 -07001066 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1067 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001068 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1069 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001070 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001071 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 }
1073
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001074 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001077 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1078 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001079 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001082 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Ian Rogersbc939662013-08-15 10:26:54 -07001085 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1086 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001087 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1088 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001089 ScopedObjectAccess soa(env);
1090 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 }
1092
Ian Rogersbc939662013-08-15 10:26:54 -07001093 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1094 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001095 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1096 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001097 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001098 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001101 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001104 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1105 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001106 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001107 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001109 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Ian Rogersbc939662013-08-15 10:26:54 -07001112 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1113 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001114 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1115 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001116 ScopedObjectAccess soa(env);
1117 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Ian Rogersbc939662013-08-15 10:26:54 -07001120 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1121 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001122 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1123 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001124 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001125 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001128 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001131 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1132 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001133 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001134 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001136 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Ian Rogersbc939662013-08-15 10:26:54 -07001139 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1140 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001141 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1142 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001143 ScopedObjectAccess soa(env);
1144 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Ian Rogersbc939662013-08-15 10:26:54 -07001147 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1148 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001149 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1150 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001152 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001155 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001158 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1159 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001160 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001163 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Ian Rogersbc939662013-08-15 10:26:54 -07001166 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1167 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001168 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1169 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001170 ScopedObjectAccess soa(env);
1171 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Ian Rogersbc939662013-08-15 10:26:54 -07001174 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1175 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001176 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1177 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001178 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001179 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001182 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001185 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1186 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001187 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 va_end(ap);
1190 }
1191
Brian Carlstromea46f952013-07-30 01:26:50 -07001192 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1193 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001194 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1195 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001196 ScopedObjectAccess soa(env);
1197 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 }
1199
Ian Rogersbc939662013-08-15 10:26:54 -07001200 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1201 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001202 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1203 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001204 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001205 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
Ian Rogersbc939662013-08-15 10:26:54 -07001208 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001209 CHECK_NON_NULL_ARGUMENT(java_class);
1210 CHECK_NON_NULL_ARGUMENT(name);
1211 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001212 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001213 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001215
Ian Rogersbc939662013-08-15 10:26:54 -07001216 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1217 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001218 CHECK_NON_NULL_ARGUMENT(java_class);
1219 CHECK_NON_NULL_ARGUMENT(name);
1220 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001221 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001222 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001224
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001225 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001226 CHECK_NON_NULL_ARGUMENT(obj);
1227 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001229 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1230 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001231 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001233
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001234 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001235 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001236 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001237 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001238 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001241 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001242 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1243 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001244 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001245 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1246 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1247 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001248 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 }
1250
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001251 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001252 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001253 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001254 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1255 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001256 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001259#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001260 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1261 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001262 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001263 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1264 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001265 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001266
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001267#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001268 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001269 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001270 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001271 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001272
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001273#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001274 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1275 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001276 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001277 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1278 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001279 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001280
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001281#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001282 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001283 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001284 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001285 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001286
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001287 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001288 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
1290
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001291 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001292 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001295 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001296 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
1298
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001299 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001300 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001303 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001304 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 }
1306
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001307 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001308 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001311 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001312 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001315 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001316 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 }
1318
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001319 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001320 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001323 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001324 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001327 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001328 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001331 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001332 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001335 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001336 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 }
1338
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001339 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001340 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001341 }
1342
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001343 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001344 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001345 }
1346
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001347 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001348 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001349 }
1350
1351 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001352 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001353 }
1354
1355 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001356 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001357 }
1358
1359 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001360 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001361 }
1362
1363 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001364 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001365 }
1366
1367 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001368 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001369 }
1370
1371 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001372 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001373 }
1374
1375 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001376 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001377 }
1378
1379 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001380 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381 }
1382
1383 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001384 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001385 }
1386
1387 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001388 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389 }
1390
1391 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001392 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001393 }
1394
1395 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001396 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001397 }
1398
1399 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001400 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001401 }
1402
1403 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001404 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001405 }
1406
1407 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001408 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001409 }
1410
1411 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001412 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001415 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001417 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001418 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001419 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001420 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001421 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 va_end(ap);
1423 return local_result;
1424 }
1425
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001426 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001427 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001428 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001429 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001430 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
1432
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001433 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001434 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001435 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001436 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001437 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001440 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001442 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001443 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001444 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001445 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001447 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001450 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001451 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001452 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001453 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001456 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001457 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001458 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001459 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001462 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001464 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001465 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001466 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001467 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001469 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 }
1471
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001472 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001473 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001474 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001475 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001478 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001479 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001480 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001481 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001484 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001486 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001487 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001488 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001489 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001491 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 }
1493
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001494 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001495 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001496 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001497 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 }
1499
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001500 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001501 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001502 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001503 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001506 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001508 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001509 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001510 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001511 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001513 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001516 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001517 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001518 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001519 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
1521
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001522 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001523 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001524 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001525 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001528 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001530 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001531 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001532 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001533 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001535 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001538 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001539 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001540 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001541 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001544 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001545 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001546 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001547 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 }
1549
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001550 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001552 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001553 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001554 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001555 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001557 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 }
1559
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001560 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001561 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001562 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001563 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001564 }
1565
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001566 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001567 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001568 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001569 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001572 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001574 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001575 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001576 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001577 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001579 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001582 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001583 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001584 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001585 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001588 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001589 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001590 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001591 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001594 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001596 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001597 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001598 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001599 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001601 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001604 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001605 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001606 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001607 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 }
1609
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001610 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001611 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001612 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001613 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001616 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001618 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001619 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001620 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001621 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 va_end(ap);
1623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001626 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001627 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001628 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001631 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001632 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001633 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001634 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
Elliott Hughes814e4032011-08-23 12:07:56 -07001637 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001638 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001639 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001640 return nullptr;
1641 }
1642 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001643 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001644 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001645 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001646 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001647 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001648 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
1651 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001652 if (utf == nullptr) {
1653 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001655 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001656 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001657 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 }
1659
Elliott Hughes814e4032011-08-23 12:07:56 -07001660 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001661 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001662 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001663 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001664 }
1665
1666 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001667 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001668 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001669 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001670 }
1671
Ian Rogersbc939662013-08-15 10:26:54 -07001672 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1673 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001674 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001675 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001676 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001677 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001678 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001679 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001680 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001681 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1682 memcpy(buf, chars + start, length * sizeof(jchar));
1683 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001684 }
1685
Ian Rogersbc939662013-08-15 10:26:54 -07001686 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1687 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001688 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001689 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001690 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001691 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001692 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001693 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001694 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001695 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1696 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1697 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001698 }
1699
Elliott Hughes75770752011-08-24 17:52:38 -07001700 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001701 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001702 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001703 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1704 mirror::CharArray* chars = s->GetCharArray();
Fred Shih56890e22014-06-02 11:11:52 -07001705 gc::Heap* heap = Runtime::Current()->GetHeap();
1706 if (heap->IsMovableObject(chars)) {
1707 if (is_copy != nullptr) {
1708 *is_copy = JNI_TRUE;
1709 }
1710 int32_t char_count = s->GetLength();
1711 int32_t offset = s->GetOffset();
1712 jchar* bytes = new jchar[char_count];
1713 for (int32_t i = 0; i < char_count; i++) {
1714 bytes[i] = chars->Get(i + offset);
1715 }
1716 return bytes;
1717 } else {
1718 if (is_copy != nullptr) {
1719 *is_copy = JNI_FALSE;
1720 }
1721 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001722 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001723 }
1724
Mathieu Chartier590fee92013-09-13 13:46:47 -07001725 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001726 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001727 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001728 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1729 mirror::CharArray* s_chars = s->GetCharArray();
1730 if (chars != (s_chars->GetData() + s->GetOffset())) {
1731 delete[] chars;
1732 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 }
1734
Elliott Hughes75770752011-08-24 17:52:38 -07001735 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001736 CHECK_NON_NULL_ARGUMENT(java_string);
1737 ScopedObjectAccess soa(env);
1738 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1739 mirror::CharArray* chars = s->GetCharArray();
1740 int32_t offset = s->GetOffset();
Fred Shih56890e22014-06-02 11:11:52 -07001741 gc::Heap* heap = Runtime::Current()->GetHeap();
1742 if (heap->IsMovableObject(chars)) {
1743 StackHandleScope<1> hs(soa.Self());
1744 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
1745 heap->IncrementDisableMovingGC(soa.Self());
1746 }
1747 if (is_copy != nullptr) {
1748 *is_copy = JNI_FALSE;
1749 }
1750 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 }
1752
Elliott Hughes75770752011-08-24 17:52:38 -07001753 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001754 UNUSED(chars);
Fred Shih56890e22014-06-02 11:11:52 -07001755 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1756 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001757 gc::Heap* heap = Runtime::Current()->GetHeap();
1758 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1759 mirror::CharArray* s_chars = s->GetCharArray();
1760 if (heap->IsMovableObject(s_chars)) {
1761 heap->DecrementDisableMovingGC(soa.Self());
1762 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 }
1764
Elliott Hughes75770752011-08-24 17:52:38 -07001765 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001766 if (java_string == nullptr) {
1767 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001768 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001769 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001770 *is_copy = JNI_TRUE;
1771 }
Ian Rogersef28b142012-11-30 14:22:18 -08001772 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001773 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001774 size_t byte_count = s->GetUtfLength();
1775 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001776 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001777 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1778 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1779 bytes[byte_count] = '\0';
1780 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001781 }
1782
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001783 static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001784 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001785 }
1786
Elliott Hughesbd935992011-08-22 11:59:34 -07001787 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001788 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001790 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001791 if (UNLIKELY(!obj->IsArrayInstance())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001792 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1793 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001794 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001795 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001796 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Elliott Hughes814e4032011-08-23 12:07:56 -07001799 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001800 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001802 mirror::ObjectArray<mirror::Object>* array =
1803 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001804 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
Ian Rogersbc939662013-08-15 10:26:54 -07001807 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1808 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001809 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001810 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001811 mirror::ObjectArray<mirror::Object>* array =
1812 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1813 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001814 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
1817 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001818 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
1821 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001822 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
1825 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001826 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 }
1828
1829 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001830 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
1833 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001834 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
1837 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001838 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 }
1840
1841 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001842 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 }
1844
Ian Rogers1d99e452014-01-02 17:36:41 -08001845 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1846 jobject initial_element) {
1847 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001848 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001849 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001850 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001851 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001852
1853 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001854 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001855 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08001856 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001857 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08001858 if (UNLIKELY(element_class->IsPrimitive())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001859 soa.Vm()->JniAbortF("NewObjectArray", "not an object type: %s",
1860 PrettyDescriptor(element_class).c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08001861 return nullptr;
1862 }
Ian Rogers1d99e452014-01-02 17:36:41 -08001863 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07001864 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08001865 if (UNLIKELY(array_class == nullptr)) {
1866 return nullptr;
1867 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 }
1869
Elliott Hughes75770752011-08-24 17:52:38 -07001870 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001871 mirror::ObjectArray<mirror::Object>* result =
1872 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001873 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001874 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08001875 if (initial_object != nullptr) {
1876 mirror::Class* element_class = result->GetClass()->GetComponentType();
1877 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001878 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
1879 "element type of '%s'",
1880 PrettyDescriptor(initial_object->GetClass()).c_str(),
1881 PrettyDescriptor(element_class).c_str());
1882 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08001883 } else {
1884 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001885 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08001886 }
1887 }
Elliott Hughes75770752011-08-24 17:52:38 -07001888 }
1889 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001890 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
1893 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001894 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001895 }
1896
Ian Rogersa15e67d2012-02-28 13:51:55 -08001897 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001898 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001899 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001900 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07001901 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001902 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
1903 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001904 return nullptr;
1905 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001906 gc::Heap* heap = Runtime::Current()->GetHeap();
1907 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08001908 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001909 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001910 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001911 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001912 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001913 *is_copy = JNI_FALSE;
1914 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001915 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001916 }
1917
Ian Rogers2d10b202014-05-12 19:15:18 -07001918 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
1919 jint mode) {
1920 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
1921 ScopedObjectAccess soa(env);
1922 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
1923 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001924 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
1925 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001926 return;
1927 }
1928 const size_t component_size = array->GetClass()->GetComponentSize();
1929 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001930 }
1931
Elliott Hughes75770752011-08-24 17:52:38 -07001932 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001933 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 }
1935
Elliott Hughes75770752011-08-24 17:52:38 -07001936 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001937 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 }
1939
Elliott Hughes75770752011-08-24 17:52:38 -07001940 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001941 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001942 }
1943
Elliott Hughes75770752011-08-24 17:52:38 -07001944 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001945 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 }
1947
Elliott Hughes75770752011-08-24 17:52:38 -07001948 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001949 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 }
1951
Elliott Hughes75770752011-08-24 17:52:38 -07001952 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001953 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Elliott Hughes75770752011-08-24 17:52:38 -07001956 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001957 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Elliott Hughes75770752011-08-24 17:52:38 -07001960 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001961 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001962 }
1963
Mathieu Chartier590fee92013-09-13 13:46:47 -07001964 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
1965 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001966 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
1967 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Mathieu Chartier590fee92013-09-13 13:46:47 -07001970 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001971 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Mathieu Chartier590fee92013-09-13 13:46:47 -07001974 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001975 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Mathieu Chartier590fee92013-09-13 13:46:47 -07001978 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
1979 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001980 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Mathieu Chartier590fee92013-09-13 13:46:47 -07001983 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
1984 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001985 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Mathieu Chartier590fee92013-09-13 13:46:47 -07001988 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001989 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Mathieu Chartier590fee92013-09-13 13:46:47 -07001992 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001993 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Mathieu Chartier590fee92013-09-13 13:46:47 -07001996 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
1997 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001998 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Ian Rogersbc939662013-08-15 10:26:54 -07002001 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2002 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002003 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002004 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 }
2006
Ian Rogersbc939662013-08-15 10:26:54 -07002007 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2008 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002009 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Ian Rogersbc939662013-08-15 10:26:54 -07002012 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2013 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002014 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 }
2016
Ian Rogersbc939662013-08-15 10:26:54 -07002017 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2018 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002019 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002020 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Ian Rogersbc939662013-08-15 10:26:54 -07002023 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2024 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002025 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002026 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Ian Rogersbc939662013-08-15 10:26:54 -07002029 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2030 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002031 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Ian Rogersbc939662013-08-15 10:26:54 -07002034 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2035 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002036 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Ian Rogersbc939662013-08-15 10:26:54 -07002039 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2040 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002041 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002042 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Ian Rogersbc939662013-08-15 10:26:54 -07002045 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2046 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002047 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002048 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Ian Rogersbc939662013-08-15 10:26:54 -07002051 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2052 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002053 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Ian Rogersbc939662013-08-15 10:26:54 -07002056 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2057 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002058 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Ian Rogersbc939662013-08-15 10:26:54 -07002061 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2062 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002063 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002064 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Ian Rogersbc939662013-08-15 10:26:54 -07002067 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2068 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002069 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002070 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Ian Rogersbc939662013-08-15 10:26:54 -07002073 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2074 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002075 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Ian Rogersbc939662013-08-15 10:26:54 -07002078 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2079 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002080 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Ian Rogersbc939662013-08-15 10:26:54 -07002083 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2084 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002085 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002086 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Ian Rogersbc939662013-08-15 10:26:54 -07002089 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2090 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002091 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2092 }
2093
Ian Rogersbc939662013-08-15 10:26:54 -07002094 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2095 jint method_count, bool return_errors) {
2096 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002097 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2098 method_count);
2099 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002100 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002101 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002102 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002103 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002104 if (UNLIKELY(method_count == 0)) {
2105 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2106 << PrettyDescriptor(c);
2107 return JNI_OK;
2108 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002109 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002110 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 const char* name = methods[i].name;
2112 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002113 const void* fnPtr = methods[i].fnPtr;
2114 if (UNLIKELY(name == nullptr)) {
2115 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2116 return JNI_ERR;
2117 } else if (UNLIKELY(sig == nullptr)) {
2118 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2119 return JNI_ERR;
2120 } else if (UNLIKELY(fnPtr == nullptr)) {
2121 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2122 return JNI_ERR;
2123 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002124 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002126 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 ++sig;
2128 }
2129
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002130 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2131 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002132 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002134 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002135 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002136 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002137 << PrettyDescriptor(c) << "." << name << sig << " in "
2138 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002139 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002141 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002142 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002143 << PrettyDescriptor(c) << "." << name << sig
2144 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002145 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 return JNI_ERR;
2147 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002148
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002149 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002151 m->RegisterNative(fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
2153 return JNI_OK;
2154 }
2155
Elliott Hughes5174fe62011-08-23 15:12:35 -07002156 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002157 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002158 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002159 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002160
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002161 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162
Ian Rogers2d10b202014-05-12 19:15:18 -07002163 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002164 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002165 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002166 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002167 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002168 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002169 }
2170 }
2171 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002172 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002173 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002174 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002175 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002176 }
2177 }
2178
Ian Rogers2d10b202014-05-12 19:15:18 -07002179 if (unregistered_count == 0) {
2180 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2181 << PrettyDescriptor(c) << "' that contains no native methods";
2182 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002183 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 }
2185
Ian Rogers719d1a32014-03-06 12:13:39 -08002186 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002187 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002188 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002189 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2190 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002191 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002192 return JNI_ERR;
2193 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002194 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002195 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Ian Rogers719d1a32014-03-06 12:13:39 -08002198 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002199 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002200 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002201 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002202 o->MonitorExit(soa.Self());
2203 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002204 return JNI_ERR;
2205 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002206 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002207 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 }
2209
2210 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002211 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002213 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002214 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002216 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002218 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 }
2220
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002222 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002223 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2224 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002225 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002226 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002227 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002228 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2229 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002230 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002231 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002232
Brian Carlstrom85a93362014-06-25 09:30:52 -07002233 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002234 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002235 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2236 "buffer capacity greater than maximum jint: %" PRId64,
2237 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002238 return nullptr;
2239 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002240 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002241 jint capacity_arg = static_cast<jint>(capacity);
2242
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002243 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2244 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002245 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002246 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 }
2248
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002250 return reinterpret_cast<void*>(env->GetLongField(
2251 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002252 }
2253
Elliott Hughesb465ab02011-08-24 11:21:21 -07002254 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002255 return static_cast<jlong>(env->GetIntField(
2256 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 }
2258
Andreas Gampea8763072014-12-20 00:08:35 -08002259 static jobjectRefType GetObjectRefType(JNIEnv* env ATTRIBUTE_UNUSED, jobject java_object) {
2260 if (java_object == nullptr) {
2261 return JNIInvalidRefType;
2262 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002263
2264 // Do we definitely know what kind of reference this is?
2265 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2266 IndirectRefKind kind = GetIndirectRefKind(ref);
2267 switch (kind) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002268 case kLocal:
2269 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002270 case kGlobal:
2271 return JNIGlobalRefType;
2272 case kWeakGlobal:
2273 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002274 case kHandleScopeOrInvalid:
Ian Rogersc0542af2014-09-03 16:16:56 -07002275 // Assume value is in a handle scope.
2276 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002277 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002278 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
Andreas Gampea8763072014-12-20 00:08:35 -08002279 UNREACHABLE();
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002281
2282 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002283 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2284 const char* caller)
2285 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002286 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002287 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002288 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2289 return JNI_ERR;
2290 }
2291 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002292 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002293 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2294 if (!okay) {
2295 soa.Self()->ThrowOutOfMemoryError(caller);
2296 }
2297 return okay ? JNI_OK : JNI_ERR;
2298 }
2299
2300 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002301 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002302 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002303 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002304 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002305 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002306 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002307 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002308 return soa.AddLocalReference<JniT>(result);
2309 }
2310
Ian Rogers2d10b202014-05-12 19:15:18 -07002311 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2312 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2313 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002314 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002315 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002316 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002317 soa.Vm()->JniAbortF(fn_name,
2318 "attempt to %s %s primitive array elements with an object of type %s",
2319 operation,
2320 PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2321 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002322 return nullptr;
2323 }
2324 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2325 return array;
2326 }
2327
2328 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2329 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2330 CHECK_NON_NULL_ARGUMENT(java_array);
2331 ScopedObjectAccess soa(env);
2332 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2333 "GetArrayElements",
2334 "get");
2335 if (UNLIKELY(array == nullptr)) {
2336 return nullptr;
2337 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002338 // Only make a copy if necessary.
2339 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2340 if (is_copy != nullptr) {
2341 *is_copy = JNI_TRUE;
2342 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002343 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002344 size_t size = array->GetLength() * component_size;
2345 void* data = new uint64_t[RoundUp(size, 8) / 8];
2346 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002347 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002348 } else {
2349 if (is_copy != nullptr) {
2350 *is_copy = JNI_FALSE;
2351 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002352 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002353 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002354 }
2355
Ian Rogers2d10b202014-05-12 19:15:18 -07002356 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002357 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002358 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002359 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002360 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2361 "ReleaseArrayElements",
2362 "release");
2363 if (array == nullptr) {
2364 return;
2365 }
2366 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2367 }
2368
2369 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2370 size_t component_size, void* elements, jint mode)
2371 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002372 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002373 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002374 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002375 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002376 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2377 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002378 if (is_copy) {
2379 // Sanity check: If elements is not the same as the java array's data, it better not be a
2380 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2381 // copies we make?
2382 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002383 soa.Vm()->JniAbortF("ReleaseArrayElements",
2384 "invalid element pointer %p, array elements are %p",
2385 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002386 return;
2387 }
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002388 if (mode != JNI_ABORT) {
2389 memcpy(array_data, elements, bytes);
2390 } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2391 // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2392 LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2393 soa.Self()->DumpJavaStack(LOG(WARNING));
2394 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002395 }
2396 if (mode != JNI_COMMIT) {
2397 if (is_copy) {
2398 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002399 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002400 // Non copy to a movable object must means that we had disabled the moving GC.
2401 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002402 }
2403 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002404 }
2405
Ian Rogers2d10b202014-05-12 19:15:18 -07002406 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2407 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2408 jsize start, jsize length, ElementT* buf) {
2409 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2410 ScopedObjectAccess soa(env);
2411 ArtArrayT* array =
2412 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2413 "GetPrimitiveArrayRegion",
2414 "get region of");
2415 if (array != nullptr) {
2416 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2417 ThrowAIOOBE(soa, array, start, length, "src");
2418 } else {
2419 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2420 ElementT* data = array->GetData();
2421 memcpy(buf, data + start, length * sizeof(ElementT));
2422 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002423 }
2424 }
2425
Ian Rogers2d10b202014-05-12 19:15:18 -07002426 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2427 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2428 jsize start, jsize length, const ElementT* buf) {
2429 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2430 ScopedObjectAccess soa(env);
2431 ArtArrayT* array =
2432 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2433 "SetPrimitiveArrayRegion",
2434 "set region of");
2435 if (array != nullptr) {
2436 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2437 ThrowAIOOBE(soa, array, start, length, "dst");
2438 } else {
2439 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2440 ElementT* data = array->GetData();
2441 memcpy(data + start, buf, length * sizeof(ElementT));
2442 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002443 }
2444 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002445};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002446
Elliott Hughes88c5c352012-03-15 18:49:48 -07002447const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002448 nullptr, // reserved0.
2449 nullptr, // reserved1.
2450 nullptr, // reserved2.
2451 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002452 JNI::GetVersion,
2453 JNI::DefineClass,
2454 JNI::FindClass,
2455 JNI::FromReflectedMethod,
2456 JNI::FromReflectedField,
2457 JNI::ToReflectedMethod,
2458 JNI::GetSuperclass,
2459 JNI::IsAssignableFrom,
2460 JNI::ToReflectedField,
2461 JNI::Throw,
2462 JNI::ThrowNew,
2463 JNI::ExceptionOccurred,
2464 JNI::ExceptionDescribe,
2465 JNI::ExceptionClear,
2466 JNI::FatalError,
2467 JNI::PushLocalFrame,
2468 JNI::PopLocalFrame,
2469 JNI::NewGlobalRef,
2470 JNI::DeleteGlobalRef,
2471 JNI::DeleteLocalRef,
2472 JNI::IsSameObject,
2473 JNI::NewLocalRef,
2474 JNI::EnsureLocalCapacity,
2475 JNI::AllocObject,
2476 JNI::NewObject,
2477 JNI::NewObjectV,
2478 JNI::NewObjectA,
2479 JNI::GetObjectClass,
2480 JNI::IsInstanceOf,
2481 JNI::GetMethodID,
2482 JNI::CallObjectMethod,
2483 JNI::CallObjectMethodV,
2484 JNI::CallObjectMethodA,
2485 JNI::CallBooleanMethod,
2486 JNI::CallBooleanMethodV,
2487 JNI::CallBooleanMethodA,
2488 JNI::CallByteMethod,
2489 JNI::CallByteMethodV,
2490 JNI::CallByteMethodA,
2491 JNI::CallCharMethod,
2492 JNI::CallCharMethodV,
2493 JNI::CallCharMethodA,
2494 JNI::CallShortMethod,
2495 JNI::CallShortMethodV,
2496 JNI::CallShortMethodA,
2497 JNI::CallIntMethod,
2498 JNI::CallIntMethodV,
2499 JNI::CallIntMethodA,
2500 JNI::CallLongMethod,
2501 JNI::CallLongMethodV,
2502 JNI::CallLongMethodA,
2503 JNI::CallFloatMethod,
2504 JNI::CallFloatMethodV,
2505 JNI::CallFloatMethodA,
2506 JNI::CallDoubleMethod,
2507 JNI::CallDoubleMethodV,
2508 JNI::CallDoubleMethodA,
2509 JNI::CallVoidMethod,
2510 JNI::CallVoidMethodV,
2511 JNI::CallVoidMethodA,
2512 JNI::CallNonvirtualObjectMethod,
2513 JNI::CallNonvirtualObjectMethodV,
2514 JNI::CallNonvirtualObjectMethodA,
2515 JNI::CallNonvirtualBooleanMethod,
2516 JNI::CallNonvirtualBooleanMethodV,
2517 JNI::CallNonvirtualBooleanMethodA,
2518 JNI::CallNonvirtualByteMethod,
2519 JNI::CallNonvirtualByteMethodV,
2520 JNI::CallNonvirtualByteMethodA,
2521 JNI::CallNonvirtualCharMethod,
2522 JNI::CallNonvirtualCharMethodV,
2523 JNI::CallNonvirtualCharMethodA,
2524 JNI::CallNonvirtualShortMethod,
2525 JNI::CallNonvirtualShortMethodV,
2526 JNI::CallNonvirtualShortMethodA,
2527 JNI::CallNonvirtualIntMethod,
2528 JNI::CallNonvirtualIntMethodV,
2529 JNI::CallNonvirtualIntMethodA,
2530 JNI::CallNonvirtualLongMethod,
2531 JNI::CallNonvirtualLongMethodV,
2532 JNI::CallNonvirtualLongMethodA,
2533 JNI::CallNonvirtualFloatMethod,
2534 JNI::CallNonvirtualFloatMethodV,
2535 JNI::CallNonvirtualFloatMethodA,
2536 JNI::CallNonvirtualDoubleMethod,
2537 JNI::CallNonvirtualDoubleMethodV,
2538 JNI::CallNonvirtualDoubleMethodA,
2539 JNI::CallNonvirtualVoidMethod,
2540 JNI::CallNonvirtualVoidMethodV,
2541 JNI::CallNonvirtualVoidMethodA,
2542 JNI::GetFieldID,
2543 JNI::GetObjectField,
2544 JNI::GetBooleanField,
2545 JNI::GetByteField,
2546 JNI::GetCharField,
2547 JNI::GetShortField,
2548 JNI::GetIntField,
2549 JNI::GetLongField,
2550 JNI::GetFloatField,
2551 JNI::GetDoubleField,
2552 JNI::SetObjectField,
2553 JNI::SetBooleanField,
2554 JNI::SetByteField,
2555 JNI::SetCharField,
2556 JNI::SetShortField,
2557 JNI::SetIntField,
2558 JNI::SetLongField,
2559 JNI::SetFloatField,
2560 JNI::SetDoubleField,
2561 JNI::GetStaticMethodID,
2562 JNI::CallStaticObjectMethod,
2563 JNI::CallStaticObjectMethodV,
2564 JNI::CallStaticObjectMethodA,
2565 JNI::CallStaticBooleanMethod,
2566 JNI::CallStaticBooleanMethodV,
2567 JNI::CallStaticBooleanMethodA,
2568 JNI::CallStaticByteMethod,
2569 JNI::CallStaticByteMethodV,
2570 JNI::CallStaticByteMethodA,
2571 JNI::CallStaticCharMethod,
2572 JNI::CallStaticCharMethodV,
2573 JNI::CallStaticCharMethodA,
2574 JNI::CallStaticShortMethod,
2575 JNI::CallStaticShortMethodV,
2576 JNI::CallStaticShortMethodA,
2577 JNI::CallStaticIntMethod,
2578 JNI::CallStaticIntMethodV,
2579 JNI::CallStaticIntMethodA,
2580 JNI::CallStaticLongMethod,
2581 JNI::CallStaticLongMethodV,
2582 JNI::CallStaticLongMethodA,
2583 JNI::CallStaticFloatMethod,
2584 JNI::CallStaticFloatMethodV,
2585 JNI::CallStaticFloatMethodA,
2586 JNI::CallStaticDoubleMethod,
2587 JNI::CallStaticDoubleMethodV,
2588 JNI::CallStaticDoubleMethodA,
2589 JNI::CallStaticVoidMethod,
2590 JNI::CallStaticVoidMethodV,
2591 JNI::CallStaticVoidMethodA,
2592 JNI::GetStaticFieldID,
2593 JNI::GetStaticObjectField,
2594 JNI::GetStaticBooleanField,
2595 JNI::GetStaticByteField,
2596 JNI::GetStaticCharField,
2597 JNI::GetStaticShortField,
2598 JNI::GetStaticIntField,
2599 JNI::GetStaticLongField,
2600 JNI::GetStaticFloatField,
2601 JNI::GetStaticDoubleField,
2602 JNI::SetStaticObjectField,
2603 JNI::SetStaticBooleanField,
2604 JNI::SetStaticByteField,
2605 JNI::SetStaticCharField,
2606 JNI::SetStaticShortField,
2607 JNI::SetStaticIntField,
2608 JNI::SetStaticLongField,
2609 JNI::SetStaticFloatField,
2610 JNI::SetStaticDoubleField,
2611 JNI::NewString,
2612 JNI::GetStringLength,
2613 JNI::GetStringChars,
2614 JNI::ReleaseStringChars,
2615 JNI::NewStringUTF,
2616 JNI::GetStringUTFLength,
2617 JNI::GetStringUTFChars,
2618 JNI::ReleaseStringUTFChars,
2619 JNI::GetArrayLength,
2620 JNI::NewObjectArray,
2621 JNI::GetObjectArrayElement,
2622 JNI::SetObjectArrayElement,
2623 JNI::NewBooleanArray,
2624 JNI::NewByteArray,
2625 JNI::NewCharArray,
2626 JNI::NewShortArray,
2627 JNI::NewIntArray,
2628 JNI::NewLongArray,
2629 JNI::NewFloatArray,
2630 JNI::NewDoubleArray,
2631 JNI::GetBooleanArrayElements,
2632 JNI::GetByteArrayElements,
2633 JNI::GetCharArrayElements,
2634 JNI::GetShortArrayElements,
2635 JNI::GetIntArrayElements,
2636 JNI::GetLongArrayElements,
2637 JNI::GetFloatArrayElements,
2638 JNI::GetDoubleArrayElements,
2639 JNI::ReleaseBooleanArrayElements,
2640 JNI::ReleaseByteArrayElements,
2641 JNI::ReleaseCharArrayElements,
2642 JNI::ReleaseShortArrayElements,
2643 JNI::ReleaseIntArrayElements,
2644 JNI::ReleaseLongArrayElements,
2645 JNI::ReleaseFloatArrayElements,
2646 JNI::ReleaseDoubleArrayElements,
2647 JNI::GetBooleanArrayRegion,
2648 JNI::GetByteArrayRegion,
2649 JNI::GetCharArrayRegion,
2650 JNI::GetShortArrayRegion,
2651 JNI::GetIntArrayRegion,
2652 JNI::GetLongArrayRegion,
2653 JNI::GetFloatArrayRegion,
2654 JNI::GetDoubleArrayRegion,
2655 JNI::SetBooleanArrayRegion,
2656 JNI::SetByteArrayRegion,
2657 JNI::SetCharArrayRegion,
2658 JNI::SetShortArrayRegion,
2659 JNI::SetIntArrayRegion,
2660 JNI::SetLongArrayRegion,
2661 JNI::SetFloatArrayRegion,
2662 JNI::SetDoubleArrayRegion,
2663 JNI::RegisterNatives,
2664 JNI::UnregisterNatives,
2665 JNI::MonitorEnter,
2666 JNI::MonitorExit,
2667 JNI::GetJavaVM,
2668 JNI::GetStringRegion,
2669 JNI::GetStringUTFRegion,
2670 JNI::GetPrimitiveArrayCritical,
2671 JNI::ReleasePrimitiveArrayCritical,
2672 JNI::GetStringCritical,
2673 JNI::ReleaseStringCritical,
2674 JNI::NewWeakGlobalRef,
2675 JNI::DeleteWeakGlobalRef,
2676 JNI::ExceptionCheck,
2677 JNI::NewDirectByteBuffer,
2678 JNI::GetDirectBufferAddress,
2679 JNI::GetDirectBufferCapacity,
2680 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002681};
2682
Ian Rogers68d8b422014-07-17 11:09:10 -07002683const JNINativeInterface* GetJniNativeInterface() {
2684 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002685}
2686
Elliott Hughesc8fece32013-01-02 11:27:23 -08002687void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002688 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002689 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002690 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002691 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2692 }
2693 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2694}
2695
Ian Rogersdf20fe02011-07-20 20:34:16 -07002696} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002697
2698std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2699 switch (rhs) {
2700 case JNIInvalidRefType:
2701 os << "JNIInvalidRefType";
2702 return os;
2703 case JNILocalRefType:
2704 os << "JNILocalRefType";
2705 return os;
2706 case JNIGlobalRefType:
2707 os << "JNIGlobalRefType";
2708 return os;
2709 case JNIWeakGlobalRefType:
2710 os << "JNIWeakGlobalRefType";
2711 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002712 default:
Ian Rogersc7dd2952014-10-21 23:31:19 -07002713 LOG(::art::FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
2714 UNREACHABLE();
Elliott Hughesb465ab02011-08-24 11:21:21 -07002715 }
2716}