blob: f158463db854078e8b666b7d42482b9fd698bf86 [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"
Carl Shapiroea4dca82011-08-01 13:45:38 -070036#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070037#include "mirror/art_field-inl.h"
38#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/class-inl.h"
40#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/object-inl.h"
42#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070043#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/throwable.h"
Yong WU355383f2014-07-24 21:32:15 +080045#include "native_bridge.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080046#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070047#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070048#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070049#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070051#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070052#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070054#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070055
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070056namespace art {
57
Brian Carlstrom7934ac22013-07-26 10:54:15 -070058static const size_t kMonitorsInitial = 32; // Arbitrary.
59static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070060
Brian Carlstrom7934ac22013-07-26 10:54:15 -070061static const size_t kLocalsInitial = 64; // Arbitrary.
62static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070063
Brian Carlstrom7934ac22013-07-26 10:54:15 -070064static const size_t kPinTableInitial = 16; // Arbitrary.
65static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070066
Brian Carlstrom7934ac22013-07-26 10:54:15 -070067static size_t gGlobalsInitial = 512; // Arbitrary.
68static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070069
Brian Carlstrom7934ac22013-07-26 10:54:15 -070070static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
71static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070072
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080073static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070074 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070075 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070076}
77
Jeff Hao19c5d372013-03-15 14:33:43 -070078static bool IsBadJniVersion(int version) {
79 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
80 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
81}
82
Elliott Hughes6b436852011-08-12 10:16:44 -070083// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
84// separated with slashes but aren't wrapped with "L;" like regular descriptors
85// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
86// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
87// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070088static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070089 std::string result;
90 // Add the missing "L;" if necessary.
91 if (name[0] == '[') {
92 result = name;
93 } else {
94 result += 'L';
95 result += name;
96 result += ';';
97 }
98 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070099 if (result.find('.') != std::string::npos) {
100 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
101 << "\"" << name << "\"";
102 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700103 }
104 return result;
105}
106
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800107static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700108 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800110 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700111 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800112 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
113 "no %s method \"%s.%s%s\"",
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700114 kind, c->GetDescriptor(&temp), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700115}
116
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200117static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa, mirror::Class* c,
118 const char* kind, jint idx, bool return_errors)
119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
120 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method in "
121 << PrettyDescriptor(c) << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
122 << ": " << kind << " is null at index " << idx;
123 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
124 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
125 "%s is null at index %d", kind, idx);
126}
127
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800128static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
130 if (LIKELY(klass->IsInitialized())) {
131 return klass;
132 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700133 StackHandleScope<1> hs(self);
134 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
135 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800136 return nullptr;
137 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700138 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800139}
140
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
142 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800144 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800145 if (c == nullptr) {
146 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700147 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800148 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700149 if (is_static) {
150 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700151 } else if (c->IsInterface()) {
152 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700153 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700154 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800155 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700156 // No virtual method matching the signature. Search declared
157 // private methods and constructors.
158 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700159 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700160 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800161 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800163 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700164 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700166}
167
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800168static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800170 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700171 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
172 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800173 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700174 }
Brian Carlstromce888532013-10-10 00:32:58 -0700175 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800176 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700177 return method->GetDeclaringClass()->GetClassLoader();
178 }
179 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800180 mirror::ClassLoader* class_loader =
181 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
182 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700183 return class_loader;
184 }
185 // See if the override ClassLoader is set for gtests.
186 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800187 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800188 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700189 CHECK(Runtime::Current()->UseCompileTimeClassPath());
190 return class_loader;
191 }
192 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800193 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700194}
195
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700196static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
197 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700199 StackHandleScope<2> hs(soa.Self());
200 Handle<mirror::Class> c(
201 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
202 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800203 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700204 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800205 mirror::ArtField* field = nullptr;
206 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
208 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700209 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800210 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700211 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700213 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800214 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700216 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800217 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700218 StackHandleScope<1> hs(soa.Self());
219 Handle<mirror::Throwable> cause(hs.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220 soa.Self()->ClearException();
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700221 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800222 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800223 "no type \"%s\" found and so no field \"%s\" "
224 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700225 c->GetDescriptor(&temp));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700226 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800227 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228 }
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700229 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700230 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700231 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700232 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700233 } else {
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700234 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700235 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800236 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800237 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
238 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
239 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700240 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800241 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700242 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700244}
245
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800246static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700249 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700250 vm->pin_table.Add(array);
251}
252
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800253static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700254 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700256 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700257 vm->pin_table.Remove(array);
258}
259
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800260static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700262 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700263 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800264 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
265 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
266 "%s offset=%d length=%d %s.length=%d",
267 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700268}
Ian Rogers0571d352011-11-03 19:51:38 -0700269
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
271 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700272 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800273 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
274 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
275 "offset=%d length=%d string.length()=%d", start, length,
276 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700277}
Elliott Hughes814e4032011-08-23 12:07:56 -0700278
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700280 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 // Turn the const char* into a java.lang.String.
282 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800283 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700285 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700286
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 // Choose an appropriate constructor and set up the arguments.
288 jvalue args[2];
289 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800290 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800292 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 signature = "(Ljava/lang/String;)V";
294 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800295 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 signature = "(Ljava/lang/Throwable;)V";
297 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700298 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700299 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
300 args[0].l = s.get();
301 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700302 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800304 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800305 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700306 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800307 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308 return JNI_ERR;
309 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700310
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800311 ScopedLocalRef<jthrowable> exception(
312 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
313 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 return JNI_ERR;
315 }
Ian Rogersef28b142012-11-30 14:22:18 -0800316 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800317 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800318 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700319 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700320}
321
Elliott Hughes462c9442012-03-23 18:47:50 -0700322static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800323 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700324 return JNI_ERR;
325 }
326
Elliott Hughes462c9442012-03-23 18:47:50 -0700327 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700328 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800329 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700330 *p_env = self->GetJniEnv();
331 return JNI_OK;
332 }
333
334 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
335
336 // No threads allowed in zygote mode.
337 if (runtime->IsZygote()) {
338 LOG(ERROR) << "Attempt to attach a thread in the zygote";
339 return JNI_ERR;
340 }
341
Elliott Hughes462c9442012-03-23 18:47:50 -0700342 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800343 const char* thread_name = nullptr;
344 jobject thread_group = nullptr;
345 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700346 if (IsBadJniVersion(args->version)) {
347 LOG(ERROR) << "Bad JNI version passed to "
348 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
349 << args->version;
350 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700351 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700352 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700353 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700354 }
Elliott Hughes75770752011-08-24 17:52:38 -0700355
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800356 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800357 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700358 return JNI_ERR;
359 } else {
360 *p_env = Thread::Current()->GetJniEnv();
361 return JNI_OK;
362 }
Elliott Hughes75770752011-08-24 17:52:38 -0700363}
364
Elliott Hughes79082e32011-08-25 12:07:32 -0700365class SharedLibrary {
366 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800367 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700368 : path_(path),
369 handle_(handle),
Yong WU355383f2014-07-24 21:32:15 +0800370 needs_native_bridge_(false),
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700371 class_loader_(GcRoot<mirror::Object>(class_loader)),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700372 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700373 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700374 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700375 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700376 }
377
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700378 mirror::Object* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700379 return class_loader_.Read();
Elliott Hughes79082e32011-08-25 12:07:32 -0700380 }
381
382 std::string GetPath() {
383 return path_;
384 }
385
386 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700387 * Check the result of an earlier call to JNI_OnLoad on this library.
388 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700389 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390 bool CheckOnLoadResult()
391 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700392 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700393 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
395 bool okay;
396 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700397 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700398
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700399 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
401 // caller can continue.
402 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
403 okay = true;
404 } else {
405 while (jni_on_load_result_ == kPending) {
406 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700407 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700409
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700410 okay = (jni_on_load_result_ == kOkay);
411 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
412 << (okay ? "succeeded" : "failed") << "]";
413 }
414 }
415 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700416 return okay;
417 }
418
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700419 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700420 Thread* self = Thread::Current();
421 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700422
Elliott Hughes79082e32011-08-25 12:07:32 -0700423 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700424 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700425
426 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700427 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700428 }
429
Yong WU355383f2014-07-24 21:32:15 +0800430 void SetNeedsNativeBridge() {
431 needs_native_bridge_ = true;
432 }
433
434 bool NeedsNativeBridge() const {
435 return needs_native_bridge_;
436 }
437
Elliott Hughes79082e32011-08-25 12:07:32 -0700438 void* FindSymbol(const std::string& symbol_name) {
439 return dlsym(handle_, symbol_name.c_str());
440 }
441
Yong WU355383f2014-07-24 21:32:15 +0800442 void* FindSymbolWithNativeBridge(const std::string& symbol_name, mirror::ArtMethod* m)
443 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
444 CHECK(NeedsNativeBridge());
445
446 uint32_t len = 0;
447 const char* shorty = nullptr;
448 if (m != nullptr) {
449 shorty = m->GetShorty(&len);
450 }
451 return NativeBridge::GetTrampoline(handle_, symbol_name.c_str(), shorty, len);
452 }
453
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800454 void VisitRoots(RootCallback* visitor, void* arg) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700455 if (!class_loader_.IsNull()) {
456 class_loader_.VisitRoot(visitor, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800457 }
458 }
459
Elliott Hughes79082e32011-08-25 12:07:32 -0700460 private:
461 enum JNI_OnLoadState {
462 kPending,
463 kFailed,
464 kOkay,
465 };
466
467 // Path to library "/system/lib/libjni.so".
468 std::string path_;
469
470 // The void* returned by dlopen(3).
471 void* handle_;
472
Yong WU355383f2014-07-24 21:32:15 +0800473 // True if a native bridge is required.
474 bool needs_native_bridge_;
475
Elliott Hughes79082e32011-08-25 12:07:32 -0700476 // The ClassLoader this library is associated with.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700477 GcRoot<mirror::Object> class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700478
479 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700480 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700481 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700482 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700483 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700484 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700486 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700487};
488
Elliott Hughes79082e32011-08-25 12:07:32 -0700489// This exists mainly to keep implementation details out of the header file.
490class Libraries {
491 public:
492 Libraries() {
493 }
494
495 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700496 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 }
498
Elliott Hughesae80b492012-04-24 10:43:17 -0700499 void Dump(std::ostream& os) const {
500 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700501 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700502 if (!first) {
503 os << ' ';
504 }
505 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700506 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700507 }
508 }
509
510 size_t size() const {
511 return libraries_.size();
512 }
513
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700515 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800516 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700517 }
518
519 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700520 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 }
522
523 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800524 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700525 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700526 std::string jni_short_name(JniShortName(m));
527 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800528 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700529 for (const auto& lib : libraries_) {
530 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700531 if (library->GetClassLoader() != declaring_class_loader) {
532 // We only search libraries loaded by the appropriate ClassLoader.
533 continue;
534 }
535 // Try the short name then the long name...
Yong WU355383f2014-07-24 21:32:15 +0800536 void* fn = nullptr;
537 if (UNLIKELY(library->NeedsNativeBridge())) {
538 fn = library->FindSymbolWithNativeBridge(jni_short_name, m);
539 if (fn == nullptr) {
540 fn = library->FindSymbolWithNativeBridge(jni_long_name, m);
541 }
542 } else {
543 fn = library->FindSymbol(jni_short_name);
544 if (fn == nullptr) {
545 fn = library->FindSymbol(jni_long_name);
546 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700547 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800548 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800549 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
550 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700551 return fn;
552 }
553 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700555 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700556 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700557 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800558 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700559 }
560
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800561 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800562 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800563 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800564 }
565 }
566
Elliott Hughes79082e32011-08-25 12:07:32 -0700567 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700568 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700569};
570
Ian Rogers2d10b202014-05-12 19:15:18 -0700571#define CHECK_NON_NULL_ARGUMENT(value) \
572 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700573
Ian Rogers2d10b202014-05-12 19:15:18 -0700574#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
575 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
576
577#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
578 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
579
580#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
581 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
582
583#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800584 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700585 JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700586 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700587 }
588
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700589#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800590 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700591 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700592 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700593 }
594
Elliott Hughescdf53122011-08-19 15:46:09 -0700595class JNI {
596 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700597 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700598 return JNI_VERSION_1_6;
599 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700600
Ian Rogers25e8b912012-09-07 11:31:36 -0700601 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700602 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800603 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700604 }
605
Elliott Hughescdf53122011-08-19 15:46:09 -0700606 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700607 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700608 Runtime* runtime = Runtime::Current();
609 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700610 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700611 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800612 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700613 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700614 StackHandleScope<1> hs(soa.Self());
615 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800616 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700617 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800618 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700619 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700621 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700622
Ian Rogers62f05122014-03-21 11:21:29 -0700623 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700624 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700625 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700626 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700627 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700628
Ian Rogers62f05122014-03-21 11:21:29 -0700629 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700630 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700631 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700632 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700633 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700634
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700636 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700637 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800638 mirror::ArtMethod* m = soa.DecodeMethod(mid);
639 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700640 jobject art_method = soa.AddLocalReference<jobject>(m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200641 jobject reflect_method;
642 if (m->IsConstructor()) {
643 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
644 } else {
645 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
646 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700647 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800648 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700649 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800650 SetObjectField(env, reflect_method,
651 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700652 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700654
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700656 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800658 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700659 jobject art_field = soa.AddLocalReference<jobject>(f);
660 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
661 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800662 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700663 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800664 SetObjectField(env, reflect_field,
665 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700666 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700668
Elliott Hughes37f7a402011-08-22 18:56:01 -0700669 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700670 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700671 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800672 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700674 }
675
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700676 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700677 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800679 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700680 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700682
Narayan Kamath1268b742014-07-11 19:15:11 +0100683 // Note: java_class1 should be safely castable to java_class2, and
684 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700686 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
687 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700688 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800689 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
690 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100691 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700692 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700693
Elliott Hughese84278b2012-03-22 10:06:53 -0700694 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700695 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800696 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700697 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 return JNI_TRUE;
699 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700700 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800701 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
702 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700703 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700704 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700706
Elliott Hughes37f7a402011-08-22 18:56:01 -0700707 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800709 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
710 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700711 return JNI_ERR;
712 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800713 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
714 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700715 return JNI_OK;
716 }
717
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700718 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700719 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800720 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700721 }
722
723 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700724 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700725 }
726
727 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700728 ScopedObjectAccess soa(env);
729 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700730 }
731
732 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700733 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700734
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700735 // If we have no exception to describe, pass through.
736 if (!soa.Self()->GetException(nullptr)) {
737 return;
738 }
739
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700740 StackHandleScope<3> hs(soa.Self());
741 // TODO: Use nullptr instead of null handles?
742 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
743 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
744 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800745 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200746 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800747 {
748 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800749 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700750 old_throw_this_object.Assign(old_throw_location.GetThis());
751 old_throw_method.Assign(old_throw_location.GetMethod());
752 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800753 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200754 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800755 soa.Self()->ClearException();
756 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800757 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700758 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700759 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
760 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800761 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700762 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700763 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700764 } else {
765 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800766 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800767 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700768 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800769 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700770 }
771 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700772 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800773 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700774
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700775 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200776 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700778
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700780 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800781 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700782 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 }
784
Ian Rogers25e8b912012-09-07 11:31:36 -0700785 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700786 LOG(FATAL) << "JNI FatalError called: " << msg;
787 }
788
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700789 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700790 // TODO: SOA may not be necessary but I do it to please lock annotations.
791 ScopedObjectAccess soa(env);
792 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700793 return JNI_ERR;
794 }
Ian Rogersef28b142012-11-30 14:22:18 -0800795 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 return JNI_OK;
797 }
798
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700799 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700800 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800801 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700802 soa.Env()->PopFrame();
803 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 }
805
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700806 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700807 // TODO: SOA may not be necessary but I do it to please lock annotations.
808 ScopedObjectAccess soa(env);
809 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700810 }
811
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700813 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800814 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800815 // Check for null after decoding the object to handle cleared weak globals.
816 if (decoded_obj == nullptr) {
817 return nullptr;
818 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700819 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700821 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700822 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 return reinterpret_cast<jobject>(ref);
824 }
825
826 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800827 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 return;
829 }
Ian Rogersef28b142012-11-30 14:22:18 -0800830 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800832 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700833 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700834
835 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
836 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
837 << "failed to find entry";
838 }
839 }
840
841 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700842 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800843 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 }
845
846 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700847 if (obj != nullptr) {
848 ScopedObjectAccess soa(env);
849 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700850 }
851 }
852
853 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700854 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800855 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800856 // Check for null after decoding the object to handle cleared weak globals.
857 if (decoded_obj == nullptr) {
858 return nullptr;
859 }
860 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 }
862
863 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800864 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 return;
866 }
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700867 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -0800868 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700869
Ian Rogersef28b142012-11-30 14:22:18 -0800870 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700871 if (!locals.Remove(cookie, obj)) {
872 // Attempting to delete a local reference that is not in the
873 // topmost local reference frame is a no-op. DeleteLocalRef returns
874 // void and doesn't throw any exceptions, but we should probably
875 // complain about it so the user will notice that things aren't
876 // going quite the way they expect.
877 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
878 << "failed to find entry";
879 }
880 }
881
882 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700883 if (obj1 == obj2) {
884 return JNI_TRUE;
885 } else {
886 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800887 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
888 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700889 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 }
891
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700892 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700893 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700894 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800895 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800896 if (c == nullptr) {
897 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700898 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700899 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 }
901
Ian Rogersbc939662013-08-15 10:26:54 -0700902 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700905 CHECK_NON_NULL_ARGUMENT(java_class);
906 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700907 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 va_end(args);
909 return result;
910 }
911
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700913 CHECK_NON_NULL_ARGUMENT(java_class);
914 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700915 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800916 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800917 if (c == nullptr) {
918 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700919 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800920 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800921 if (result == nullptr) {
922 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700923 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700924 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700925 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800926 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800927 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700928 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800929 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700933 CHECK_NON_NULL_ARGUMENT(java_class);
934 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800936 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800937 if (c == nullptr) {
938 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700939 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800940 mirror::Object* result = c->AllocObject(soa.Self());
941 if (result == nullptr) {
942 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700943 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800946 if (soa.Self()->IsExceptionPending()) {
947 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700948 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800949 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 }
951
Ian Rogersbc939662013-08-15 10:26:54 -0700952 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700953 CHECK_NON_NULL_ARGUMENT(java_class);
954 CHECK_NON_NULL_ARGUMENT(name);
955 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700957 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Ian Rogersbc939662013-08-15 10:26:54 -0700960 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
961 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700962 CHECK_NON_NULL_ARGUMENT(java_class);
963 CHECK_NON_NULL_ARGUMENT(name);
964 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700965 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700966 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 va_list ap;
971 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700972 CHECK_NON_NULL_ARGUMENT(obj);
973 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700974 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700975 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 }
979
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700981 CHECK_NON_NULL_ARGUMENT(obj);
982 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700983 ScopedObjectAccess soa(env);
984 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
985 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700989 CHECK_NON_NULL_ARGUMENT(obj);
990 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700991 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700992 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
993 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 va_list ap;
999 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001000 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1001 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001002 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001005 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001009 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1010 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 ScopedObjectAccess soa(env);
1012 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001016 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1017 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001018 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001019 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1020 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 va_list ap;
1025 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001026 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1027 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001028 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001031 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001035 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1036 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 ScopedObjectAccess soa(env);
1038 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001042 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1043 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001044 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001045 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1046 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 }
1048
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 va_list ap;
1051 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001052 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1053 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001054 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001055 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001057 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list 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);
1064 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001068 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1069 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001070 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001071 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1072 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 va_list ap;
1077 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001078 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1079 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001080 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001083 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001087 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1088 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001089 ScopedObjectAccess soa(env);
1090 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 }
1092
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001094 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1095 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001096 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001097 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1098 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 va_list ap;
1103 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001104 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1105 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001106 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001107 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001109 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001113 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1114 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001115 ScopedObjectAccess soa(env);
1116 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001120 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1121 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001122 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001123 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1124 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 va_list ap;
1129 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001130 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1131 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001132 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001133 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001135 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001139 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1140 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001141 ScopedObjectAccess soa(env);
1142 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001146 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1147 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001149 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1150 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_list ap;
1155 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001156 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1157 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001158 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001159 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001161 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001165 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1166 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001167 ScopedObjectAccess soa(env);
1168 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001172 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1173 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001174 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001175 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1176 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 va_list ap;
1181 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001182 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1183 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001184 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001185 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001187 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001191 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1192 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001193 ScopedObjectAccess soa(env);
1194 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001198 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1199 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001200 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001201 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1202 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 va_list ap;
1207 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001208 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1209 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001210 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001211 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001216 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1217 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001218 ScopedObjectAccess soa(env);
1219 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001223 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1224 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001225 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001226 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001229 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001232 CHECK_NON_NULL_ARGUMENT(obj);
1233 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001234 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001235 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1236 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 va_end(ap);
1238 return local_result;
1239 }
1240
Ian Rogersbc939662013-08-15 10:26:54 -07001241 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1242 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001243 CHECK_NON_NULL_ARGUMENT(obj);
1244 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001245 ScopedObjectAccess soa(env);
1246 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1247 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
Ian Rogersbc939662013-08-15 10:26:54 -07001250 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1251 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001252 CHECK_NON_NULL_ARGUMENT(obj);
1253 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001254 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001255 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001256 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
Ian Rogersbc939662013-08-15 10:26:54 -07001259 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1260 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001263 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1264 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001265 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001266 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001268 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
Ian Rogersbc939662013-08-15 10:26:54 -07001271 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1272 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001273 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1274 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001275 ScopedObjectAccess soa(env);
1276 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 }
1278
Ian Rogersbc939662013-08-15 10:26:54 -07001279 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1280 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001281 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1282 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001283 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001284 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001287 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001289 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001290 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1291 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001292 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001293 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001295 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 }
1297
Ian Rogersbc939662013-08-15 10:26:54 -07001298 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1299 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001300 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1301 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001302 ScopedObjectAccess soa(env);
1303 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
Ian Rogersbc939662013-08-15 10:26:54 -07001306 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1307 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001308 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1309 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001310 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001311 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001314 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001317 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1318 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001319 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001320 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001322 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
Ian Rogersbc939662013-08-15 10:26:54 -07001325 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1326 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001327 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1328 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001329 ScopedObjectAccess soa(env);
1330 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
Ian Rogersbc939662013-08-15 10:26:54 -07001333 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1334 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001335 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1336 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001337 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001338 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001341 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001343 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001344 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1345 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001346 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001347 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001349 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
Ian Rogersbc939662013-08-15 10:26:54 -07001352 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1353 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001354 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1355 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001356 ScopedObjectAccess soa(env);
1357 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 }
1359
Ian Rogersbc939662013-08-15 10:26:54 -07001360 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1361 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001362 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1363 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001364 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001365 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
1367
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001368 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001370 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001371 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1372 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001373 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001374 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001376 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
Ian Rogersbc939662013-08-15 10:26:54 -07001379 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1380 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001381 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1382 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001383 ScopedObjectAccess soa(env);
1384 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
1386
Ian Rogersbc939662013-08-15 10:26:54 -07001387 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1388 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001389 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1390 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001391 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001392 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001395 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001397 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001398 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1399 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001400 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001401 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001403 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Ian Rogersbc939662013-08-15 10:26:54 -07001406 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1407 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001408 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1409 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001410 ScopedObjectAccess soa(env);
1411 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
Ian Rogersbc939662013-08-15 10:26:54 -07001414 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1415 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001416 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1417 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001418 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001419 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001422 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001424 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001425 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1426 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001427 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001428 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001430 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
1432
Ian Rogersbc939662013-08-15 10:26:54 -07001433 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1434 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001435 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1436 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001437 ScopedObjectAccess soa(env);
1438 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 }
1440
Ian Rogersbc939662013-08-15 10:26:54 -07001441 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1442 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001443 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1444 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001445 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001446 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 }
1448
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001449 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001451 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001452 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1453 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001454 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001455 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001457 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Ian Rogersbc939662013-08-15 10:26:54 -07001460 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1461 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001462 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1463 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001464 ScopedObjectAccess soa(env);
1465 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Ian Rogersbc939662013-08-15 10:26:54 -07001468 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1469 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001470 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1471 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001472 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001473 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001476 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001478 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001479 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1480 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001481 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001482 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 va_end(ap);
1484 }
1485
Brian Carlstromea46f952013-07-30 01:26:50 -07001486 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1487 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001488 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1489 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001490 ScopedObjectAccess soa(env);
1491 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 }
1493
Ian Rogersbc939662013-08-15 10:26:54 -07001494 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1495 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001496 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1497 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001498 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001499 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
Ian Rogersbc939662013-08-15 10:26:54 -07001502 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001503 CHECK_NON_NULL_ARGUMENT(java_class);
1504 CHECK_NON_NULL_ARGUMENT(name);
1505 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001506 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001507 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001509
Ian Rogersbc939662013-08-15 10:26:54 -07001510 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1511 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001512 CHECK_NON_NULL_ARGUMENT(java_class);
1513 CHECK_NON_NULL_ARGUMENT(name);
1514 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001515 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001516 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001518
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001520 CHECK_NON_NULL_ARGUMENT(obj);
1521 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001522 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001523 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1524 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001525 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001527
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001529 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001530 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001531 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001532 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001536 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1537 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001538 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001539 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1540 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1541 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001542 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001546 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001547 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001548 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1549 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001550 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001553#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001554 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1555 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001556 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001557 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1558 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001559 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001560
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001561#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001562 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001563 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001564 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001565 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001566
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001567#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001568 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1569 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001570 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001571 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1572 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001573 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001575#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001576 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001577 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001578 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001579 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001580
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001581 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001582 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001585 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001586 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001589 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001590 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001593 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001594 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001597 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001598 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001601 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001602 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001605 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001606 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001609 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001610 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001613 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001614 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 }
1616
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001617 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001618 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001621 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001622 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001626 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001629 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001630 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 }
1632
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001633 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001634 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001635 }
1636
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001637 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001638 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001639 }
1640
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001641 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001642 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001643 }
1644
1645 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001646 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001647 }
1648
1649 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001650 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001651 }
1652
1653 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001654 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001655 }
1656
1657 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001658 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001659 }
1660
1661 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001662 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001663 }
1664
1665 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001666 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001667 }
1668
1669 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001670 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001671 }
1672
1673 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001674 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001675 }
1676
1677 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001678 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001679 }
1680
1681 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001682 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001683 }
1684
1685 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001686 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001687 }
1688
1689 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001690 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001691 }
1692
1693 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001694 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001695 }
1696
1697 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001698 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001699 }
1700
1701 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001702 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001703 }
1704
1705 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001706 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 }
1708
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001709 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001711 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001712 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001713 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001714 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001715 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 va_end(ap);
1717 return local_result;
1718 }
1719
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001720 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001721 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001722 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001723 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001724 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001727 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001728 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001729 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001730 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001731 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001734 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001736 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001737 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001738 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001739 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001741 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 }
1743
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001744 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001745 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001746 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001747 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001750 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001751 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001752 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001753 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 }
1755
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001756 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001758 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001759 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001760 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001761 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001763 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 }
1765
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001766 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001767 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001768 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001769 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 }
1771
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001772 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001773 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001774 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001775 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 }
1777
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001778 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001780 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001781 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001782 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001783 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001785 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 }
1787
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001788 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001789 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001790 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001791 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 }
1793
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001794 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001795 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001796 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001797 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001800 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001802 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001803 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001804 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001805 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001807 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001810 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001811 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001812 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001813 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 }
1815
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001816 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001817 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001818 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001819 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001822 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001824 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001825 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001826 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001827 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001829 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 }
1831
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001832 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001833 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001834 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001835 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001838 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001839 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001840 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001841 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 }
1843
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001844 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001846 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001847 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001848 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001849 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001851 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 }
1853
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001854 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001855 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001856 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001857 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 }
1859
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001860 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001861 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001862 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001863 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 }
1865
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001866 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001868 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001869 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001870 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001871 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001873 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 }
1875
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001876 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001877 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001878 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001879 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 }
1881
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001882 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001883 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001885 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001886 }
1887
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001888 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001890 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001891 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001892 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001893 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001894 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001895 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001896 }
1897
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001898 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001899 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001900 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001901 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001902 }
1903
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001904 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001905 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001906 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001907 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001908 }
1909
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001910 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001912 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001913 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001914 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001915 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001916 va_end(ap);
1917 }
1918
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001919 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001920 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001921 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001922 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 }
1924
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001925 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001926 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001927 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001928 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001929 }
1930
Elliott Hughes814e4032011-08-23 12:07:56 -07001931 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001932 if (UNLIKELY(char_count < 0)) {
1933 JniAbortF("NewString", "char_count < 0: %d", char_count);
1934 return nullptr;
1935 }
1936 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1937 JniAbortF("NewString", "chars == null && char_count > 0");
1938 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001939 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001940 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001941 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001942 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
1945 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001946 if (utf == nullptr) {
1947 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001949 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001950 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001951 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 }
1953
Elliott Hughes814e4032011-08-23 12:07:56 -07001954 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001955 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001956 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001957 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001958 }
1959
1960 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001961 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001962 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001963 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001964 }
1965
Ian Rogersbc939662013-08-15 10:26:54 -07001966 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1967 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001968 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001969 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001970 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001971 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001972 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001973 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001974 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001975 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1976 memcpy(buf, chars + start, length * sizeof(jchar));
1977 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001978 }
1979
Ian Rogersbc939662013-08-15 10:26:54 -07001980 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1981 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001982 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001983 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001984 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001985 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001986 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001987 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001988 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001989 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1990 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1991 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001992 }
1993
Elliott Hughes75770752011-08-24 17:52:38 -07001994 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001995 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001996 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001997 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1998 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001999 PinPrimitiveArray(soa, chars);
Fred Shih56890e22014-06-02 11:11:52 -07002000 gc::Heap* heap = Runtime::Current()->GetHeap();
2001 if (heap->IsMovableObject(chars)) {
2002 if (is_copy != nullptr) {
2003 *is_copy = JNI_TRUE;
2004 }
2005 int32_t char_count = s->GetLength();
2006 int32_t offset = s->GetOffset();
2007 jchar* bytes = new jchar[char_count];
2008 for (int32_t i = 0; i < char_count; i++) {
2009 bytes[i] = chars->Get(i + offset);
2010 }
2011 return bytes;
2012 } else {
2013 if (is_copy != nullptr) {
2014 *is_copy = JNI_FALSE;
2015 }
2016 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07002017 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002018 }
2019
Mathieu Chartier590fee92013-09-13 13:46:47 -07002020 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002021 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002022 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07002023 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2024 mirror::CharArray* s_chars = s->GetCharArray();
2025 if (chars != (s_chars->GetData() + s->GetOffset())) {
2026 delete[] chars;
2027 }
2028 UnpinPrimitiveArray(soa, s->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes75770752011-08-24 17:52:38 -07002031 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07002032 CHECK_NON_NULL_ARGUMENT(java_string);
2033 ScopedObjectAccess soa(env);
2034 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2035 mirror::CharArray* chars = s->GetCharArray();
2036 int32_t offset = s->GetOffset();
2037 PinPrimitiveArray(soa, chars);
2038 gc::Heap* heap = Runtime::Current()->GetHeap();
2039 if (heap->IsMovableObject(chars)) {
2040 StackHandleScope<1> hs(soa.Self());
2041 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
2042 heap->IncrementDisableMovingGC(soa.Self());
2043 }
2044 if (is_copy != nullptr) {
2045 *is_copy = JNI_FALSE;
2046 }
2047 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Elliott Hughes75770752011-08-24 17:52:38 -07002050 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07002051 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
2052 ScopedObjectAccess soa(env);
2053 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
2054 gc::Heap* heap = Runtime::Current()->GetHeap();
2055 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2056 mirror::CharArray* s_chars = s->GetCharArray();
2057 if (heap->IsMovableObject(s_chars)) {
2058 heap->DecrementDisableMovingGC(soa.Self());
2059 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Elliott Hughes75770752011-08-24 17:52:38 -07002062 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002063 if (java_string == nullptr) {
2064 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002065 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002066 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002067 *is_copy = JNI_TRUE;
2068 }
Ian Rogersef28b142012-11-30 14:22:18 -08002069 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002070 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002071 size_t byte_count = s->GetUtfLength();
2072 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002073 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002074 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2075 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2076 bytes[byte_count] = '\0';
2077 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002078 }
2079
Elliott Hughes75770752011-08-24 17:52:38 -07002080 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002081 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002082 }
2083
Elliott Hughesbd935992011-08-22 11:59:34 -07002084 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002085 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002086 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002087 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002088 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002089 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2090 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002091 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002092 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002096 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002097 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002098 mirror::ObjectArray<mirror::Object>* array =
2099 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002100 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Ian Rogersbc939662013-08-15 10:26:54 -07002103 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2104 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002105 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002106 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002107 mirror::ObjectArray<mirror::Object>* array =
2108 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2109 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002110 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
2113 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002114 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 }
2116
2117 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002118 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
2121 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002122 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 }
2124
2125 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002126 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
2129 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002130 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
2133 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002134 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 }
2136
2137 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002138 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140
Ian Rogers1d99e452014-01-02 17:36:41 -08002141 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2142 jobject initial_element) {
2143 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002144 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002145 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002146 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002147 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002148
2149 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002150 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002151 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002152 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002153 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002154 if (UNLIKELY(element_class->IsPrimitive())) {
2155 JniAbortF("NewObjectArray", "not an object type: %s",
2156 PrettyDescriptor(element_class).c_str());
2157 return nullptr;
2158 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002159 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002160 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002161 if (UNLIKELY(array_class == nullptr)) {
2162 return nullptr;
2163 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 }
2165
Elliott Hughes75770752011-08-24 17:52:38 -07002166 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002167 mirror::ObjectArray<mirror::Object>* result =
2168 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002169 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002170 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002171 if (initial_object != nullptr) {
2172 mirror::Class* element_class = result->GetClass()->GetComponentType();
2173 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2174 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2175 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2176 PrettyDescriptor(element_class).c_str());
2177
2178 } else {
2179 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002180 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002181 }
2182 }
Elliott Hughes75770752011-08-24 17:52:38 -07002183 }
2184 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002185 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
2188 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002189 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Ian Rogersa15e67d2012-02-28 13:51:55 -08002192 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002193 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002194 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002195 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002196 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2197 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2198 PrettyDescriptor(array->GetClass()).c_str());
2199 return nullptr;
2200 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002201 gc::Heap* heap = Runtime::Current()->GetHeap();
2202 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002203 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002204 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002205 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002206 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002207 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002208 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002209 *is_copy = JNI_FALSE;
2210 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002211 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002212 }
2213
Ian Rogers2d10b202014-05-12 19:15:18 -07002214 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2215 jint mode) {
2216 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2217 ScopedObjectAccess soa(env);
2218 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2219 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2220 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2221 PrettyDescriptor(array->GetClass()).c_str());
2222 return;
2223 }
2224 const size_t component_size = array->GetClass()->GetComponentSize();
2225 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002226 }
2227
Elliott Hughes75770752011-08-24 17:52:38 -07002228 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002229 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
2231
Elliott Hughes75770752011-08-24 17:52:38 -07002232 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002233 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Elliott Hughes75770752011-08-24 17:52:38 -07002236 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002237 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239
Elliott Hughes75770752011-08-24 17:52:38 -07002240 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002241 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Elliott Hughes75770752011-08-24 17:52:38 -07002244 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002245 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 }
2247
Elliott Hughes75770752011-08-24 17:52:38 -07002248 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002249 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Elliott Hughes75770752011-08-24 17:52:38 -07002252 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002253 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
Elliott Hughes75770752011-08-24 17:52:38 -07002256 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002257 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002258 }
2259
Mathieu Chartier590fee92013-09-13 13:46:47 -07002260 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2261 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002262 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2263 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002264 }
2265
Mathieu Chartier590fee92013-09-13 13:46:47 -07002266 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002267 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 }
2269
Mathieu Chartier590fee92013-09-13 13:46:47 -07002270 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002271 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273
Mathieu Chartier590fee92013-09-13 13:46:47 -07002274 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2275 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002276 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 }
2278
Mathieu Chartier590fee92013-09-13 13:46:47 -07002279 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2280 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002281 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002282 }
2283
Mathieu Chartier590fee92013-09-13 13:46:47 -07002284 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002285 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002286 }
2287
Mathieu Chartier590fee92013-09-13 13:46:47 -07002288 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002289 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 }
2291
Mathieu Chartier590fee92013-09-13 13:46:47 -07002292 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2293 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002294 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002295 }
2296
Ian Rogersbc939662013-08-15 10:26:54 -07002297 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2298 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002299 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002300 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002301 }
2302
Ian Rogersbc939662013-08-15 10:26:54 -07002303 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2304 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002305 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 }
2307
Ian Rogersbc939662013-08-15 10:26:54 -07002308 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2309 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002310 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 }
2312
Ian Rogersbc939662013-08-15 10:26:54 -07002313 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2314 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002315 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002316 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002317 }
2318
Ian Rogersbc939662013-08-15 10:26:54 -07002319 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2320 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002321 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002322 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002323 }
2324
Ian Rogersbc939662013-08-15 10:26:54 -07002325 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2326 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002327 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002328 }
2329
Ian Rogersbc939662013-08-15 10:26:54 -07002330 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2331 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002332 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002333 }
2334
Ian Rogersbc939662013-08-15 10:26:54 -07002335 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2336 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002337 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002338 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002339 }
2340
Ian Rogersbc939662013-08-15 10:26:54 -07002341 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2342 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002343 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002344 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002345 }
2346
Ian Rogersbc939662013-08-15 10:26:54 -07002347 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2348 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002349 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 }
2351
Ian Rogersbc939662013-08-15 10:26:54 -07002352 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2353 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002354 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002355 }
2356
Ian Rogersbc939662013-08-15 10:26:54 -07002357 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2358 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002359 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002360 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002361 }
2362
Ian Rogersbc939662013-08-15 10:26:54 -07002363 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2364 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002365 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002366 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002367 }
2368
Ian Rogersbc939662013-08-15 10:26:54 -07002369 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2370 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002371 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002372 }
2373
Ian Rogersbc939662013-08-15 10:26:54 -07002374 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2375 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002376 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002377 }
2378
Ian Rogersbc939662013-08-15 10:26:54 -07002379 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2380 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002381 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002382 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002383 }
2384
Ian Rogersbc939662013-08-15 10:26:54 -07002385 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2386 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002387 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2388 }
2389
Ian Rogersbc939662013-08-15 10:26:54 -07002390 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2391 jint method_count, bool return_errors) {
2392 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002393 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002394 return JNI_ERR; // Not reached.
2395 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002396 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002397 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002398 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002399 if (UNLIKELY(method_count == 0)) {
2400 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2401 << PrettyDescriptor(c);
2402 return JNI_OK;
2403 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002404 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002405 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002406 const char* name = methods[i].name;
2407 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002408 const void* fnPtr = methods[i].fnPtr;
2409 if (UNLIKELY(name == nullptr)) {
2410 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2411 return JNI_ERR;
2412 } else if (UNLIKELY(sig == nullptr)) {
2413 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2414 return JNI_ERR;
2415 } else if (UNLIKELY(fnPtr == nullptr)) {
2416 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2417 return JNI_ERR;
2418 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002419 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002420 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002421 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002422 ++sig;
2423 }
2424
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002425 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2426 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002427 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002428 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002429 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002430 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002431 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002432 << PrettyDescriptor(c) << "." << name << sig << " in "
2433 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002434 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002435 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002436 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002437 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002438 << PrettyDescriptor(c) << "." << name << sig
2439 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002440 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002441 return JNI_ERR;
2442 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002443
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002444 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002445
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002446 m->RegisterNative(soa.Self(), fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002447 }
2448 return JNI_OK;
2449 }
2450
Elliott Hughes5174fe62011-08-23 15:12:35 -07002451 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002452 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002453 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002454 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002455
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002456 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002457
Ian Rogers2d10b202014-05-12 19:15:18 -07002458 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002459 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002460 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002461 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002462 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002463 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002464 }
2465 }
2466 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002467 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002468 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002469 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002470 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002471 }
2472 }
2473
Ian Rogers2d10b202014-05-12 19:15:18 -07002474 if (unregistered_count == 0) {
2475 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2476 << PrettyDescriptor(c) << "' that contains no native methods";
2477 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002478 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002479 }
2480
Ian Rogers719d1a32014-03-06 12:13:39 -08002481 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002482 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002483 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002484 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2485 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002486 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002487 return JNI_ERR;
2488 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002489 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002490 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002491 }
2492
Ian Rogers719d1a32014-03-06 12:13:39 -08002493 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002494 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002495 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002496 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002497 o->MonitorExit(soa.Self());
2498 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002499 return JNI_ERR;
2500 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002501 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002502 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002503 }
2504
2505 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002506 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002507 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002508 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002509 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002510 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002511 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002512 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002513 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002514 }
2515
Elliott Hughescdf53122011-08-19 15:46:09 -07002516 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002517 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002518 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002519 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002520 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002521 if (address == nullptr && capacity != 0) {
2522 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002523 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002524 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002525
Brian Carlstrom85a93362014-06-25 09:30:52 -07002526 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002527 if (capacity > INT_MAX) {
2528 JniAbortF("NewDirectByteBuffer", "buffer capacity greater than maximum jint: %" PRId64, capacity);
2529 return nullptr;
2530 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002531 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002532 jint capacity_arg = static_cast<jint>(capacity);
2533
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002534 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2535 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002536 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002537 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002538 }
2539
Elliott Hughesb465ab02011-08-24 11:21:21 -07002540 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002541 return reinterpret_cast<void*>(env->GetLongField(
2542 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002543 }
2544
Elliott Hughesb465ab02011-08-24 11:21:21 -07002545 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002546 return static_cast<jlong>(env->GetIntField(
2547 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002548 }
2549
Elliott Hughesb465ab02011-08-24 11:21:21 -07002550 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002551 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002552
2553 // Do we definitely know what kind of reference this is?
2554 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2555 IndirectRefKind kind = GetIndirectRefKind(ref);
2556 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002557 case kLocal: {
2558 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002559 // The local refs don't need a read barrier.
2560 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2561 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002562 return JNILocalRefType;
2563 }
2564 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002565 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002566 case kGlobal:
2567 return JNIGlobalRefType;
2568 case kWeakGlobal:
2569 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002570 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002571 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002572 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002573 return JNILocalRefType;
2574 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002575 return JNIInvalidRefType;
2576 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002577 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2578 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002579 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002580
2581 private:
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002582 static jint EnsureLocalCapacity(ScopedObjectAccess& soa, jint desired_capacity,
2583 const char* caller) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002584 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002585 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002586 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2587 return JNI_ERR;
2588 }
2589 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002590 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002591 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2592 if (!okay) {
2593 soa.Self()->ThrowOutOfMemoryError(caller);
2594 }
2595 return okay ? JNI_OK : JNI_ERR;
2596 }
2597
2598 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002599 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002600 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002601 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002602 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002603 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002604 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002605 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002606 return soa.AddLocalReference<JniT>(result);
2607 }
2608
Ian Rogers2d10b202014-05-12 19:15:18 -07002609 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2610 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2611 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002612 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002613 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002614 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2615 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2616 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2617 PrettyDescriptor(array->GetClass()).c_str());
2618 return nullptr;
2619 }
2620 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2621 return array;
2622 }
2623
2624 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2625 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2626 CHECK_NON_NULL_ARGUMENT(java_array);
2627 ScopedObjectAccess soa(env);
2628 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2629 "GetArrayElements",
2630 "get");
2631 if (UNLIKELY(array == nullptr)) {
2632 return nullptr;
2633 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002634 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002635 // Only make a copy if necessary.
2636 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2637 if (is_copy != nullptr) {
2638 *is_copy = JNI_TRUE;
2639 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002640 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002641 size_t size = array->GetLength() * component_size;
2642 void* data = new uint64_t[RoundUp(size, 8) / 8];
2643 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002644 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002645 } else {
2646 if (is_copy != nullptr) {
2647 *is_copy = JNI_FALSE;
2648 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002649 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002650 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002651 }
2652
Ian Rogers2d10b202014-05-12 19:15:18 -07002653 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002654 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002655 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002656 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002657 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2658 "ReleaseArrayElements",
2659 "release");
2660 if (array == nullptr) {
2661 return;
2662 }
2663 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2664 }
2665
2666 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2667 size_t component_size, void* elements, jint mode)
2668 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002669 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002670 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002671 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002672 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002673 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2674 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002675 if (is_copy) {
2676 // Sanity check: If elements is not the same as the java array's data, it better not be a
2677 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2678 // copies we make?
2679 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2680 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2681 reinterpret_cast<void*>(elements), array_data);
2682 return;
2683 }
2684 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002685 // Don't need to copy if we had a direct pointer.
2686 if (mode != JNI_ABORT && is_copy) {
2687 memcpy(array_data, elements, bytes);
2688 }
2689 if (mode != JNI_COMMIT) {
2690 if (is_copy) {
2691 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002692 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002693 // Non copy to a movable object must means that we had disabled the moving GC.
2694 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002695 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002696 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002697 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002698 }
2699
Ian Rogers2d10b202014-05-12 19:15:18 -07002700 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2701 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2702 jsize start, jsize length, ElementT* buf) {
2703 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2704 ScopedObjectAccess soa(env);
2705 ArtArrayT* array =
2706 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2707 "GetPrimitiveArrayRegion",
2708 "get region of");
2709 if (array != nullptr) {
2710 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2711 ThrowAIOOBE(soa, array, start, length, "src");
2712 } else {
2713 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2714 ElementT* data = array->GetData();
2715 memcpy(buf, data + start, length * sizeof(ElementT));
2716 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002717 }
2718 }
2719
Ian Rogers2d10b202014-05-12 19:15:18 -07002720 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2721 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2722 jsize start, jsize length, const ElementT* buf) {
2723 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2724 ScopedObjectAccess soa(env);
2725 ArtArrayT* array =
2726 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2727 "SetPrimitiveArrayRegion",
2728 "set region of");
2729 if (array != nullptr) {
2730 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2731 ThrowAIOOBE(soa, array, start, length, "dst");
2732 } else {
2733 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2734 ElementT* data = array->GetData();
2735 memcpy(data + start, buf, length * sizeof(ElementT));
2736 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002737 }
2738 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002739};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002740
Elliott Hughes88c5c352012-03-15 18:49:48 -07002741const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002742 nullptr, // reserved0.
2743 nullptr, // reserved1.
2744 nullptr, // reserved2.
2745 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002746 JNI::GetVersion,
2747 JNI::DefineClass,
2748 JNI::FindClass,
2749 JNI::FromReflectedMethod,
2750 JNI::FromReflectedField,
2751 JNI::ToReflectedMethod,
2752 JNI::GetSuperclass,
2753 JNI::IsAssignableFrom,
2754 JNI::ToReflectedField,
2755 JNI::Throw,
2756 JNI::ThrowNew,
2757 JNI::ExceptionOccurred,
2758 JNI::ExceptionDescribe,
2759 JNI::ExceptionClear,
2760 JNI::FatalError,
2761 JNI::PushLocalFrame,
2762 JNI::PopLocalFrame,
2763 JNI::NewGlobalRef,
2764 JNI::DeleteGlobalRef,
2765 JNI::DeleteLocalRef,
2766 JNI::IsSameObject,
2767 JNI::NewLocalRef,
2768 JNI::EnsureLocalCapacity,
2769 JNI::AllocObject,
2770 JNI::NewObject,
2771 JNI::NewObjectV,
2772 JNI::NewObjectA,
2773 JNI::GetObjectClass,
2774 JNI::IsInstanceOf,
2775 JNI::GetMethodID,
2776 JNI::CallObjectMethod,
2777 JNI::CallObjectMethodV,
2778 JNI::CallObjectMethodA,
2779 JNI::CallBooleanMethod,
2780 JNI::CallBooleanMethodV,
2781 JNI::CallBooleanMethodA,
2782 JNI::CallByteMethod,
2783 JNI::CallByteMethodV,
2784 JNI::CallByteMethodA,
2785 JNI::CallCharMethod,
2786 JNI::CallCharMethodV,
2787 JNI::CallCharMethodA,
2788 JNI::CallShortMethod,
2789 JNI::CallShortMethodV,
2790 JNI::CallShortMethodA,
2791 JNI::CallIntMethod,
2792 JNI::CallIntMethodV,
2793 JNI::CallIntMethodA,
2794 JNI::CallLongMethod,
2795 JNI::CallLongMethodV,
2796 JNI::CallLongMethodA,
2797 JNI::CallFloatMethod,
2798 JNI::CallFloatMethodV,
2799 JNI::CallFloatMethodA,
2800 JNI::CallDoubleMethod,
2801 JNI::CallDoubleMethodV,
2802 JNI::CallDoubleMethodA,
2803 JNI::CallVoidMethod,
2804 JNI::CallVoidMethodV,
2805 JNI::CallVoidMethodA,
2806 JNI::CallNonvirtualObjectMethod,
2807 JNI::CallNonvirtualObjectMethodV,
2808 JNI::CallNonvirtualObjectMethodA,
2809 JNI::CallNonvirtualBooleanMethod,
2810 JNI::CallNonvirtualBooleanMethodV,
2811 JNI::CallNonvirtualBooleanMethodA,
2812 JNI::CallNonvirtualByteMethod,
2813 JNI::CallNonvirtualByteMethodV,
2814 JNI::CallNonvirtualByteMethodA,
2815 JNI::CallNonvirtualCharMethod,
2816 JNI::CallNonvirtualCharMethodV,
2817 JNI::CallNonvirtualCharMethodA,
2818 JNI::CallNonvirtualShortMethod,
2819 JNI::CallNonvirtualShortMethodV,
2820 JNI::CallNonvirtualShortMethodA,
2821 JNI::CallNonvirtualIntMethod,
2822 JNI::CallNonvirtualIntMethodV,
2823 JNI::CallNonvirtualIntMethodA,
2824 JNI::CallNonvirtualLongMethod,
2825 JNI::CallNonvirtualLongMethodV,
2826 JNI::CallNonvirtualLongMethodA,
2827 JNI::CallNonvirtualFloatMethod,
2828 JNI::CallNonvirtualFloatMethodV,
2829 JNI::CallNonvirtualFloatMethodA,
2830 JNI::CallNonvirtualDoubleMethod,
2831 JNI::CallNonvirtualDoubleMethodV,
2832 JNI::CallNonvirtualDoubleMethodA,
2833 JNI::CallNonvirtualVoidMethod,
2834 JNI::CallNonvirtualVoidMethodV,
2835 JNI::CallNonvirtualVoidMethodA,
2836 JNI::GetFieldID,
2837 JNI::GetObjectField,
2838 JNI::GetBooleanField,
2839 JNI::GetByteField,
2840 JNI::GetCharField,
2841 JNI::GetShortField,
2842 JNI::GetIntField,
2843 JNI::GetLongField,
2844 JNI::GetFloatField,
2845 JNI::GetDoubleField,
2846 JNI::SetObjectField,
2847 JNI::SetBooleanField,
2848 JNI::SetByteField,
2849 JNI::SetCharField,
2850 JNI::SetShortField,
2851 JNI::SetIntField,
2852 JNI::SetLongField,
2853 JNI::SetFloatField,
2854 JNI::SetDoubleField,
2855 JNI::GetStaticMethodID,
2856 JNI::CallStaticObjectMethod,
2857 JNI::CallStaticObjectMethodV,
2858 JNI::CallStaticObjectMethodA,
2859 JNI::CallStaticBooleanMethod,
2860 JNI::CallStaticBooleanMethodV,
2861 JNI::CallStaticBooleanMethodA,
2862 JNI::CallStaticByteMethod,
2863 JNI::CallStaticByteMethodV,
2864 JNI::CallStaticByteMethodA,
2865 JNI::CallStaticCharMethod,
2866 JNI::CallStaticCharMethodV,
2867 JNI::CallStaticCharMethodA,
2868 JNI::CallStaticShortMethod,
2869 JNI::CallStaticShortMethodV,
2870 JNI::CallStaticShortMethodA,
2871 JNI::CallStaticIntMethod,
2872 JNI::CallStaticIntMethodV,
2873 JNI::CallStaticIntMethodA,
2874 JNI::CallStaticLongMethod,
2875 JNI::CallStaticLongMethodV,
2876 JNI::CallStaticLongMethodA,
2877 JNI::CallStaticFloatMethod,
2878 JNI::CallStaticFloatMethodV,
2879 JNI::CallStaticFloatMethodA,
2880 JNI::CallStaticDoubleMethod,
2881 JNI::CallStaticDoubleMethodV,
2882 JNI::CallStaticDoubleMethodA,
2883 JNI::CallStaticVoidMethod,
2884 JNI::CallStaticVoidMethodV,
2885 JNI::CallStaticVoidMethodA,
2886 JNI::GetStaticFieldID,
2887 JNI::GetStaticObjectField,
2888 JNI::GetStaticBooleanField,
2889 JNI::GetStaticByteField,
2890 JNI::GetStaticCharField,
2891 JNI::GetStaticShortField,
2892 JNI::GetStaticIntField,
2893 JNI::GetStaticLongField,
2894 JNI::GetStaticFloatField,
2895 JNI::GetStaticDoubleField,
2896 JNI::SetStaticObjectField,
2897 JNI::SetStaticBooleanField,
2898 JNI::SetStaticByteField,
2899 JNI::SetStaticCharField,
2900 JNI::SetStaticShortField,
2901 JNI::SetStaticIntField,
2902 JNI::SetStaticLongField,
2903 JNI::SetStaticFloatField,
2904 JNI::SetStaticDoubleField,
2905 JNI::NewString,
2906 JNI::GetStringLength,
2907 JNI::GetStringChars,
2908 JNI::ReleaseStringChars,
2909 JNI::NewStringUTF,
2910 JNI::GetStringUTFLength,
2911 JNI::GetStringUTFChars,
2912 JNI::ReleaseStringUTFChars,
2913 JNI::GetArrayLength,
2914 JNI::NewObjectArray,
2915 JNI::GetObjectArrayElement,
2916 JNI::SetObjectArrayElement,
2917 JNI::NewBooleanArray,
2918 JNI::NewByteArray,
2919 JNI::NewCharArray,
2920 JNI::NewShortArray,
2921 JNI::NewIntArray,
2922 JNI::NewLongArray,
2923 JNI::NewFloatArray,
2924 JNI::NewDoubleArray,
2925 JNI::GetBooleanArrayElements,
2926 JNI::GetByteArrayElements,
2927 JNI::GetCharArrayElements,
2928 JNI::GetShortArrayElements,
2929 JNI::GetIntArrayElements,
2930 JNI::GetLongArrayElements,
2931 JNI::GetFloatArrayElements,
2932 JNI::GetDoubleArrayElements,
2933 JNI::ReleaseBooleanArrayElements,
2934 JNI::ReleaseByteArrayElements,
2935 JNI::ReleaseCharArrayElements,
2936 JNI::ReleaseShortArrayElements,
2937 JNI::ReleaseIntArrayElements,
2938 JNI::ReleaseLongArrayElements,
2939 JNI::ReleaseFloatArrayElements,
2940 JNI::ReleaseDoubleArrayElements,
2941 JNI::GetBooleanArrayRegion,
2942 JNI::GetByteArrayRegion,
2943 JNI::GetCharArrayRegion,
2944 JNI::GetShortArrayRegion,
2945 JNI::GetIntArrayRegion,
2946 JNI::GetLongArrayRegion,
2947 JNI::GetFloatArrayRegion,
2948 JNI::GetDoubleArrayRegion,
2949 JNI::SetBooleanArrayRegion,
2950 JNI::SetByteArrayRegion,
2951 JNI::SetCharArrayRegion,
2952 JNI::SetShortArrayRegion,
2953 JNI::SetIntArrayRegion,
2954 JNI::SetLongArrayRegion,
2955 JNI::SetFloatArrayRegion,
2956 JNI::SetDoubleArrayRegion,
2957 JNI::RegisterNatives,
2958 JNI::UnregisterNatives,
2959 JNI::MonitorEnter,
2960 JNI::MonitorExit,
2961 JNI::GetJavaVM,
2962 JNI::GetStringRegion,
2963 JNI::GetStringUTFRegion,
2964 JNI::GetPrimitiveArrayCritical,
2965 JNI::ReleasePrimitiveArrayCritical,
2966 JNI::GetStringCritical,
2967 JNI::ReleaseStringCritical,
2968 JNI::NewWeakGlobalRef,
2969 JNI::DeleteWeakGlobalRef,
2970 JNI::ExceptionCheck,
2971 JNI::NewDirectByteBuffer,
2972 JNI::GetDirectBufferAddress,
2973 JNI::GetDirectBufferCapacity,
2974 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002975};
2976
Elliott Hughes75770752011-08-24 17:52:38 -07002977JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002978 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002979 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002980 local_ref_cookie(IRT_FIRST_SEGMENT),
2981 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002982 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002983 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002984 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002985 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002986 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002987 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002988 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002989}
2990
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002991JNIEnvExt::~JNIEnvExt() {
2992}
2993
Mathieu Chartier590fee92013-09-13 13:46:47 -07002994jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2995 if (obj == nullptr) {
2996 return nullptr;
2997 }
2998 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2999}
3000
3001void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3002 if (obj != nullptr) {
3003 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
3004 }
3005}
Elliott Hughes88c5c352012-03-15 18:49:48 -07003006void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
3007 check_jni = enabled;
3008 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003009}
3010
Elliott Hughes73e66f72012-05-09 09:34:45 -07003011void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
3012 locals.Dump(os);
3013 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003014}
3015
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07003016void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3017 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003018 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003019 stacked_local_ref_cookies.push_back(local_ref_cookie);
3020 local_ref_cookie = locals.GetSegmentState();
3021}
3022
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07003023void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003024 locals.SetSegmentState(local_ref_cookie);
3025 local_ref_cookie = stacked_local_ref_cookies.back();
3026 stacked_local_ref_cookies.pop_back();
3027}
3028
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003029Offset JNIEnvExt::SegmentStateOffset() {
3030 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
3031 IndirectReferenceTable::SegmentStateOffset().Int32Value());
3032}
3033
Carl Shapiroea4dca82011-08-01 13:45:38 -07003034// JNI Invocation interface.
3035
Brian Carlstrombddf9762013-05-14 11:35:37 -07003036extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003037 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07003038 if (IsBadJniVersion(args->version)) {
3039 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003040 return JNI_EVERSION;
3041 }
Ian Rogerse63db272014-07-15 15:36:11 -07003042 RuntimeOptions options;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003043 for (int i = 0; i < args->nOptions; ++i) {
3044 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08003045 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003046 }
3047 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003048 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003049 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003050 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003051 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003052 bool started = runtime->Start();
3053 if (!started) {
3054 delete Thread::Current()->GetJniEnv();
3055 delete runtime->GetJavaVM();
3056 LOG(WARNING) << "CreateJavaVM failed";
3057 return JNI_ERR;
3058 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003059 *p_env = Thread::Current()->GetJniEnv();
3060 *p_vm = runtime->GetJavaVM();
3061 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003062}
3063
Elliott Hughesf2682d52011-08-15 16:37:04 -07003064extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003065 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003066 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003067 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003068 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003069 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003070 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003071 }
3072 return JNI_OK;
3073}
3074
3075// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003076extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003077 return JNI_ERR;
3078}
3079
Elliott Hughescdf53122011-08-19 15:46:09 -07003080class JII {
3081 public:
3082 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003083 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003084 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003085 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003086 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3087 delete raw_vm->runtime;
3088 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003089 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003090
Elliott Hughescdf53122011-08-19 15:46:09 -07003091 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003092 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003093 }
3094
3095 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003096 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003097 }
3098
3099 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003100 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003101 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003102 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003103 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3104 Runtime* runtime = raw_vm->runtime;
3105 runtime->DetachCurrentThread();
3106 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003107 }
3108
3109 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003110 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3111 // and unlike other calls that take a JNI version doesn't care if you supply
3112 // JNI_VERSION_1_1, which we don't otherwise support.
3113 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003114 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003115 return JNI_EVERSION;
3116 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003117 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003118 return JNI_ERR;
3119 }
3120 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003121 if (thread == nullptr) {
3122 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003123 return JNI_EDETACHED;
3124 }
3125 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003126 return JNI_OK;
3127 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003128};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003129
Elliott Hughes88c5c352012-03-15 18:49:48 -07003130const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003131 nullptr, // reserved0
3132 nullptr, // reserved1
3133 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003134 JII::DestroyJavaVM,
3135 JII::AttachCurrentThread,
3136 JII::DetachCurrentThread,
3137 JII::GetEnv,
3138 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003139};
3140
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003141JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003142 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003143 check_jni_abort_hook(nullptr),
3144 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003145 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003146 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003147 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08003148 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003149 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003150 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003151 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003152 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003153 libraries(new Libraries),
3154 weak_globals_lock_("JNI weak global reference table lock"),
3155 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3156 allow_new_weak_globals_(true),
3157 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003158 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003159 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003160 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003161 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003162}
3163
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003164JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003165 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003166}
3167
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003168jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3169 if (obj == nullptr) {
3170 return nullptr;
3171 }
3172 MutexLock mu(self, weak_globals_lock_);
3173 while (UNLIKELY(!allow_new_weak_globals_)) {
3174 weak_globals_add_condition_.WaitHoldingLocks(self);
3175 }
3176 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3177 return reinterpret_cast<jweak>(ref);
3178}
3179
3180void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3181 MutexLock mu(self, weak_globals_lock_);
3182 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3183 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3184 << "failed to find entry";
3185 }
3186}
3187
Elliott Hughes88c5c352012-03-15 18:49:48 -07003188void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3189 check_jni = enabled;
3190 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003191}
3192
Elliott Hughesae80b492012-04-24 10:43:17 -07003193void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3194 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3195 if (force_copy) {
3196 os << " (with forcecopy)";
3197 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003198 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003199 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003200 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003201 os << "; pins=" << pin_table.Size();
3202 }
3203 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003204 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003205 os << "; globals=" << globals.Capacity();
3206 }
3207 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003208 MutexLock mu(self, weak_globals_lock_);
3209 if (weak_globals_.Capacity() > 0) {
3210 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003211 }
3212 }
3213 os << '\n';
3214
3215 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003216 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003217 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3218 }
3219}
3220
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003221void JavaVMExt::DisallowNewWeakGlobals() {
3222 MutexLock mu(Thread::Current(), weak_globals_lock_);
3223 allow_new_weak_globals_ = false;
3224}
3225
3226void JavaVMExt::AllowNewWeakGlobals() {
3227 Thread* self = Thread::Current();
3228 MutexLock mu(self, weak_globals_lock_);
3229 allow_new_weak_globals_ = true;
3230 weak_globals_add_condition_.Broadcast(self);
3231}
3232
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003233mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3234 MutexLock mu(self, weak_globals_lock_);
3235 while (UNLIKELY(!allow_new_weak_globals_)) {
3236 weak_globals_add_condition_.WaitHoldingLocks(self);
3237 }
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -07003238 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003239}
3240
Elliott Hughes73e66f72012-05-09 09:34:45 -07003241void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003242 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003243 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003244 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003245 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003246 }
3247 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003248 MutexLock mu(self, weak_globals_lock_);
3249 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003250 }
3251 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003252 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003253 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003254 }
3255}
3256
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003257bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07003258 Handle<mirror::ClassLoader> class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003259 std::string* detail) {
3260 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003261
3262 // See if we've already loaded this library. If we have, and the class loader
3263 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003264 // TODO: for better results we should canonicalize the pathname (or even compare
3265 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003266 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003267 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003268 {
3269 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003270 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003271 library = libraries->Get(path);
3272 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003273 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003274 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003275 // The library will be associated with class_loader. The JNI
3276 // spec says we can't load the same library into more than one
3277 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003278 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003279 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003280 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003281 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003282 return false;
3283 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003284 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003285 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003286 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003287 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003288 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003289 return false;
3290 }
3291 return true;
3292 }
3293
3294 // Open the shared library. Because we're using a full path, the system
3295 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3296 // resolve this library's dependencies though.)
3297
3298 // Failures here are expected when java.library.path has several entries
3299 // and we have to hunt for the lib.
3300
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003301 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3302 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3303 // dlopen) becomes zero from dlclose.
3304
Elliott Hughescdf53122011-08-19 15:46:09 -07003305 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003306 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003307 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Yong WU355383f2014-07-24 21:32:15 +08003308 const char* path_str = path.empty() ? nullptr : path.c_str();
3309 void* handle = dlopen(path_str, RTLD_LAZY);
3310 bool needs_native_bridge = false;
3311 if (handle == nullptr) {
3312 if (NativeBridge::IsSupported(path_str)) {
3313 handle = NativeBridge::LoadLibrary(path_str, RTLD_LAZY);
3314 needs_native_bridge = true;
3315 }
3316 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003317 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003318
Elliott Hughes84b2f142012-09-27 09:16:28 -07003319 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003320
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003321 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003322 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003323 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003324 return false;
3325 }
3326
3327 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003328 // TODO: move the locking (and more of this logic) into Libraries.
3329 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003330 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003331 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003332 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003333 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003334 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003335 libraries->Put(path, library);
3336 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003337 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003338 }
3339 if (!created_library) {
3340 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003341 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003342 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003343 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003344
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003345 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003346 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003347
Elliott Hughes79353722013-08-02 16:52:18 -07003348 bool was_successful = false;
Yong WU355383f2014-07-24 21:32:15 +08003349 void* sym = nullptr;
3350 if (UNLIKELY(needs_native_bridge)) {
3351 library->SetNeedsNativeBridge();
3352 sym = library->FindSymbolWithNativeBridge("JNI_OnLoad", nullptr);
3353 } else {
3354 sym = dlsym(handle, "JNI_OnLoad");
3355 }
3356
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003357 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003358 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003359 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003360 } else {
3361 // Call JNI_OnLoad. We have to override the current class
3362 // loader, which will always be "null" since the stuff at the
3363 // top of the stack is around Runtime.loadLibrary(). (See
3364 // the comments in the JNI FindClass function.)
3365 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3366 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003367 StackHandleScope<1> hs(self);
3368 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3369 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003370
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003371 int version = 0;
3372 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003373 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003374 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003375 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003376 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003377
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003378 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003379
Elliott Hughes79353722013-08-02 16:52:18 -07003380 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003381 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003382 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003383 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003384 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003385 // It's unwise to call dlclose() here, but we can mark it
3386 // as bad and ensure that future load attempts will fail.
3387 // We don't know how far JNI_OnLoad got, so there could
3388 // be some partially-initialized stuff accessible through
3389 // newly-registered native method calls. We could try to
3390 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003391 } else {
3392 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003393 }
Elliott Hughes79353722013-08-02 16:52:18 -07003394 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003395 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003396 }
3397
Elliott Hughes79353722013-08-02 16:52:18 -07003398 library->SetResult(was_successful);
3399 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003400}
3401
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003402void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003403 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003404 mirror::Class* c = m->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -07003405 // If this is a static method, it could be called before the class has been initialized.
Elliott Hughes79082e32011-08-25 12:07:32 -07003406 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003407 c = EnsureInitialized(Thread::Current(), c);
3408 if (c == nullptr) {
3409 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003410 }
3411 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003412 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003413 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003414 std::string detail;
3415 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003416 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003417 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003418 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003419 native_method = libraries->FindNativeMethod(m, detail);
3420 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003421 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003422 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003423 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3424 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003425 }
3426 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003427}
3428
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003429void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003430 MutexLock mu(Thread::Current(), weak_globals_lock_);
3431 for (mirror::Object** entry : weak_globals_) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003432 // Since this is called by the GC, we don't need a read barrier.
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003433 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003434 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003435 if (new_obj == nullptr) {
3436 new_obj = kClearedJniWeakGlobal;
3437 }
3438 *entry = new_obj;
3439 }
3440}
3441
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003442void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003443 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003444 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003445 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003446 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003447 }
3448 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003449 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003450 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003451 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003452 {
3453 MutexLock mu(self, libraries_lock);
3454 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003455 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003456 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003457 // The weak_globals table is visited by the GC itself (because it mutates the table).
3458}
3459
Elliott Hughesc8fece32013-01-02 11:27:23 -08003460void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003461 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003462 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003463 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003464 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3465 }
3466 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3467}
3468
Ian Rogersdf20fe02011-07-20 20:34:16 -07003469} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003470
3471std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3472 switch (rhs) {
3473 case JNIInvalidRefType:
3474 os << "JNIInvalidRefType";
3475 return os;
3476 case JNILocalRefType:
3477 os << "JNILocalRefType";
3478 return os;
3479 case JNIGlobalRefType:
3480 os << "JNIGlobalRefType";
3481 return os;
3482 case JNIWeakGlobalRefType:
3483 os << "JNIWeakGlobalRefType";
3484 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003485 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003486 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003487 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003488 }
3489}