blob: d5e92a40cb8eb8a8c224181fb5b38a9379f4c7dd [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"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080028#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080029#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080030#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "dex_file-inl.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070032#include "gc_root.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/accounting/card_table-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070034#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070035#include "interpreter/interpreter.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070036#include "jni_env_ext.h"
37#include "java_vm_ext.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070038#include "mirror/art_field-inl.h"
39#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/class-inl.h"
41#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/object-inl.h"
43#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070044#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "mirror/throwable.h"
Yong WU355383f2014-07-24 21:32:15 +080046#include "native_bridge.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080047#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070048#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070049#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070050#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070052#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070053#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070055#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070056
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070057namespace art {
58
Elliott Hughes6b436852011-08-12 10:16:44 -070059// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
60// separated with slashes but aren't wrapped with "L;" like regular descriptors
61// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
62// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
63// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070064static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070065 std::string result;
66 // Add the missing "L;" if necessary.
67 if (name[0] == '[') {
68 result = name;
69 } else {
70 result += 'L';
71 result += name;
72 result += ';';
73 }
74 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070075 if (result.find('.') != std::string::npos) {
76 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
77 << "\"" << name << "\"";
78 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -070079 }
80 return result;
81}
82
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080083static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070084 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -070085 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080086 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
87 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
88 "no %s method \"%s.%s%s\"",
Mathieu Chartierf8322842014-05-16 10:59:25 -070089 kind, c->GetDescriptor().c_str(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -070090}
91
Sebastien Hertzfa65e842014-07-03 09:39:53 +020092static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa, mirror::Class* c,
93 const char* kind, jint idx, bool return_errors)
94 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
95 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method in "
96 << PrettyDescriptor(c) << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
97 << ": " << kind << " is null at index " << idx;
98 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
99 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
100 "%s is null at index %d", kind, idx);
101}
102
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800103static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
105 if (LIKELY(klass->IsInitialized())) {
106 return klass;
107 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700108 StackHandleScope<1> hs(self);
109 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
110 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800111 return nullptr;
112 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700113 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800114}
115
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
117 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800119 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800120 if (c == nullptr) {
121 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700122 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800123 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700124 if (is_static) {
125 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700126 } else if (c->IsInterface()) {
127 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700128 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700129 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800130 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700131 // No virtual method matching the signature. Search declared
132 // private methods and constructors.
133 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700134 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700135 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800136 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700137 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800138 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700139 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700141}
142
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800143static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700144 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800145 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700146 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
147 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700148 return soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700149 }
Brian Carlstromce888532013-10-10 00:32:58 -0700150 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800151 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700152 return method->GetDeclaringClass()->GetClassLoader();
153 }
154 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800155 mirror::ClassLoader* class_loader =
156 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
157 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700158 return class_loader;
159 }
160 // See if the override ClassLoader is set for gtests.
Ian Rogers68d8b422014-07-17 11:09:10 -0700161 class_loader = soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800162 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800163 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700164 CHECK(Runtime::Current()->UseCompileTimeClassPath());
165 return class_loader;
166 }
167 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800168 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700169}
170
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700171static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
172 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700174 StackHandleScope<2> hs(soa.Self());
175 Handle<mirror::Class> c(
176 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
177 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800178 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700179 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800180 mirror::ArtField* field = nullptr;
181 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700182 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
183 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700184 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800185 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700186 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700187 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700188 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800189 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700190 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800192 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700193 StackHandleScope<1> hs(soa.Self());
194 Handle<mirror::Throwable> cause(hs.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700195 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800196 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800197 "no type \"%s\" found and so no field \"%s\" "
198 "could be found in class \"%s\" or its superclasses", sig, name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700199 c->GetDescriptor().c_str());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700200 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800201 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700202 }
203 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700204 field = mirror::Class::FindStaticField(soa.Self(), c, name,
205 field_type->GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 } else {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700207 field = c->FindInstanceField(name, field_type->GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700208 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800209 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800210 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
211 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
212 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartierf8322842014-05-16 10:59:25 -0700213 sig, name, c->GetDescriptor().c_str());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800214 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700215 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700216 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700217}
218
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800219static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700222 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800223 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
224 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
225 "%s offset=%d length=%d %s.length=%d",
226 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700227}
Ian Rogers0571d352011-11-03 19:51:38 -0700228
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700229static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
230 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800232 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
233 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
234 "offset=%d length=%d string.length()=%d", start, length,
235 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700236}
Elliott Hughes814e4032011-08-23 12:07:56 -0700237
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700238int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700239 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240 // Turn the const char* into a java.lang.String.
241 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800242 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700244 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700245
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 // Choose an appropriate constructor and set up the arguments.
247 jvalue args[2];
248 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800249 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700250 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800251 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252 signature = "(Ljava/lang/String;)V";
253 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800254 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 signature = "(Ljava/lang/Throwable;)V";
256 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700257 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
259 args[0].l = s.get();
260 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700261 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800263 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800264 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800266 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267 return JNI_ERR;
268 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700269
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800270 ScopedLocalRef<jthrowable> exception(
271 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
272 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273 return JNI_ERR;
274 }
Ian Rogersef28b142012-11-30 14:22:18 -0800275 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800276 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800277 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700279}
280
Ian Rogers68d8b422014-07-17 11:09:10 -0700281static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
282 return reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughes75770752011-08-24 17:52:38 -0700283}
284
Ian Rogers2d10b202014-05-12 19:15:18 -0700285#define CHECK_NON_NULL_ARGUMENT(value) \
286 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700287
Ian Rogers2d10b202014-05-12 19:15:18 -0700288#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
289 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
290
291#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
292 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
293
294#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
295 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
296
297#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800298 if (UNLIKELY(value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700299 JavaVmExtFromEnv(env)->JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700300 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700301 }
302
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700303#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800304 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700305 JavaVmExtFromEnv(env)->JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700306 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700307 }
308
Elliott Hughescdf53122011-08-19 15:46:09 -0700309class JNI {
310 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700311 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700312 return JNI_VERSION_1_6;
313 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700314
Ian Rogers25e8b912012-09-07 11:31:36 -0700315 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700316 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800317 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700318 }
319
Elliott Hughescdf53122011-08-19 15:46:09 -0700320 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700321 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700322 Runtime* runtime = Runtime::Current();
323 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700324 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700325 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800326 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700327 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700328 StackHandleScope<1> hs(soa.Self());
329 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800330 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700331 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800332 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700333 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700334 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700335 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700336
Ian Rogers62f05122014-03-21 11:21:29 -0700337 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700338 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700340 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700341 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700342
Ian Rogers62f05122014-03-21 11:21:29 -0700343 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700344 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700346 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700347 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700348
Elliott Hughescdf53122011-08-19 15:46:09 -0700349 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700350 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700351 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800352 mirror::ArtMethod* m = soa.DecodeMethod(mid);
353 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700354 jobject art_method = soa.AddLocalReference<jobject>(m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200355 jobject reflect_method;
356 if (m->IsConstructor()) {
357 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
358 } else {
359 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
360 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700361 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800362 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700363 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800364 SetObjectField(env, reflect_method,
365 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700366 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700367 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700368
Elliott Hughescdf53122011-08-19 15:46:09 -0700369 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700370 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700371 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800372 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700373 jobject art_field = soa.AddLocalReference<jobject>(f);
374 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
375 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800376 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700377 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800378 SetObjectField(env, reflect_field,
379 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700380 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700381 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700382
Elliott Hughes37f7a402011-08-22 18:56:01 -0700383 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700384 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700385 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800386 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700387 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700388 }
389
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700390 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700391 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700392 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800393 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700395 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700396
Narayan Kamath1268b742014-07-11 19:15:11 +0100397 // Note: java_class1 should be safely castable to java_class2, and
398 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700399 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700400 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
401 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700402 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800403 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
404 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100405 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700406 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700407
Elliott Hughese84278b2012-03-22 10:06:53 -0700408 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700409 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800410 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700411 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700412 return JNI_TRUE;
413 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700414 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800415 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
416 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700417 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700418 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700419 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700420
Elliott Hughes37f7a402011-08-22 18:56:01 -0700421 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700422 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800423 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
424 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700425 return JNI_ERR;
426 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800427 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
428 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700429 return JNI_OK;
430 }
431
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700432 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700433 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800434 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700435 }
436
437 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700438 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700439 }
440
441 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700442 ScopedObjectAccess soa(env);
443 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700444 }
445
446 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700447 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700448
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700449 // If we have no exception to describe, pass through.
450 if (!soa.Self()->GetException(nullptr)) {
451 return;
452 }
453
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700454 StackHandleScope<3> hs(soa.Self());
455 // TODO: Use nullptr instead of null handles?
456 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
457 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
458 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800459 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200460 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800461 {
462 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800463 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700464 old_throw_this_object.Assign(old_throw_location.GetThis());
465 old_throw_method.Assign(old_throw_location.GetMethod());
466 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800467 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200468 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800469 soa.Self()->ClearException();
470 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800471 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700472 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700473 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
474 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800475 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700476 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700477 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700478 } else {
479 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800480 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800481 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700482 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800483 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700484 }
485 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700486 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800487 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700488
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700489 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200490 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700491 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700492
Elliott Hughescdf53122011-08-19 15:46:09 -0700493 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700494 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800495 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700496 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700497 }
498
Ian Rogers25e8b912012-09-07 11:31:36 -0700499 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700500 LOG(FATAL) << "JNI FatalError called: " << msg;
501 }
502
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700503 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700504 // TODO: SOA may not be necessary but I do it to please lock annotations.
505 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700506 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700507 return JNI_ERR;
508 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700509 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700510 return JNI_OK;
511 }
512
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700513 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700514 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800515 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700516 soa.Env()->PopFrame();
517 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700518 }
519
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700520 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700521 // TODO: SOA may not be necessary but I do it to please lock annotations.
522 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700523 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700524 }
525
Elliott Hughescdf53122011-08-19 15:46:09 -0700526 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700527 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800528 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700529 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700530 }
531
532 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700533 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
534 Thread* self = down_cast<JNIEnvExt*>(env)->self;
535 vm->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700536 }
537
538 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700539 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700540 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
541 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700542 }
543
544 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700545 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
546 Thread* self = down_cast<JNIEnvExt*>(env)->self;
547 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700548 }
549
550 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700551 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800552 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800553 // Check for null after decoding the object to handle cleared weak globals.
554 if (decoded_obj == nullptr) {
555 return nullptr;
556 }
557 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700558 }
559
560 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800561 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700562 return;
563 }
Ian Rogersef28b142012-11-30 14:22:18 -0800564 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700565
Ian Rogersef28b142012-11-30 14:22:18 -0800566 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 if (!locals.Remove(cookie, obj)) {
568 // Attempting to delete a local reference that is not in the
569 // topmost local reference frame is a no-op. DeleteLocalRef returns
570 // void and doesn't throw any exceptions, but we should probably
571 // complain about it so the user will notice that things aren't
572 // going quite the way they expect.
573 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
574 << "failed to find entry";
575 }
576 }
577
578 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700579 if (obj1 == obj2) {
580 return JNI_TRUE;
581 } else {
582 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800583 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
584 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700585 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700586 }
587
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700588 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700589 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700590 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800591 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800592 if (c == nullptr) {
593 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700594 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700595 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700596 }
597
Ian Rogersbc939662013-08-15 10:26:54 -0700598 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700599 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700600 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700601 CHECK_NON_NULL_ARGUMENT(java_class);
602 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700603 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700604 va_end(args);
605 return result;
606 }
607
Elliott Hughes72025e52011-08-23 17:50:30 -0700608 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700609 CHECK_NON_NULL_ARGUMENT(java_class);
610 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800612 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800613 if (c == nullptr) {
614 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700615 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800616 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800617 if (result == nullptr) {
618 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700619 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700621 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800622 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800623 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700624 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800625 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 }
627
Elliott Hughes72025e52011-08-23 17:50:30 -0700628 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700629 CHECK_NON_NULL_ARGUMENT(java_class);
630 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700631 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800632 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800633 if (c == nullptr) {
634 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700635 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800636 mirror::Object* result = c->AllocObject(soa.Self());
637 if (result == nullptr) {
638 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700639 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700640 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700641 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800642 if (soa.Self()->IsExceptionPending()) {
643 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700644 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800645 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700646 }
647
Ian Rogersbc939662013-08-15 10:26:54 -0700648 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700649 CHECK_NON_NULL_ARGUMENT(java_class);
650 CHECK_NON_NULL_ARGUMENT(name);
651 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700653 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700654 }
655
Ian Rogersbc939662013-08-15 10:26:54 -0700656 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
657 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700658 CHECK_NON_NULL_ARGUMENT(java_class);
659 CHECK_NON_NULL_ARGUMENT(name);
660 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700662 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 }
664
Elliott Hughes72025e52011-08-23 17:50:30 -0700665 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700666 va_list ap;
667 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700668 CHECK_NON_NULL_ARGUMENT(obj);
669 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700670 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700671 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700672 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 }
675
Elliott Hughes72025e52011-08-23 17:50:30 -0700676 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700677 CHECK_NON_NULL_ARGUMENT(obj);
678 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 ScopedObjectAccess soa(env);
680 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
681 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 CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* 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);
Ian Rogers53b8b092014-03-13 23:45:53 -0700688 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
689 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700690 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 }
692
Elliott Hughes72025e52011-08-23 17:50:30 -0700693 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700694 va_list ap;
695 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700696 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
697 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700698 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700699 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700700 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700701 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700702 }
703
Elliott Hughes72025e52011-08-23 17:50:30 -0700704 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700705 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
706 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700707 ScopedObjectAccess soa(env);
708 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 }
710
Elliott Hughes72025e52011-08-23 17:50:30 -0700711 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700712 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
713 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700715 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
716 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700717 }
718
Elliott Hughes72025e52011-08-23 17:50:30 -0700719 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700720 va_list ap;
721 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700722 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
723 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700724 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700725 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700726 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700727 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 }
729
Elliott Hughes72025e52011-08-23 17:50:30 -0700730 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700731 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
732 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700733 ScopedObjectAccess soa(env);
734 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 }
736
Elliott Hughes72025e52011-08-23 17:50:30 -0700737 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700738 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
739 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700740 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700741 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
742 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 }
744
Elliott Hughes72025e52011-08-23 17:50:30 -0700745 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700746 va_list ap;
747 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700748 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
749 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700750 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700751 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700752 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700753 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 }
755
Elliott Hughes72025e52011-08-23 17:50:30 -0700756 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700757 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
758 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700759 ScopedObjectAccess soa(env);
760 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 }
762
Elliott Hughes72025e52011-08-23 17:50:30 -0700763 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700764 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
765 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700767 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
768 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 }
770
Elliott Hughes72025e52011-08-23 17:50:30 -0700771 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700772 va_list ap;
773 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700774 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
775 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700776 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700777 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700778 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700779 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 }
781
Elliott Hughes72025e52011-08-23 17:50:30 -0700782 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700783 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
784 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700785 ScopedObjectAccess soa(env);
786 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 }
788
Elliott Hughes72025e52011-08-23 17:50:30 -0700789 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700790 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
791 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700792 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700793 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
794 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700795 }
796
Elliott Hughes72025e52011-08-23 17:50:30 -0700797 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700798 va_list ap;
799 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700800 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
801 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700802 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700803 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700804 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700805 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 }
807
Elliott Hughes72025e52011-08-23 17:50:30 -0700808 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700809 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
810 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700811 ScopedObjectAccess soa(env);
812 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 }
814
Elliott Hughes72025e52011-08-23 17:50:30 -0700815 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700816 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
817 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700818 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700819 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
820 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 }
822
Elliott Hughes72025e52011-08-23 17:50:30 -0700823 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700824 va_list ap;
825 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700826 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
827 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700828 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700829 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700830 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700831 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 }
833
Elliott Hughes72025e52011-08-23 17:50:30 -0700834 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700835 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
836 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700837 ScopedObjectAccess soa(env);
838 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 }
840
Elliott Hughes72025e52011-08-23 17:50:30 -0700841 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700842 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
843 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700844 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700845 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
846 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 }
848
Elliott Hughes72025e52011-08-23 17:50:30 -0700849 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700850 va_list ap;
851 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700852 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
853 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700854 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700855 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700856 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700857 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700858 }
859
Elliott Hughes72025e52011-08-23 17:50:30 -0700860 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700861 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
862 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 ScopedObjectAccess soa(env);
864 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 }
866
Elliott Hughes72025e52011-08-23 17:50:30 -0700867 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700868 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
869 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700870 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700871 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
872 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 }
874
Elliott Hughes72025e52011-08-23 17:50:30 -0700875 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700876 va_list ap;
877 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700878 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
879 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700880 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700881 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700882 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700883 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 }
885
Elliott Hughes72025e52011-08-23 17:50:30 -0700886 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700887 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
888 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700889 ScopedObjectAccess soa(env);
890 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700891 }
892
Elliott Hughes72025e52011-08-23 17:50:30 -0700893 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700894 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
895 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700896 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700897 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
898 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 }
900
Elliott Hughes72025e52011-08-23 17:50:30 -0700901 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700902 va_list ap;
903 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700904 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
905 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700906 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -0700907 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -0700909 }
910
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700912 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
913 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700914 ScopedObjectAccess soa(env);
915 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 }
917
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700919 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
920 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700921 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700922 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 }
924
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700925 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700926 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700928 CHECK_NON_NULL_ARGUMENT(obj);
929 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700930 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700931 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
932 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 va_end(ap);
934 return local_result;
935 }
936
Ian Rogersbc939662013-08-15 10:26:54 -0700937 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
938 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700939 CHECK_NON_NULL_ARGUMENT(obj);
940 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700941 ScopedObjectAccess soa(env);
942 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
943 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 }
945
Ian Rogersbc939662013-08-15 10:26:54 -0700946 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
947 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700948 CHECK_NON_NULL_ARGUMENT(obj);
949 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700950 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700951 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700952 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 }
954
Ian Rogersbc939662013-08-15 10:26:54 -0700955 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
956 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700958 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700959 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
960 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700961 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700962 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700964 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Ian Rogersbc939662013-08-15 10:26:54 -0700967 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
968 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700969 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
970 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700971 ScopedObjectAccess soa(env);
972 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Ian Rogersbc939662013-08-15 10:26:54 -0700975 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
976 jvalue* 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);
Ian Rogers53b8b092014-03-13 23:45:53 -0700980 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700983 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700986 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
987 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700988 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700991 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 }
993
Ian Rogersbc939662013-08-15 10:26:54 -0700994 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
995 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700996 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
997 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 ScopedObjectAccess soa(env);
999 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Ian Rogersbc939662013-08-15 10:26:54 -07001002 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1003 jvalue* 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);
Ian Rogers53b8b092014-03-13 23:45:53 -07001007 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001010 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001013 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1014 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001015 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001016 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001018 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Ian Rogersbc939662013-08-15 10:26:54 -07001021 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1022 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001023 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1024 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001025 ScopedObjectAccess soa(env);
1026 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Ian Rogersbc939662013-08-15 10:26:54 -07001029 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1030 jvalue* 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);
Ian Rogers53b8b092014-03-13 23:45:53 -07001034 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001037 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001040 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1041 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001042 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001045 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Ian Rogersbc939662013-08-15 10:26:54 -07001048 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1049 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001050 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1051 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001052 ScopedObjectAccess soa(env);
1053 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Ian Rogersbc939662013-08-15 10:26:54 -07001056 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1057 jvalue* 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);
Ian Rogers53b8b092014-03-13 23:45:53 -07001061 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001064 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001067 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1068 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001069 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001070 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001072 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Ian Rogersbc939662013-08-15 10:26:54 -07001075 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1076 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001077 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1078 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 ScopedObjectAccess soa(env);
1080 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Ian Rogersbc939662013-08-15 10:26:54 -07001083 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1084 jvalue* 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);
Ian Rogers53b8b092014-03-13 23:45:53 -07001088 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001091 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001094 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1095 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001096 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001097 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001099 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Ian Rogersbc939662013-08-15 10:26:54 -07001102 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1103 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001104 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1105 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 ScopedObjectAccess soa(env);
1107 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Ian Rogersbc939662013-08-15 10:26:54 -07001110 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1111 jvalue* 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);
Ian Rogers53b8b092014-03-13 23:45:53 -07001115 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001118 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001121 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1122 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001123 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001124 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001126 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Ian Rogersbc939662013-08-15 10:26:54 -07001129 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1130 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001131 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1132 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001133 ScopedObjectAccess soa(env);
1134 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Ian Rogersbc939662013-08-15 10:26:54 -07001137 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1138 jvalue* 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);
Ian Rogers53b8b092014-03-13 23:45:53 -07001142 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001145 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001148 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1149 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001150 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001153 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 }
1155
Ian Rogersbc939662013-08-15 10:26:54 -07001156 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1157 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001158 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1159 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001160 ScopedObjectAccess soa(env);
1161 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Ian Rogersbc939662013-08-15 10:26:54 -07001164 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1165 jvalue* 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);
Ian Rogers53b8b092014-03-13 23:45:53 -07001169 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 }
1171
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001172 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001175 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1176 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001177 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001178 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 va_end(ap);
1180 }
1181
Brian Carlstromea46f952013-07-30 01:26:50 -07001182 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1183 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001184 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1185 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186 ScopedObjectAccess soa(env);
1187 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
Ian Rogersbc939662013-08-15 10:26:54 -07001190 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1191 jvalue* 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);
Ian Rogers53b8b092014-03-13 23:45:53 -07001195 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Ian Rogersbc939662013-08-15 10:26:54 -07001198 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001199 CHECK_NON_NULL_ARGUMENT(java_class);
1200 CHECK_NON_NULL_ARGUMENT(name);
1201 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001202 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001203 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001205
Ian Rogersbc939662013-08-15 10:26:54 -07001206 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1207 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001208 CHECK_NON_NULL_ARGUMENT(java_class);
1209 CHECK_NON_NULL_ARGUMENT(name);
1210 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001211 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001212 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001214
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001215 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001216 CHECK_NON_NULL_ARGUMENT(obj);
1217 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001218 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001219 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1220 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001221 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001223
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001224 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001225 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::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001228 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001231 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001232 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1233 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001234 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001235 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1236 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1237 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001238 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001241 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001242 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001243 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001244 mirror::Object* 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>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001249#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001250 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1251 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001252 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001253 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1254 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001255 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001256
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001257#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001258 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001259 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001260 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001261 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001262
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001263#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001264 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1265 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001266 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001267 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1268 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001269 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001270
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001271#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001272 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001273 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001274 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001275 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001276
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001277 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001278 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 }
1280
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001281 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001282 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 }
1284
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001285 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001286 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001289 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001290 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001293 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001294 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001297 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001298 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001301 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001302 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001305 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001306 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001309 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001310 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 }
1312
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001313 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001314 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001317 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001318 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001321 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001322 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001325 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001326 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001329 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001330 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001331 }
1332
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001333 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001334 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001335 }
1336
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001337 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001338 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001339 }
1340
1341 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001342 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001343 }
1344
1345 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001346 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001347 }
1348
1349 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001350 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001351 }
1352
1353 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001354 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001355 }
1356
1357 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001358 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001359 }
1360
1361 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001362 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001363 }
1364
1365 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001366 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001367 }
1368
1369 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001370 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001371 }
1372
1373 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001374 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001375 }
1376
1377 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001378 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001379 }
1380
1381 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001382 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001383 }
1384
1385 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001386 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001387 }
1388
1389 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001390 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001391 }
1392
1393 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001394 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001395 }
1396
1397 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001398 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399 }
1400
1401 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001402 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 }
1404
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001405 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001407 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001408 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001409 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001410 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001411 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 va_end(ap);
1413 return local_result;
1414 }
1415
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001416 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001417 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001418 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001419 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001420 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001423 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001424 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001425 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001426 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001427 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001430 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001432 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001433 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001434 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001435 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001437 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001440 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001441 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001442 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001443 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001446 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001447 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001448 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001449 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001452 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001454 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001455 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001456 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001457 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001459 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001462 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001463 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001464 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001465 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001468 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001469 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001470 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001471 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001474 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001476 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001477 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001478 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001479 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001481 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001484 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001485 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001486 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001487 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 }
1489
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001490 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001491 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001492 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001493 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001496 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001498 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001499 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001500 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001501 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001503 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001506 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001507 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001508 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001509 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
1511
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001512 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001513 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001514 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001515 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001518 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001520 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001521 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001522 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001523 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001525 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001528 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001529 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001530 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001531 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
1533
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001534 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001535 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001536 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001537 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001540 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001542 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001543 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001544 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001545 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001547 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 }
1549
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001550 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001551 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001552 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001553 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001556 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001557 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001558 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001559 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 }
1561
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001562 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001564 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001565 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001566 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001567 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001569 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001572 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001573 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001574 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001575 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 }
1577
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001578 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001579 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001580 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001581 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001584 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001586 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001587 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001588 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001589 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001591 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001594 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001595 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001596 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001597 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001600 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001601 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001602 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001603 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 }
1605
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001606 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001608 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001609 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001610 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001611 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 va_end(ap);
1613 }
1614
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001615 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001616 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001617 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001618 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001621 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001622 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001623 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001624 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 }
1626
Elliott Hughes814e4032011-08-23 12:07:56 -07001627 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001628 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001629 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001630 return nullptr;
1631 }
1632 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001633 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001634 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001635 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001636 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001637 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001638 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
1641 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001642 if (utf == nullptr) {
1643 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001645 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001646 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001647 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes814e4032011-08-23 12:07:56 -07001650 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001651 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001652 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001653 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001654 }
1655
1656 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001657 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001658 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001659 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001660 }
1661
Ian Rogersbc939662013-08-15 10:26:54 -07001662 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1663 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001664 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001665 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001666 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001667 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001668 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001669 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001670 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001671 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1672 memcpy(buf, chars + start, length * sizeof(jchar));
1673 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001674 }
1675
Ian Rogersbc939662013-08-15 10:26:54 -07001676 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1677 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001678 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001679 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001680 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001681 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001682 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001683 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001684 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001685 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1686 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1687 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001688 }
1689
Elliott Hughes75770752011-08-24 17:52:38 -07001690 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001691 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001692 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001693 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1694 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers68d8b422014-07-17 11:09:10 -07001695 soa.Vm()->PinPrimitiveArray(soa.Self(), chars);
Fred Shih56890e22014-06-02 11:11:52 -07001696 gc::Heap* heap = Runtime::Current()->GetHeap();
1697 if (heap->IsMovableObject(chars)) {
1698 if (is_copy != nullptr) {
1699 *is_copy = JNI_TRUE;
1700 }
1701 int32_t char_count = s->GetLength();
1702 int32_t offset = s->GetOffset();
1703 jchar* bytes = new jchar[char_count];
1704 for (int32_t i = 0; i < char_count; i++) {
1705 bytes[i] = chars->Get(i + offset);
1706 }
1707 return bytes;
1708 } else {
1709 if (is_copy != nullptr) {
1710 *is_copy = JNI_FALSE;
1711 }
1712 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001713 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001714 }
1715
Mathieu Chartier590fee92013-09-13 13:46:47 -07001716 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001717 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001718 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001719 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1720 mirror::CharArray* s_chars = s->GetCharArray();
1721 if (chars != (s_chars->GetData() + s->GetOffset())) {
1722 delete[] chars;
1723 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001724 soa.Vm()->UnpinPrimitiveArray(soa.Self(), s->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
Elliott Hughes75770752011-08-24 17:52:38 -07001727 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001728 CHECK_NON_NULL_ARGUMENT(java_string);
1729 ScopedObjectAccess soa(env);
1730 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1731 mirror::CharArray* chars = s->GetCharArray();
1732 int32_t offset = s->GetOffset();
Ian Rogers68d8b422014-07-17 11:09:10 -07001733 soa.Vm()->PinPrimitiveArray(soa.Self(), chars);
Fred Shih56890e22014-06-02 11:11:52 -07001734 gc::Heap* heap = Runtime::Current()->GetHeap();
1735 if (heap->IsMovableObject(chars)) {
1736 StackHandleScope<1> hs(soa.Self());
1737 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
1738 heap->IncrementDisableMovingGC(soa.Self());
1739 }
1740 if (is_copy != nullptr) {
1741 *is_copy = JNI_FALSE;
1742 }
1743 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
Elliott Hughes75770752011-08-24 17:52:38 -07001746 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07001747 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1748 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -07001749 soa.Vm()->UnpinPrimitiveArray(soa.Self(),
1750 soa.Decode<mirror::String*>(java_string)->GetCharArray());
Fred Shih56890e22014-06-02 11:11:52 -07001751 gc::Heap* heap = Runtime::Current()->GetHeap();
1752 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1753 mirror::CharArray* s_chars = s->GetCharArray();
1754 if (heap->IsMovableObject(s_chars)) {
1755 heap->DecrementDisableMovingGC(soa.Self());
1756 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 }
1758
Elliott Hughes75770752011-08-24 17:52:38 -07001759 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001760 if (java_string == nullptr) {
1761 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001762 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001763 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001764 *is_copy = JNI_TRUE;
1765 }
Ian Rogersef28b142012-11-30 14:22:18 -08001766 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001767 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001768 size_t byte_count = s->GetUtfLength();
1769 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001770 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001771 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1772 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1773 bytes[byte_count] = '\0';
1774 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001775 }
1776
Elliott Hughes75770752011-08-24 17:52:38 -07001777 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001778 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001779 }
1780
Elliott Hughesbd935992011-08-22 11:59:34 -07001781 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001782 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001783 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001784 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001785 if (UNLIKELY(!obj->IsArrayInstance())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001786 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1787 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001788 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001789 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001790 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
Elliott Hughes814e4032011-08-23 12:07:56 -07001793 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001794 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001795 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001796 mirror::ObjectArray<mirror::Object>* array =
1797 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001798 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
Ian Rogersbc939662013-08-15 10:26:54 -07001801 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1802 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001803 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001804 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001805 mirror::ObjectArray<mirror::Object>* array =
1806 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1807 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001808 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
1811 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001812 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
1815 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001816 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 }
1818
1819 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001820 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
1823 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001824 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
1827 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001828 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
1831 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001832 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 }
1834
1835 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001836 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
Ian Rogers1d99e452014-01-02 17:36:41 -08001839 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1840 jobject initial_element) {
1841 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001842 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001843 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001844 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001845 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001846
1847 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001848 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001849 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08001850 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001851 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08001852 if (UNLIKELY(element_class->IsPrimitive())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001853 soa.Vm()->JniAbortF("NewObjectArray", "not an object type: %s",
1854 PrettyDescriptor(element_class).c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08001855 return nullptr;
1856 }
Ian Rogers1d99e452014-01-02 17:36:41 -08001857 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07001858 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08001859 if (UNLIKELY(array_class == nullptr)) {
1860 return nullptr;
1861 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 }
1863
Elliott Hughes75770752011-08-24 17:52:38 -07001864 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001865 mirror::ObjectArray<mirror::Object>* result =
1866 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001867 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001868 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08001869 if (initial_object != nullptr) {
1870 mirror::Class* element_class = result->GetClass()->GetComponentType();
1871 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001872 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
1873 "element type of '%s'",
1874 PrettyDescriptor(initial_object->GetClass()).c_str(),
1875 PrettyDescriptor(element_class).c_str());
1876 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08001877 } else {
1878 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001879 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08001880 }
1881 }
Elliott Hughes75770752011-08-24 17:52:38 -07001882 }
1883 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 }
1886
1887 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001888 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 }
1890
Ian Rogersa15e67d2012-02-28 13:51:55 -08001891 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001892 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001893 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001894 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07001895 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001896 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
1897 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001898 return nullptr;
1899 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001900 gc::Heap* heap = Runtime::Current()->GetHeap();
1901 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08001902 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001903 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001904 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001905 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001906 soa.Vm()->PinPrimitiveArray(soa.Self(), array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001907 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001908 *is_copy = JNI_FALSE;
1909 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001910 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001911 }
1912
Ian Rogers2d10b202014-05-12 19:15:18 -07001913 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
1914 jint mode) {
1915 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
1916 ScopedObjectAccess soa(env);
1917 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
1918 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001919 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
1920 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001921 return;
1922 }
1923 const size_t component_size = array->GetClass()->GetComponentSize();
1924 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001925 }
1926
Elliott Hughes75770752011-08-24 17:52:38 -07001927 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001928 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001929 }
1930
Elliott Hughes75770752011-08-24 17:52:38 -07001931 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001932 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes75770752011-08-24 17:52:38 -07001935 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001936 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001937 }
1938
Elliott Hughes75770752011-08-24 17:52:38 -07001939 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001940 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001944 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes75770752011-08-24 17:52:38 -07001947 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001948 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
Elliott Hughes75770752011-08-24 17:52:38 -07001951 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001952 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Elliott Hughes75770752011-08-24 17:52:38 -07001955 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001956 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 }
1958
Mathieu Chartier590fee92013-09-13 13:46:47 -07001959 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
1960 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001961 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
1962 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 }
1964
Mathieu Chartier590fee92013-09-13 13:46:47 -07001965 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001966 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 }
1968
Mathieu Chartier590fee92013-09-13 13:46:47 -07001969 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001970 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Mathieu Chartier590fee92013-09-13 13:46:47 -07001973 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
1974 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001975 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Mathieu Chartier590fee92013-09-13 13:46:47 -07001978 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
1979 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001980 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Mathieu Chartier590fee92013-09-13 13:46:47 -07001983 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001984 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 }
1986
Mathieu Chartier590fee92013-09-13 13:46:47 -07001987 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001988 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Mathieu Chartier590fee92013-09-13 13:46:47 -07001991 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
1992 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001993 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Ian Rogersbc939662013-08-15 10:26:54 -07001996 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
1997 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001998 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001999 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
Ian Rogersbc939662013-08-15 10:26:54 -07002002 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2003 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002004 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 }
2006
Ian Rogersbc939662013-08-15 10:26:54 -07002007 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2008 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002009 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Ian Rogersbc939662013-08-15 10:26:54 -07002012 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2013 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002014 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002015 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Ian Rogersbc939662013-08-15 10:26:54 -07002018 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2019 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002020 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002021 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Ian Rogersbc939662013-08-15 10:26:54 -07002024 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2025 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002026 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Ian Rogersbc939662013-08-15 10:26:54 -07002029 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2030 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002031 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Ian Rogersbc939662013-08-15 10:26:54 -07002034 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2035 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002036 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002037 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Ian Rogersbc939662013-08-15 10:26:54 -07002040 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2041 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002042 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002043 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Ian Rogersbc939662013-08-15 10:26:54 -07002046 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2047 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002048 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Ian Rogersbc939662013-08-15 10:26:54 -07002051 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2052 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002053 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Ian Rogersbc939662013-08-15 10:26:54 -07002056 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2057 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002058 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002059 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Ian Rogersbc939662013-08-15 10:26:54 -07002062 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2063 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002064 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002065 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Ian Rogersbc939662013-08-15 10:26:54 -07002068 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2069 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002070 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Ian Rogersbc939662013-08-15 10:26:54 -07002073 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2074 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002075 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Ian Rogersbc939662013-08-15 10:26:54 -07002078 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2079 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002080 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002081 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Ian Rogersbc939662013-08-15 10:26:54 -07002084 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2085 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002086 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2087 }
2088
Ian Rogersbc939662013-08-15 10:26:54 -07002089 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2090 jint method_count, bool return_errors) {
2091 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002092 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2093 method_count);
2094 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002095 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002096 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002097 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002098 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002099 if (UNLIKELY(method_count == 0)) {
2100 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2101 << PrettyDescriptor(c);
2102 return JNI_OK;
2103 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002104 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002105 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 const char* name = methods[i].name;
2107 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002108 const void* fnPtr = methods[i].fnPtr;
2109 if (UNLIKELY(name == nullptr)) {
2110 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2111 return JNI_ERR;
2112 } else if (UNLIKELY(sig == nullptr)) {
2113 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2114 return JNI_ERR;
2115 } else if (UNLIKELY(fnPtr == nullptr)) {
2116 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2117 return JNI_ERR;
2118 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002119 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002121 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 ++sig;
2123 }
2124
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002125 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2126 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002127 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002129 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002130 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002131 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002132 << PrettyDescriptor(c) << "." << name << sig << " in "
2133 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002134 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002136 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002137 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002138 << PrettyDescriptor(c) << "." << name << sig
2139 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002140 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 return JNI_ERR;
2142 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002144 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002145
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002146 m->RegisterNative(soa.Self(), fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148 return JNI_OK;
2149 }
2150
Elliott Hughes5174fe62011-08-23 15:12:35 -07002151 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002152 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002153 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002154 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002156 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002157
Ian Rogers2d10b202014-05-12 19:15:18 -07002158 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002159 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002160 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002161 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002162 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002163 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002164 }
2165 }
2166 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002167 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002168 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002169 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002170 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002171 }
2172 }
2173
Ian Rogers2d10b202014-05-12 19:15:18 -07002174 if (unregistered_count == 0) {
2175 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2176 << PrettyDescriptor(c) << "' that contains no native methods";
2177 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002178 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
2180
Ian Rogers719d1a32014-03-06 12:13:39 -08002181 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002182 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002183 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002184 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2185 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002186 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002187 return JNI_ERR;
2188 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002189 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002190 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Ian Rogers719d1a32014-03-06 12:13:39 -08002193 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002194 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002195 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002196 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002197 o->MonitorExit(soa.Self());
2198 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002199 return JNI_ERR;
2200 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002201 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002202 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 }
2204
2205 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002206 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002208 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002209 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002211 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002213 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 }
2215
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002217 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002218 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2219 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002220 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002221 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002222 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002223 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2224 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002225 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002226 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002227
Brian Carlstrom85a93362014-06-25 09:30:52 -07002228 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002229 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002230 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2231 "buffer capacity greater than maximum jint: %" PRId64,
2232 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002233 return nullptr;
2234 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002235 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002236 jint capacity_arg = static_cast<jint>(capacity);
2237
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002238 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2239 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002240 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002241 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Elliott Hughesb465ab02011-08-24 11:21:21 -07002244 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002245 return reinterpret_cast<void*>(env->GetLongField(
2246 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 }
2248
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002250 return static_cast<jlong>(env->GetIntField(
2251 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002252 }
2253
Elliott Hughesb465ab02011-08-24 11:21:21 -07002254 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002255 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002256
2257 // Do we definitely know what kind of reference this is?
2258 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2259 IndirectRefKind kind = GetIndirectRefKind(ref);
2260 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002261 case kLocal: {
2262 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002263 // The local refs don't need a read barrier.
2264 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2265 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002266 return JNILocalRefType;
2267 }
2268 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002269 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002270 case kGlobal:
2271 return JNIGlobalRefType;
2272 case kWeakGlobal:
2273 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002274 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002275 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002276 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002277 return JNILocalRefType;
2278 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002279 return JNIInvalidRefType;
2280 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002281 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2282 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002283 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002284
2285 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002286 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2287 const char* caller)
2288 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002289 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002290 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002291 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2292 return JNI_ERR;
2293 }
2294 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002295 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002296 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2297 if (!okay) {
2298 soa.Self()->ThrowOutOfMemoryError(caller);
2299 }
2300 return okay ? JNI_OK : JNI_ERR;
2301 }
2302
2303 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002304 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002305 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002306 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002307 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002308 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002309 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002310 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002311 return soa.AddLocalReference<JniT>(result);
2312 }
2313
Ian Rogers2d10b202014-05-12 19:15:18 -07002314 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2315 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2316 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002317 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002318 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002319 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002320 soa.Vm()->JniAbortF(fn_name,
2321 "attempt to %s %s primitive array elements with an object of type %s",
2322 operation,
2323 PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2324 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002325 return nullptr;
2326 }
2327 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2328 return array;
2329 }
2330
2331 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2332 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2333 CHECK_NON_NULL_ARGUMENT(java_array);
2334 ScopedObjectAccess soa(env);
2335 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2336 "GetArrayElements",
2337 "get");
2338 if (UNLIKELY(array == nullptr)) {
2339 return nullptr;
2340 }
Ian Rogers68d8b422014-07-17 11:09:10 -07002341 soa.Vm()->PinPrimitiveArray(soa.Self(), array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002342 // Only make a copy if necessary.
2343 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2344 if (is_copy != nullptr) {
2345 *is_copy = JNI_TRUE;
2346 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002347 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002348 size_t size = array->GetLength() * component_size;
2349 void* data = new uint64_t[RoundUp(size, 8) / 8];
2350 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002351 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002352 } else {
2353 if (is_copy != nullptr) {
2354 *is_copy = JNI_FALSE;
2355 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002356 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002357 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002358 }
2359
Ian Rogers2d10b202014-05-12 19:15:18 -07002360 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002361 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002362 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002363 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002364 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2365 "ReleaseArrayElements",
2366 "release");
2367 if (array == nullptr) {
2368 return;
2369 }
2370 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2371 }
2372
2373 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2374 size_t component_size, void* elements, jint mode)
2375 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002376 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002377 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002378 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002379 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002380 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2381 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002382 if (is_copy) {
2383 // Sanity check: If elements is not the same as the java array's data, it better not be a
2384 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2385 // copies we make?
2386 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002387 soa.Vm()->JniAbortF("ReleaseArrayElements",
2388 "invalid element pointer %p, array elements are %p",
2389 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002390 return;
2391 }
2392 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002393 // Don't need to copy if we had a direct pointer.
2394 if (mode != JNI_ABORT && is_copy) {
2395 memcpy(array_data, elements, bytes);
2396 }
2397 if (mode != JNI_COMMIT) {
2398 if (is_copy) {
2399 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002400 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002401 // Non copy to a movable object must means that we had disabled the moving GC.
2402 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002403 }
Ian Rogers68d8b422014-07-17 11:09:10 -07002404 soa.Vm()->UnpinPrimitiveArray(soa.Self(), array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002405 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002406 }
2407
Ian Rogers2d10b202014-05-12 19:15:18 -07002408 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2409 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2410 jsize start, jsize length, ElementT* buf) {
2411 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2412 ScopedObjectAccess soa(env);
2413 ArtArrayT* array =
2414 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2415 "GetPrimitiveArrayRegion",
2416 "get region of");
2417 if (array != nullptr) {
2418 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2419 ThrowAIOOBE(soa, array, start, length, "src");
2420 } else {
2421 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2422 ElementT* data = array->GetData();
2423 memcpy(buf, data + start, length * sizeof(ElementT));
2424 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002425 }
2426 }
2427
Ian Rogers2d10b202014-05-12 19:15:18 -07002428 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2429 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2430 jsize start, jsize length, const ElementT* buf) {
2431 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2432 ScopedObjectAccess soa(env);
2433 ArtArrayT* array =
2434 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2435 "SetPrimitiveArrayRegion",
2436 "set region of");
2437 if (array != nullptr) {
2438 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2439 ThrowAIOOBE(soa, array, start, length, "dst");
2440 } else {
2441 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2442 ElementT* data = array->GetData();
2443 memcpy(data + start, buf, length * sizeof(ElementT));
2444 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002445 }
2446 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002447};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002448
Elliott Hughes88c5c352012-03-15 18:49:48 -07002449const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002450 nullptr, // reserved0.
2451 nullptr, // reserved1.
2452 nullptr, // reserved2.
2453 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002454 JNI::GetVersion,
2455 JNI::DefineClass,
2456 JNI::FindClass,
2457 JNI::FromReflectedMethod,
2458 JNI::FromReflectedField,
2459 JNI::ToReflectedMethod,
2460 JNI::GetSuperclass,
2461 JNI::IsAssignableFrom,
2462 JNI::ToReflectedField,
2463 JNI::Throw,
2464 JNI::ThrowNew,
2465 JNI::ExceptionOccurred,
2466 JNI::ExceptionDescribe,
2467 JNI::ExceptionClear,
2468 JNI::FatalError,
2469 JNI::PushLocalFrame,
2470 JNI::PopLocalFrame,
2471 JNI::NewGlobalRef,
2472 JNI::DeleteGlobalRef,
2473 JNI::DeleteLocalRef,
2474 JNI::IsSameObject,
2475 JNI::NewLocalRef,
2476 JNI::EnsureLocalCapacity,
2477 JNI::AllocObject,
2478 JNI::NewObject,
2479 JNI::NewObjectV,
2480 JNI::NewObjectA,
2481 JNI::GetObjectClass,
2482 JNI::IsInstanceOf,
2483 JNI::GetMethodID,
2484 JNI::CallObjectMethod,
2485 JNI::CallObjectMethodV,
2486 JNI::CallObjectMethodA,
2487 JNI::CallBooleanMethod,
2488 JNI::CallBooleanMethodV,
2489 JNI::CallBooleanMethodA,
2490 JNI::CallByteMethod,
2491 JNI::CallByteMethodV,
2492 JNI::CallByteMethodA,
2493 JNI::CallCharMethod,
2494 JNI::CallCharMethodV,
2495 JNI::CallCharMethodA,
2496 JNI::CallShortMethod,
2497 JNI::CallShortMethodV,
2498 JNI::CallShortMethodA,
2499 JNI::CallIntMethod,
2500 JNI::CallIntMethodV,
2501 JNI::CallIntMethodA,
2502 JNI::CallLongMethod,
2503 JNI::CallLongMethodV,
2504 JNI::CallLongMethodA,
2505 JNI::CallFloatMethod,
2506 JNI::CallFloatMethodV,
2507 JNI::CallFloatMethodA,
2508 JNI::CallDoubleMethod,
2509 JNI::CallDoubleMethodV,
2510 JNI::CallDoubleMethodA,
2511 JNI::CallVoidMethod,
2512 JNI::CallVoidMethodV,
2513 JNI::CallVoidMethodA,
2514 JNI::CallNonvirtualObjectMethod,
2515 JNI::CallNonvirtualObjectMethodV,
2516 JNI::CallNonvirtualObjectMethodA,
2517 JNI::CallNonvirtualBooleanMethod,
2518 JNI::CallNonvirtualBooleanMethodV,
2519 JNI::CallNonvirtualBooleanMethodA,
2520 JNI::CallNonvirtualByteMethod,
2521 JNI::CallNonvirtualByteMethodV,
2522 JNI::CallNonvirtualByteMethodA,
2523 JNI::CallNonvirtualCharMethod,
2524 JNI::CallNonvirtualCharMethodV,
2525 JNI::CallNonvirtualCharMethodA,
2526 JNI::CallNonvirtualShortMethod,
2527 JNI::CallNonvirtualShortMethodV,
2528 JNI::CallNonvirtualShortMethodA,
2529 JNI::CallNonvirtualIntMethod,
2530 JNI::CallNonvirtualIntMethodV,
2531 JNI::CallNonvirtualIntMethodA,
2532 JNI::CallNonvirtualLongMethod,
2533 JNI::CallNonvirtualLongMethodV,
2534 JNI::CallNonvirtualLongMethodA,
2535 JNI::CallNonvirtualFloatMethod,
2536 JNI::CallNonvirtualFloatMethodV,
2537 JNI::CallNonvirtualFloatMethodA,
2538 JNI::CallNonvirtualDoubleMethod,
2539 JNI::CallNonvirtualDoubleMethodV,
2540 JNI::CallNonvirtualDoubleMethodA,
2541 JNI::CallNonvirtualVoidMethod,
2542 JNI::CallNonvirtualVoidMethodV,
2543 JNI::CallNonvirtualVoidMethodA,
2544 JNI::GetFieldID,
2545 JNI::GetObjectField,
2546 JNI::GetBooleanField,
2547 JNI::GetByteField,
2548 JNI::GetCharField,
2549 JNI::GetShortField,
2550 JNI::GetIntField,
2551 JNI::GetLongField,
2552 JNI::GetFloatField,
2553 JNI::GetDoubleField,
2554 JNI::SetObjectField,
2555 JNI::SetBooleanField,
2556 JNI::SetByteField,
2557 JNI::SetCharField,
2558 JNI::SetShortField,
2559 JNI::SetIntField,
2560 JNI::SetLongField,
2561 JNI::SetFloatField,
2562 JNI::SetDoubleField,
2563 JNI::GetStaticMethodID,
2564 JNI::CallStaticObjectMethod,
2565 JNI::CallStaticObjectMethodV,
2566 JNI::CallStaticObjectMethodA,
2567 JNI::CallStaticBooleanMethod,
2568 JNI::CallStaticBooleanMethodV,
2569 JNI::CallStaticBooleanMethodA,
2570 JNI::CallStaticByteMethod,
2571 JNI::CallStaticByteMethodV,
2572 JNI::CallStaticByteMethodA,
2573 JNI::CallStaticCharMethod,
2574 JNI::CallStaticCharMethodV,
2575 JNI::CallStaticCharMethodA,
2576 JNI::CallStaticShortMethod,
2577 JNI::CallStaticShortMethodV,
2578 JNI::CallStaticShortMethodA,
2579 JNI::CallStaticIntMethod,
2580 JNI::CallStaticIntMethodV,
2581 JNI::CallStaticIntMethodA,
2582 JNI::CallStaticLongMethod,
2583 JNI::CallStaticLongMethodV,
2584 JNI::CallStaticLongMethodA,
2585 JNI::CallStaticFloatMethod,
2586 JNI::CallStaticFloatMethodV,
2587 JNI::CallStaticFloatMethodA,
2588 JNI::CallStaticDoubleMethod,
2589 JNI::CallStaticDoubleMethodV,
2590 JNI::CallStaticDoubleMethodA,
2591 JNI::CallStaticVoidMethod,
2592 JNI::CallStaticVoidMethodV,
2593 JNI::CallStaticVoidMethodA,
2594 JNI::GetStaticFieldID,
2595 JNI::GetStaticObjectField,
2596 JNI::GetStaticBooleanField,
2597 JNI::GetStaticByteField,
2598 JNI::GetStaticCharField,
2599 JNI::GetStaticShortField,
2600 JNI::GetStaticIntField,
2601 JNI::GetStaticLongField,
2602 JNI::GetStaticFloatField,
2603 JNI::GetStaticDoubleField,
2604 JNI::SetStaticObjectField,
2605 JNI::SetStaticBooleanField,
2606 JNI::SetStaticByteField,
2607 JNI::SetStaticCharField,
2608 JNI::SetStaticShortField,
2609 JNI::SetStaticIntField,
2610 JNI::SetStaticLongField,
2611 JNI::SetStaticFloatField,
2612 JNI::SetStaticDoubleField,
2613 JNI::NewString,
2614 JNI::GetStringLength,
2615 JNI::GetStringChars,
2616 JNI::ReleaseStringChars,
2617 JNI::NewStringUTF,
2618 JNI::GetStringUTFLength,
2619 JNI::GetStringUTFChars,
2620 JNI::ReleaseStringUTFChars,
2621 JNI::GetArrayLength,
2622 JNI::NewObjectArray,
2623 JNI::GetObjectArrayElement,
2624 JNI::SetObjectArrayElement,
2625 JNI::NewBooleanArray,
2626 JNI::NewByteArray,
2627 JNI::NewCharArray,
2628 JNI::NewShortArray,
2629 JNI::NewIntArray,
2630 JNI::NewLongArray,
2631 JNI::NewFloatArray,
2632 JNI::NewDoubleArray,
2633 JNI::GetBooleanArrayElements,
2634 JNI::GetByteArrayElements,
2635 JNI::GetCharArrayElements,
2636 JNI::GetShortArrayElements,
2637 JNI::GetIntArrayElements,
2638 JNI::GetLongArrayElements,
2639 JNI::GetFloatArrayElements,
2640 JNI::GetDoubleArrayElements,
2641 JNI::ReleaseBooleanArrayElements,
2642 JNI::ReleaseByteArrayElements,
2643 JNI::ReleaseCharArrayElements,
2644 JNI::ReleaseShortArrayElements,
2645 JNI::ReleaseIntArrayElements,
2646 JNI::ReleaseLongArrayElements,
2647 JNI::ReleaseFloatArrayElements,
2648 JNI::ReleaseDoubleArrayElements,
2649 JNI::GetBooleanArrayRegion,
2650 JNI::GetByteArrayRegion,
2651 JNI::GetCharArrayRegion,
2652 JNI::GetShortArrayRegion,
2653 JNI::GetIntArrayRegion,
2654 JNI::GetLongArrayRegion,
2655 JNI::GetFloatArrayRegion,
2656 JNI::GetDoubleArrayRegion,
2657 JNI::SetBooleanArrayRegion,
2658 JNI::SetByteArrayRegion,
2659 JNI::SetCharArrayRegion,
2660 JNI::SetShortArrayRegion,
2661 JNI::SetIntArrayRegion,
2662 JNI::SetLongArrayRegion,
2663 JNI::SetFloatArrayRegion,
2664 JNI::SetDoubleArrayRegion,
2665 JNI::RegisterNatives,
2666 JNI::UnregisterNatives,
2667 JNI::MonitorEnter,
2668 JNI::MonitorExit,
2669 JNI::GetJavaVM,
2670 JNI::GetStringRegion,
2671 JNI::GetStringUTFRegion,
2672 JNI::GetPrimitiveArrayCritical,
2673 JNI::ReleasePrimitiveArrayCritical,
2674 JNI::GetStringCritical,
2675 JNI::ReleaseStringCritical,
2676 JNI::NewWeakGlobalRef,
2677 JNI::DeleteWeakGlobalRef,
2678 JNI::ExceptionCheck,
2679 JNI::NewDirectByteBuffer,
2680 JNI::GetDirectBufferAddress,
2681 JNI::GetDirectBufferCapacity,
2682 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002683};
2684
Ian Rogers68d8b422014-07-17 11:09:10 -07002685const JNINativeInterface* GetJniNativeInterface() {
2686 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002687}
2688
Elliott Hughesc8fece32013-01-02 11:27:23 -08002689void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002690 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002691 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002692 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002693 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2694 }
2695 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2696}
2697
Ian Rogersdf20fe02011-07-20 20:34:16 -07002698} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002699
2700std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2701 switch (rhs) {
2702 case JNIInvalidRefType:
2703 os << "JNIInvalidRefType";
2704 return os;
2705 case JNILocalRefType:
2706 os << "JNILocalRefType";
2707 return os;
2708 case JNIGlobalRefType:
2709 os << "JNIGlobalRefType";
2710 return os;
2711 case JNIWeakGlobalRefType:
2712 os << "JNIWeakGlobalRefType";
2713 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002714 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002715 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002716 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002717 }
2718}