blob: 213de6f1c7d09344ffa53820488fcdae000953e7 [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());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800199 StackHandleScope<1> hs2(soa.Self());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000200 Handle<mirror::Throwable> cause(hs2.NewHandle(soa.Self()->GetException()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201 soa.Self()->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700202 std::string temp;
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000203 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
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));
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000208 soa.Self()->GetException()->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800209 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700211 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700213 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700214 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700216 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700217 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800218 if (field == nullptr) {
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);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000285 soa.Self()->SetException(soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700286 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700287}
288
Ian Rogers68d8b422014-07-17 11:09:10 -0700289static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
290 return reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughes75770752011-08-24 17:52:38 -0700291}
292
Ian Rogers2d10b202014-05-12 19:15:18 -0700293#define CHECK_NON_NULL_ARGUMENT(value) \
294 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700295
Ian Rogers2d10b202014-05-12 19:15:18 -0700296#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
297 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
298
299#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
300 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
301
302#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
303 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
304
305#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800306 if (UNLIKELY(value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700307 JavaVmExtFromEnv(env)->JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700308 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700309 }
310
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700311#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800312 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700313 JavaVmExtFromEnv(env)->JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700314 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700315 }
316
Elliott Hughescdf53122011-08-19 15:46:09 -0700317class JNI {
318 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700319 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700320 return JNI_VERSION_1_6;
321 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700322
Ian Rogers25e8b912012-09-07 11:31:36 -0700323 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700324 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800325 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700326 }
327
Elliott Hughescdf53122011-08-19 15:46:09 -0700328 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700329 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700330 Runtime* runtime = Runtime::Current();
331 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700332 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700333 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800334 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700335 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700336 StackHandleScope<1> hs(soa.Self());
337 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800338 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700339 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800340 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700341 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700343 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700344
Ian Rogers62f05122014-03-21 11:21:29 -0700345 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700346 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700347 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700348 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700349 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700350
Ian Rogers62f05122014-03-21 11:21:29 -0700351 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700352 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700353 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700354 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700355 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700356
Elliott Hughescdf53122011-08-19 15:46:09 -0700357 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700358 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700359 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800360 mirror::ArtMethod* m = soa.DecodeMethod(mid);
361 CHECK(!kMovingMethods);
Mathieu Chartier41da5962014-11-15 13:07:39 -0800362 ScopedLocalRef<jobject> art_method(env, soa.AddLocalReference<jobject>(m));
Sebastien Hertzd3333762014-06-26 14:45:07 +0200363 jobject reflect_method;
364 if (m->IsConstructor()) {
365 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
366 } else {
367 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
368 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700369 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800370 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700371 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800372 SetObjectField(env, reflect_method,
Mathieu Chartier41da5962014-11-15 13:07:39 -0800373 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method.get());
Brian Carlstromea46f952013-07-30 01:26:50 -0700374 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700375 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700376
Elliott Hughescdf53122011-08-19 15:46:09 -0700377 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700378 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700379 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800380 mirror::ArtField* f = soa.DecodeField(fid);
Mathieu Chartier41da5962014-11-15 13:07:39 -0800381 ScopedLocalRef<jobject> art_field(env, soa.AddLocalReference<jobject>(f));
Brian Carlstromea46f952013-07-30 01:26:50 -0700382 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
383 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800384 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700385 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800386 SetObjectField(env, reflect_field,
Mathieu Chartier41da5962014-11-15 13:07:39 -0800387 WellKnownClasses::java_lang_reflect_Field_artField, art_field.get());
Brian Carlstromea46f952013-07-30 01:26:50 -0700388 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700389 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700390
Elliott Hughes37f7a402011-08-22 18:56:01 -0700391 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700392 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800394 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700395 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700396 }
397
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700398 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700399 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800401 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700402 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700403 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700404
Narayan Kamath1268b742014-07-11 19:15:11 +0100405 // Note: java_class1 should be safely castable to java_class2, and
406 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700407 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700408 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
409 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700410 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800411 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
412 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100413 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700414 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700415
Elliott Hughese84278b2012-03-22 10:06:53 -0700416 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700417 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800418 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700419 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700420 return JNI_TRUE;
421 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700422 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800423 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
424 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700425 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700426 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700427 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700428
Elliott Hughes37f7a402011-08-22 18:56:01 -0700429 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700430 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800431 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
432 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700433 return JNI_ERR;
434 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000435 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700436 return JNI_OK;
437 }
438
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700439 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700440 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800441 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700442 }
443
444 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700445 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700446 }
447
448 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700449 ScopedObjectAccess soa(env);
450 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700451 }
452
453 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700454 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700455
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700456 // If we have no exception to describe, pass through.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000457 if (!soa.Self()->GetException()) {
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700458 return;
459 }
460
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000461 StackHandleScope<1> hs(soa.Self());
462 Handle<mirror::Throwable> old_exception(
463 hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()));
464 soa.Self()->ClearException();
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800465 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700466 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700467 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
468 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800469 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700470 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700471 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700472 } else {
473 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800474 if (soa.Self()->IsExceptionPending()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000475 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700476 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800477 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700478 }
479 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000480 soa.Self()->SetException(old_exception.Get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700481 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700482
Elliott Hughescdf53122011-08-19 15:46:09 -0700483 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700484 ScopedObjectAccess soa(env);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000485 mirror::Object* exception = soa.Self()->GetException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700486 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700487 }
488
Ian Rogers25e8b912012-09-07 11:31:36 -0700489 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700490 LOG(FATAL) << "JNI FatalError called: " << msg;
491 }
492
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700493 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700494 // TODO: SOA may not be necessary but I do it to please lock annotations.
495 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700496 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700497 return JNI_ERR;
498 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700499 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700500 return JNI_OK;
501 }
502
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700503 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700504 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800505 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700506 soa.Env()->PopFrame();
507 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700508 }
509
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700510 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700511 // TODO: SOA may not be necessary but I do it to please lock annotations.
512 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700513 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700514 }
515
Elliott Hughescdf53122011-08-19 15:46:09 -0700516 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700517 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800518 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700519 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700520 }
521
522 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700523 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
524 Thread* self = down_cast<JNIEnvExt*>(env)->self;
525 vm->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700526 }
527
528 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700529 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700530 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
531 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700532 }
533
534 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700535 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
536 Thread* self = down_cast<JNIEnvExt*>(env)->self;
537 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700538 }
539
540 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700541 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800542 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800543 // Check for null after decoding the object to handle cleared weak globals.
544 if (decoded_obj == nullptr) {
545 return nullptr;
546 }
547 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700548 }
549
Stephen Hines95c51b32014-11-26 01:24:13 -0800550 static void DeleteLocalRef(JNIEnv* env, jobject obj)
551 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800552 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700553 return;
554 }
Ian Rogersef28b142012-11-30 14:22:18 -0800555 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700556
Ian Rogersef28b142012-11-30 14:22:18 -0800557 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700558 if (!locals.Remove(cookie, obj)) {
559 // Attempting to delete a local reference that is not in the
560 // topmost local reference frame is a no-op. DeleteLocalRef returns
561 // void and doesn't throw any exceptions, but we should probably
562 // complain about it so the user will notice that things aren't
563 // going quite the way they expect.
564 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
565 << "failed to find entry";
566 }
567 }
568
569 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700570 if (obj1 == obj2) {
571 return JNI_TRUE;
572 } else {
573 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800574 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
575 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700576 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700577 }
578
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700579 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700580 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700581 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800582 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800583 if (c == nullptr) {
584 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700585 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700586 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700587 }
588
Ian Rogersbc939662013-08-15 10:26:54 -0700589 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700590 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700591 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700592 CHECK_NON_NULL_ARGUMENT(java_class);
593 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700594 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700595 va_end(args);
596 return result;
597 }
598
Elliott Hughes72025e52011-08-23 17:50:30 -0700599 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700600 CHECK_NON_NULL_ARGUMENT(java_class);
601 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700602 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800603 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800604 if (c == nullptr) {
605 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700606 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800607 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800608 if (result == nullptr) {
609 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700610 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700612 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800613 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800614 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700615 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800616 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700617 }
618
Elliott Hughes72025e52011-08-23 17:50:30 -0700619 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700620 CHECK_NON_NULL_ARGUMENT(java_class);
621 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800623 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800624 if (c == nullptr) {
625 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700626 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800627 mirror::Object* result = c->AllocObject(soa.Self());
628 if (result == nullptr) {
629 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700630 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700631 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700632 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800633 if (soa.Self()->IsExceptionPending()) {
634 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700635 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800636 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 }
638
Ian Rogersbc939662013-08-15 10:26:54 -0700639 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700640 CHECK_NON_NULL_ARGUMENT(java_class);
641 CHECK_NON_NULL_ARGUMENT(name);
642 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700643 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700644 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 }
646
Ian Rogersbc939662013-08-15 10:26:54 -0700647 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
648 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700649 CHECK_NON_NULL_ARGUMENT(java_class);
650 CHECK_NON_NULL_ARGUMENT(name);
651 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700653 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700654 }
655
Elliott Hughes72025e52011-08-23 17:50:30 -0700656 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700657 va_list ap;
658 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700659 CHECK_NON_NULL_ARGUMENT(obj);
660 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700661 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700663 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700665 }
666
Elliott Hughes72025e52011-08-23 17:50:30 -0700667 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700668 CHECK_NON_NULL_ARGUMENT(obj);
669 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 ScopedObjectAccess soa(env);
671 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
672 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 }
674
Elliott Hughes72025e52011-08-23 17:50:30 -0700675 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700676 CHECK_NON_NULL_ARGUMENT(obj);
677 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700679 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
680 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700681 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 }
683
Elliott Hughes72025e52011-08-23 17:50:30 -0700684 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700685 va_list ap;
686 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700687 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
688 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700689 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700690 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700691 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700692 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 }
694
Elliott Hughes72025e52011-08-23 17:50:30 -0700695 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700696 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
697 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700698 ScopedObjectAccess soa(env);
699 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700700 }
701
Elliott Hughes72025e52011-08-23 17:50:30 -0700702 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700703 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
704 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700705 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700706 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
707 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 }
709
Elliott Hughes72025e52011-08-23 17:50:30 -0700710 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700711 va_list ap;
712 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700713 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
714 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700715 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700717 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700718 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 }
720
Elliott Hughes72025e52011-08-23 17:50:30 -0700721 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700722 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
723 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700724 ScopedObjectAccess soa(env);
725 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 }
727
Elliott Hughes72025e52011-08-23 17:50:30 -0700728 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700729 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
730 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700731 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700732 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
733 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700734 }
735
Elliott Hughes72025e52011-08-23 17:50:30 -0700736 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700737 va_list ap;
738 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700739 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
740 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700741 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700742 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700743 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700744 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 }
746
Elliott Hughes72025e52011-08-23 17:50:30 -0700747 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700748 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
749 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700750 ScopedObjectAccess soa(env);
751 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 }
753
Elliott Hughes72025e52011-08-23 17:50:30 -0700754 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700755 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
756 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700757 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700758 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
759 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700760 }
761
Elliott Hughes72025e52011-08-23 17:50:30 -0700762 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700763 va_list ap;
764 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700765 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
766 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700767 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700768 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700769 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700770 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700771 }
772
Elliott Hughes72025e52011-08-23 17:50:30 -0700773 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700774 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
775 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700776 ScopedObjectAccess soa(env);
777 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 }
779
Elliott Hughes72025e52011-08-23 17:50:30 -0700780 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700781 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
782 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700783 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700784 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
785 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700786 }
787
Elliott Hughes72025e52011-08-23 17:50:30 -0700788 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700789 va_list ap;
790 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700791 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
792 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700793 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700794 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700795 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700796 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 }
798
Elliott Hughes72025e52011-08-23 17:50:30 -0700799 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700800 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
801 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700802 ScopedObjectAccess soa(env);
803 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 }
805
Elliott Hughes72025e52011-08-23 17:50:30 -0700806 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700807 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
808 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700809 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700810 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
811 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 }
813
Elliott Hughes72025e52011-08-23 17:50:30 -0700814 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700815 va_list ap;
816 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700817 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
818 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700819 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700820 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700821 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700822 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 }
824
Elliott Hughes72025e52011-08-23 17:50:30 -0700825 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700826 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
827 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700828 ScopedObjectAccess soa(env);
829 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 }
831
Elliott Hughes72025e52011-08-23 17:50:30 -0700832 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700833 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
834 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700835 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700836 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
837 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700838 }
839
Elliott Hughes72025e52011-08-23 17:50:30 -0700840 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700841 va_list ap;
842 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700843 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
844 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700845 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700847 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700848 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 }
850
Elliott Hughes72025e52011-08-23 17:50:30 -0700851 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700852 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
853 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700854 ScopedObjectAccess soa(env);
855 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 }
857
Elliott Hughes72025e52011-08-23 17:50:30 -0700858 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700859 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
860 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700861 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700862 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
863 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700864 }
865
Elliott Hughes72025e52011-08-23 17:50:30 -0700866 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700867 va_list ap;
868 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700869 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
870 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700871 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700872 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700873 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700874 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 }
876
Elliott Hughes72025e52011-08-23 17:50:30 -0700877 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700878 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
879 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700880 ScopedObjectAccess soa(env);
881 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 }
883
Elliott Hughes72025e52011-08-23 17:50:30 -0700884 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700885 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
886 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700887 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700888 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
889 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 }
891
Elliott Hughes72025e52011-08-23 17:50:30 -0700892 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700893 va_list ap;
894 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700895 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
896 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700897 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -0700898 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 }
901
Elliott Hughes72025e52011-08-23 17:50:30 -0700902 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700903 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
904 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700905 ScopedObjectAccess soa(env);
906 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 }
908
Elliott Hughes72025e52011-08-23 17:50:30 -0700909 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700910 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
911 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700912 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700913 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 }
915
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700916 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700919 CHECK_NON_NULL_ARGUMENT(obj);
920 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700921 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700922 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
923 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 va_end(ap);
925 return local_result;
926 }
927
Ian Rogersbc939662013-08-15 10:26:54 -0700928 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
929 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700930 CHECK_NON_NULL_ARGUMENT(obj);
931 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700932 ScopedObjectAccess soa(env);
933 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
934 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 }
936
Ian Rogersbc939662013-08-15 10:26:54 -0700937 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
938 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700939 CHECK_NON_NULL_ARGUMENT(obj);
940 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700941 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700942 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700943 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 }
945
Ian Rogersbc939662013-08-15 10:26:54 -0700946 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
947 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700949 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700950 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
951 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700952 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700953 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700955 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 }
957
Ian Rogersbc939662013-08-15 10:26:54 -0700958 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
959 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700960 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
961 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700962 ScopedObjectAccess soa(env);
963 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 }
965
Ian Rogersbc939662013-08-15 10:26:54 -0700966 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
967 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700968 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
969 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700971 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700974 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700977 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
978 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700979 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700980 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700982 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 }
984
Ian Rogersbc939662013-08-15 10:26:54 -0700985 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
986 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700987 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
988 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 ScopedObjectAccess soa(env);
990 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Ian Rogersbc939662013-08-15 10:26:54 -0700993 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
994 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700995 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
996 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700997 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700998 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001001 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001004 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1005 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001006 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001007 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001009 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Ian Rogersbc939662013-08-15 10:26:54 -07001012 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1013 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001014 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1015 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001016 ScopedObjectAccess soa(env);
1017 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 }
1019
Ian Rogersbc939662013-08-15 10:26:54 -07001020 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1021 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001022 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1023 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001025 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001028 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001031 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1032 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001033 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001034 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001036 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Ian Rogersbc939662013-08-15 10:26:54 -07001039 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1040 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001041 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1042 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 ScopedObjectAccess soa(env);
1044 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Ian Rogersbc939662013-08-15 10:26:54 -07001047 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1048 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001049 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1050 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001052 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001055 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001058 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1059 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001060 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001061 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001063 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Ian Rogersbc939662013-08-15 10:26:54 -07001066 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1067 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001068 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1069 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001070 ScopedObjectAccess soa(env);
1071 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 }
1073
Ian Rogersbc939662013-08-15 10:26:54 -07001074 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1075 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001076 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1077 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001078 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001079 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001082 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001085 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1086 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001087 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001090 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 }
1092
Ian Rogersbc939662013-08-15 10:26:54 -07001093 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1094 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001095 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1096 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001097 ScopedObjectAccess soa(env);
1098 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Ian Rogersbc939662013-08-15 10:26:54 -07001101 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1102 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001103 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1104 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001105 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001106 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001109 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001112 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1113 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001114 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001115 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001117 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Ian Rogersbc939662013-08-15 10:26:54 -07001120 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1121 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001122 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1123 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001124 ScopedObjectAccess soa(env);
1125 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Ian Rogersbc939662013-08-15 10:26:54 -07001128 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1129 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001130 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1131 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001133 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001136 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001139 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1140 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001141 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001144 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Ian Rogersbc939662013-08-15 10:26:54 -07001147 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1148 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001149 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1150 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151 ScopedObjectAccess soa(env);
1152 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Ian Rogersbc939662013-08-15 10:26:54 -07001155 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1156 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001157 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1158 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001159 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001160 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001163 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001166 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1167 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001168 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001169 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 va_end(ap);
1171 }
1172
Brian Carlstromea46f952013-07-30 01:26:50 -07001173 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1174 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001175 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1176 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177 ScopedObjectAccess soa(env);
1178 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 }
1180
Ian Rogersbc939662013-08-15 10:26:54 -07001181 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1182 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001183 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1184 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001185 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001186 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Ian Rogersbc939662013-08-15 10:26:54 -07001189 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001190 CHECK_NON_NULL_ARGUMENT(java_class);
1191 CHECK_NON_NULL_ARGUMENT(name);
1192 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001193 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001194 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001196
Ian Rogersbc939662013-08-15 10:26:54 -07001197 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1198 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001199 CHECK_NON_NULL_ARGUMENT(java_class);
1200 CHECK_NON_NULL_ARGUMENT(name);
1201 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001202 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001203 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001205
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001206 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001207 CHECK_NON_NULL_ARGUMENT(obj);
1208 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001209 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001210 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1211 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001212 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001214
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001215 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001216 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001218 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001219 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001222 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001223 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1224 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001225 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001226 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1227 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1228 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001229 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 }
1231
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001232 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001233 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001234 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001235 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1236 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001237 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001240#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001241 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1242 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(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*>(instance); \
1245 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001246 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001247
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001248#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001249 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001250 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001251 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001252 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001253
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001254#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001255 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1256 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001257 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001258 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1259 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001260 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001261
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001262#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001263 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001264 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001265 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001266 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001267
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001268 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001269 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001272 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001273 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 }
1275
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001276 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001277 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001280 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001281 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001284 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001285 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
1287
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001288 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001289 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001292 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001293 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001296 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001297 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001300 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001301 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001304 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001305 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001308 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001309 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001312 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001313 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001316 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001317 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001320 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001321 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001322 }
1323
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001324 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001325 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001326 }
1327
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001328 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001329 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001330 }
1331
1332 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001333 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001334 }
1335
1336 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001337 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001338 }
1339
1340 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001341 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001342 }
1343
1344 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001345 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001346 }
1347
1348 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001349 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001350 }
1351
1352 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001353 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001354 }
1355
1356 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001357 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001358 }
1359
1360 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001361 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001362 }
1363
1364 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001365 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001366 }
1367
1368 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001369 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001370 }
1371
1372 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001373 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001374 }
1375
1376 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001377 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001378 }
1379
1380 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001381 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001382 }
1383
1384 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001385 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 }
1387
1388 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001389 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001390 }
1391
1392 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001393 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001396 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001398 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001399 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001400 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001401 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001402 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 va_end(ap);
1404 return local_result;
1405 }
1406
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001407 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001408 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001409 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001410 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001411 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001414 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001415 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001416 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001417 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001418 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001421 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001423 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001424 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001425 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001426 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001428 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001431 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001432 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001433 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001434 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
1436
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001437 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001438 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001440 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001443 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001445 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001446 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001447 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001448 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001450 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 }
1452
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001453 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001454 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001455 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001456 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001459 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001460 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001461 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001462 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 }
1464
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001465 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001467 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001468 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001469 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001470 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001471 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001472 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001475 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001476 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001477 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001478 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001479 }
1480
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001481 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001482 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001483 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001484 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001487 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001489 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001490 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001491 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001492 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001494 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 }
1496
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001497 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001498 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001499 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001500 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001503 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001504 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001505 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001506 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 }
1508
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001509 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001511 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001512 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001513 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001514 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001516 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
1518
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001519 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001520 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001521 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001522 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 }
1524
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001525 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001526 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001527 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001528 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
1530
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001531 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001533 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001534 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001535 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001536 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001538 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001539 }
1540
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001541 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001542 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001543 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001544 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001547 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001548 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001549 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001550 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001553 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001555 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001556 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001557 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001558 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001560 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001563 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001564 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001565 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001566 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001569 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001570 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001571 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001572 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001575 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001577 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001578 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001579 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001580 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001582 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001585 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001586 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001587 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001588 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001591 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001592 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001593 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001594 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001597 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001599 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001600 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001601 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001602 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 va_end(ap);
1604 }
1605
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001606 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001607 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001608 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001609 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001612 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001613 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001614 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001615 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
Elliott Hughes814e4032011-08-23 12:07:56 -07001618 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001619 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001620 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001621 return nullptr;
1622 }
1623 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001624 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001625 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001626 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001627 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001628 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001629 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
1632 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001633 if (utf == nullptr) {
1634 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001636 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001637 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001638 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes814e4032011-08-23 12:07:56 -07001641 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001642 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001643 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001644 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001645 }
1646
1647 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001648 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001649 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001650 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001651 }
1652
Ian Rogersbc939662013-08-15 10:26:54 -07001653 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1654 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001655 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001656 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001657 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001658 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001659 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001660 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001661 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001662 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1663 memcpy(buf, chars + start, length * sizeof(jchar));
1664 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001665 }
1666
Ian Rogersbc939662013-08-15 10:26:54 -07001667 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1668 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001669 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001671 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001672 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001673 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001674 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001675 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001676 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1677 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1678 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001679 }
1680
Elliott Hughes75770752011-08-24 17:52:38 -07001681 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001682 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001683 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001684 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1685 mirror::CharArray* chars = s->GetCharArray();
Fred Shih56890e22014-06-02 11:11:52 -07001686 gc::Heap* heap = Runtime::Current()->GetHeap();
1687 if (heap->IsMovableObject(chars)) {
1688 if (is_copy != nullptr) {
1689 *is_copy = JNI_TRUE;
1690 }
1691 int32_t char_count = s->GetLength();
1692 int32_t offset = s->GetOffset();
1693 jchar* bytes = new jchar[char_count];
1694 for (int32_t i = 0; i < char_count; i++) {
1695 bytes[i] = chars->Get(i + offset);
1696 }
1697 return bytes;
1698 } else {
1699 if (is_copy != nullptr) {
1700 *is_copy = JNI_FALSE;
1701 }
1702 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001703 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001704 }
1705
Mathieu Chartier590fee92013-09-13 13:46:47 -07001706 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001707 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001708 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001709 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1710 mirror::CharArray* s_chars = s->GetCharArray();
1711 if (chars != (s_chars->GetData() + s->GetOffset())) {
1712 delete[] chars;
1713 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 }
1715
Elliott Hughes75770752011-08-24 17:52:38 -07001716 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001717 CHECK_NON_NULL_ARGUMENT(java_string);
1718 ScopedObjectAccess soa(env);
1719 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1720 mirror::CharArray* chars = s->GetCharArray();
1721 int32_t offset = s->GetOffset();
Fred Shih56890e22014-06-02 11:11:52 -07001722 gc::Heap* heap = Runtime::Current()->GetHeap();
1723 if (heap->IsMovableObject(chars)) {
1724 StackHandleScope<1> hs(soa.Self());
1725 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
1726 heap->IncrementDisableMovingGC(soa.Self());
1727 }
1728 if (is_copy != nullptr) {
1729 *is_copy = JNI_FALSE;
1730 }
1731 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes75770752011-08-24 17:52:38 -07001734 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001735 UNUSED(chars);
Fred Shih56890e22014-06-02 11:11:52 -07001736 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1737 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001738 gc::Heap* heap = Runtime::Current()->GetHeap();
1739 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1740 mirror::CharArray* s_chars = s->GetCharArray();
1741 if (heap->IsMovableObject(s_chars)) {
1742 heap->DecrementDisableMovingGC(soa.Self());
1743 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
Elliott Hughes75770752011-08-24 17:52:38 -07001746 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001747 if (java_string == nullptr) {
1748 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001749 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001750 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001751 *is_copy = JNI_TRUE;
1752 }
Ian Rogersef28b142012-11-30 14:22:18 -08001753 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001754 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001755 size_t byte_count = s->GetUtfLength();
1756 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001757 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001758 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1759 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1760 bytes[byte_count] = '\0';
1761 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001762 }
1763
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001764 static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001765 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001766 }
1767
Elliott Hughesbd935992011-08-22 11:59:34 -07001768 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001769 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001770 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001771 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001772 if (UNLIKELY(!obj->IsArrayInstance())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001773 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1774 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001775 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001776 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001777 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 }
1779
Elliott Hughes814e4032011-08-23 12:07:56 -07001780 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001781 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001782 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001783 mirror::ObjectArray<mirror::Object>* array =
1784 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001785 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 }
1787
Ian Rogersbc939662013-08-15 10:26:54 -07001788 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1789 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001790 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001791 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001792 mirror::ObjectArray<mirror::Object>* array =
1793 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1794 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001795 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 }
1797
1798 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001799 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
1802 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001803 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
1806 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001807 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
1810 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001811 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
1814 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001815 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
1818 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001819 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
1822 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001823 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
Ian Rogers1d99e452014-01-02 17:36:41 -08001826 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1827 jobject initial_element) {
1828 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001829 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001830 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001831 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001832 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001833
1834 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001835 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001836 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08001837 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001838 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08001839 if (UNLIKELY(element_class->IsPrimitive())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001840 soa.Vm()->JniAbortF("NewObjectArray", "not an object type: %s",
1841 PrettyDescriptor(element_class).c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08001842 return nullptr;
1843 }
Ian Rogers1d99e452014-01-02 17:36:41 -08001844 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07001845 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08001846 if (UNLIKELY(array_class == nullptr)) {
1847 return nullptr;
1848 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 }
1850
Elliott Hughes75770752011-08-24 17:52:38 -07001851 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001852 mirror::ObjectArray<mirror::Object>* result =
1853 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001854 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001855 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08001856 if (initial_object != nullptr) {
1857 mirror::Class* element_class = result->GetClass()->GetComponentType();
1858 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001859 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
1860 "element type of '%s'",
1861 PrettyDescriptor(initial_object->GetClass()).c_str(),
1862 PrettyDescriptor(element_class).c_str());
1863 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08001864 } else {
1865 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001866 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08001867 }
1868 }
Elliott Hughes75770752011-08-24 17:52:38 -07001869 }
1870 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001871 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 }
1873
1874 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001875 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 }
1877
Ian Rogersa15e67d2012-02-28 13:51:55 -08001878 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001879 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001880 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001881 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07001882 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001883 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
1884 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001885 return nullptr;
1886 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001887 gc::Heap* heap = Runtime::Current()->GetHeap();
1888 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08001889 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001890 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001891 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001892 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001893 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001894 *is_copy = JNI_FALSE;
1895 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001896 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001897 }
1898
Ian Rogers2d10b202014-05-12 19:15:18 -07001899 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
1900 jint mode) {
1901 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
1902 ScopedObjectAccess soa(env);
1903 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
1904 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001905 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
1906 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001907 return;
1908 }
1909 const size_t component_size = array->GetClass()->GetComponentSize();
1910 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001911 }
1912
Elliott Hughes75770752011-08-24 17:52:38 -07001913 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001914 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001915 }
1916
Elliott Hughes75770752011-08-24 17:52:38 -07001917 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001918 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 }
1920
Elliott Hughes75770752011-08-24 17:52:38 -07001921 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001922 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 }
1924
Elliott Hughes75770752011-08-24 17:52:38 -07001925 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001926 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 }
1928
Elliott Hughes75770752011-08-24 17:52:38 -07001929 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001930 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 }
1932
Elliott Hughes75770752011-08-24 17:52:38 -07001933 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001934 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes75770752011-08-24 17:52:38 -07001937 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001938 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Elliott Hughes75770752011-08-24 17:52:38 -07001941 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001942 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
Mathieu Chartier590fee92013-09-13 13:46:47 -07001945 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
1946 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001947 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
1948 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
Mathieu Chartier590fee92013-09-13 13:46:47 -07001951 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001952 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Mathieu Chartier590fee92013-09-13 13:46:47 -07001955 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001956 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 }
1958
Mathieu Chartier590fee92013-09-13 13:46:47 -07001959 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
1960 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001961 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001962 }
1963
Mathieu Chartier590fee92013-09-13 13:46:47 -07001964 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
1965 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001966 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 }
1968
Mathieu Chartier590fee92013-09-13 13:46:47 -07001969 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001970 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Mathieu Chartier590fee92013-09-13 13:46:47 -07001973 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001974 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
Mathieu Chartier590fee92013-09-13 13:46:47 -07001977 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
1978 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001979 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
Ian Rogersbc939662013-08-15 10:26:54 -07001982 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
1983 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001984 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001985 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Ian Rogersbc939662013-08-15 10:26:54 -07001988 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
1989 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001990 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Ian Rogersbc939662013-08-15 10:26:54 -07001993 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
1994 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001995 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Ian Rogersbc939662013-08-15 10:26:54 -07001998 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
1999 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002000 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002001 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Ian Rogersbc939662013-08-15 10:26:54 -07002004 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2005 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002006 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002007 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Ian Rogersbc939662013-08-15 10:26:54 -07002010 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2011 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002012 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Ian Rogersbc939662013-08-15 10:26:54 -07002015 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2016 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002017 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Ian Rogersbc939662013-08-15 10:26:54 -07002020 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2021 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002022 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002023 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Ian Rogersbc939662013-08-15 10:26:54 -07002026 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2027 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002028 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002029 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Ian Rogersbc939662013-08-15 10:26:54 -07002032 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2033 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002034 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Ian Rogersbc939662013-08-15 10:26:54 -07002037 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2038 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002039 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Ian Rogersbc939662013-08-15 10:26:54 -07002042 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2043 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002044 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002045 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Ian Rogersbc939662013-08-15 10:26:54 -07002048 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2049 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002050 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002051 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Ian Rogersbc939662013-08-15 10:26:54 -07002054 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2055 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002056 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Ian Rogersbc939662013-08-15 10:26:54 -07002059 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2060 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002061 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Ian Rogersbc939662013-08-15 10:26:54 -07002064 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2065 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002066 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002067 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Ian Rogersbc939662013-08-15 10:26:54 -07002070 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2071 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002072 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2073 }
2074
Ian Rogersbc939662013-08-15 10:26:54 -07002075 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2076 jint method_count, bool return_errors) {
2077 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002078 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2079 method_count);
2080 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002081 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002082 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002083 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002084 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002085 if (UNLIKELY(method_count == 0)) {
2086 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2087 << PrettyDescriptor(c);
2088 return JNI_OK;
2089 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002090 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002091 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 const char* name = methods[i].name;
2093 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002094 const void* fnPtr = methods[i].fnPtr;
2095 if (UNLIKELY(name == nullptr)) {
2096 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2097 return JNI_ERR;
2098 } else if (UNLIKELY(sig == nullptr)) {
2099 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2100 return JNI_ERR;
2101 } else if (UNLIKELY(fnPtr == nullptr)) {
2102 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2103 return JNI_ERR;
2104 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002105 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002107 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 ++sig;
2109 }
2110
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002111 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2112 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002113 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002115 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002116 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002117 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002118 << PrettyDescriptor(c) << "." << name << sig << " in "
2119 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002120 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002122 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002123 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002124 << PrettyDescriptor(c) << "." << name << sig
2125 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002126 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 return JNI_ERR;
2128 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002129
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002130 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002131
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002132 m->RegisterNative(fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 }
2134 return JNI_OK;
2135 }
2136
Elliott Hughes5174fe62011-08-23 15:12:35 -07002137 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002138 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002139 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002140 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002141
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002142 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143
Ian Rogers2d10b202014-05-12 19:15:18 -07002144 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002145 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002146 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002147 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002148 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002149 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150 }
2151 }
2152 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002153 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002154 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002155 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002156 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002157 }
2158 }
2159
Ian Rogers2d10b202014-05-12 19:15:18 -07002160 if (unregistered_count == 0) {
2161 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2162 << PrettyDescriptor(c) << "' that contains no native methods";
2163 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002164 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Ian Rogers719d1a32014-03-06 12:13:39 -08002167 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002168 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002169 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002170 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2171 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002172 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002173 return JNI_ERR;
2174 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002175 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002176 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178
Ian Rogers719d1a32014-03-06 12:13:39 -08002179 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002180 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002181 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002182 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002183 o->MonitorExit(soa.Self());
2184 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002185 return JNI_ERR;
2186 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002187 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002188 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
2191 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002192 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002194 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002195 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002197 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002199 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002203 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002204 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2205 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002206 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002207 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002208 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002209 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2210 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002211 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002212 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002213
Brian Carlstrom85a93362014-06-25 09:30:52 -07002214 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002215 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002216 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2217 "buffer capacity greater than maximum jint: %" PRId64,
2218 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002219 return nullptr;
2220 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002221 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002222 jint capacity_arg = static_cast<jint>(capacity);
2223
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002224 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2225 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002226 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002227 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
Elliott Hughesb465ab02011-08-24 11:21:21 -07002230 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002231 return reinterpret_cast<void*>(env->GetLongField(
2232 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Elliott Hughesb465ab02011-08-24 11:21:21 -07002235 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002236 return static_cast<jlong>(env->GetIntField(
2237 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239
Andreas Gampea8763072014-12-20 00:08:35 -08002240 static jobjectRefType GetObjectRefType(JNIEnv* env ATTRIBUTE_UNUSED, jobject java_object) {
2241 if (java_object == nullptr) {
2242 return JNIInvalidRefType;
2243 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002244
2245 // Do we definitely know what kind of reference this is?
2246 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2247 IndirectRefKind kind = GetIndirectRefKind(ref);
2248 switch (kind) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002249 case kLocal:
2250 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002251 case kGlobal:
2252 return JNIGlobalRefType;
2253 case kWeakGlobal:
2254 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002255 case kHandleScopeOrInvalid:
Ian Rogersc0542af2014-09-03 16:16:56 -07002256 // Assume value is in a handle scope.
2257 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002258 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002259 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
Andreas Gampea8763072014-12-20 00:08:35 -08002260 UNREACHABLE();
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002262
2263 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002264 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2265 const char* caller)
2266 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002267 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002268 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002269 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2270 return JNI_ERR;
2271 }
2272 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002273 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002274 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2275 if (!okay) {
2276 soa.Self()->ThrowOutOfMemoryError(caller);
2277 }
2278 return okay ? JNI_OK : JNI_ERR;
2279 }
2280
2281 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002282 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002283 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002284 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002285 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002286 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002287 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002288 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002289 return soa.AddLocalReference<JniT>(result);
2290 }
2291
Ian Rogers2d10b202014-05-12 19:15:18 -07002292 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2293 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2294 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002295 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002296 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002297 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002298 soa.Vm()->JniAbortF(fn_name,
2299 "attempt to %s %s primitive array elements with an object of type %s",
2300 operation,
2301 PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2302 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002303 return nullptr;
2304 }
2305 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2306 return array;
2307 }
2308
2309 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2310 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2311 CHECK_NON_NULL_ARGUMENT(java_array);
2312 ScopedObjectAccess soa(env);
2313 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2314 "GetArrayElements",
2315 "get");
2316 if (UNLIKELY(array == nullptr)) {
2317 return nullptr;
2318 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002319 // Only make a copy if necessary.
2320 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2321 if (is_copy != nullptr) {
2322 *is_copy = JNI_TRUE;
2323 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002324 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002325 size_t size = array->GetLength() * component_size;
2326 void* data = new uint64_t[RoundUp(size, 8) / 8];
2327 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002328 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002329 } else {
2330 if (is_copy != nullptr) {
2331 *is_copy = JNI_FALSE;
2332 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002333 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002334 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002335 }
2336
Ian Rogers2d10b202014-05-12 19:15:18 -07002337 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002338 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002339 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002340 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002341 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2342 "ReleaseArrayElements",
2343 "release");
2344 if (array == nullptr) {
2345 return;
2346 }
2347 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2348 }
2349
2350 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2351 size_t component_size, void* elements, jint mode)
2352 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002353 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002354 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002355 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002356 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002357 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2358 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002359 if (is_copy) {
2360 // Sanity check: If elements is not the same as the java array's data, it better not be a
2361 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2362 // copies we make?
2363 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002364 soa.Vm()->JniAbortF("ReleaseArrayElements",
2365 "invalid element pointer %p, array elements are %p",
2366 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002367 return;
2368 }
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002369 if (mode != JNI_ABORT) {
2370 memcpy(array_data, elements, bytes);
2371 } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2372 // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2373 LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2374 soa.Self()->DumpJavaStack(LOG(WARNING));
2375 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002376 }
2377 if (mode != JNI_COMMIT) {
2378 if (is_copy) {
2379 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002380 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002381 // Non copy to a movable object must means that we had disabled the moving GC.
2382 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002383 }
2384 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002385 }
2386
Ian Rogers2d10b202014-05-12 19:15:18 -07002387 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2388 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2389 jsize start, jsize length, ElementT* buf) {
2390 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2391 ScopedObjectAccess soa(env);
2392 ArtArrayT* array =
2393 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2394 "GetPrimitiveArrayRegion",
2395 "get region of");
2396 if (array != nullptr) {
2397 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2398 ThrowAIOOBE(soa, array, start, length, "src");
2399 } else {
2400 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2401 ElementT* data = array->GetData();
2402 memcpy(buf, data + start, length * sizeof(ElementT));
2403 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002404 }
2405 }
2406
Ian Rogers2d10b202014-05-12 19:15:18 -07002407 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2408 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2409 jsize start, jsize length, const ElementT* buf) {
2410 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2411 ScopedObjectAccess soa(env);
2412 ArtArrayT* array =
2413 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2414 "SetPrimitiveArrayRegion",
2415 "set region of");
2416 if (array != nullptr) {
2417 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2418 ThrowAIOOBE(soa, array, start, length, "dst");
2419 } else {
2420 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2421 ElementT* data = array->GetData();
2422 memcpy(data + start, buf, length * sizeof(ElementT));
2423 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002424 }
2425 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002426};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002427
Elliott Hughes88c5c352012-03-15 18:49:48 -07002428const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002429 nullptr, // reserved0.
2430 nullptr, // reserved1.
2431 nullptr, // reserved2.
2432 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002433 JNI::GetVersion,
2434 JNI::DefineClass,
2435 JNI::FindClass,
2436 JNI::FromReflectedMethod,
2437 JNI::FromReflectedField,
2438 JNI::ToReflectedMethod,
2439 JNI::GetSuperclass,
2440 JNI::IsAssignableFrom,
2441 JNI::ToReflectedField,
2442 JNI::Throw,
2443 JNI::ThrowNew,
2444 JNI::ExceptionOccurred,
2445 JNI::ExceptionDescribe,
2446 JNI::ExceptionClear,
2447 JNI::FatalError,
2448 JNI::PushLocalFrame,
2449 JNI::PopLocalFrame,
2450 JNI::NewGlobalRef,
2451 JNI::DeleteGlobalRef,
2452 JNI::DeleteLocalRef,
2453 JNI::IsSameObject,
2454 JNI::NewLocalRef,
2455 JNI::EnsureLocalCapacity,
2456 JNI::AllocObject,
2457 JNI::NewObject,
2458 JNI::NewObjectV,
2459 JNI::NewObjectA,
2460 JNI::GetObjectClass,
2461 JNI::IsInstanceOf,
2462 JNI::GetMethodID,
2463 JNI::CallObjectMethod,
2464 JNI::CallObjectMethodV,
2465 JNI::CallObjectMethodA,
2466 JNI::CallBooleanMethod,
2467 JNI::CallBooleanMethodV,
2468 JNI::CallBooleanMethodA,
2469 JNI::CallByteMethod,
2470 JNI::CallByteMethodV,
2471 JNI::CallByteMethodA,
2472 JNI::CallCharMethod,
2473 JNI::CallCharMethodV,
2474 JNI::CallCharMethodA,
2475 JNI::CallShortMethod,
2476 JNI::CallShortMethodV,
2477 JNI::CallShortMethodA,
2478 JNI::CallIntMethod,
2479 JNI::CallIntMethodV,
2480 JNI::CallIntMethodA,
2481 JNI::CallLongMethod,
2482 JNI::CallLongMethodV,
2483 JNI::CallLongMethodA,
2484 JNI::CallFloatMethod,
2485 JNI::CallFloatMethodV,
2486 JNI::CallFloatMethodA,
2487 JNI::CallDoubleMethod,
2488 JNI::CallDoubleMethodV,
2489 JNI::CallDoubleMethodA,
2490 JNI::CallVoidMethod,
2491 JNI::CallVoidMethodV,
2492 JNI::CallVoidMethodA,
2493 JNI::CallNonvirtualObjectMethod,
2494 JNI::CallNonvirtualObjectMethodV,
2495 JNI::CallNonvirtualObjectMethodA,
2496 JNI::CallNonvirtualBooleanMethod,
2497 JNI::CallNonvirtualBooleanMethodV,
2498 JNI::CallNonvirtualBooleanMethodA,
2499 JNI::CallNonvirtualByteMethod,
2500 JNI::CallNonvirtualByteMethodV,
2501 JNI::CallNonvirtualByteMethodA,
2502 JNI::CallNonvirtualCharMethod,
2503 JNI::CallNonvirtualCharMethodV,
2504 JNI::CallNonvirtualCharMethodA,
2505 JNI::CallNonvirtualShortMethod,
2506 JNI::CallNonvirtualShortMethodV,
2507 JNI::CallNonvirtualShortMethodA,
2508 JNI::CallNonvirtualIntMethod,
2509 JNI::CallNonvirtualIntMethodV,
2510 JNI::CallNonvirtualIntMethodA,
2511 JNI::CallNonvirtualLongMethod,
2512 JNI::CallNonvirtualLongMethodV,
2513 JNI::CallNonvirtualLongMethodA,
2514 JNI::CallNonvirtualFloatMethod,
2515 JNI::CallNonvirtualFloatMethodV,
2516 JNI::CallNonvirtualFloatMethodA,
2517 JNI::CallNonvirtualDoubleMethod,
2518 JNI::CallNonvirtualDoubleMethodV,
2519 JNI::CallNonvirtualDoubleMethodA,
2520 JNI::CallNonvirtualVoidMethod,
2521 JNI::CallNonvirtualVoidMethodV,
2522 JNI::CallNonvirtualVoidMethodA,
2523 JNI::GetFieldID,
2524 JNI::GetObjectField,
2525 JNI::GetBooleanField,
2526 JNI::GetByteField,
2527 JNI::GetCharField,
2528 JNI::GetShortField,
2529 JNI::GetIntField,
2530 JNI::GetLongField,
2531 JNI::GetFloatField,
2532 JNI::GetDoubleField,
2533 JNI::SetObjectField,
2534 JNI::SetBooleanField,
2535 JNI::SetByteField,
2536 JNI::SetCharField,
2537 JNI::SetShortField,
2538 JNI::SetIntField,
2539 JNI::SetLongField,
2540 JNI::SetFloatField,
2541 JNI::SetDoubleField,
2542 JNI::GetStaticMethodID,
2543 JNI::CallStaticObjectMethod,
2544 JNI::CallStaticObjectMethodV,
2545 JNI::CallStaticObjectMethodA,
2546 JNI::CallStaticBooleanMethod,
2547 JNI::CallStaticBooleanMethodV,
2548 JNI::CallStaticBooleanMethodA,
2549 JNI::CallStaticByteMethod,
2550 JNI::CallStaticByteMethodV,
2551 JNI::CallStaticByteMethodA,
2552 JNI::CallStaticCharMethod,
2553 JNI::CallStaticCharMethodV,
2554 JNI::CallStaticCharMethodA,
2555 JNI::CallStaticShortMethod,
2556 JNI::CallStaticShortMethodV,
2557 JNI::CallStaticShortMethodA,
2558 JNI::CallStaticIntMethod,
2559 JNI::CallStaticIntMethodV,
2560 JNI::CallStaticIntMethodA,
2561 JNI::CallStaticLongMethod,
2562 JNI::CallStaticLongMethodV,
2563 JNI::CallStaticLongMethodA,
2564 JNI::CallStaticFloatMethod,
2565 JNI::CallStaticFloatMethodV,
2566 JNI::CallStaticFloatMethodA,
2567 JNI::CallStaticDoubleMethod,
2568 JNI::CallStaticDoubleMethodV,
2569 JNI::CallStaticDoubleMethodA,
2570 JNI::CallStaticVoidMethod,
2571 JNI::CallStaticVoidMethodV,
2572 JNI::CallStaticVoidMethodA,
2573 JNI::GetStaticFieldID,
2574 JNI::GetStaticObjectField,
2575 JNI::GetStaticBooleanField,
2576 JNI::GetStaticByteField,
2577 JNI::GetStaticCharField,
2578 JNI::GetStaticShortField,
2579 JNI::GetStaticIntField,
2580 JNI::GetStaticLongField,
2581 JNI::GetStaticFloatField,
2582 JNI::GetStaticDoubleField,
2583 JNI::SetStaticObjectField,
2584 JNI::SetStaticBooleanField,
2585 JNI::SetStaticByteField,
2586 JNI::SetStaticCharField,
2587 JNI::SetStaticShortField,
2588 JNI::SetStaticIntField,
2589 JNI::SetStaticLongField,
2590 JNI::SetStaticFloatField,
2591 JNI::SetStaticDoubleField,
2592 JNI::NewString,
2593 JNI::GetStringLength,
2594 JNI::GetStringChars,
2595 JNI::ReleaseStringChars,
2596 JNI::NewStringUTF,
2597 JNI::GetStringUTFLength,
2598 JNI::GetStringUTFChars,
2599 JNI::ReleaseStringUTFChars,
2600 JNI::GetArrayLength,
2601 JNI::NewObjectArray,
2602 JNI::GetObjectArrayElement,
2603 JNI::SetObjectArrayElement,
2604 JNI::NewBooleanArray,
2605 JNI::NewByteArray,
2606 JNI::NewCharArray,
2607 JNI::NewShortArray,
2608 JNI::NewIntArray,
2609 JNI::NewLongArray,
2610 JNI::NewFloatArray,
2611 JNI::NewDoubleArray,
2612 JNI::GetBooleanArrayElements,
2613 JNI::GetByteArrayElements,
2614 JNI::GetCharArrayElements,
2615 JNI::GetShortArrayElements,
2616 JNI::GetIntArrayElements,
2617 JNI::GetLongArrayElements,
2618 JNI::GetFloatArrayElements,
2619 JNI::GetDoubleArrayElements,
2620 JNI::ReleaseBooleanArrayElements,
2621 JNI::ReleaseByteArrayElements,
2622 JNI::ReleaseCharArrayElements,
2623 JNI::ReleaseShortArrayElements,
2624 JNI::ReleaseIntArrayElements,
2625 JNI::ReleaseLongArrayElements,
2626 JNI::ReleaseFloatArrayElements,
2627 JNI::ReleaseDoubleArrayElements,
2628 JNI::GetBooleanArrayRegion,
2629 JNI::GetByteArrayRegion,
2630 JNI::GetCharArrayRegion,
2631 JNI::GetShortArrayRegion,
2632 JNI::GetIntArrayRegion,
2633 JNI::GetLongArrayRegion,
2634 JNI::GetFloatArrayRegion,
2635 JNI::GetDoubleArrayRegion,
2636 JNI::SetBooleanArrayRegion,
2637 JNI::SetByteArrayRegion,
2638 JNI::SetCharArrayRegion,
2639 JNI::SetShortArrayRegion,
2640 JNI::SetIntArrayRegion,
2641 JNI::SetLongArrayRegion,
2642 JNI::SetFloatArrayRegion,
2643 JNI::SetDoubleArrayRegion,
2644 JNI::RegisterNatives,
2645 JNI::UnregisterNatives,
2646 JNI::MonitorEnter,
2647 JNI::MonitorExit,
2648 JNI::GetJavaVM,
2649 JNI::GetStringRegion,
2650 JNI::GetStringUTFRegion,
2651 JNI::GetPrimitiveArrayCritical,
2652 JNI::ReleasePrimitiveArrayCritical,
2653 JNI::GetStringCritical,
2654 JNI::ReleaseStringCritical,
2655 JNI::NewWeakGlobalRef,
2656 JNI::DeleteWeakGlobalRef,
2657 JNI::ExceptionCheck,
2658 JNI::NewDirectByteBuffer,
2659 JNI::GetDirectBufferAddress,
2660 JNI::GetDirectBufferCapacity,
2661 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002662};
2663
Ian Rogers68d8b422014-07-17 11:09:10 -07002664const JNINativeInterface* GetJniNativeInterface() {
2665 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002666}
2667
Elliott Hughesc8fece32013-01-02 11:27:23 -08002668void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002669 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002670 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002671 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002672 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2673 }
2674 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2675}
2676
Ian Rogersdf20fe02011-07-20 20:34:16 -07002677} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002678
2679std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2680 switch (rhs) {
2681 case JNIInvalidRefType:
2682 os << "JNIInvalidRefType";
2683 return os;
2684 case JNILocalRefType:
2685 os << "JNILocalRefType";
2686 return os;
2687 case JNIGlobalRefType:
2688 os << "JNIGlobalRefType";
2689 return os;
2690 case JNIWeakGlobalRefType:
2691 os << "JNIWeakGlobalRefType";
2692 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002693 default:
Ian Rogersc7dd2952014-10-21 23:31:19 -07002694 LOG(::art::FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
2695 UNREACHABLE();
Elliott Hughesb465ab02011-08-24 11:21:21 -07002696 }
2697}