blob: 1dcfcabf9db6dee2fde9b1bc6c85afcee373b3fb [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
569 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800570 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700571 return;
572 }
Ian Rogersef28b142012-11-30 14:22:18 -0800573 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700574
Ian Rogersef28b142012-11-30 14:22:18 -0800575 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700576 if (!locals.Remove(cookie, obj)) {
577 // Attempting to delete a local reference that is not in the
578 // topmost local reference frame is a no-op. DeleteLocalRef returns
579 // void and doesn't throw any exceptions, but we should probably
580 // complain about it so the user will notice that things aren't
581 // going quite the way they expect.
582 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
583 << "failed to find entry";
584 }
585 }
586
587 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700588 if (obj1 == obj2) {
589 return JNI_TRUE;
590 } else {
591 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800592 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
593 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700594 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700595 }
596
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700597 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700598 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700599 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800600 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800601 if (c == nullptr) {
602 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700603 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700604 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700605 }
606
Ian Rogersbc939662013-08-15 10:26:54 -0700607 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700608 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700609 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700610 CHECK_NON_NULL_ARGUMENT(java_class);
611 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700612 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700613 va_end(args);
614 return result;
615 }
616
Elliott Hughes72025e52011-08-23 17:50:30 -0700617 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700618 CHECK_NON_NULL_ARGUMENT(java_class);
619 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800621 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800622 if (c == nullptr) {
623 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700624 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800625 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800626 if (result == nullptr) {
627 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700628 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700629 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700630 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800631 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800632 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700633 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800634 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 }
636
Elliott Hughes72025e52011-08-23 17:50:30 -0700637 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700638 CHECK_NON_NULL_ARGUMENT(java_class);
639 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700640 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800641 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800642 if (c == nullptr) {
643 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700644 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800645 mirror::Object* result = c->AllocObject(soa.Self());
646 if (result == nullptr) {
647 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700648 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700649 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700650 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800651 if (soa.Self()->IsExceptionPending()) {
652 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700653 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800654 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 }
656
Ian Rogersbc939662013-08-15 10:26:54 -0700657 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700658 CHECK_NON_NULL_ARGUMENT(java_class);
659 CHECK_NON_NULL_ARGUMENT(name);
660 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700662 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 }
664
Ian Rogersbc939662013-08-15 10:26:54 -0700665 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
666 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700667 CHECK_NON_NULL_ARGUMENT(java_class);
668 CHECK_NON_NULL_ARGUMENT(name);
669 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700671 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700672 }
673
Elliott Hughes72025e52011-08-23 17:50:30 -0700674 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700675 va_list ap;
676 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700677 CHECK_NON_NULL_ARGUMENT(obj);
678 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700679 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700680 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700681 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700682 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 }
684
Elliott Hughes72025e52011-08-23 17:50:30 -0700685 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700686 CHECK_NON_NULL_ARGUMENT(obj);
687 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700688 ScopedObjectAccess soa(env);
689 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
690 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 }
692
Elliott Hughes72025e52011-08-23 17:50:30 -0700693 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700694 CHECK_NON_NULL_ARGUMENT(obj);
695 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700697 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
698 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700699 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700700 }
701
Elliott Hughes72025e52011-08-23 17:50:30 -0700702 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700703 va_list ap;
704 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700705 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
706 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700707 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700709 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700710 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 }
712
Elliott Hughes72025e52011-08-23 17:50:30 -0700713 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700714 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
715 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 ScopedObjectAccess soa(env);
717 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 }
719
Elliott Hughes72025e52011-08-23 17:50:30 -0700720 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700721 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
722 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700723 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700724 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
725 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 }
727
Elliott Hughes72025e52011-08-23 17:50:30 -0700728 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700729 va_list ap;
730 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700731 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
732 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700733 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700735 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700736 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700737 }
738
Elliott Hughes72025e52011-08-23 17:50:30 -0700739 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700740 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
741 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700742 ScopedObjectAccess soa(env);
743 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 }
745
Elliott Hughes72025e52011-08-23 17:50:30 -0700746 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700747 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
748 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700749 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700750 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
751 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 }
753
Elliott Hughes72025e52011-08-23 17:50:30 -0700754 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700755 va_list ap;
756 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700757 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
758 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700759 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700760 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700761 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700762 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700763 }
764
Elliott Hughes72025e52011-08-23 17:50:30 -0700765 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700766 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
767 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700768 ScopedObjectAccess soa(env);
769 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700770 }
771
Elliott Hughes72025e52011-08-23 17:50:30 -0700772 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700773 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
774 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700775 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700776 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
777 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 }
779
Elliott Hughes72025e52011-08-23 17:50:30 -0700780 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700781 va_list ap;
782 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700783 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
784 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700785 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700786 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700787 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700788 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700789 }
790
Elliott Hughes72025e52011-08-23 17:50:30 -0700791 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700792 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
793 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700794 ScopedObjectAccess soa(env);
795 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 }
797
Elliott Hughes72025e52011-08-23 17:50:30 -0700798 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700799 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
800 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700801 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700802 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
803 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 }
805
Elliott Hughes72025e52011-08-23 17:50:30 -0700806 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700807 va_list ap;
808 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700809 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
810 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700811 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700813 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700814 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 }
816
Elliott Hughes72025e52011-08-23 17:50:30 -0700817 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700818 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
819 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700820 ScopedObjectAccess soa(env);
821 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 }
823
Elliott Hughes72025e52011-08-23 17:50:30 -0700824 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700825 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
826 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700827 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700828 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
829 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 }
831
Elliott Hughes72025e52011-08-23 17:50:30 -0700832 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700833 va_list ap;
834 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700835 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
836 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700837 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700838 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700839 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700840 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 }
842
Elliott Hughes72025e52011-08-23 17:50:30 -0700843 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700844 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
845 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 ScopedObjectAccess soa(env);
847 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 }
849
Elliott Hughes72025e52011-08-23 17:50:30 -0700850 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700851 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
852 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700853 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700854 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
855 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 }
857
Elliott Hughes72025e52011-08-23 17:50:30 -0700858 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700859 va_list ap;
860 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700861 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
862 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700863 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700864 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700865 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700866 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700867 }
868
Elliott Hughes72025e52011-08-23 17:50:30 -0700869 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700870 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
871 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700872 ScopedObjectAccess soa(env);
873 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 }
875
Elliott Hughes72025e52011-08-23 17:50:30 -0700876 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700877 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
878 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700879 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700880 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
881 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 }
883
Elliott Hughes72025e52011-08-23 17:50:30 -0700884 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700885 va_list ap;
886 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700887 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
888 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700889 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700890 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700891 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700892 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 }
894
Elliott Hughes72025e52011-08-23 17:50:30 -0700895 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700896 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
897 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 ScopedObjectAccess soa(env);
899 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 }
901
Elliott Hughes72025e52011-08-23 17:50:30 -0700902 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700903 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
904 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700905 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700906 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
907 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 }
909
Elliott Hughes72025e52011-08-23 17:50:30 -0700910 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 va_list ap;
912 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700913 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
914 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700915 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -0700916 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700917 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -0700918 }
919
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700921 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
922 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700923 ScopedObjectAccess soa(env);
924 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 }
926
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700928 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
929 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700930 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700931 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700932 }
933
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700934 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700936 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700937 CHECK_NON_NULL_ARGUMENT(obj);
938 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700939 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700940 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
941 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 va_end(ap);
943 return local_result;
944 }
945
Ian Rogersbc939662013-08-15 10:26:54 -0700946 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
947 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700948 CHECK_NON_NULL_ARGUMENT(obj);
949 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700950 ScopedObjectAccess soa(env);
951 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
952 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 }
954
Ian Rogersbc939662013-08-15 10:26:54 -0700955 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
956 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700957 CHECK_NON_NULL_ARGUMENT(obj);
958 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700959 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700960 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Ian Rogersbc939662013-08-15 10:26:54 -0700964 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
965 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700968 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
969 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700970 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700971 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700973 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Ian Rogersbc939662013-08-15 10:26:54 -0700976 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
977 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700978 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
979 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700980 ScopedObjectAccess soa(env);
981 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 }
983
Ian Rogersbc939662013-08-15 10:26:54 -0700984 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
985 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700986 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
987 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700988 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700989 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 }
991
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700992 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700995 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
996 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700997 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001000 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Ian Rogersbc939662013-08-15 10:26:54 -07001003 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1004 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001005 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1006 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001007 ScopedObjectAccess soa(env);
1008 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 }
1010
Ian Rogersbc939662013-08-15 10:26:54 -07001011 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1012 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001013 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1014 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001015 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001016 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001019 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001022 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1023 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001024 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001025 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001027 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Ian Rogersbc939662013-08-15 10:26:54 -07001030 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1031 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001032 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1033 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001034 ScopedObjectAccess soa(env);
1035 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Ian Rogersbc939662013-08-15 10:26:54 -07001038 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1039 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001040 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1041 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001042 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001043 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001046 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001049 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1050 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001051 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001052 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001054 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Ian Rogersbc939662013-08-15 10:26:54 -07001057 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1058 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001059 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1060 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001061 ScopedObjectAccess soa(env);
1062 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 }
1064
Ian Rogersbc939662013-08-15 10:26:54 -07001065 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1066 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001067 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1068 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001069 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001070 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001073 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001076 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1077 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001078 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001081 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 }
1083
Ian Rogersbc939662013-08-15 10:26:54 -07001084 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1085 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001086 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1087 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088 ScopedObjectAccess soa(env);
1089 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Ian Rogersbc939662013-08-15 10:26:54 -07001092 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1093 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001094 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1095 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001096 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001097 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001100 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001103 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1104 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001105 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001108 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Ian Rogersbc939662013-08-15 10:26:54 -07001111 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1112 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001113 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1114 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001115 ScopedObjectAccess soa(env);
1116 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Ian Rogersbc939662013-08-15 10:26:54 -07001119 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1120 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001121 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1122 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001123 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001124 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001127 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001130 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1131 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001132 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001133 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001135 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
Ian Rogersbc939662013-08-15 10:26:54 -07001138 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1139 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001140 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1141 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 ScopedObjectAccess soa(env);
1143 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Ian Rogersbc939662013-08-15 10:26:54 -07001146 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1147 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001148 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1149 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001150 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001151 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 }
1153
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001154 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001157 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1158 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001159 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001160 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001162 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Ian Rogersbc939662013-08-15 10:26:54 -07001165 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1166 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001167 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1168 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001169 ScopedObjectAccess soa(env);
1170 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Ian Rogersbc939662013-08-15 10:26:54 -07001173 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1174 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001175 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1176 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001178 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 }
1180
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001181 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001184 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1185 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001186 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001187 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 va_end(ap);
1189 }
1190
Brian Carlstromea46f952013-07-30 01:26:50 -07001191 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1192 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001193 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1194 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001195 ScopedObjectAccess soa(env);
1196 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
Ian Rogersbc939662013-08-15 10:26:54 -07001199 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1200 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001201 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1202 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001203 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001204 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
Ian Rogersbc939662013-08-15 10:26:54 -07001207 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001208 CHECK_NON_NULL_ARGUMENT(java_class);
1209 CHECK_NON_NULL_ARGUMENT(name);
1210 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001211 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001212 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001214
Ian Rogersbc939662013-08-15 10:26:54 -07001215 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1216 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001217 CHECK_NON_NULL_ARGUMENT(java_class);
1218 CHECK_NON_NULL_ARGUMENT(name);
1219 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001220 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001221 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001223
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001224 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001225 CHECK_NON_NULL_ARGUMENT(obj);
1226 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001227 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001228 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1229 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001230 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001232
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001233 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001234 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001235 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001236 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001237 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001240 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001241 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1242 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001243 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001244 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1245 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1246 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001247 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001250 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001251 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001252 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001253 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1254 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001255 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001258#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001259 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1260 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001261 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001262 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1263 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001264 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001265
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001266#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001267 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001268 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001269 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001270 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001271
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001272#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001273 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1274 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001275 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001276 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1277 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001278 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001279
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001280#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001281 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001282 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001283 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001284 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001285
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001286 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001287 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001290 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001291 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001294 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001295 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 }
1297
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001298 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001299 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001302 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001303 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001306 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001307 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001310 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001311 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001314 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001315 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001318 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001319 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001322 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001323 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001326 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001327 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001331 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
1333
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001334 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001335 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 }
1337
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001338 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001339 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001340 }
1341
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001342 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001343 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001344 }
1345
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001346 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001347 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001348 }
1349
1350 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001351 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001352 }
1353
1354 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001355 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001356 }
1357
1358 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001359 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001360 }
1361
1362 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001363 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001364 }
1365
1366 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001367 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001368 }
1369
1370 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001371 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001372 }
1373
1374 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001375 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001376 }
1377
1378 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001379 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001380 }
1381
1382 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001383 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001384 }
1385
1386 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001387 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 }
1389
1390 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001391 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 }
1393
1394 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001395 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 }
1397
1398 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001399 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 }
1401
1402 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001403 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404 }
1405
1406 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001407 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001408 }
1409
1410 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001411 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001414 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001416 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001417 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001418 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001419 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001420 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 va_end(ap);
1422 return local_result;
1423 }
1424
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001425 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001426 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001427 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001428 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001429 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001432 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001433 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001434 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001435 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001436 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001439 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001441 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001442 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001443 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001444 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001446 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 }
1448
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001449 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001450 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001451 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001452 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001455 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001456 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001457 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001458 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
1460
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001461 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001463 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001464 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001465 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001466 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001468 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001471 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001472 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001473 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001474 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 }
1476
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001477 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001478 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001479 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001480 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001483 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001485 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001486 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001487 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001488 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001490 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 }
1492
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001493 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001494 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001495 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001496 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001499 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001500 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001501 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001502 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 }
1504
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001505 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001507 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001508 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001509 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001510 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001511 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001512 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 }
1514
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001515 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001516 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001517 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001518 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 }
1520
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001521 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001522 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001523 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001524 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
1526
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001527 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001529 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001530 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001531 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001532 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001534 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
1536
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001537 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001538 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001539 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001540 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 }
1542
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001543 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001544 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001545 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001546 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001549 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001551 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001552 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001553 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001554 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001556 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001559 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001560 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001561 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001562 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001565 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001566 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001567 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001568 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001571 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001572 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001573 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001574 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001575 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001576 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001578 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 }
1580
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001581 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001582 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001583 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001584 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001587 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001588 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001589 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001590 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001593 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001595 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001596 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001597 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001598 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001600 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001603 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001604 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001605 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001606 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001609 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001610 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001611 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001612 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001615 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001617 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001618 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001619 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001620 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 va_end(ap);
1622 }
1623
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001624 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001625 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001626 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001627 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001630 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001631 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001632 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001633 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes814e4032011-08-23 12:07:56 -07001636 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001637 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001638 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001639 return nullptr;
1640 }
1641 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001642 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001643 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001644 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001645 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001646 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001647 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
1650 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001651 if (utf == nullptr) {
1652 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001654 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001655 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001656 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 }
1658
Elliott Hughes814e4032011-08-23 12:07:56 -07001659 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001660 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001661 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001662 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001663 }
1664
1665 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001666 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001667 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001668 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001669 }
1670
Ian Rogersbc939662013-08-15 10:26:54 -07001671 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1672 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001673 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001674 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001675 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001676 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001677 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001678 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001679 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001680 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1681 memcpy(buf, chars + start, length * sizeof(jchar));
1682 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001683 }
1684
Ian Rogersbc939662013-08-15 10:26:54 -07001685 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1686 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001687 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001688 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001689 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001690 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001691 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001692 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001693 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001694 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1695 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1696 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001697 }
1698
Elliott Hughes75770752011-08-24 17:52:38 -07001699 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001700 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001701 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001702 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1703 mirror::CharArray* chars = s->GetCharArray();
Fred Shih56890e22014-06-02 11:11:52 -07001704 gc::Heap* heap = Runtime::Current()->GetHeap();
1705 if (heap->IsMovableObject(chars)) {
1706 if (is_copy != nullptr) {
1707 *is_copy = JNI_TRUE;
1708 }
1709 int32_t char_count = s->GetLength();
1710 int32_t offset = s->GetOffset();
1711 jchar* bytes = new jchar[char_count];
1712 for (int32_t i = 0; i < char_count; i++) {
1713 bytes[i] = chars->Get(i + offset);
1714 }
1715 return bytes;
1716 } else {
1717 if (is_copy != nullptr) {
1718 *is_copy = JNI_FALSE;
1719 }
1720 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001721 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001722 }
1723
Mathieu Chartier590fee92013-09-13 13:46:47 -07001724 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001725 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001727 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1728 mirror::CharArray* s_chars = s->GetCharArray();
1729 if (chars != (s_chars->GetData() + s->GetOffset())) {
1730 delete[] chars;
1731 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes75770752011-08-24 17:52:38 -07001734 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001735 CHECK_NON_NULL_ARGUMENT(java_string);
1736 ScopedObjectAccess soa(env);
1737 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1738 mirror::CharArray* chars = s->GetCharArray();
1739 int32_t offset = s->GetOffset();
Fred Shih56890e22014-06-02 11:11:52 -07001740 gc::Heap* heap = Runtime::Current()->GetHeap();
1741 if (heap->IsMovableObject(chars)) {
1742 StackHandleScope<1> hs(soa.Self());
1743 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
1744 heap->IncrementDisableMovingGC(soa.Self());
1745 }
1746 if (is_copy != nullptr) {
1747 *is_copy = JNI_FALSE;
1748 }
1749 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes75770752011-08-24 17:52:38 -07001752 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001753 UNUSED(chars);
Fred Shih56890e22014-06-02 11:11:52 -07001754 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1755 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001756 gc::Heap* heap = Runtime::Current()->GetHeap();
1757 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1758 mirror::CharArray* s_chars = s->GetCharArray();
1759 if (heap->IsMovableObject(s_chars)) {
1760 heap->DecrementDisableMovingGC(soa.Self());
1761 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 }
1763
Elliott Hughes75770752011-08-24 17:52:38 -07001764 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001765 if (java_string == nullptr) {
1766 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001767 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001768 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001769 *is_copy = JNI_TRUE;
1770 }
Ian Rogersef28b142012-11-30 14:22:18 -08001771 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001772 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001773 size_t byte_count = s->GetUtfLength();
1774 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001775 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001776 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1777 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1778 bytes[byte_count] = '\0';
1779 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001780 }
1781
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001782 static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001783 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001784 }
1785
Elliott Hughesbd935992011-08-22 11:59:34 -07001786 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001787 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001788 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001789 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001790 if (UNLIKELY(!obj->IsArrayInstance())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001791 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1792 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001793 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001794 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001795 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 }
1797
Elliott Hughes814e4032011-08-23 12:07:56 -07001798 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001799 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001800 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001801 mirror::ObjectArray<mirror::Object>* array =
1802 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001803 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
Ian Rogersbc939662013-08-15 10:26:54 -07001806 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1807 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001808 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001809 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001810 mirror::ObjectArray<mirror::Object>* array =
1811 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1812 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001813 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 }
1815
1816 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001817 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 }
1819
1820 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001821 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 }
1823
1824 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001825 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
1828 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001829 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 }
1831
1832 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001833 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 }
1835
1836 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001837 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 }
1839
1840 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001841 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 }
1843
Ian Rogers1d99e452014-01-02 17:36:41 -08001844 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1845 jobject initial_element) {
1846 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001847 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001848 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001849 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001850 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001851
1852 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001853 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001854 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08001855 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001856 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08001857 if (UNLIKELY(element_class->IsPrimitive())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001858 soa.Vm()->JniAbortF("NewObjectArray", "not an object type: %s",
1859 PrettyDescriptor(element_class).c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08001860 return nullptr;
1861 }
Ian Rogers1d99e452014-01-02 17:36:41 -08001862 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07001863 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08001864 if (UNLIKELY(array_class == nullptr)) {
1865 return nullptr;
1866 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 }
1868
Elliott Hughes75770752011-08-24 17:52:38 -07001869 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001870 mirror::ObjectArray<mirror::Object>* result =
1871 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001872 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001873 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08001874 if (initial_object != nullptr) {
1875 mirror::Class* element_class = result->GetClass()->GetComponentType();
1876 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001877 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
1878 "element type of '%s'",
1879 PrettyDescriptor(initial_object->GetClass()).c_str(),
1880 PrettyDescriptor(element_class).c_str());
1881 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08001882 } else {
1883 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001884 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08001885 }
1886 }
Elliott Hughes75770752011-08-24 17:52:38 -07001887 }
1888 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001890 }
1891
1892 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001893 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001894 }
1895
Ian Rogersa15e67d2012-02-28 13:51:55 -08001896 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001897 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001898 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001899 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07001900 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001901 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
1902 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001903 return nullptr;
1904 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001905 gc::Heap* heap = Runtime::Current()->GetHeap();
1906 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08001907 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001908 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001909 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001910 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001911 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001912 *is_copy = JNI_FALSE;
1913 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001914 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001915 }
1916
Ian Rogers2d10b202014-05-12 19:15:18 -07001917 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
1918 jint mode) {
1919 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
1920 ScopedObjectAccess soa(env);
1921 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
1922 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001923 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
1924 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001925 return;
1926 }
1927 const size_t component_size = array->GetClass()->GetComponentSize();
1928 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001929 }
1930
Elliott Hughes75770752011-08-24 17:52:38 -07001931 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001932 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes75770752011-08-24 17:52:38 -07001935 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001936 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001937 }
1938
Elliott Hughes75770752011-08-24 17:52:38 -07001939 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001940 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001944 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes75770752011-08-24 17:52:38 -07001947 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001948 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
Elliott Hughes75770752011-08-24 17:52:38 -07001951 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001952 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Elliott Hughes75770752011-08-24 17:52:38 -07001955 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001956 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 }
1958
Elliott Hughes75770752011-08-24 17:52:38 -07001959 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001960 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 }
1962
Mathieu Chartier590fee92013-09-13 13:46:47 -07001963 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
1964 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001965 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
1966 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 }
1968
Mathieu Chartier590fee92013-09-13 13:46:47 -07001969 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001970 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Mathieu Chartier590fee92013-09-13 13:46:47 -07001973 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001974 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
Mathieu Chartier590fee92013-09-13 13:46:47 -07001977 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
1978 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001979 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
Mathieu Chartier590fee92013-09-13 13:46:47 -07001982 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
1983 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001984 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 }
1986
Mathieu Chartier590fee92013-09-13 13:46:47 -07001987 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001988 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Mathieu Chartier590fee92013-09-13 13:46:47 -07001991 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001992 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 }
1994
Mathieu Chartier590fee92013-09-13 13:46:47 -07001995 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
1996 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001997 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Ian Rogersbc939662013-08-15 10:26:54 -07002000 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2001 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002002 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002003 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Ian Rogersbc939662013-08-15 10:26:54 -07002006 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2007 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002008 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Ian Rogersbc939662013-08-15 10:26:54 -07002011 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2012 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002013 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Ian Rogersbc939662013-08-15 10:26:54 -07002016 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2017 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002018 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002019 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
Ian Rogersbc939662013-08-15 10:26:54 -07002022 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2023 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002024 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002025 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Ian Rogersbc939662013-08-15 10:26:54 -07002028 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2029 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002030 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Ian Rogersbc939662013-08-15 10:26:54 -07002033 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2034 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002035 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Ian Rogersbc939662013-08-15 10:26:54 -07002038 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2039 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002040 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002041 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Ian Rogersbc939662013-08-15 10:26:54 -07002044 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2045 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002046 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002047 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Ian Rogersbc939662013-08-15 10:26:54 -07002050 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2051 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002052 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Ian Rogersbc939662013-08-15 10:26:54 -07002055 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2056 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002057 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
Ian Rogersbc939662013-08-15 10:26:54 -07002060 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2061 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002062 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002063 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Ian Rogersbc939662013-08-15 10:26:54 -07002066 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2067 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002068 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002069 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Ian Rogersbc939662013-08-15 10:26:54 -07002072 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2073 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002074 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Ian Rogersbc939662013-08-15 10:26:54 -07002077 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2078 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002079 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Ian Rogersbc939662013-08-15 10:26:54 -07002082 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2083 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002084 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002085 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Ian Rogersbc939662013-08-15 10:26:54 -07002088 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2089 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002090 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2091 }
2092
Ian Rogersbc939662013-08-15 10:26:54 -07002093 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2094 jint method_count, bool return_errors) {
2095 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002096 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2097 method_count);
2098 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002099 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002100 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002101 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002102 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002103 if (UNLIKELY(method_count == 0)) {
2104 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2105 << PrettyDescriptor(c);
2106 return JNI_OK;
2107 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002108 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002109 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 const char* name = methods[i].name;
2111 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002112 const void* fnPtr = methods[i].fnPtr;
2113 if (UNLIKELY(name == nullptr)) {
2114 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2115 return JNI_ERR;
2116 } else if (UNLIKELY(sig == nullptr)) {
2117 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2118 return JNI_ERR;
2119 } else if (UNLIKELY(fnPtr == nullptr)) {
2120 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2121 return JNI_ERR;
2122 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002123 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002125 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 ++sig;
2127 }
2128
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002129 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2130 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002131 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002133 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002134 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002135 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002136 << PrettyDescriptor(c) << "." << name << sig << " in "
2137 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002138 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002140 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002141 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002142 << PrettyDescriptor(c) << "." << name << sig
2143 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002144 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 return JNI_ERR;
2146 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002147
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002148 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002149
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002150 m->RegisterNative(fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152 return JNI_OK;
2153 }
2154
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002156 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002157 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002158 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002159
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002160 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002161
Ian Rogers2d10b202014-05-12 19:15:18 -07002162 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002163 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002164 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002165 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002166 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002167 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002168 }
2169 }
2170 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002171 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002172 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002173 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002174 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002175 }
2176 }
2177
Ian Rogers2d10b202014-05-12 19:15:18 -07002178 if (unregistered_count == 0) {
2179 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2180 << PrettyDescriptor(c) << "' that contains no native methods";
2181 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002182 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184
Ian Rogers719d1a32014-03-06 12:13:39 -08002185 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002186 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002187 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002188 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2189 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002190 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002191 return JNI_ERR;
2192 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002193 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002194 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Ian Rogers719d1a32014-03-06 12:13:39 -08002197 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002198 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002199 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002200 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002201 o->MonitorExit(soa.Self());
2202 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002203 return JNI_ERR;
2204 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002205 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002206 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
2209 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002210 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002212 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002213 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002215 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002217 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 }
2219
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002221 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002222 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2223 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002224 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002225 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002226 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002227 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2228 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002229 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002230 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002231
Brian Carlstrom85a93362014-06-25 09:30:52 -07002232 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002233 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002234 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2235 "buffer capacity greater than maximum jint: %" PRId64,
2236 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002237 return nullptr;
2238 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002239 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002240 jint capacity_arg = static_cast<jint>(capacity);
2241
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002242 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2243 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002244 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002245 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 }
2247
Elliott Hughesb465ab02011-08-24 11:21:21 -07002248 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002249 return reinterpret_cast<void*>(env->GetLongField(
2250 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 }
2252
Elliott Hughesb465ab02011-08-24 11:21:21 -07002253 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002254 return static_cast<jlong>(env->GetIntField(
2255 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
Elliott Hughesb465ab02011-08-24 11:21:21 -07002258 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002259 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002260
2261 // Do we definitely know what kind of reference this is?
2262 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2263 IndirectRefKind kind = GetIndirectRefKind(ref);
2264 switch (kind) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002265 case kLocal:
2266 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002267 case kGlobal:
2268 return JNIGlobalRefType;
2269 case kWeakGlobal:
2270 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002271 case kHandleScopeOrInvalid:
Ian Rogersc0542af2014-09-03 16:16:56 -07002272 // Assume value is in a handle scope.
2273 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002274 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002275 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2276 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002278
2279 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002280 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2281 const char* caller)
2282 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002283 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002284 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002285 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2286 return JNI_ERR;
2287 }
2288 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002289 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002290 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2291 if (!okay) {
2292 soa.Self()->ThrowOutOfMemoryError(caller);
2293 }
2294 return okay ? JNI_OK : JNI_ERR;
2295 }
2296
2297 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002298 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002299 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002300 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002301 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002302 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002303 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002304 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002305 return soa.AddLocalReference<JniT>(result);
2306 }
2307
Ian Rogers2d10b202014-05-12 19:15:18 -07002308 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2309 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2310 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002311 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002312 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002313 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002314 soa.Vm()->JniAbortF(fn_name,
2315 "attempt to %s %s primitive array elements with an object of type %s",
2316 operation,
2317 PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2318 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002319 return nullptr;
2320 }
2321 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2322 return array;
2323 }
2324
2325 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2326 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2327 CHECK_NON_NULL_ARGUMENT(java_array);
2328 ScopedObjectAccess soa(env);
2329 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2330 "GetArrayElements",
2331 "get");
2332 if (UNLIKELY(array == nullptr)) {
2333 return nullptr;
2334 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002335 // Only make a copy if necessary.
2336 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2337 if (is_copy != nullptr) {
2338 *is_copy = JNI_TRUE;
2339 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002340 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002341 size_t size = array->GetLength() * component_size;
2342 void* data = new uint64_t[RoundUp(size, 8) / 8];
2343 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002344 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002345 } else {
2346 if (is_copy != nullptr) {
2347 *is_copy = JNI_FALSE;
2348 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002349 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002350 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002351 }
2352
Ian Rogers2d10b202014-05-12 19:15:18 -07002353 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002354 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002355 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002356 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002357 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2358 "ReleaseArrayElements",
2359 "release");
2360 if (array == nullptr) {
2361 return;
2362 }
2363 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2364 }
2365
2366 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2367 size_t component_size, void* elements, jint mode)
2368 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002369 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002370 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002371 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002372 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002373 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2374 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002375 if (is_copy) {
2376 // Sanity check: If elements is not the same as the java array's data, it better not be a
2377 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2378 // copies we make?
2379 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002380 soa.Vm()->JniAbortF("ReleaseArrayElements",
2381 "invalid element pointer %p, array elements are %p",
2382 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002383 return;
2384 }
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002385 if (mode != JNI_ABORT) {
2386 memcpy(array_data, elements, bytes);
2387 } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2388 // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2389 LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2390 soa.Self()->DumpJavaStack(LOG(WARNING));
2391 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002392 }
2393 if (mode != JNI_COMMIT) {
2394 if (is_copy) {
2395 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002396 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002397 // Non copy to a movable object must means that we had disabled the moving GC.
2398 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002399 }
2400 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002401 }
2402
Ian Rogers2d10b202014-05-12 19:15:18 -07002403 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2404 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2405 jsize start, jsize length, ElementT* buf) {
2406 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2407 ScopedObjectAccess soa(env);
2408 ArtArrayT* array =
2409 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2410 "GetPrimitiveArrayRegion",
2411 "get region of");
2412 if (array != nullptr) {
2413 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2414 ThrowAIOOBE(soa, array, start, length, "src");
2415 } else {
2416 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2417 ElementT* data = array->GetData();
2418 memcpy(buf, data + start, length * sizeof(ElementT));
2419 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002420 }
2421 }
2422
Ian Rogers2d10b202014-05-12 19:15:18 -07002423 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2424 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2425 jsize start, jsize length, const ElementT* buf) {
2426 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2427 ScopedObjectAccess soa(env);
2428 ArtArrayT* array =
2429 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2430 "SetPrimitiveArrayRegion",
2431 "set region of");
2432 if (array != nullptr) {
2433 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2434 ThrowAIOOBE(soa, array, start, length, "dst");
2435 } else {
2436 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2437 ElementT* data = array->GetData();
2438 memcpy(data + start, buf, length * sizeof(ElementT));
2439 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002440 }
2441 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002442};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002443
Elliott Hughes88c5c352012-03-15 18:49:48 -07002444const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002445 nullptr, // reserved0.
2446 nullptr, // reserved1.
2447 nullptr, // reserved2.
2448 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002449 JNI::GetVersion,
2450 JNI::DefineClass,
2451 JNI::FindClass,
2452 JNI::FromReflectedMethod,
2453 JNI::FromReflectedField,
2454 JNI::ToReflectedMethod,
2455 JNI::GetSuperclass,
2456 JNI::IsAssignableFrom,
2457 JNI::ToReflectedField,
2458 JNI::Throw,
2459 JNI::ThrowNew,
2460 JNI::ExceptionOccurred,
2461 JNI::ExceptionDescribe,
2462 JNI::ExceptionClear,
2463 JNI::FatalError,
2464 JNI::PushLocalFrame,
2465 JNI::PopLocalFrame,
2466 JNI::NewGlobalRef,
2467 JNI::DeleteGlobalRef,
2468 JNI::DeleteLocalRef,
2469 JNI::IsSameObject,
2470 JNI::NewLocalRef,
2471 JNI::EnsureLocalCapacity,
2472 JNI::AllocObject,
2473 JNI::NewObject,
2474 JNI::NewObjectV,
2475 JNI::NewObjectA,
2476 JNI::GetObjectClass,
2477 JNI::IsInstanceOf,
2478 JNI::GetMethodID,
2479 JNI::CallObjectMethod,
2480 JNI::CallObjectMethodV,
2481 JNI::CallObjectMethodA,
2482 JNI::CallBooleanMethod,
2483 JNI::CallBooleanMethodV,
2484 JNI::CallBooleanMethodA,
2485 JNI::CallByteMethod,
2486 JNI::CallByteMethodV,
2487 JNI::CallByteMethodA,
2488 JNI::CallCharMethod,
2489 JNI::CallCharMethodV,
2490 JNI::CallCharMethodA,
2491 JNI::CallShortMethod,
2492 JNI::CallShortMethodV,
2493 JNI::CallShortMethodA,
2494 JNI::CallIntMethod,
2495 JNI::CallIntMethodV,
2496 JNI::CallIntMethodA,
2497 JNI::CallLongMethod,
2498 JNI::CallLongMethodV,
2499 JNI::CallLongMethodA,
2500 JNI::CallFloatMethod,
2501 JNI::CallFloatMethodV,
2502 JNI::CallFloatMethodA,
2503 JNI::CallDoubleMethod,
2504 JNI::CallDoubleMethodV,
2505 JNI::CallDoubleMethodA,
2506 JNI::CallVoidMethod,
2507 JNI::CallVoidMethodV,
2508 JNI::CallVoidMethodA,
2509 JNI::CallNonvirtualObjectMethod,
2510 JNI::CallNonvirtualObjectMethodV,
2511 JNI::CallNonvirtualObjectMethodA,
2512 JNI::CallNonvirtualBooleanMethod,
2513 JNI::CallNonvirtualBooleanMethodV,
2514 JNI::CallNonvirtualBooleanMethodA,
2515 JNI::CallNonvirtualByteMethod,
2516 JNI::CallNonvirtualByteMethodV,
2517 JNI::CallNonvirtualByteMethodA,
2518 JNI::CallNonvirtualCharMethod,
2519 JNI::CallNonvirtualCharMethodV,
2520 JNI::CallNonvirtualCharMethodA,
2521 JNI::CallNonvirtualShortMethod,
2522 JNI::CallNonvirtualShortMethodV,
2523 JNI::CallNonvirtualShortMethodA,
2524 JNI::CallNonvirtualIntMethod,
2525 JNI::CallNonvirtualIntMethodV,
2526 JNI::CallNonvirtualIntMethodA,
2527 JNI::CallNonvirtualLongMethod,
2528 JNI::CallNonvirtualLongMethodV,
2529 JNI::CallNonvirtualLongMethodA,
2530 JNI::CallNonvirtualFloatMethod,
2531 JNI::CallNonvirtualFloatMethodV,
2532 JNI::CallNonvirtualFloatMethodA,
2533 JNI::CallNonvirtualDoubleMethod,
2534 JNI::CallNonvirtualDoubleMethodV,
2535 JNI::CallNonvirtualDoubleMethodA,
2536 JNI::CallNonvirtualVoidMethod,
2537 JNI::CallNonvirtualVoidMethodV,
2538 JNI::CallNonvirtualVoidMethodA,
2539 JNI::GetFieldID,
2540 JNI::GetObjectField,
2541 JNI::GetBooleanField,
2542 JNI::GetByteField,
2543 JNI::GetCharField,
2544 JNI::GetShortField,
2545 JNI::GetIntField,
2546 JNI::GetLongField,
2547 JNI::GetFloatField,
2548 JNI::GetDoubleField,
2549 JNI::SetObjectField,
2550 JNI::SetBooleanField,
2551 JNI::SetByteField,
2552 JNI::SetCharField,
2553 JNI::SetShortField,
2554 JNI::SetIntField,
2555 JNI::SetLongField,
2556 JNI::SetFloatField,
2557 JNI::SetDoubleField,
2558 JNI::GetStaticMethodID,
2559 JNI::CallStaticObjectMethod,
2560 JNI::CallStaticObjectMethodV,
2561 JNI::CallStaticObjectMethodA,
2562 JNI::CallStaticBooleanMethod,
2563 JNI::CallStaticBooleanMethodV,
2564 JNI::CallStaticBooleanMethodA,
2565 JNI::CallStaticByteMethod,
2566 JNI::CallStaticByteMethodV,
2567 JNI::CallStaticByteMethodA,
2568 JNI::CallStaticCharMethod,
2569 JNI::CallStaticCharMethodV,
2570 JNI::CallStaticCharMethodA,
2571 JNI::CallStaticShortMethod,
2572 JNI::CallStaticShortMethodV,
2573 JNI::CallStaticShortMethodA,
2574 JNI::CallStaticIntMethod,
2575 JNI::CallStaticIntMethodV,
2576 JNI::CallStaticIntMethodA,
2577 JNI::CallStaticLongMethod,
2578 JNI::CallStaticLongMethodV,
2579 JNI::CallStaticLongMethodA,
2580 JNI::CallStaticFloatMethod,
2581 JNI::CallStaticFloatMethodV,
2582 JNI::CallStaticFloatMethodA,
2583 JNI::CallStaticDoubleMethod,
2584 JNI::CallStaticDoubleMethodV,
2585 JNI::CallStaticDoubleMethodA,
2586 JNI::CallStaticVoidMethod,
2587 JNI::CallStaticVoidMethodV,
2588 JNI::CallStaticVoidMethodA,
2589 JNI::GetStaticFieldID,
2590 JNI::GetStaticObjectField,
2591 JNI::GetStaticBooleanField,
2592 JNI::GetStaticByteField,
2593 JNI::GetStaticCharField,
2594 JNI::GetStaticShortField,
2595 JNI::GetStaticIntField,
2596 JNI::GetStaticLongField,
2597 JNI::GetStaticFloatField,
2598 JNI::GetStaticDoubleField,
2599 JNI::SetStaticObjectField,
2600 JNI::SetStaticBooleanField,
2601 JNI::SetStaticByteField,
2602 JNI::SetStaticCharField,
2603 JNI::SetStaticShortField,
2604 JNI::SetStaticIntField,
2605 JNI::SetStaticLongField,
2606 JNI::SetStaticFloatField,
2607 JNI::SetStaticDoubleField,
2608 JNI::NewString,
2609 JNI::GetStringLength,
2610 JNI::GetStringChars,
2611 JNI::ReleaseStringChars,
2612 JNI::NewStringUTF,
2613 JNI::GetStringUTFLength,
2614 JNI::GetStringUTFChars,
2615 JNI::ReleaseStringUTFChars,
2616 JNI::GetArrayLength,
2617 JNI::NewObjectArray,
2618 JNI::GetObjectArrayElement,
2619 JNI::SetObjectArrayElement,
2620 JNI::NewBooleanArray,
2621 JNI::NewByteArray,
2622 JNI::NewCharArray,
2623 JNI::NewShortArray,
2624 JNI::NewIntArray,
2625 JNI::NewLongArray,
2626 JNI::NewFloatArray,
2627 JNI::NewDoubleArray,
2628 JNI::GetBooleanArrayElements,
2629 JNI::GetByteArrayElements,
2630 JNI::GetCharArrayElements,
2631 JNI::GetShortArrayElements,
2632 JNI::GetIntArrayElements,
2633 JNI::GetLongArrayElements,
2634 JNI::GetFloatArrayElements,
2635 JNI::GetDoubleArrayElements,
2636 JNI::ReleaseBooleanArrayElements,
2637 JNI::ReleaseByteArrayElements,
2638 JNI::ReleaseCharArrayElements,
2639 JNI::ReleaseShortArrayElements,
2640 JNI::ReleaseIntArrayElements,
2641 JNI::ReleaseLongArrayElements,
2642 JNI::ReleaseFloatArrayElements,
2643 JNI::ReleaseDoubleArrayElements,
2644 JNI::GetBooleanArrayRegion,
2645 JNI::GetByteArrayRegion,
2646 JNI::GetCharArrayRegion,
2647 JNI::GetShortArrayRegion,
2648 JNI::GetIntArrayRegion,
2649 JNI::GetLongArrayRegion,
2650 JNI::GetFloatArrayRegion,
2651 JNI::GetDoubleArrayRegion,
2652 JNI::SetBooleanArrayRegion,
2653 JNI::SetByteArrayRegion,
2654 JNI::SetCharArrayRegion,
2655 JNI::SetShortArrayRegion,
2656 JNI::SetIntArrayRegion,
2657 JNI::SetLongArrayRegion,
2658 JNI::SetFloatArrayRegion,
2659 JNI::SetDoubleArrayRegion,
2660 JNI::RegisterNatives,
2661 JNI::UnregisterNatives,
2662 JNI::MonitorEnter,
2663 JNI::MonitorExit,
2664 JNI::GetJavaVM,
2665 JNI::GetStringRegion,
2666 JNI::GetStringUTFRegion,
2667 JNI::GetPrimitiveArrayCritical,
2668 JNI::ReleasePrimitiveArrayCritical,
2669 JNI::GetStringCritical,
2670 JNI::ReleaseStringCritical,
2671 JNI::NewWeakGlobalRef,
2672 JNI::DeleteWeakGlobalRef,
2673 JNI::ExceptionCheck,
2674 JNI::NewDirectByteBuffer,
2675 JNI::GetDirectBufferAddress,
2676 JNI::GetDirectBufferCapacity,
2677 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002678};
2679
Ian Rogers68d8b422014-07-17 11:09:10 -07002680const JNINativeInterface* GetJniNativeInterface() {
2681 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002682}
2683
Elliott Hughesc8fece32013-01-02 11:27:23 -08002684void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002685 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002686 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002687 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002688 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2689 }
2690 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2691}
2692
Ian Rogersdf20fe02011-07-20 20:34:16 -07002693} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002694
2695std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2696 switch (rhs) {
2697 case JNIInvalidRefType:
2698 os << "JNIInvalidRefType";
2699 return os;
2700 case JNILocalRefType:
2701 os << "JNILocalRefType";
2702 return os;
2703 case JNIGlobalRefType:
2704 os << "JNIGlobalRefType";
2705 return os;
2706 case JNIWeakGlobalRefType:
2707 os << "JNIWeakGlobalRefType";
2708 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002709 default:
Ian Rogersc7dd2952014-10-21 23:31:19 -07002710 LOG(::art::FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
2711 UNREACHABLE();
Elliott Hughesb465ab02011-08-24 11:21:21 -07002712 }
2713}