blob: b7d485e5207a37be3d806358269c80d592cfed66 [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();
Ian Rogers1ff3c982014-08-12 02:30:58 -070087 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -080088 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
89 "no %s method \"%s.%s%s\"",
Ian Rogers1ff3c982014-08-12 02:30:58 -070090 kind, c->GetDescriptor(&temp), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -070091}
92
Sebastien Hertzfa65e842014-07-03 09:39:53 +020093static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa, mirror::Class* c,
94 const char* kind, jint idx, bool return_errors)
95 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
96 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method in "
97 << PrettyDescriptor(c) << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
98 << ": " << kind << " is null at index " << idx;
99 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
100 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
101 "%s is null at index %d", kind, idx);
102}
103
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800104static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
106 if (LIKELY(klass->IsInitialized())) {
107 return klass;
108 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700109 StackHandleScope<1> hs(self);
110 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
111 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800112 return nullptr;
113 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700114 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800115}
116
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700117static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
118 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800120 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800121 if (c == nullptr) {
122 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700123 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800124 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700125 if (is_static) {
126 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700127 } else if (c->IsInterface()) {
128 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700129 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700130 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800131 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700132 // No virtual method matching the signature. Search declared
133 // private methods and constructors.
134 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700135 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700136 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800137 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800139 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700140 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700142}
143
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800144static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800146 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700147 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
148 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700149 return soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700150 }
Brian Carlstromce888532013-10-10 00:32:58 -0700151 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800152 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700153 return method->GetDeclaringClass()->GetClassLoader();
154 }
155 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800156 mirror::ClassLoader* class_loader =
157 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
158 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700159 return class_loader;
160 }
161 // See if the override ClassLoader is set for gtests.
Ian Rogers68d8b422014-07-17 11:09:10 -0700162 class_loader = soa.Decode<mirror::ClassLoader*>(soa.Self()->GetClassLoaderOverride());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800163 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800164 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700165 CHECK(Runtime::Current()->UseCompileTimeClassPath());
166 return class_loader;
167 }
168 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800169 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700170}
171
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700172static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
173 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700175 StackHandleScope<2> hs(soa.Self());
176 Handle<mirror::Class> c(
177 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
178 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800179 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700180 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800181 mirror::ArtField* field = nullptr;
182 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700183 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
184 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700185 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800186 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700187 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700188 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700189 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800190 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700191 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700192 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800193 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700194 StackHandleScope<1> hs(soa.Self());
195 Handle<mirror::Throwable> cause(hs.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700196 soa.Self()->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700197 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800199 "no type \"%s\" found and so no field \"%s\" "
200 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700201 c->GetDescriptor(&temp));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700202 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800203 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700204 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700205 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700207 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700208 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700209 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700210 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800212 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800213 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
214 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
215 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700216 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800217 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700218 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700220}
221
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800222static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700224 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700225 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800226 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
227 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
228 "%s offset=%d length=%d %s.length=%d",
229 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700230}
Ian Rogers0571d352011-11-03 19:51:38 -0700231
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
233 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800235 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
236 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
237 "offset=%d length=%d string.length()=%d", start, length,
238 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700239}
Elliott Hughes814e4032011-08-23 12:07:56 -0700240
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700241int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 // Turn the const char* into a java.lang.String.
244 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800245 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700247 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700248
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 // Choose an appropriate constructor and set up the arguments.
250 jvalue args[2];
251 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800252 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 signature = "()V";
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/String;)V";
256 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800257 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 signature = "(Ljava/lang/Throwable;)V";
259 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700260 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
262 args[0].l = s.get();
263 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700264 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800266 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800267 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800269 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 return JNI_ERR;
271 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700272
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800273 ScopedLocalRef<jthrowable> exception(
274 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
275 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276 return JNI_ERR;
277 }
Ian Rogersef28b142012-11-30 14:22:18 -0800278 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800279 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800280 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700282}
283
Ian Rogers68d8b422014-07-17 11:09:10 -0700284static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
285 return reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughes75770752011-08-24 17:52:38 -0700286}
287
Ian Rogers2d10b202014-05-12 19:15:18 -0700288#define CHECK_NON_NULL_ARGUMENT(value) \
289 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700290
Ian Rogers2d10b202014-05-12 19:15:18 -0700291#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
292 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
293
294#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
295 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
296
297#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
298 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
299
300#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800301 if (UNLIKELY(value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700302 JavaVmExtFromEnv(env)->JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700303 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700304 }
305
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700306#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800307 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700308 JavaVmExtFromEnv(env)->JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700309 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700310 }
311
Elliott Hughescdf53122011-08-19 15:46:09 -0700312class JNI {
313 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700314 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700315 return JNI_VERSION_1_6;
316 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700317
Ian Rogers25e8b912012-09-07 11:31:36 -0700318 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700319 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800320 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700321 }
322
Elliott Hughescdf53122011-08-19 15:46:09 -0700323 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700324 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700325 Runtime* runtime = Runtime::Current();
326 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700327 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700328 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800329 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700330 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700331 StackHandleScope<1> hs(soa.Self());
332 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800333 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700334 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800335 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700336 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700337 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700338 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700339
Ian Rogers62f05122014-03-21 11:21:29 -0700340 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700341 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700343 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700344 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700345
Ian Rogers62f05122014-03-21 11:21:29 -0700346 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700347 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700348 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700349 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700350 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700351
Elliott Hughescdf53122011-08-19 15:46:09 -0700352 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700353 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800355 mirror::ArtMethod* m = soa.DecodeMethod(mid);
356 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700357 jobject art_method = soa.AddLocalReference<jobject>(m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200358 jobject reflect_method;
359 if (m->IsConstructor()) {
360 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
361 } else {
362 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
363 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700364 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800365 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700366 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800367 SetObjectField(env, reflect_method,
368 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700369 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700370 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700371
Elliott Hughescdf53122011-08-19 15:46:09 -0700372 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700373 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800375 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700376 jobject art_field = soa.AddLocalReference<jobject>(f);
377 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
378 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800379 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700380 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800381 SetObjectField(env, reflect_field,
382 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700383 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700384 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700385
Elliott Hughes37f7a402011-08-22 18:56:01 -0700386 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700387 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700388 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800389 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700391 }
392
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700393 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700394 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700395 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800396 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700397 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700398 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700399
Narayan Kamath1268b742014-07-11 19:15:11 +0100400 // Note: java_class1 should be safely castable to java_class2, and
401 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700402 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700403 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
404 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700405 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800406 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
407 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100408 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700409 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700410
Elliott Hughese84278b2012-03-22 10:06:53 -0700411 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700412 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800413 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700414 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700415 return JNI_TRUE;
416 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700417 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800418 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
419 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700420 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700421 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700422 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700423
Elliott Hughes37f7a402011-08-22 18:56:01 -0700424 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700425 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800426 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
427 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700428 return JNI_ERR;
429 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800430 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
431 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700432 return JNI_OK;
433 }
434
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700435 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700436 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800437 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700438 }
439
440 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700441 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700442 }
443
444 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700445 ScopedObjectAccess soa(env);
446 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700447 }
448
449 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700450 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700451
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700452 // If we have no exception to describe, pass through.
453 if (!soa.Self()->GetException(nullptr)) {
454 return;
455 }
456
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700457 StackHandleScope<3> hs(soa.Self());
458 // TODO: Use nullptr instead of null handles?
459 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
460 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
461 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800462 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200463 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800464 {
465 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800466 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700467 old_throw_this_object.Assign(old_throw_location.GetThis());
468 old_throw_method.Assign(old_throw_location.GetMethod());
469 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800470 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200471 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800472 soa.Self()->ClearException();
473 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800474 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700475 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700476 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
477 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800478 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700479 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700480 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700481 } else {
482 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800483 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800484 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700485 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800486 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700487 }
488 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700489 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800490 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700491
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700492 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200493 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700494 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700495
Elliott Hughescdf53122011-08-19 15:46:09 -0700496 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700497 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800498 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700499 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700500 }
501
Ian Rogers25e8b912012-09-07 11:31:36 -0700502 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700503 LOG(FATAL) << "JNI FatalError called: " << msg;
504 }
505
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700506 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700507 // TODO: SOA may not be necessary but I do it to please lock annotations.
508 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700509 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700510 return JNI_ERR;
511 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700512 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700513 return JNI_OK;
514 }
515
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700516 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700517 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800518 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700519 soa.Env()->PopFrame();
520 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700521 }
522
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700523 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700524 // TODO: SOA may not be necessary but I do it to please lock annotations.
525 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700526 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700527 }
528
Elliott Hughescdf53122011-08-19 15:46:09 -0700529 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700530 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800531 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700532 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700533 }
534
535 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700536 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
537 Thread* self = down_cast<JNIEnvExt*>(env)->self;
538 vm->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700539 }
540
541 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700542 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700543 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
544 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700545 }
546
547 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700548 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
549 Thread* self = down_cast<JNIEnvExt*>(env)->self;
550 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700551 }
552
553 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700554 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800555 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800556 // Check for null after decoding the object to handle cleared weak globals.
557 if (decoded_obj == nullptr) {
558 return nullptr;
559 }
560 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700561 }
562
563 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800564 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700565 return;
566 }
Ian Rogersef28b142012-11-30 14:22:18 -0800567 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700568
Ian Rogersef28b142012-11-30 14:22:18 -0800569 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700570 if (!locals.Remove(cookie, obj)) {
571 // Attempting to delete a local reference that is not in the
572 // topmost local reference frame is a no-op. DeleteLocalRef returns
573 // void and doesn't throw any exceptions, but we should probably
574 // complain about it so the user will notice that things aren't
575 // going quite the way they expect.
576 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
577 << "failed to find entry";
578 }
579 }
580
581 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700582 if (obj1 == obj2) {
583 return JNI_TRUE;
584 } else {
585 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800586 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
587 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700588 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700589 }
590
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700591 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700592 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700593 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800594 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800595 if (c == nullptr) {
596 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700597 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700598 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700599 }
600
Ian Rogersbc939662013-08-15 10:26:54 -0700601 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700602 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700603 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700604 CHECK_NON_NULL_ARGUMENT(java_class);
605 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700606 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700607 va_end(args);
608 return result;
609 }
610
Elliott Hughes72025e52011-08-23 17:50:30 -0700611 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700612 CHECK_NON_NULL_ARGUMENT(java_class);
613 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800615 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800616 if (c == nullptr) {
617 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700618 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800619 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800620 if (result == nullptr) {
621 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700622 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700623 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700624 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800625 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800626 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700627 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800628 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700629 }
630
Elliott Hughes72025e52011-08-23 17:50:30 -0700631 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700632 CHECK_NON_NULL_ARGUMENT(java_class);
633 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800635 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800636 if (c == nullptr) {
637 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700638 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800639 mirror::Object* result = c->AllocObject(soa.Self());
640 if (result == nullptr) {
641 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700642 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700643 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700644 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800645 if (soa.Self()->IsExceptionPending()) {
646 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700647 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800648 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 }
650
Ian Rogersbc939662013-08-15 10:26:54 -0700651 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700652 CHECK_NON_NULL_ARGUMENT(java_class);
653 CHECK_NON_NULL_ARGUMENT(name);
654 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700655 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700656 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 }
658
Ian Rogersbc939662013-08-15 10:26:54 -0700659 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
660 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700661 CHECK_NON_NULL_ARGUMENT(java_class);
662 CHECK_NON_NULL_ARGUMENT(name);
663 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700665 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 }
667
Elliott Hughes72025e52011-08-23 17:50:30 -0700668 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700669 va_list ap;
670 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700671 CHECK_NON_NULL_ARGUMENT(obj);
672 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700673 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700674 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700675 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700676 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700677 }
678
Elliott Hughes72025e52011-08-23 17:50:30 -0700679 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700680 CHECK_NON_NULL_ARGUMENT(obj);
681 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700682 ScopedObjectAccess soa(env);
683 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
684 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700685 }
686
Elliott Hughes72025e52011-08-23 17:50:30 -0700687 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700688 CHECK_NON_NULL_ARGUMENT(obj);
689 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700690 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700691 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
692 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700693 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 }
695
Elliott Hughes72025e52011-08-23 17:50:30 -0700696 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700697 va_list ap;
698 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700699 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
700 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700701 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700702 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700703 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700704 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 }
706
Elliott Hughes72025e52011-08-23 17:50:30 -0700707 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700708 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
709 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 ScopedObjectAccess soa(env);
711 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700712 }
713
Elliott Hughes72025e52011-08-23 17:50:30 -0700714 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700715 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
716 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700717 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700718 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
719 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 }
721
Elliott Hughes72025e52011-08-23 17:50:30 -0700722 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700723 va_list ap;
724 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700725 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
726 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700727 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700729 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700730 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 }
732
Elliott Hughes72025e52011-08-23 17:50:30 -0700733 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700734 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
735 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700736 ScopedObjectAccess soa(env);
737 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 }
739
Elliott Hughes72025e52011-08-23 17:50:30 -0700740 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700741 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
742 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700743 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700744 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
745 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700746 }
747
Elliott Hughes72025e52011-08-23 17:50:30 -0700748 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700749 va_list ap;
750 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700751 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
752 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700753 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700754 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700755 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700756 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700757 }
758
Elliott Hughes72025e52011-08-23 17:50:30 -0700759 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700760 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
761 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700762 ScopedObjectAccess soa(env);
763 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700764 }
765
Elliott Hughes72025e52011-08-23 17:50:30 -0700766 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700767 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
768 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700769 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700770 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
771 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 }
773
Elliott Hughes72025e52011-08-23 17:50:30 -0700774 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700775 va_list ap;
776 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700777 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
778 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700779 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700780 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700781 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700782 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 }
784
Elliott Hughes72025e52011-08-23 17:50:30 -0700785 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700786 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
787 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700788 ScopedObjectAccess soa(env);
789 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700790 }
791
Elliott Hughes72025e52011-08-23 17:50:30 -0700792 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700793 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
794 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700795 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700796 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
797 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700798 }
799
Elliott Hughes72025e52011-08-23 17:50:30 -0700800 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700801 va_list ap;
802 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700803 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
804 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700805 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700806 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700807 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700808 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700809 }
810
Elliott Hughes72025e52011-08-23 17:50:30 -0700811 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700812 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
813 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700814 ScopedObjectAccess soa(env);
815 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 }
817
Elliott Hughes72025e52011-08-23 17:50:30 -0700818 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700819 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
820 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700821 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700822 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
823 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 }
825
Elliott Hughes72025e52011-08-23 17:50:30 -0700826 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700827 va_list ap;
828 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700829 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
830 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700831 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700832 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700833 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700834 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 }
836
Elliott Hughes72025e52011-08-23 17:50:30 -0700837 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700838 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
839 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700840 ScopedObjectAccess soa(env);
841 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 }
843
Elliott Hughes72025e52011-08-23 17:50:30 -0700844 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700845 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
846 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700847 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700848 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
849 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700850 }
851
Elliott Hughes72025e52011-08-23 17:50:30 -0700852 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700853 va_list ap;
854 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700855 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
856 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700857 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700858 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700859 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700860 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 }
862
Elliott Hughes72025e52011-08-23 17:50:30 -0700863 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700864 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
865 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700866 ScopedObjectAccess soa(env);
867 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 }
869
Elliott Hughes72025e52011-08-23 17:50:30 -0700870 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700871 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
872 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700873 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700874 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
875 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700876 }
877
Elliott Hughes72025e52011-08-23 17:50:30 -0700878 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700879 va_list ap;
880 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700881 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
882 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700883 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700884 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700885 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700886 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 }
888
Elliott Hughes72025e52011-08-23 17:50:30 -0700889 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700890 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
891 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700892 ScopedObjectAccess soa(env);
893 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700894 }
895
Elliott Hughes72025e52011-08-23 17:50:30 -0700896 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700897 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
898 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700899 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700900 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
901 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 }
903
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 va_list ap;
906 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700907 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
908 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700909 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -0700910 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -0700912 }
913
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700915 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
916 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700917 ScopedObjectAccess soa(env);
918 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 }
920
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700922 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
923 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700924 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700925 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700926 }
927
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700928 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700931 CHECK_NON_NULL_ARGUMENT(obj);
932 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700933 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700934 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
935 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 va_end(ap);
937 return local_result;
938 }
939
Ian Rogersbc939662013-08-15 10:26:54 -0700940 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
941 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700942 CHECK_NON_NULL_ARGUMENT(obj);
943 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944 ScopedObjectAccess soa(env);
945 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
946 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 }
948
Ian Rogersbc939662013-08-15 10:26:54 -0700949 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
950 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700951 CHECK_NON_NULL_ARGUMENT(obj);
952 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700953 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700954 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700955 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 }
957
Ian Rogersbc939662013-08-15 10:26:54 -0700958 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
959 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700962 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
963 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700964 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700965 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700967 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Ian Rogersbc939662013-08-15 10:26:54 -0700970 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
971 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700972 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
973 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974 ScopedObjectAccess soa(env);
975 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Ian Rogersbc939662013-08-15 10:26:54 -0700978 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
979 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700980 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
981 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700982 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700983 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 }
985
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700986 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700989 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
990 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700991 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700992 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700994 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Ian Rogersbc939662013-08-15 10:26:54 -0700997 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
998 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700999 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1000 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001001 ScopedObjectAccess soa(env);
1002 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 }
1004
Ian Rogersbc939662013-08-15 10:26:54 -07001005 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1006 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001007 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1008 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001009 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001010 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 }
1012
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001013 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001016 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1017 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001018 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001019 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001021 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 }
1023
Ian Rogersbc939662013-08-15 10:26:54 -07001024 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1025 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001026 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1027 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001028 ScopedObjectAccess soa(env);
1029 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Ian Rogersbc939662013-08-15 10:26:54 -07001032 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1033 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001034 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1035 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001036 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001037 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001040 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001043 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1044 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001045 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001046 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001048 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 }
1050
Ian Rogersbc939662013-08-15 10:26:54 -07001051 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1052 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001053 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1054 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001055 ScopedObjectAccess soa(env);
1056 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Ian Rogersbc939662013-08-15 10:26:54 -07001059 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1060 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001061 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1062 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001064 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001067 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001070 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1071 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001072 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001075 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Ian Rogersbc939662013-08-15 10:26:54 -07001078 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1079 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001080 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1081 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082 ScopedObjectAccess soa(env);
1083 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Ian Rogersbc939662013-08-15 10:26:54 -07001086 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1087 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001088 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1089 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001090 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001091 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001094 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001097 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1098 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001099 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001102 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Ian Rogersbc939662013-08-15 10:26:54 -07001105 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1106 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001107 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1108 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001109 ScopedObjectAccess soa(env);
1110 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Ian Rogersbc939662013-08-15 10:26:54 -07001113 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1114 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001115 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1116 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001117 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001118 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001121 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001124 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1125 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001126 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001127 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001129 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Ian Rogersbc939662013-08-15 10:26:54 -07001132 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1133 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001134 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1135 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001136 ScopedObjectAccess soa(env);
1137 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Ian Rogersbc939662013-08-15 10:26:54 -07001140 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1141 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001142 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1143 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001144 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001145 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 }
1147
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001148 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001151 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1152 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001153 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001154 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001156 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
Ian Rogersbc939662013-08-15 10:26:54 -07001159 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1160 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001161 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1162 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163 ScopedObjectAccess soa(env);
1164 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
Ian Rogersbc939662013-08-15 10:26:54 -07001167 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1168 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001169 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1170 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001171 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001172 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 }
1174
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001175 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001178 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1179 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001180 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001181 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 va_end(ap);
1183 }
1184
Brian Carlstromea46f952013-07-30 01:26:50 -07001185 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1186 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001187 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1188 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001189 ScopedObjectAccess soa(env);
1190 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
Ian Rogersbc939662013-08-15 10:26:54 -07001193 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1194 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001195 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1196 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001197 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001198 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
Ian Rogersbc939662013-08-15 10:26:54 -07001201 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001202 CHECK_NON_NULL_ARGUMENT(java_class);
1203 CHECK_NON_NULL_ARGUMENT(name);
1204 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001205 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001206 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001208
Ian Rogersbc939662013-08-15 10:26:54 -07001209 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1210 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001211 CHECK_NON_NULL_ARGUMENT(java_class);
1212 CHECK_NON_NULL_ARGUMENT(name);
1213 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001214 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001215 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001217
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001218 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001219 CHECK_NON_NULL_ARGUMENT(obj);
1220 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001221 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001222 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1223 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001224 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001226
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001227 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001228 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001229 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001230 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001231 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001234 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001235 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1236 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001237 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001238 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1239 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1240 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001241 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 }
1243
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001244 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001245 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001247 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1248 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001249 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001252#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001253 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1254 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001255 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001256 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1257 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001258 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001259
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001260#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001261 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001262 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001263 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001264 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001265
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001266#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001267 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1268 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001269 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001270 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1271 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001272 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001273
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001274#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001275 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001276 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001277 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001278 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001279
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001280 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001281 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001284 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001285 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
1287
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001288 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001289 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001292 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001293 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001296 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001297 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001300 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001301 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001304 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001305 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001308 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001309 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001312 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001313 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001316 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001317 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001320 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001321 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001324 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001325 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001328 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001329 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 }
1331
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001332 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001333 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001334 }
1335
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001336 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001337 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001338 }
1339
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001340 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001341 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001342 }
1343
1344 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001345 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001346 }
1347
1348 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001349 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001350 }
1351
1352 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001353 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001354 }
1355
1356 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001357 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001358 }
1359
1360 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001361 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001362 }
1363
1364 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001365 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001366 }
1367
1368 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001369 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001370 }
1371
1372 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001373 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001374 }
1375
1376 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001377 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001378 }
1379
1380 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001381 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001382 }
1383
1384 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001385 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 }
1387
1388 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001389 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001390 }
1391
1392 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001393 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 }
1395
1396 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001397 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001398 }
1399
1400 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001401 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 }
1403
1404 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001405 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001408 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001410 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001411 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001412 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001413 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001414 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 va_end(ap);
1416 return local_result;
1417 }
1418
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001419 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001420 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001421 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001422 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001423 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001426 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001427 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001428 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001429 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001430 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
1432
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001433 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001435 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001436 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001437 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001438 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001440 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001443 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001444 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001445 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001446 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 }
1448
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001449 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001450 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001451 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001452 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001455 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001457 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001458 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001459 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001460 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001462 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 }
1464
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001465 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001466 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001467 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001468 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001471 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001472 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001473 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001474 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 }
1476
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001477 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001479 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001480 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001481 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001482 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001484 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001487 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001488 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001489 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001490 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 }
1492
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001493 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001494 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001495 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001496 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001499 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001501 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001502 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001503 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001504 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001506 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 }
1508
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001509 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001510 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001511 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001512 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 }
1514
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001515 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001516 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001517 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001518 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 }
1520
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001521 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001523 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001524 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001525 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001526 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001528 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
1530
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001531 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001532 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001533 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001534 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
1536
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001537 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001538 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001539 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001540 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 }
1542
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001543 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001545 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001546 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001547 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001548 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001550 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001553 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001554 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001555 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001556 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001559 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001560 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001561 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001562 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001565 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001567 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001568 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001569 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001570 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001572 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001575 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001576 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001577 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001578 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 }
1580
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001581 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001582 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001583 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001584 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001587 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001589 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001590 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001591 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001592 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001594 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001597 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001598 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001599 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001600 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001603 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001604 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001605 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001606 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001609 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001611 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001612 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001613 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001614 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 va_end(ap);
1616 }
1617
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001618 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001619 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001620 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001621 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001624 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001625 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001626 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001627 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
Elliott Hughes814e4032011-08-23 12:07:56 -07001630 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001631 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001632 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001633 return nullptr;
1634 }
1635 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001636 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001637 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001638 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001639 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001640 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001641 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 }
1643
1644 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001645 if (utf == nullptr) {
1646 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001648 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001649 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001650 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
1652
Elliott Hughes814e4032011-08-23 12:07:56 -07001653 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001654 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001655 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001656 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001657 }
1658
1659 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001660 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001661 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001662 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001663 }
1664
Ian Rogersbc939662013-08-15 10:26:54 -07001665 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1666 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001667 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001668 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001669 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001670 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001671 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001672 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001673 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001674 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1675 memcpy(buf, chars + start, length * sizeof(jchar));
1676 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001677 }
1678
Ian Rogersbc939662013-08-15 10:26:54 -07001679 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1680 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001681 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001682 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001683 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001684 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001685 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001686 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001687 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001688 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1689 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1690 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001691 }
1692
Elliott Hughes75770752011-08-24 17:52:38 -07001693 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001694 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001695 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001696 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1697 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers68d8b422014-07-17 11:09:10 -07001698 soa.Vm()->PinPrimitiveArray(soa.Self(), chars);
Fred Shih56890e22014-06-02 11:11:52 -07001699 gc::Heap* heap = Runtime::Current()->GetHeap();
1700 if (heap->IsMovableObject(chars)) {
1701 if (is_copy != nullptr) {
1702 *is_copy = JNI_TRUE;
1703 }
1704 int32_t char_count = s->GetLength();
1705 int32_t offset = s->GetOffset();
1706 jchar* bytes = new jchar[char_count];
1707 for (int32_t i = 0; i < char_count; i++) {
1708 bytes[i] = chars->Get(i + offset);
1709 }
1710 return bytes;
1711 } else {
1712 if (is_copy != nullptr) {
1713 *is_copy = JNI_FALSE;
1714 }
1715 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07001716 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001717 }
1718
Mathieu Chartier590fee92013-09-13 13:46:47 -07001719 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001720 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001721 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001722 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1723 mirror::CharArray* s_chars = s->GetCharArray();
1724 if (chars != (s_chars->GetData() + s->GetOffset())) {
1725 delete[] chars;
1726 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001727 soa.Vm()->UnpinPrimitiveArray(soa.Self(), s->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes75770752011-08-24 17:52:38 -07001730 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001731 CHECK_NON_NULL_ARGUMENT(java_string);
1732 ScopedObjectAccess soa(env);
1733 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1734 mirror::CharArray* chars = s->GetCharArray();
1735 int32_t offset = s->GetOffset();
Ian Rogers68d8b422014-07-17 11:09:10 -07001736 soa.Vm()->PinPrimitiveArray(soa.Self(), chars);
Fred Shih56890e22014-06-02 11:11:52 -07001737 gc::Heap* heap = Runtime::Current()->GetHeap();
1738 if (heap->IsMovableObject(chars)) {
1739 StackHandleScope<1> hs(soa.Self());
1740 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
1741 heap->IncrementDisableMovingGC(soa.Self());
1742 }
1743 if (is_copy != nullptr) {
1744 *is_copy = JNI_FALSE;
1745 }
1746 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes75770752011-08-24 17:52:38 -07001749 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07001750 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1751 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -07001752 soa.Vm()->UnpinPrimitiveArray(soa.Self(),
1753 soa.Decode<mirror::String*>(java_string)->GetCharArray());
Fred Shih56890e22014-06-02 11:11:52 -07001754 gc::Heap* heap = Runtime::Current()->GetHeap();
1755 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1756 mirror::CharArray* s_chars = s->GetCharArray();
1757 if (heap->IsMovableObject(s_chars)) {
1758 heap->DecrementDisableMovingGC(soa.Self());
1759 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes75770752011-08-24 17:52:38 -07001762 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001763 if (java_string == nullptr) {
1764 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001765 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001766 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001767 *is_copy = JNI_TRUE;
1768 }
Ian Rogersef28b142012-11-30 14:22:18 -08001769 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001770 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001771 size_t byte_count = s->GetUtfLength();
1772 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001773 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001774 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1775 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1776 bytes[byte_count] = '\0';
1777 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001778 }
1779
Elliott Hughes75770752011-08-24 17:52:38 -07001780 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001781 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001782 }
1783
Elliott Hughesbd935992011-08-22 11:59:34 -07001784 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001785 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001786 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001787 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001788 if (UNLIKELY(!obj->IsArrayInstance())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001789 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1790 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001791 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001792 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001793 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes814e4032011-08-23 12:07:56 -07001796 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001797 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001798 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001799 mirror::ObjectArray<mirror::Object>* array =
1800 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 }
1803
Ian Rogersbc939662013-08-15 10:26:54 -07001804 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1805 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001806 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001807 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001808 mirror::ObjectArray<mirror::Object>* array =
1809 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
1810 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001811 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
1814 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001815 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
1818 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001819 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
1822 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001823 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
1826 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001827 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
1830 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001831 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
1834 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001835 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
1838 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001839 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 }
1841
Ian Rogers1d99e452014-01-02 17:36:41 -08001842 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1843 jobject initial_element) {
1844 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001845 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001846 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001847 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001848 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001849
1850 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001851 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001852 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08001853 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001854 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08001855 if (UNLIKELY(element_class->IsPrimitive())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001856 soa.Vm()->JniAbortF("NewObjectArray", "not an object type: %s",
1857 PrettyDescriptor(element_class).c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08001858 return nullptr;
1859 }
Ian Rogers1d99e452014-01-02 17:36:41 -08001860 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07001861 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08001862 if (UNLIKELY(array_class == nullptr)) {
1863 return nullptr;
1864 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
Elliott Hughes75770752011-08-24 17:52:38 -07001867 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001868 mirror::ObjectArray<mirror::Object>* result =
1869 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001870 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001871 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08001872 if (initial_object != nullptr) {
1873 mirror::Class* element_class = result->GetClass()->GetComponentType();
1874 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001875 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
1876 "element type of '%s'",
1877 PrettyDescriptor(initial_object->GetClass()).c_str(),
1878 PrettyDescriptor(element_class).c_str());
1879 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08001880 } else {
1881 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001882 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08001883 }
1884 }
Elliott Hughes75770752011-08-24 17:52:38 -07001885 }
1886 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001887 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 }
1889
1890 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001891 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 }
1893
Ian Rogersa15e67d2012-02-28 13:51:55 -08001894 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001895 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001896 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001897 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07001898 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001899 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
1900 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001901 return nullptr;
1902 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001903 gc::Heap* heap = Runtime::Current()->GetHeap();
1904 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08001905 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001906 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001907 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001908 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001909 soa.Vm()->PinPrimitiveArray(soa.Self(), array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001910 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001911 *is_copy = JNI_FALSE;
1912 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001913 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001914 }
1915
Ian Rogers2d10b202014-05-12 19:15:18 -07001916 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
1917 jint mode) {
1918 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
1919 ScopedObjectAccess soa(env);
1920 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
1921 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001922 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
1923 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07001924 return;
1925 }
1926 const size_t component_size = array->GetClass()->GetComponentSize();
1927 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001928 }
1929
Elliott Hughes75770752011-08-24 17:52:38 -07001930 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001931 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 }
1933
Elliott Hughes75770752011-08-24 17:52:38 -07001934 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001935 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 }
1937
Elliott Hughes75770752011-08-24 17:52:38 -07001938 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001939 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
Elliott Hughes75770752011-08-24 17:52:38 -07001942 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001943 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 }
1945
Elliott Hughes75770752011-08-24 17:52:38 -07001946 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001947 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
1949
Elliott Hughes75770752011-08-24 17:52:38 -07001950 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001951 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 }
1953
Elliott Hughes75770752011-08-24 17:52:38 -07001954 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001955 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 }
1957
Elliott Hughes75770752011-08-24 17:52:38 -07001958 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001959 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
Mathieu Chartier590fee92013-09-13 13:46:47 -07001962 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
1963 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001964 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
1965 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Mathieu Chartier590fee92013-09-13 13:46:47 -07001968 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001969 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Mathieu Chartier590fee92013-09-13 13:46:47 -07001972 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001973 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Mathieu Chartier590fee92013-09-13 13:46:47 -07001976 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
1977 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001978 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Mathieu Chartier590fee92013-09-13 13:46:47 -07001981 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
1982 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001983 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Mathieu Chartier590fee92013-09-13 13:46:47 -07001986 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001987 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Mathieu Chartier590fee92013-09-13 13:46:47 -07001990 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001991 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Mathieu Chartier590fee92013-09-13 13:46:47 -07001994 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
1995 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001996 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Ian Rogersbc939662013-08-15 10:26:54 -07001999 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2000 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002001 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002002 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
Ian Rogersbc939662013-08-15 10:26:54 -07002005 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2006 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002007 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Ian Rogersbc939662013-08-15 10:26:54 -07002010 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2011 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002012 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Ian Rogersbc939662013-08-15 10:26:54 -07002015 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2016 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002017 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002018 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Ian Rogersbc939662013-08-15 10:26:54 -07002021 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2022 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002023 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002024 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 }
2026
Ian Rogersbc939662013-08-15 10:26:54 -07002027 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2028 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002029 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Ian Rogersbc939662013-08-15 10:26:54 -07002032 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2033 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002034 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Ian Rogersbc939662013-08-15 10:26:54 -07002037 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2038 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002039 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002040 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Ian Rogersbc939662013-08-15 10:26:54 -07002043 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2044 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002045 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002046 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Ian Rogersbc939662013-08-15 10:26:54 -07002049 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2050 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002051 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Ian Rogersbc939662013-08-15 10:26:54 -07002054 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2055 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002056 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Ian Rogersbc939662013-08-15 10:26:54 -07002059 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2060 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002061 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002062 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Ian Rogersbc939662013-08-15 10:26:54 -07002065 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2066 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002067 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002068 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Ian Rogersbc939662013-08-15 10:26:54 -07002071 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2072 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002073 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Ian Rogersbc939662013-08-15 10:26:54 -07002076 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2077 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002078 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Ian Rogersbc939662013-08-15 10:26:54 -07002081 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2082 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002083 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002084 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Ian Rogersbc939662013-08-15 10:26:54 -07002087 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2088 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002089 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2090 }
2091
Ian Rogersbc939662013-08-15 10:26:54 -07002092 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2093 jint method_count, bool return_errors) {
2094 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002095 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2096 method_count);
2097 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002098 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002099 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002100 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002101 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002102 if (UNLIKELY(method_count == 0)) {
2103 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2104 << PrettyDescriptor(c);
2105 return JNI_OK;
2106 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002107 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002108 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 const char* name = methods[i].name;
2110 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002111 const void* fnPtr = methods[i].fnPtr;
2112 if (UNLIKELY(name == nullptr)) {
2113 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2114 return JNI_ERR;
2115 } else if (UNLIKELY(sig == nullptr)) {
2116 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2117 return JNI_ERR;
2118 } else if (UNLIKELY(fnPtr == nullptr)) {
2119 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2120 return JNI_ERR;
2121 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002122 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002124 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 ++sig;
2126 }
2127
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002128 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2129 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002130 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002132 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002133 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002134 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002135 << PrettyDescriptor(c) << "." << name << sig << " in "
2136 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002137 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002139 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002140 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002141 << PrettyDescriptor(c) << "." << name << sig
2142 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002143 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 return JNI_ERR;
2145 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002146
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002147 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002148
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002149 m->RegisterNative(soa.Self(), fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 }
2151 return JNI_OK;
2152 }
2153
Elliott Hughes5174fe62011-08-23 15:12:35 -07002154 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002155 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002156 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002157 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002159 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002160
Ian Rogers2d10b202014-05-12 19:15:18 -07002161 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002163 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002164 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002165 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002166 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002167 }
2168 }
2169 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002170 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002171 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002172 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002173 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002174 }
2175 }
2176
Ian Rogers2d10b202014-05-12 19:15:18 -07002177 if (unregistered_count == 0) {
2178 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2179 << PrettyDescriptor(c) << "' that contains no native methods";
2180 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002181 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 }
2183
Ian Rogers719d1a32014-03-06 12:13:39 -08002184 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002185 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002186 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002187 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2188 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002189 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002190 return JNI_ERR;
2191 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002192 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002193 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
2195
Ian Rogers719d1a32014-03-06 12:13:39 -08002196 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002197 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002198 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002199 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002200 o->MonitorExit(soa.Self());
2201 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002202 return JNI_ERR;
2203 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002204 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002205 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
2208 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002209 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002211 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002212 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002214 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002216 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002220 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002221 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2222 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002223 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002224 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002225 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002226 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2227 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002228 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002229 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002230
Brian Carlstrom85a93362014-06-25 09:30:52 -07002231 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002232 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002233 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2234 "buffer capacity greater than maximum jint: %" PRId64,
2235 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002236 return nullptr;
2237 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002238 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002239 jint capacity_arg = static_cast<jint>(capacity);
2240
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002241 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2242 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002243 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002244 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002248 return reinterpret_cast<void*>(env->GetLongField(
2249 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Elliott Hughesb465ab02011-08-24 11:21:21 -07002252 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002253 return static_cast<jlong>(env->GetIntField(
2254 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002255 }
2256
Elliott Hughesb465ab02011-08-24 11:21:21 -07002257 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002258 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002259
2260 // Do we definitely know what kind of reference this is?
2261 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2262 IndirectRefKind kind = GetIndirectRefKind(ref);
2263 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002264 case kLocal: {
2265 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002266 // The local refs don't need a read barrier.
2267 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2268 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002269 return JNILocalRefType;
2270 }
2271 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002272 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002273 case kGlobal:
2274 return JNIGlobalRefType;
2275 case kWeakGlobal:
2276 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002277 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002278 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002279 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002280 return JNILocalRefType;
2281 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002282 return JNIInvalidRefType;
2283 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002284 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2285 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002286 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002287
2288 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002289 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2290 const char* caller)
2291 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002292 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002293 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002294 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2295 return JNI_ERR;
2296 }
2297 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002298 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002299 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2300 if (!okay) {
2301 soa.Self()->ThrowOutOfMemoryError(caller);
2302 }
2303 return okay ? JNI_OK : JNI_ERR;
2304 }
2305
2306 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002307 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002308 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002309 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002310 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002311 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002312 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002313 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002314 return soa.AddLocalReference<JniT>(result);
2315 }
2316
Ian Rogers2d10b202014-05-12 19:15:18 -07002317 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2318 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2319 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002321 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002322 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002323 soa.Vm()->JniAbortF(fn_name,
2324 "attempt to %s %s primitive array elements with an object of type %s",
2325 operation,
2326 PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2327 PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002328 return nullptr;
2329 }
2330 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2331 return array;
2332 }
2333
2334 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2335 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2336 CHECK_NON_NULL_ARGUMENT(java_array);
2337 ScopedObjectAccess soa(env);
2338 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2339 "GetArrayElements",
2340 "get");
2341 if (UNLIKELY(array == nullptr)) {
2342 return nullptr;
2343 }
Ian Rogers68d8b422014-07-17 11:09:10 -07002344 soa.Vm()->PinPrimitiveArray(soa.Self(), array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002345 // Only make a copy if necessary.
2346 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2347 if (is_copy != nullptr) {
2348 *is_copy = JNI_TRUE;
2349 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002350 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002351 size_t size = array->GetLength() * component_size;
2352 void* data = new uint64_t[RoundUp(size, 8) / 8];
2353 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002354 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002355 } else {
2356 if (is_copy != nullptr) {
2357 *is_copy = JNI_FALSE;
2358 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002359 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002360 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002361 }
2362
Ian Rogers2d10b202014-05-12 19:15:18 -07002363 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002364 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002365 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002366 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002367 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2368 "ReleaseArrayElements",
2369 "release");
2370 if (array == nullptr) {
2371 return;
2372 }
2373 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2374 }
2375
2376 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2377 size_t component_size, void* elements, jint mode)
2378 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002379 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002380 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002381 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002382 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002383 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2384 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002385 if (is_copy) {
2386 // Sanity check: If elements is not the same as the java array's data, it better not be a
2387 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2388 // copies we make?
2389 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002390 soa.Vm()->JniAbortF("ReleaseArrayElements",
2391 "invalid element pointer %p, array elements are %p",
2392 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002393 return;
2394 }
2395 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002396 // Don't need to copy if we had a direct pointer.
2397 if (mode != JNI_ABORT && is_copy) {
2398 memcpy(array_data, elements, bytes);
2399 }
2400 if (mode != JNI_COMMIT) {
2401 if (is_copy) {
2402 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002403 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002404 // Non copy to a movable object must means that we had disabled the moving GC.
2405 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002406 }
Ian Rogers68d8b422014-07-17 11:09:10 -07002407 soa.Vm()->UnpinPrimitiveArray(soa.Self(), array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002408 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002409 }
2410
Ian Rogers2d10b202014-05-12 19:15:18 -07002411 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2412 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2413 jsize start, jsize length, ElementT* buf) {
2414 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2415 ScopedObjectAccess soa(env);
2416 ArtArrayT* array =
2417 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2418 "GetPrimitiveArrayRegion",
2419 "get region of");
2420 if (array != nullptr) {
2421 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2422 ThrowAIOOBE(soa, array, start, length, "src");
2423 } else {
2424 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2425 ElementT* data = array->GetData();
2426 memcpy(buf, data + start, length * sizeof(ElementT));
2427 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002428 }
2429 }
2430
Ian Rogers2d10b202014-05-12 19:15:18 -07002431 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2432 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2433 jsize start, jsize length, const ElementT* buf) {
2434 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2435 ScopedObjectAccess soa(env);
2436 ArtArrayT* array =
2437 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2438 "SetPrimitiveArrayRegion",
2439 "set region of");
2440 if (array != nullptr) {
2441 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2442 ThrowAIOOBE(soa, array, start, length, "dst");
2443 } else {
2444 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2445 ElementT* data = array->GetData();
2446 memcpy(data + start, buf, length * sizeof(ElementT));
2447 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002448 }
2449 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002450};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002451
Elliott Hughes88c5c352012-03-15 18:49:48 -07002452const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002453 nullptr, // reserved0.
2454 nullptr, // reserved1.
2455 nullptr, // reserved2.
2456 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002457 JNI::GetVersion,
2458 JNI::DefineClass,
2459 JNI::FindClass,
2460 JNI::FromReflectedMethod,
2461 JNI::FromReflectedField,
2462 JNI::ToReflectedMethod,
2463 JNI::GetSuperclass,
2464 JNI::IsAssignableFrom,
2465 JNI::ToReflectedField,
2466 JNI::Throw,
2467 JNI::ThrowNew,
2468 JNI::ExceptionOccurred,
2469 JNI::ExceptionDescribe,
2470 JNI::ExceptionClear,
2471 JNI::FatalError,
2472 JNI::PushLocalFrame,
2473 JNI::PopLocalFrame,
2474 JNI::NewGlobalRef,
2475 JNI::DeleteGlobalRef,
2476 JNI::DeleteLocalRef,
2477 JNI::IsSameObject,
2478 JNI::NewLocalRef,
2479 JNI::EnsureLocalCapacity,
2480 JNI::AllocObject,
2481 JNI::NewObject,
2482 JNI::NewObjectV,
2483 JNI::NewObjectA,
2484 JNI::GetObjectClass,
2485 JNI::IsInstanceOf,
2486 JNI::GetMethodID,
2487 JNI::CallObjectMethod,
2488 JNI::CallObjectMethodV,
2489 JNI::CallObjectMethodA,
2490 JNI::CallBooleanMethod,
2491 JNI::CallBooleanMethodV,
2492 JNI::CallBooleanMethodA,
2493 JNI::CallByteMethod,
2494 JNI::CallByteMethodV,
2495 JNI::CallByteMethodA,
2496 JNI::CallCharMethod,
2497 JNI::CallCharMethodV,
2498 JNI::CallCharMethodA,
2499 JNI::CallShortMethod,
2500 JNI::CallShortMethodV,
2501 JNI::CallShortMethodA,
2502 JNI::CallIntMethod,
2503 JNI::CallIntMethodV,
2504 JNI::CallIntMethodA,
2505 JNI::CallLongMethod,
2506 JNI::CallLongMethodV,
2507 JNI::CallLongMethodA,
2508 JNI::CallFloatMethod,
2509 JNI::CallFloatMethodV,
2510 JNI::CallFloatMethodA,
2511 JNI::CallDoubleMethod,
2512 JNI::CallDoubleMethodV,
2513 JNI::CallDoubleMethodA,
2514 JNI::CallVoidMethod,
2515 JNI::CallVoidMethodV,
2516 JNI::CallVoidMethodA,
2517 JNI::CallNonvirtualObjectMethod,
2518 JNI::CallNonvirtualObjectMethodV,
2519 JNI::CallNonvirtualObjectMethodA,
2520 JNI::CallNonvirtualBooleanMethod,
2521 JNI::CallNonvirtualBooleanMethodV,
2522 JNI::CallNonvirtualBooleanMethodA,
2523 JNI::CallNonvirtualByteMethod,
2524 JNI::CallNonvirtualByteMethodV,
2525 JNI::CallNonvirtualByteMethodA,
2526 JNI::CallNonvirtualCharMethod,
2527 JNI::CallNonvirtualCharMethodV,
2528 JNI::CallNonvirtualCharMethodA,
2529 JNI::CallNonvirtualShortMethod,
2530 JNI::CallNonvirtualShortMethodV,
2531 JNI::CallNonvirtualShortMethodA,
2532 JNI::CallNonvirtualIntMethod,
2533 JNI::CallNonvirtualIntMethodV,
2534 JNI::CallNonvirtualIntMethodA,
2535 JNI::CallNonvirtualLongMethod,
2536 JNI::CallNonvirtualLongMethodV,
2537 JNI::CallNonvirtualLongMethodA,
2538 JNI::CallNonvirtualFloatMethod,
2539 JNI::CallNonvirtualFloatMethodV,
2540 JNI::CallNonvirtualFloatMethodA,
2541 JNI::CallNonvirtualDoubleMethod,
2542 JNI::CallNonvirtualDoubleMethodV,
2543 JNI::CallNonvirtualDoubleMethodA,
2544 JNI::CallNonvirtualVoidMethod,
2545 JNI::CallNonvirtualVoidMethodV,
2546 JNI::CallNonvirtualVoidMethodA,
2547 JNI::GetFieldID,
2548 JNI::GetObjectField,
2549 JNI::GetBooleanField,
2550 JNI::GetByteField,
2551 JNI::GetCharField,
2552 JNI::GetShortField,
2553 JNI::GetIntField,
2554 JNI::GetLongField,
2555 JNI::GetFloatField,
2556 JNI::GetDoubleField,
2557 JNI::SetObjectField,
2558 JNI::SetBooleanField,
2559 JNI::SetByteField,
2560 JNI::SetCharField,
2561 JNI::SetShortField,
2562 JNI::SetIntField,
2563 JNI::SetLongField,
2564 JNI::SetFloatField,
2565 JNI::SetDoubleField,
2566 JNI::GetStaticMethodID,
2567 JNI::CallStaticObjectMethod,
2568 JNI::CallStaticObjectMethodV,
2569 JNI::CallStaticObjectMethodA,
2570 JNI::CallStaticBooleanMethod,
2571 JNI::CallStaticBooleanMethodV,
2572 JNI::CallStaticBooleanMethodA,
2573 JNI::CallStaticByteMethod,
2574 JNI::CallStaticByteMethodV,
2575 JNI::CallStaticByteMethodA,
2576 JNI::CallStaticCharMethod,
2577 JNI::CallStaticCharMethodV,
2578 JNI::CallStaticCharMethodA,
2579 JNI::CallStaticShortMethod,
2580 JNI::CallStaticShortMethodV,
2581 JNI::CallStaticShortMethodA,
2582 JNI::CallStaticIntMethod,
2583 JNI::CallStaticIntMethodV,
2584 JNI::CallStaticIntMethodA,
2585 JNI::CallStaticLongMethod,
2586 JNI::CallStaticLongMethodV,
2587 JNI::CallStaticLongMethodA,
2588 JNI::CallStaticFloatMethod,
2589 JNI::CallStaticFloatMethodV,
2590 JNI::CallStaticFloatMethodA,
2591 JNI::CallStaticDoubleMethod,
2592 JNI::CallStaticDoubleMethodV,
2593 JNI::CallStaticDoubleMethodA,
2594 JNI::CallStaticVoidMethod,
2595 JNI::CallStaticVoidMethodV,
2596 JNI::CallStaticVoidMethodA,
2597 JNI::GetStaticFieldID,
2598 JNI::GetStaticObjectField,
2599 JNI::GetStaticBooleanField,
2600 JNI::GetStaticByteField,
2601 JNI::GetStaticCharField,
2602 JNI::GetStaticShortField,
2603 JNI::GetStaticIntField,
2604 JNI::GetStaticLongField,
2605 JNI::GetStaticFloatField,
2606 JNI::GetStaticDoubleField,
2607 JNI::SetStaticObjectField,
2608 JNI::SetStaticBooleanField,
2609 JNI::SetStaticByteField,
2610 JNI::SetStaticCharField,
2611 JNI::SetStaticShortField,
2612 JNI::SetStaticIntField,
2613 JNI::SetStaticLongField,
2614 JNI::SetStaticFloatField,
2615 JNI::SetStaticDoubleField,
2616 JNI::NewString,
2617 JNI::GetStringLength,
2618 JNI::GetStringChars,
2619 JNI::ReleaseStringChars,
2620 JNI::NewStringUTF,
2621 JNI::GetStringUTFLength,
2622 JNI::GetStringUTFChars,
2623 JNI::ReleaseStringUTFChars,
2624 JNI::GetArrayLength,
2625 JNI::NewObjectArray,
2626 JNI::GetObjectArrayElement,
2627 JNI::SetObjectArrayElement,
2628 JNI::NewBooleanArray,
2629 JNI::NewByteArray,
2630 JNI::NewCharArray,
2631 JNI::NewShortArray,
2632 JNI::NewIntArray,
2633 JNI::NewLongArray,
2634 JNI::NewFloatArray,
2635 JNI::NewDoubleArray,
2636 JNI::GetBooleanArrayElements,
2637 JNI::GetByteArrayElements,
2638 JNI::GetCharArrayElements,
2639 JNI::GetShortArrayElements,
2640 JNI::GetIntArrayElements,
2641 JNI::GetLongArrayElements,
2642 JNI::GetFloatArrayElements,
2643 JNI::GetDoubleArrayElements,
2644 JNI::ReleaseBooleanArrayElements,
2645 JNI::ReleaseByteArrayElements,
2646 JNI::ReleaseCharArrayElements,
2647 JNI::ReleaseShortArrayElements,
2648 JNI::ReleaseIntArrayElements,
2649 JNI::ReleaseLongArrayElements,
2650 JNI::ReleaseFloatArrayElements,
2651 JNI::ReleaseDoubleArrayElements,
2652 JNI::GetBooleanArrayRegion,
2653 JNI::GetByteArrayRegion,
2654 JNI::GetCharArrayRegion,
2655 JNI::GetShortArrayRegion,
2656 JNI::GetIntArrayRegion,
2657 JNI::GetLongArrayRegion,
2658 JNI::GetFloatArrayRegion,
2659 JNI::GetDoubleArrayRegion,
2660 JNI::SetBooleanArrayRegion,
2661 JNI::SetByteArrayRegion,
2662 JNI::SetCharArrayRegion,
2663 JNI::SetShortArrayRegion,
2664 JNI::SetIntArrayRegion,
2665 JNI::SetLongArrayRegion,
2666 JNI::SetFloatArrayRegion,
2667 JNI::SetDoubleArrayRegion,
2668 JNI::RegisterNatives,
2669 JNI::UnregisterNatives,
2670 JNI::MonitorEnter,
2671 JNI::MonitorExit,
2672 JNI::GetJavaVM,
2673 JNI::GetStringRegion,
2674 JNI::GetStringUTFRegion,
2675 JNI::GetPrimitiveArrayCritical,
2676 JNI::ReleasePrimitiveArrayCritical,
2677 JNI::GetStringCritical,
2678 JNI::ReleaseStringCritical,
2679 JNI::NewWeakGlobalRef,
2680 JNI::DeleteWeakGlobalRef,
2681 JNI::ExceptionCheck,
2682 JNI::NewDirectByteBuffer,
2683 JNI::GetDirectBufferAddress,
2684 JNI::GetDirectBufferCapacity,
2685 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002686};
2687
Ian Rogers68d8b422014-07-17 11:09:10 -07002688const JNINativeInterface* GetJniNativeInterface() {
2689 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002690}
2691
Elliott Hughesc8fece32013-01-02 11:27:23 -08002692void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002693 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002694 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002695 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002696 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2697 }
2698 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2699}
2700
Ian Rogersdf20fe02011-07-20 20:34:16 -07002701} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002702
2703std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2704 switch (rhs) {
2705 case JNIInvalidRefType:
2706 os << "JNIInvalidRefType";
2707 return os;
2708 case JNILocalRefType:
2709 os << "JNILocalRefType";
2710 return os;
2711 case JNIGlobalRefType:
2712 os << "JNIGlobalRefType";
2713 return os;
2714 case JNIWeakGlobalRefType:
2715 os << "JNIWeakGlobalRefType";
2716 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002717 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002718 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002719 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002720 }
2721}