blob: 67e52cbef9330b6c9da78f8237e253fc739ea301 [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"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080048#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070049#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070050#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070051#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070053#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070054#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070056#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070057
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070058namespace art {
59
Mathieu Chartier24555ad2014-10-06 13:41:33 -070060// Consider turning this on when there is errors which could be related to JNI array copies such as
61// things not rendering correctly. E.g. b/16858794
62static constexpr bool kWarnJniAbort = false;
63
Elliott Hughes6b436852011-08-12 10:16:44 -070064// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
65// separated with slashes but aren't wrapped with "L;" like regular descriptors
66// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
67// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
68// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070069static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070070 std::string result;
71 // Add the missing "L;" if necessary.
72 if (name[0] == '[') {
73 result = name;
74 } else {
75 result += 'L';
76 result += name;
77 result += ';';
78 }
79 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070080 if (result.find('.') != std::string::npos) {
81 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
82 << "\"" << name << "\"";
83 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -070084 }
85 return result;
86}
87
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080088static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070089 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -070090 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080091 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Ian Rogers1ff3c982014-08-12 02:30:58 -070092 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -080093 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
94 "no %s method \"%s.%s%s\"",
Ian Rogers1ff3c982014-08-12 02:30:58 -070095 kind, c->GetDescriptor(&temp), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -070096}
97
Sebastien Hertzfa65e842014-07-03 09:39:53 +020098static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa, mirror::Class* c,
99 const char* kind, jint idx, bool return_errors)
100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
101 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method in "
102 << PrettyDescriptor(c) << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
103 << ": " << kind << " is null at index " << idx;
104 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
105 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
106 "%s is null at index %d", kind, idx);
107}
108
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800109static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
111 if (LIKELY(klass->IsInitialized())) {
112 return klass;
113 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700114 StackHandleScope<1> hs(self);
115 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700116 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800117 return nullptr;
118 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800120}
121
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
123 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800125 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800126 if (c == nullptr) {
127 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700128 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800129 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700130 if (is_static) {
131 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700132 } else if (c->IsInterface()) {
133 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700134 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700135 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800136 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700137 // No virtual method matching the signature. Search declared
138 // private methods and constructors.
139 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700140 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700141 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800142 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800144 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700145 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700147}
148
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800149static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800151 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700152 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
153 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700154 return soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700155 }
Brian Carlstromce888532013-10-10 00:32:58 -0700156 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800157 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700158 return method->GetDeclaringClass()->GetClassLoader();
159 }
160 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800161 mirror::ClassLoader* class_loader =
162 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
163 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700164 return class_loader;
165 }
166 // See if the override ClassLoader is set for gtests.
Ian Rogers68d8b422014-07-17 11:09:10 -0700167 class_loader = soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800168 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800169 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700170 CHECK(Runtime::Current()->UseCompileTimeClassPath());
171 return class_loader;
172 }
173 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800174 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700175}
176
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700177static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
178 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700180 StackHandleScope<2> hs(soa.Self());
181 Handle<mirror::Class> c(
182 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
183 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800184 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700185 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800186 mirror::ArtField* field = nullptr;
187 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700188 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
189 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700190 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800191 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700192 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700193 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700194 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800195 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700196 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700197 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 ThrowLocation throw_location;
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800199 StackHandleScope<1> hs2(soa.Self());
200 Handle<mirror::Throwable> cause(hs2.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201 soa.Self()->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700202 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800203 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800204 "no type \"%s\" found and so no field \"%s\" "
205 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700206 c->GetDescriptor(&temp));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700207 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800208 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700209 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700210 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700212 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700213 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700214 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700215 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800217 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800218 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
219 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
220 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700221 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800222 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700223 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700224 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700225}
226
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800227static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700230 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800231 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
232 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
233 "%s offset=%d length=%d %s.length=%d",
234 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700235}
Ian Rogers0571d352011-11-03 19:51:38 -0700236
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
238 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700239 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800240 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
241 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
242 "offset=%d length=%d string.length()=%d", start, length,
243 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700244}
Elliott Hughes814e4032011-08-23 12:07:56 -0700245
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700247 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248 // Turn the const char* into a java.lang.String.
249 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800250 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700252 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700253
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254 // Choose an appropriate constructor and set up the arguments.
255 jvalue args[2];
256 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800257 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800259 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700260 signature = "(Ljava/lang/String;)V";
261 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800262 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700263 signature = "(Ljava/lang/Throwable;)V";
264 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700265 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
267 args[0].l = s.get();
268 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700269 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800271 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800272 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800274 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 return JNI_ERR;
276 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700277
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800278 ScopedLocalRef<jthrowable> exception(
279 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
280 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 return JNI_ERR;
282 }
Ian Rogersef28b142012-11-30 14:22:18 -0800283 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800284 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800285 soa.Self()->SetException(throw_location, 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);
Brian Carlstromea46f952013-07-30 01:26:50 -0700362 jobject art_method = 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,
373 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
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);
Brian Carlstromea46f952013-07-30 01:26:50 -0700381 jobject art_field = soa.AddLocalReference<jobject>(f);
382 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,
387 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
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 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800435 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
436 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700437 return JNI_OK;
438 }
439
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700440 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700441 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800442 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700443 }
444
445 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700446 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700447 }
448
449 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700450 ScopedObjectAccess soa(env);
451 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700452 }
453
454 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700455 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700456
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700457 // If we have no exception to describe, pass through.
458 if (!soa.Self()->GetException(nullptr)) {
459 return;
460 }
461
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700462 StackHandleScope<3> hs(soa.Self());
463 // TODO: Use nullptr instead of null handles?
464 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
465 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
466 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800467 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200468 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800469 {
470 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800471 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700472 old_throw_this_object.Assign(old_throw_location.GetThis());
473 old_throw_method.Assign(old_throw_location.GetMethod());
474 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800475 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200476 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800477 soa.Self()->ClearException();
478 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800479 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700480 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700481 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
482 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800483 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700484 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700485 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700486 } else {
487 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800488 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800489 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700490 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800491 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700492 }
493 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700494 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800495 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700496
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700497 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200498 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700499 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700500
Elliott Hughescdf53122011-08-19 15:46:09 -0700501 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700502 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800503 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700504 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700505 }
506
Ian Rogers25e8b912012-09-07 11:31:36 -0700507 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700508 LOG(FATAL) << "JNI FatalError called: " << msg;
509 }
510
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700511 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700512 // TODO: SOA may not be necessary but I do it to please lock annotations.
513 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700514 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700515 return JNI_ERR;
516 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700517 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700518 return JNI_OK;
519 }
520
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700521 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700522 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800523 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700524 soa.Env()->PopFrame();
525 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700526 }
527
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700528 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700529 // TODO: SOA may not be necessary but I do it to please lock annotations.
530 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700531 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700532 }
533
Elliott Hughescdf53122011-08-19 15:46:09 -0700534 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700535 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800536 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700537 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700538 }
539
540 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700541 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
542 Thread* self = down_cast<JNIEnvExt*>(env)->self;
543 vm->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700544 }
545
546 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700547 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700548 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
549 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700550 }
551
552 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700553 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
554 Thread* self = down_cast<JNIEnvExt*>(env)->self;
555 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700556 }
557
558 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700559 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800560 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800561 // Check for null after decoding the object to handle cleared weak globals.
562 if (decoded_obj == nullptr) {
563 return nullptr;
564 }
565 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700566 }
567
568 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800569 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700570 return;
571 }
Ian Rogersef28b142012-11-30 14:22:18 -0800572 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700573
Ian Rogersef28b142012-11-30 14:22:18 -0800574 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700575 if (!locals.Remove(cookie, obj)) {
576 // Attempting to delete a local reference that is not in the
577 // topmost local reference frame is a no-op. DeleteLocalRef returns
578 // void and doesn't throw any exceptions, but we should probably
579 // complain about it so the user will notice that things aren't
580 // going quite the way they expect.
581 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
582 << "failed to find entry";
583 }
584 }
585
586 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700587 if (obj1 == obj2) {
588 return JNI_TRUE;
589 } else {
590 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800591 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
592 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700593 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700594 }
595
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700596 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700597 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700598 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800599 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800600 if (c == nullptr) {
601 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700602 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700603 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700604 }
605
Ian Rogersbc939662013-08-15 10:26:54 -0700606 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700607 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700608 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700609 CHECK_NON_NULL_ARGUMENT(java_class);
610 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700611 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700612 va_end(args);
613 return result;
614 }
615
Elliott Hughes72025e52011-08-23 17:50:30 -0700616 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700617 CHECK_NON_NULL_ARGUMENT(java_class);
618 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800620 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800621 if (c == nullptr) {
622 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700623 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800624 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800625 if (result == nullptr) {
626 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700627 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700629 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800630 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800631 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700632 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800633 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700634 }
635
Elliott Hughes72025e52011-08-23 17:50:30 -0700636 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700637 CHECK_NON_NULL_ARGUMENT(java_class);
638 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700639 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800640 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800641 if (c == nullptr) {
642 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700643 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800644 mirror::Object* result = c->AllocObject(soa.Self());
645 if (result == nullptr) {
646 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700647 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700649 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800650 if (soa.Self()->IsExceptionPending()) {
651 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700652 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800653 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700654 }
655
Ian Rogersbc939662013-08-15 10:26:54 -0700656 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700657 CHECK_NON_NULL_ARGUMENT(java_class);
658 CHECK_NON_NULL_ARGUMENT(name);
659 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700660 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700661 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700662 }
663
Ian Rogersbc939662013-08-15 10:26:54 -0700664 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
665 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700666 CHECK_NON_NULL_ARGUMENT(java_class);
667 CHECK_NON_NULL_ARGUMENT(name);
668 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700670 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 }
672
Elliott Hughes72025e52011-08-23 17:50:30 -0700673 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700674 va_list ap;
675 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700676 CHECK_NON_NULL_ARGUMENT(obj);
677 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700678 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700680 va_end(ap);
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 jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700685 CHECK_NON_NULL_ARGUMENT(obj);
686 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700687 ScopedObjectAccess soa(env);
688 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
689 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 }
691
Elliott Hughes72025e52011-08-23 17:50:30 -0700692 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700693 CHECK_NON_NULL_ARGUMENT(obj);
694 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700695 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700696 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
697 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700698 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 }
700
Elliott Hughes72025e52011-08-23 17:50:30 -0700701 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700702 va_list ap;
703 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700704 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
705 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700706 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700707 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700708 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700709 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700710 }
711
Elliott Hughes72025e52011-08-23 17:50:30 -0700712 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700713 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
714 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700715 ScopedObjectAccess soa(env);
716 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700717 }
718
Elliott Hughes72025e52011-08-23 17:50:30 -0700719 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700720 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
721 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700722 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700723 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
724 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700725 }
726
Elliott Hughes72025e52011-08-23 17:50:30 -0700727 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700728 va_list ap;
729 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700730 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
731 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700732 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700733 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700734 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700735 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 }
737
Elliott Hughes72025e52011-08-23 17:50:30 -0700738 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700739 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
740 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 ScopedObjectAccess soa(env);
742 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 }
744
Elliott Hughes72025e52011-08-23 17:50:30 -0700745 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700746 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
747 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700748 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700749 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
750 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 }
752
Elliott Hughes72025e52011-08-23 17:50:30 -0700753 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700754 va_list ap;
755 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700756 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
757 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700758 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700759 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700760 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700761 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700762 }
763
Elliott Hughes72025e52011-08-23 17:50:30 -0700764 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700765 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
766 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700767 ScopedObjectAccess soa(env);
768 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 }
770
Elliott Hughes72025e52011-08-23 17:50:30 -0700771 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700772 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
773 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700774 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700775 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
776 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 }
778
Elliott Hughes72025e52011-08-23 17:50:30 -0700779 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700780 va_list ap;
781 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700782 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
783 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700784 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700785 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700786 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700787 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700788 }
789
Elliott Hughes72025e52011-08-23 17:50:30 -0700790 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700791 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
792 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700793 ScopedObjectAccess soa(env);
794 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700795 }
796
Elliott Hughes72025e52011-08-23 17:50:30 -0700797 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700798 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
799 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700800 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700801 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
802 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 }
804
Elliott Hughes72025e52011-08-23 17:50:30 -0700805 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700806 va_list ap;
807 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700808 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
809 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700810 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700811 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700812 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700813 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 }
815
Elliott Hughes72025e52011-08-23 17:50:30 -0700816 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700817 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
818 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700819 ScopedObjectAccess soa(env);
820 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 }
822
Elliott Hughes72025e52011-08-23 17:50:30 -0700823 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700824 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
825 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700826 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700827 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
828 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 }
830
Elliott Hughes72025e52011-08-23 17:50:30 -0700831 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700832 va_list ap;
833 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700834 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
835 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700836 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700837 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700838 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700839 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 }
841
Elliott Hughes72025e52011-08-23 17:50:30 -0700842 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700843 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
844 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 ScopedObjectAccess soa(env);
846 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 }
848
Elliott Hughes72025e52011-08-23 17:50:30 -0700849 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700850 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
851 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700852 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700853 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
854 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 }
856
Elliott Hughes72025e52011-08-23 17:50:30 -0700857 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700858 va_list ap;
859 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700860 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
861 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700862 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700864 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700865 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 }
867
Elliott Hughes72025e52011-08-23 17:50:30 -0700868 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700869 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
870 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700871 ScopedObjectAccess soa(env);
872 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 }
874
Elliott Hughes72025e52011-08-23 17:50:30 -0700875 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700876 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
877 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700878 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700879 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
880 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700881 }
882
Elliott Hughes72025e52011-08-23 17:50:30 -0700883 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700884 va_list ap;
885 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700886 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
887 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700888 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700889 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700890 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700891 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 }
893
Elliott Hughes72025e52011-08-23 17:50:30 -0700894 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700895 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
896 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700897 ScopedObjectAccess soa(env);
898 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 }
900
Elliott Hughes72025e52011-08-23 17:50:30 -0700901 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700902 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
903 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700904 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700905 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
906 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 }
908
Elliott Hughes72025e52011-08-23 17:50:30 -0700909 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700910 va_list ap;
911 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700912 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
913 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700914 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -0700915 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700916 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 }
918
Elliott Hughes72025e52011-08-23 17:50:30 -0700919 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700920 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
921 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700922 ScopedObjectAccess soa(env);
923 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 }
925
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700927 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
928 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700929 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700930 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 }
932
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700933 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700935 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700936 CHECK_NON_NULL_ARGUMENT(obj);
937 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700938 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700939 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
940 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 va_end(ap);
942 return local_result;
943 }
944
Ian Rogersbc939662013-08-15 10:26:54 -0700945 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
946 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700947 CHECK_NON_NULL_ARGUMENT(obj);
948 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700949 ScopedObjectAccess soa(env);
950 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
951 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 }
953
Ian Rogersbc939662013-08-15 10:26:54 -0700954 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
955 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700956 CHECK_NON_NULL_ARGUMENT(obj);
957 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700958 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700959 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 }
962
Ian Rogersbc939662013-08-15 10:26:54 -0700963 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
964 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700967 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
968 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700969 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700972 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Ian Rogersbc939662013-08-15 10:26:54 -0700975 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
976 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700977 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
978 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700979 ScopedObjectAccess soa(env);
980 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Ian Rogersbc939662013-08-15 10:26:54 -0700983 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
984 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700985 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
986 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700987 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700988 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 }
990
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700991 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700994 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
995 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700996 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700997 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700999 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Ian Rogersbc939662013-08-15 10:26:54 -07001002 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1003 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001004 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1005 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001006 ScopedObjectAccess soa(env);
1007 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Ian Rogersbc939662013-08-15 10:26:54 -07001010 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1011 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001012 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1013 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001014 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001015 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001018 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001021 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1022 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001023 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001026 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Ian Rogersbc939662013-08-15 10:26:54 -07001029 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1030 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001031 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1032 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001033 ScopedObjectAccess soa(env);
1034 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Ian Rogersbc939662013-08-15 10:26:54 -07001037 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1038 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001039 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1040 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001041 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001042 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001045 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001048 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1049 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001050 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001053 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Ian Rogersbc939662013-08-15 10:26:54 -07001056 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1057 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001058 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1059 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001060 ScopedObjectAccess soa(env);
1061 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Ian Rogersbc939662013-08-15 10:26:54 -07001064 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1065 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001066 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1067 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001069 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001072 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001075 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1076 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001077 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001078 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001080 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Ian Rogersbc939662013-08-15 10:26:54 -07001083 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1084 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001085 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1086 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001087 ScopedObjectAccess soa(env);
1088 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Ian Rogersbc939662013-08-15 10:26:54 -07001091 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1092 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001093 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1094 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001095 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001096 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001099 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001102 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1103 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001104 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001105 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001107 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Ian Rogersbc939662013-08-15 10:26:54 -07001110 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1111 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001112 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1113 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001114 ScopedObjectAccess soa(env);
1115 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Ian Rogersbc939662013-08-15 10:26:54 -07001118 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1119 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001120 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1121 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001122 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001123 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001126 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001129 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1130 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001131 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001134 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Ian Rogersbc939662013-08-15 10:26:54 -07001137 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1138 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001139 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1140 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001141 ScopedObjectAccess soa(env);
1142 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
Ian Rogersbc939662013-08-15 10:26:54 -07001145 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1146 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001147 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1148 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001149 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001150 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001153 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001156 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1157 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001158 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001159 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001161 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Ian Rogersbc939662013-08-15 10:26:54 -07001164 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1165 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001166 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1167 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001168 ScopedObjectAccess soa(env);
1169 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 }
1171
Ian Rogersbc939662013-08-15 10:26:54 -07001172 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1173 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001174 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1175 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001176 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001177 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001180 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001183 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1184 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001185 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 va_end(ap);
1188 }
1189
Brian Carlstromea46f952013-07-30 01:26:50 -07001190 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1191 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001192 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1193 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001194 ScopedObjectAccess soa(env);
1195 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Ian Rogersbc939662013-08-15 10:26:54 -07001198 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1199 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001200 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1201 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001202 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001203 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
1205
Ian Rogersbc939662013-08-15 10:26:54 -07001206 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001207 CHECK_NON_NULL_ARGUMENT(java_class);
1208 CHECK_NON_NULL_ARGUMENT(name);
1209 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001210 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001211 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001213
Ian Rogersbc939662013-08-15 10:26:54 -07001214 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1215 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001216 CHECK_NON_NULL_ARGUMENT(java_class);
1217 CHECK_NON_NULL_ARGUMENT(name);
1218 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001219 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001220 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001222
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001223 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001224 CHECK_NON_NULL_ARGUMENT(obj);
1225 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001226 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001227 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1228 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001229 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001231
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001232 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001233 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001234 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001235 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001236 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001239 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001240 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1241 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001242 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001243 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1244 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1245 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001246 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001249 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001250 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001251 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001252 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1253 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001254 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 }
1256
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001257#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001258 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1259 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001260 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001261 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1262 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001263 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001264
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001265#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001266 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001267 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001268 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001269 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001270
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001271#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001272 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1273 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001274 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001275 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1276 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001277 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001278
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001279#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001280 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001281 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001282 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001283 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001284
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001285 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001286 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001289 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001290 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001293 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001294 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001297 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001298 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001301 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001302 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001305 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001306 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001309 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001310 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 }
1312
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001313 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001314 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001317 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001318 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001321 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001322 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001325 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001326 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001329 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001330 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001333 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001334 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001337 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001338 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001339 }
1340
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001341 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001342 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001343 }
1344
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001345 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001346 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001347 }
1348
1349 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001350 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001351 }
1352
1353 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001354 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001355 }
1356
1357 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001358 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001359 }
1360
1361 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001362 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001363 }
1364
1365 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001366 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001367 }
1368
1369 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001370 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001371 }
1372
1373 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001374 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001375 }
1376
1377 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001378 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001379 }
1380
1381 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001382 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001383 }
1384
1385 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001386 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001387 }
1388
1389 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001390 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001391 }
1392
1393 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001394 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001395 }
1396
1397 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001398 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399 }
1400
1401 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001402 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001403 }
1404
1405 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001406 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407 }
1408
1409 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001410 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 }
1412
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001413 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001415 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001416 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001417 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001418 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001419 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 va_end(ap);
1421 return local_result;
1422 }
1423
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001424 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001425 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001426 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001427 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001428 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001431 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001432 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001433 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001434 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001435 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001438 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001440 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001441 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001442 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001443 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001445 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001448 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001449 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001450 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001451 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001454 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001455 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001456 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001457 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001460 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001462 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001463 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001464 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001465 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001467 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001470 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001471 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001472 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001473 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001476 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001477 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001478 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001479 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 }
1481
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001482 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001484 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001485 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001486 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001487 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001489 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 }
1491
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001492 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001493 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001494 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001495 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001496 }
1497
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001498 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001499 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001500 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001501 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001504 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001506 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001507 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001508 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001509 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001511 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001514 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001515 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001516 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001517 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001520 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001521 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001522 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001523 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
1525
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001526 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001528 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001529 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001530 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001531 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001533 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001536 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001537 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001538 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001539 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001542 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001543 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001544 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001545 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001548 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001550 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001551 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001552 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001553 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001555 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 }
1557
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001558 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001559 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001560 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001561 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001564 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001565 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001566 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001567 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 }
1569
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001570 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001572 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001573 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001574 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001575 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001577 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001580 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001581 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001582 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001583 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 }
1585
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001586 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001587 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001588 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001589 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001592 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001594 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001595 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001596 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001597 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001599 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 }
1601
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001602 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001603 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001604 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001605 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001608 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001609 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001610 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001611 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 }
1613
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001614 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001616 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001617 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001618 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001619 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 va_end(ap);
1621 }
1622
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001623 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001624 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001625 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001626 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001629 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001630 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001631 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001632 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 }
1634
Elliott Hughes814e4032011-08-23 12:07:56 -07001635 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001636 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001637 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001638 return nullptr;
1639 }
1640 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001641 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001642 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001643 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001644 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001645 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001646 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 }
1648
1649 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001650 if (utf == nullptr) {
1651 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001653 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001654 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001655 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 }
1657
Elliott Hughes814e4032011-08-23 12:07:56 -07001658 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001659 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001660 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001661 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001662 }
1663
1664 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001665 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001666 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001667 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001668 }
1669
Ian Rogersbc939662013-08-15 10:26:54 -07001670 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1671 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001672 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001673 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001674 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001675 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001676 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001677 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001678 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001679 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1680 memcpy(buf, chars + start, length * sizeof(jchar));
1681 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001682 }
1683
Ian Rogersbc939662013-08-15 10:26:54 -07001684 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1685 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001686 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001688 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001689 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001690 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001691 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001692 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001693 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1694 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1695 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001696 }
1697
Elliott Hughes75770752011-08-24 17:52:38 -07001698 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001699 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001700 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001701 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1702 mirror::CharArray* chars = s->GetCharArray();
Fred Shih56890e22014-06-02 11:11:52 -07001703 gc::Heap* heap = Runtime::Current()->GetHeap();
1704 if (heap->IsMovableObject(chars)) {
1705 if (is_copy != nullptr) {
1706 *is_copy = JNI_TRUE;
1707 }
1708 int32_t char_count = s->GetLength();
1709 int32_t offset = s->GetOffset();
1710 jchar* bytes = new jchar[char_count];
1711 for (int32_t i = 0; i < char_count; i++) {
1712 bytes[i] = chars->Get(i + offset);
1713 }
1714 return bytes;
1715 } else {
1716 if (is_copy != nullptr) {
1717 *is_copy = JNI_FALSE;
1718 }
1719 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001720 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001721 }
1722
Mathieu Chartier590fee92013-09-13 13:46:47 -07001723 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001724 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001725 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001726 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1727 mirror::CharArray* s_chars = s->GetCharArray();
1728 if (chars != (s_chars->GetData() + s->GetOffset())) {
1729 delete[] chars;
1730 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes75770752011-08-24 17:52:38 -07001733 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001734 CHECK_NON_NULL_ARGUMENT(java_string);
1735 ScopedObjectAccess soa(env);
1736 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1737 mirror::CharArray* chars = s->GetCharArray();
1738 int32_t offset = s->GetOffset();
Fred Shih56890e22014-06-02 11:11:52 -07001739 gc::Heap* heap = Runtime::Current()->GetHeap();
1740 if (heap->IsMovableObject(chars)) {
1741 StackHandleScope<1> hs(soa.Self());
1742 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
1743 heap->IncrementDisableMovingGC(soa.Self());
1744 }
1745 if (is_copy != nullptr) {
1746 *is_copy = JNI_FALSE;
1747 }
1748 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
Elliott Hughes75770752011-08-24 17:52:38 -07001751 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001752 UNUSED(chars);
Fred Shih56890e22014-06-02 11:11:52 -07001753 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1754 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001755 gc::Heap* heap = Runtime::Current()->GetHeap();
1756 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1757 mirror::CharArray* s_chars = s->GetCharArray();
1758 if (heap->IsMovableObject(s_chars)) {
1759 heap->DecrementDisableMovingGC(soa.Self());
1760 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes75770752011-08-24 17:52:38 -07001763 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001764 if (java_string == nullptr) {
1765 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001766 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001767 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001768 *is_copy = JNI_TRUE;
1769 }
Ian Rogersef28b142012-11-30 14:22:18 -08001770 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001771 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001772 size_t byte_count = s->GetUtfLength();
1773 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001774 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001775 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1776 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1777 bytes[byte_count] = '\0';
1778 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001779 }
1780
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001781 static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001782 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001783 }
1784
Elliott Hughesbd935992011-08-22 11:59:34 -07001785 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001786 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001787 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001788 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001789 if (UNLIKELY(!obj->IsArrayInstance())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001790 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1791 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001792 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001793 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001794 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 }
1796
Elliott Hughes814e4032011-08-23 12:07:56 -07001797 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001798 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001799 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001800 mirror::ObjectArray<mirror::Object>* array =
1801 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001802 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Ian Rogersbc939662013-08-15 10:26:54 -07001805 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1806 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001807 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001808 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001809 mirror::ObjectArray<mirror::Object>* array =
1810 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1811 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001812 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
1815 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001816 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 }
1818
1819 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001820 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
1823 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001824 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
1827 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001828 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
1831 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001832 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 }
1834
1835 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001836 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
1839 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001840 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
Ian Rogers1d99e452014-01-02 17:36:41 -08001843 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1844 jobject initial_element) {
1845 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001846 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001847 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001848 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001849 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001850
1851 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001852 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001853 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08001854 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001855 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08001856 if (UNLIKELY(element_class->IsPrimitive())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001857 soa.Vm()->JniAbortF("NewObjectArray", "not an object type: %s",
1858 PrettyDescriptor(element_class).c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08001859 return nullptr;
1860 }
Ian Rogers1d99e452014-01-02 17:36:41 -08001861 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07001862 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08001863 if (UNLIKELY(array_class == nullptr)) {
1864 return nullptr;
1865 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 }
1867
Elliott Hughes75770752011-08-24 17:52:38 -07001868 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001869 mirror::ObjectArray<mirror::Object>* result =
1870 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001871 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001872 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08001873 if (initial_object != nullptr) {
1874 mirror::Class* element_class = result->GetClass()->GetComponentType();
1875 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001876 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
1877 "element type of '%s'",
1878 PrettyDescriptor(initial_object->GetClass()).c_str(),
1879 PrettyDescriptor(element_class).c_str());
1880 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08001881 } else {
1882 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001883 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08001884 }
1885 }
Elliott Hughes75770752011-08-24 17:52:38 -07001886 }
1887 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001888 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 }
1890
1891 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001892 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 }
1894
Ian Rogersa15e67d2012-02-28 13:51:55 -08001895 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001896 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001897 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001898 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07001899 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001900 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
1901 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001902 return nullptr;
1903 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001904 gc::Heap* heap = Runtime::Current()->GetHeap();
1905 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08001906 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001907 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001908 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001909 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001910 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001911 *is_copy = JNI_FALSE;
1912 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001913 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001914 }
1915
Ian Rogers2d10b202014-05-12 19:15:18 -07001916 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
1917 jint mode) {
1918 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
1919 ScopedObjectAccess soa(env);
1920 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
1921 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001922 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
1923 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001924 return;
1925 }
1926 const size_t component_size = array->GetClass()->GetComponentSize();
1927 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001928 }
1929
Elliott Hughes75770752011-08-24 17:52:38 -07001930 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001931 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 }
1933
Elliott Hughes75770752011-08-24 17:52:38 -07001934 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001935 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 }
1937
Elliott Hughes75770752011-08-24 17:52:38 -07001938 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001939 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
Elliott Hughes75770752011-08-24 17:52:38 -07001942 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001943 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 }
1945
Elliott Hughes75770752011-08-24 17:52:38 -07001946 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001947 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
1949
Elliott Hughes75770752011-08-24 17:52:38 -07001950 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001951 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 }
1953
Elliott Hughes75770752011-08-24 17:52:38 -07001954 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001955 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 }
1957
Elliott Hughes75770752011-08-24 17:52:38 -07001958 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001959 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
Mathieu Chartier590fee92013-09-13 13:46:47 -07001962 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
1963 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001964 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
1965 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Mathieu Chartier590fee92013-09-13 13:46:47 -07001968 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001969 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Mathieu Chartier590fee92013-09-13 13:46:47 -07001972 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001973 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Mathieu Chartier590fee92013-09-13 13:46:47 -07001976 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
1977 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001978 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Mathieu Chartier590fee92013-09-13 13:46:47 -07001981 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
1982 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001983 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Mathieu Chartier590fee92013-09-13 13:46:47 -07001986 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001987 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Mathieu Chartier590fee92013-09-13 13:46:47 -07001990 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001991 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Mathieu Chartier590fee92013-09-13 13:46:47 -07001994 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
1995 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001996 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Ian Rogersbc939662013-08-15 10:26:54 -07001999 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2000 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002001 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002002 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
Ian Rogersbc939662013-08-15 10:26:54 -07002005 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2006 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002007 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Ian Rogersbc939662013-08-15 10:26:54 -07002010 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2011 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002012 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Ian Rogersbc939662013-08-15 10:26:54 -07002015 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2016 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002017 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002018 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Ian Rogersbc939662013-08-15 10:26:54 -07002021 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2022 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002023 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002024 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 }
2026
Ian Rogersbc939662013-08-15 10:26:54 -07002027 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2028 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002029 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Ian Rogersbc939662013-08-15 10:26:54 -07002032 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2033 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002034 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Ian Rogersbc939662013-08-15 10:26:54 -07002037 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2038 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002039 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002040 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Ian Rogersbc939662013-08-15 10:26:54 -07002043 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2044 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002045 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002046 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Ian Rogersbc939662013-08-15 10:26:54 -07002049 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2050 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002051 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Ian Rogersbc939662013-08-15 10:26:54 -07002054 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2055 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002056 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Ian Rogersbc939662013-08-15 10:26:54 -07002059 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2060 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002061 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002062 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Ian Rogersbc939662013-08-15 10:26:54 -07002065 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2066 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002067 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002068 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Ian Rogersbc939662013-08-15 10:26:54 -07002071 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2072 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002073 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Ian Rogersbc939662013-08-15 10:26:54 -07002076 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2077 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002078 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Ian Rogersbc939662013-08-15 10:26:54 -07002081 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2082 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002083 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002084 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Ian Rogersbc939662013-08-15 10:26:54 -07002087 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2088 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002089 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2090 }
2091
Ian Rogersbc939662013-08-15 10:26:54 -07002092 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2093 jint method_count, bool return_errors) {
2094 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002095 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2096 method_count);
2097 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002098 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002099 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002100 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002101 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002102 if (UNLIKELY(method_count == 0)) {
2103 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2104 << PrettyDescriptor(c);
2105 return JNI_OK;
2106 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002107 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002108 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 const char* name = methods[i].name;
2110 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002111 const void* fnPtr = methods[i].fnPtr;
2112 if (UNLIKELY(name == nullptr)) {
2113 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2114 return JNI_ERR;
2115 } else if (UNLIKELY(sig == nullptr)) {
2116 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2117 return JNI_ERR;
2118 } else if (UNLIKELY(fnPtr == nullptr)) {
2119 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2120 return JNI_ERR;
2121 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002122 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002124 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 ++sig;
2126 }
2127
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002128 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2129 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002130 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002132 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002133 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002134 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002135 << PrettyDescriptor(c) << "." << name << sig << " in "
2136 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002137 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002139 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002140 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002141 << PrettyDescriptor(c) << "." << name << sig
2142 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002143 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 return JNI_ERR;
2145 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002146
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002147 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002148
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002149 m->RegisterNative(fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 }
2151 return JNI_OK;
2152 }
2153
Elliott Hughes5174fe62011-08-23 15:12:35 -07002154 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002155 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002156 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002157 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002159 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002160
Ian Rogers2d10b202014-05-12 19:15:18 -07002161 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002163 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002164 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002165 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002166 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002167 }
2168 }
2169 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002170 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002171 if (m->IsNative()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002172 m->UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002173 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002174 }
2175 }
2176
Ian Rogers2d10b202014-05-12 19:15:18 -07002177 if (unregistered_count == 0) {
2178 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2179 << PrettyDescriptor(c) << "' that contains no native methods";
2180 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002181 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 }
2183
Ian Rogers719d1a32014-03-06 12:13:39 -08002184 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002185 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002186 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002187 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2188 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002189 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002190 return JNI_ERR;
2191 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002192 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002193 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
2195
Ian Rogers719d1a32014-03-06 12:13:39 -08002196 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002197 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002198 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002199 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002200 o->MonitorExit(soa.Self());
2201 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002202 return JNI_ERR;
2203 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002204 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002205 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
2208 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002209 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002211 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002212 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002214 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002216 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002220 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002221 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2222 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002223 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002224 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002225 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002226 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2227 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002228 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002229 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002230
Brian Carlstrom85a93362014-06-25 09:30:52 -07002231 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002232 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002233 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2234 "buffer capacity greater than maximum jint: %" PRId64,
2235 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002236 return nullptr;
2237 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002238 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002239 jint capacity_arg = static_cast<jint>(capacity);
2240
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002241 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2242 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002243 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002244 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002248 return reinterpret_cast<void*>(env->GetLongField(
2249 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Elliott Hughesb465ab02011-08-24 11:21:21 -07002252 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002253 return static_cast<jlong>(env->GetIntField(
2254 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002255 }
2256
Elliott Hughesb465ab02011-08-24 11:21:21 -07002257 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002258 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002259
2260 // Do we definitely know what kind of reference this is?
2261 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2262 IndirectRefKind kind = GetIndirectRefKind(ref);
2263 switch (kind) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002264 case kLocal:
2265 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002266 case kGlobal:
2267 return JNIGlobalRefType;
2268 case kWeakGlobal:
2269 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002270 case kHandleScopeOrInvalid:
Ian Rogersc0542af2014-09-03 16:16:56 -07002271 // Assume value is in a handle scope.
2272 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002273 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002274 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2275 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002276 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002277
2278 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002279 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2280 const char* caller)
2281 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002282 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002283 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002284 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2285 return JNI_ERR;
2286 }
2287 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002288 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002289 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2290 if (!okay) {
2291 soa.Self()->ThrowOutOfMemoryError(caller);
2292 }
2293 return okay ? JNI_OK : JNI_ERR;
2294 }
2295
2296 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002297 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002298 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002299 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002300 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002301 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002302 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002303 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002304 return soa.AddLocalReference<JniT>(result);
2305 }
2306
Ian Rogers2d10b202014-05-12 19:15:18 -07002307 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2308 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2309 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002310 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002311 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002312 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002313 soa.Vm()->JniAbortF(fn_name,
2314 "attempt to %s %s primitive array elements with an object of type %s",
2315 operation,
2316 PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2317 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002318 return nullptr;
2319 }
2320 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2321 return array;
2322 }
2323
2324 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2325 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2326 CHECK_NON_NULL_ARGUMENT(java_array);
2327 ScopedObjectAccess soa(env);
2328 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2329 "GetArrayElements",
2330 "get");
2331 if (UNLIKELY(array == nullptr)) {
2332 return nullptr;
2333 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002334 // Only make a copy if necessary.
2335 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2336 if (is_copy != nullptr) {
2337 *is_copy = JNI_TRUE;
2338 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002339 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002340 size_t size = array->GetLength() * component_size;
2341 void* data = new uint64_t[RoundUp(size, 8) / 8];
2342 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002343 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002344 } else {
2345 if (is_copy != nullptr) {
2346 *is_copy = JNI_FALSE;
2347 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002348 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002349 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002350 }
2351
Ian Rogers2d10b202014-05-12 19:15:18 -07002352 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002353 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002354 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002355 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002356 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2357 "ReleaseArrayElements",
2358 "release");
2359 if (array == nullptr) {
2360 return;
2361 }
2362 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2363 }
2364
2365 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2366 size_t component_size, void* elements, jint mode)
2367 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002368 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002369 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002370 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002371 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002372 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2373 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002374 if (is_copy) {
2375 // Sanity check: If elements is not the same as the java array's data, it better not be a
2376 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2377 // copies we make?
2378 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002379 soa.Vm()->JniAbortF("ReleaseArrayElements",
2380 "invalid element pointer %p, array elements are %p",
2381 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002382 return;
2383 }
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002384 if (mode != JNI_ABORT) {
2385 memcpy(array_data, elements, bytes);
2386 } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2387 // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2388 LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2389 soa.Self()->DumpJavaStack(LOG(WARNING));
2390 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002391 }
2392 if (mode != JNI_COMMIT) {
2393 if (is_copy) {
2394 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002395 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002396 // Non copy to a movable object must means that we had disabled the moving GC.
2397 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002398 }
2399 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002400 }
2401
Ian Rogers2d10b202014-05-12 19:15:18 -07002402 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2403 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2404 jsize start, jsize length, ElementT* buf) {
2405 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2406 ScopedObjectAccess soa(env);
2407 ArtArrayT* array =
2408 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2409 "GetPrimitiveArrayRegion",
2410 "get region of");
2411 if (array != nullptr) {
2412 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2413 ThrowAIOOBE(soa, array, start, length, "src");
2414 } else {
2415 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2416 ElementT* data = array->GetData();
2417 memcpy(buf, data + start, length * sizeof(ElementT));
2418 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002419 }
2420 }
2421
Ian Rogers2d10b202014-05-12 19:15:18 -07002422 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2423 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2424 jsize start, jsize length, const ElementT* buf) {
2425 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2426 ScopedObjectAccess soa(env);
2427 ArtArrayT* array =
2428 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2429 "SetPrimitiveArrayRegion",
2430 "set region of");
2431 if (array != nullptr) {
2432 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2433 ThrowAIOOBE(soa, array, start, length, "dst");
2434 } else {
2435 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2436 ElementT* data = array->GetData();
2437 memcpy(data + start, buf, length * sizeof(ElementT));
2438 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002439 }
2440 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002441};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002442
Elliott Hughes88c5c352012-03-15 18:49:48 -07002443const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002444 nullptr, // reserved0.
2445 nullptr, // reserved1.
2446 nullptr, // reserved2.
2447 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002448 JNI::GetVersion,
2449 JNI::DefineClass,
2450 JNI::FindClass,
2451 JNI::FromReflectedMethod,
2452 JNI::FromReflectedField,
2453 JNI::ToReflectedMethod,
2454 JNI::GetSuperclass,
2455 JNI::IsAssignableFrom,
2456 JNI::ToReflectedField,
2457 JNI::Throw,
2458 JNI::ThrowNew,
2459 JNI::ExceptionOccurred,
2460 JNI::ExceptionDescribe,
2461 JNI::ExceptionClear,
2462 JNI::FatalError,
2463 JNI::PushLocalFrame,
2464 JNI::PopLocalFrame,
2465 JNI::NewGlobalRef,
2466 JNI::DeleteGlobalRef,
2467 JNI::DeleteLocalRef,
2468 JNI::IsSameObject,
2469 JNI::NewLocalRef,
2470 JNI::EnsureLocalCapacity,
2471 JNI::AllocObject,
2472 JNI::NewObject,
2473 JNI::NewObjectV,
2474 JNI::NewObjectA,
2475 JNI::GetObjectClass,
2476 JNI::IsInstanceOf,
2477 JNI::GetMethodID,
2478 JNI::CallObjectMethod,
2479 JNI::CallObjectMethodV,
2480 JNI::CallObjectMethodA,
2481 JNI::CallBooleanMethod,
2482 JNI::CallBooleanMethodV,
2483 JNI::CallBooleanMethodA,
2484 JNI::CallByteMethod,
2485 JNI::CallByteMethodV,
2486 JNI::CallByteMethodA,
2487 JNI::CallCharMethod,
2488 JNI::CallCharMethodV,
2489 JNI::CallCharMethodA,
2490 JNI::CallShortMethod,
2491 JNI::CallShortMethodV,
2492 JNI::CallShortMethodA,
2493 JNI::CallIntMethod,
2494 JNI::CallIntMethodV,
2495 JNI::CallIntMethodA,
2496 JNI::CallLongMethod,
2497 JNI::CallLongMethodV,
2498 JNI::CallLongMethodA,
2499 JNI::CallFloatMethod,
2500 JNI::CallFloatMethodV,
2501 JNI::CallFloatMethodA,
2502 JNI::CallDoubleMethod,
2503 JNI::CallDoubleMethodV,
2504 JNI::CallDoubleMethodA,
2505 JNI::CallVoidMethod,
2506 JNI::CallVoidMethodV,
2507 JNI::CallVoidMethodA,
2508 JNI::CallNonvirtualObjectMethod,
2509 JNI::CallNonvirtualObjectMethodV,
2510 JNI::CallNonvirtualObjectMethodA,
2511 JNI::CallNonvirtualBooleanMethod,
2512 JNI::CallNonvirtualBooleanMethodV,
2513 JNI::CallNonvirtualBooleanMethodA,
2514 JNI::CallNonvirtualByteMethod,
2515 JNI::CallNonvirtualByteMethodV,
2516 JNI::CallNonvirtualByteMethodA,
2517 JNI::CallNonvirtualCharMethod,
2518 JNI::CallNonvirtualCharMethodV,
2519 JNI::CallNonvirtualCharMethodA,
2520 JNI::CallNonvirtualShortMethod,
2521 JNI::CallNonvirtualShortMethodV,
2522 JNI::CallNonvirtualShortMethodA,
2523 JNI::CallNonvirtualIntMethod,
2524 JNI::CallNonvirtualIntMethodV,
2525 JNI::CallNonvirtualIntMethodA,
2526 JNI::CallNonvirtualLongMethod,
2527 JNI::CallNonvirtualLongMethodV,
2528 JNI::CallNonvirtualLongMethodA,
2529 JNI::CallNonvirtualFloatMethod,
2530 JNI::CallNonvirtualFloatMethodV,
2531 JNI::CallNonvirtualFloatMethodA,
2532 JNI::CallNonvirtualDoubleMethod,
2533 JNI::CallNonvirtualDoubleMethodV,
2534 JNI::CallNonvirtualDoubleMethodA,
2535 JNI::CallNonvirtualVoidMethod,
2536 JNI::CallNonvirtualVoidMethodV,
2537 JNI::CallNonvirtualVoidMethodA,
2538 JNI::GetFieldID,
2539 JNI::GetObjectField,
2540 JNI::GetBooleanField,
2541 JNI::GetByteField,
2542 JNI::GetCharField,
2543 JNI::GetShortField,
2544 JNI::GetIntField,
2545 JNI::GetLongField,
2546 JNI::GetFloatField,
2547 JNI::GetDoubleField,
2548 JNI::SetObjectField,
2549 JNI::SetBooleanField,
2550 JNI::SetByteField,
2551 JNI::SetCharField,
2552 JNI::SetShortField,
2553 JNI::SetIntField,
2554 JNI::SetLongField,
2555 JNI::SetFloatField,
2556 JNI::SetDoubleField,
2557 JNI::GetStaticMethodID,
2558 JNI::CallStaticObjectMethod,
2559 JNI::CallStaticObjectMethodV,
2560 JNI::CallStaticObjectMethodA,
2561 JNI::CallStaticBooleanMethod,
2562 JNI::CallStaticBooleanMethodV,
2563 JNI::CallStaticBooleanMethodA,
2564 JNI::CallStaticByteMethod,
2565 JNI::CallStaticByteMethodV,
2566 JNI::CallStaticByteMethodA,
2567 JNI::CallStaticCharMethod,
2568 JNI::CallStaticCharMethodV,
2569 JNI::CallStaticCharMethodA,
2570 JNI::CallStaticShortMethod,
2571 JNI::CallStaticShortMethodV,
2572 JNI::CallStaticShortMethodA,
2573 JNI::CallStaticIntMethod,
2574 JNI::CallStaticIntMethodV,
2575 JNI::CallStaticIntMethodA,
2576 JNI::CallStaticLongMethod,
2577 JNI::CallStaticLongMethodV,
2578 JNI::CallStaticLongMethodA,
2579 JNI::CallStaticFloatMethod,
2580 JNI::CallStaticFloatMethodV,
2581 JNI::CallStaticFloatMethodA,
2582 JNI::CallStaticDoubleMethod,
2583 JNI::CallStaticDoubleMethodV,
2584 JNI::CallStaticDoubleMethodA,
2585 JNI::CallStaticVoidMethod,
2586 JNI::CallStaticVoidMethodV,
2587 JNI::CallStaticVoidMethodA,
2588 JNI::GetStaticFieldID,
2589 JNI::GetStaticObjectField,
2590 JNI::GetStaticBooleanField,
2591 JNI::GetStaticByteField,
2592 JNI::GetStaticCharField,
2593 JNI::GetStaticShortField,
2594 JNI::GetStaticIntField,
2595 JNI::GetStaticLongField,
2596 JNI::GetStaticFloatField,
2597 JNI::GetStaticDoubleField,
2598 JNI::SetStaticObjectField,
2599 JNI::SetStaticBooleanField,
2600 JNI::SetStaticByteField,
2601 JNI::SetStaticCharField,
2602 JNI::SetStaticShortField,
2603 JNI::SetStaticIntField,
2604 JNI::SetStaticLongField,
2605 JNI::SetStaticFloatField,
2606 JNI::SetStaticDoubleField,
2607 JNI::NewString,
2608 JNI::GetStringLength,
2609 JNI::GetStringChars,
2610 JNI::ReleaseStringChars,
2611 JNI::NewStringUTF,
2612 JNI::GetStringUTFLength,
2613 JNI::GetStringUTFChars,
2614 JNI::ReleaseStringUTFChars,
2615 JNI::GetArrayLength,
2616 JNI::NewObjectArray,
2617 JNI::GetObjectArrayElement,
2618 JNI::SetObjectArrayElement,
2619 JNI::NewBooleanArray,
2620 JNI::NewByteArray,
2621 JNI::NewCharArray,
2622 JNI::NewShortArray,
2623 JNI::NewIntArray,
2624 JNI::NewLongArray,
2625 JNI::NewFloatArray,
2626 JNI::NewDoubleArray,
2627 JNI::GetBooleanArrayElements,
2628 JNI::GetByteArrayElements,
2629 JNI::GetCharArrayElements,
2630 JNI::GetShortArrayElements,
2631 JNI::GetIntArrayElements,
2632 JNI::GetLongArrayElements,
2633 JNI::GetFloatArrayElements,
2634 JNI::GetDoubleArrayElements,
2635 JNI::ReleaseBooleanArrayElements,
2636 JNI::ReleaseByteArrayElements,
2637 JNI::ReleaseCharArrayElements,
2638 JNI::ReleaseShortArrayElements,
2639 JNI::ReleaseIntArrayElements,
2640 JNI::ReleaseLongArrayElements,
2641 JNI::ReleaseFloatArrayElements,
2642 JNI::ReleaseDoubleArrayElements,
2643 JNI::GetBooleanArrayRegion,
2644 JNI::GetByteArrayRegion,
2645 JNI::GetCharArrayRegion,
2646 JNI::GetShortArrayRegion,
2647 JNI::GetIntArrayRegion,
2648 JNI::GetLongArrayRegion,
2649 JNI::GetFloatArrayRegion,
2650 JNI::GetDoubleArrayRegion,
2651 JNI::SetBooleanArrayRegion,
2652 JNI::SetByteArrayRegion,
2653 JNI::SetCharArrayRegion,
2654 JNI::SetShortArrayRegion,
2655 JNI::SetIntArrayRegion,
2656 JNI::SetLongArrayRegion,
2657 JNI::SetFloatArrayRegion,
2658 JNI::SetDoubleArrayRegion,
2659 JNI::RegisterNatives,
2660 JNI::UnregisterNatives,
2661 JNI::MonitorEnter,
2662 JNI::MonitorExit,
2663 JNI::GetJavaVM,
2664 JNI::GetStringRegion,
2665 JNI::GetStringUTFRegion,
2666 JNI::GetPrimitiveArrayCritical,
2667 JNI::ReleasePrimitiveArrayCritical,
2668 JNI::GetStringCritical,
2669 JNI::ReleaseStringCritical,
2670 JNI::NewWeakGlobalRef,
2671 JNI::DeleteWeakGlobalRef,
2672 JNI::ExceptionCheck,
2673 JNI::NewDirectByteBuffer,
2674 JNI::GetDirectBufferAddress,
2675 JNI::GetDirectBufferCapacity,
2676 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002677};
2678
Ian Rogers68d8b422014-07-17 11:09:10 -07002679const JNINativeInterface* GetJniNativeInterface() {
2680 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002681}
2682
Elliott Hughesc8fece32013-01-02 11:27:23 -08002683void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002684 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002685 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002686 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002687 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2688 }
2689 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2690}
2691
Ian Rogersdf20fe02011-07-20 20:34:16 -07002692} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002693
2694std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2695 switch (rhs) {
2696 case JNIInvalidRefType:
2697 os << "JNIInvalidRefType";
2698 return os;
2699 case JNILocalRefType:
2700 os << "JNILocalRefType";
2701 return os;
2702 case JNIGlobalRefType:
2703 os << "JNIGlobalRefType";
2704 return os;
2705 case JNIWeakGlobalRefType:
2706 os << "JNIWeakGlobalRefType";
2707 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002708 default:
Ian Rogersc7dd2952014-10-21 23:31:19 -07002709 LOG(::art::FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
2710 UNREACHABLE();
Elliott Hughesb465ab02011-08-24 11:21:21 -07002711 }
2712}