blob: 2156ed1912cd7a592c684f5dabeccfdcf37899cc [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"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080045#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070046#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070047#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070048#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070049#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070050#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070051#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070053#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070054
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070055namespace art {
56
Brian Carlstrom7934ac22013-07-26 10:54:15 -070057static const size_t kMonitorsInitial = 32; // Arbitrary.
58static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070059
Brian Carlstrom7934ac22013-07-26 10:54:15 -070060static const size_t kLocalsInitial = 64; // Arbitrary.
61static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070062
Brian Carlstrom7934ac22013-07-26 10:54:15 -070063static const size_t kPinTableInitial = 16; // Arbitrary.
64static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070065
Brian Carlstrom7934ac22013-07-26 10:54:15 -070066static size_t gGlobalsInitial = 512; // Arbitrary.
67static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070068
Brian Carlstrom7934ac22013-07-26 10:54:15 -070069static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
70static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070071
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080072static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070074 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070075}
76
Jeff Hao19c5d372013-03-15 14:33:43 -070077static bool IsBadJniVersion(int version) {
78 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
79 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
80}
81
Elliott Hughes6b436852011-08-12 10:16:44 -070082// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
83// separated with slashes but aren't wrapped with "L;" like regular descriptors
84// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
85// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
86// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070087static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070088 std::string result;
89 // Add the missing "L;" if necessary.
90 if (name[0] == '[') {
91 result = name;
92 } else {
93 result += 'L';
94 result += name;
95 result += ';';
96 }
97 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070098 if (result.find('.') != std::string::npos) {
99 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
100 << "\"" << name << "\"";
101 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700102 }
103 return result;
104}
105
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800106static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700110 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800111 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
112 "no %s method \"%s.%s%s\"",
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700113 kind, c->GetDescriptor(&temp), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700114}
115
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200116static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa, mirror::Class* c,
117 const char* kind, jint idx, bool return_errors)
118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
119 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method in "
120 << PrettyDescriptor(c) << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
121 << ": " << kind << " is null at index " << idx;
122 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
123 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
124 "%s is null at index %d", kind, idx);
125}
126
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800127static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
129 if (LIKELY(klass->IsInitialized())) {
130 return klass;
131 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700132 StackHandleScope<1> hs(self);
133 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
134 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800135 return nullptr;
136 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700137 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800138}
139
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
141 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800143 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800144 if (c == nullptr) {
145 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700146 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800147 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700148 if (is_static) {
149 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700150 } else if (c->IsInterface()) {
151 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700152 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700153 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800154 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700155 // No virtual method matching the signature. Search declared
156 // private methods and constructors.
157 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700158 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700159 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800160 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800162 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700163 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700165}
166
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800167static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800169 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700170 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
171 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800172 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700173 }
Brian Carlstromce888532013-10-10 00:32:58 -0700174 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800175 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700176 return method->GetDeclaringClass()->GetClassLoader();
177 }
178 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800179 mirror::ClassLoader* class_loader =
180 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
181 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700182 return class_loader;
183 }
184 // See if the override ClassLoader is set for gtests.
185 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800186 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800187 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700188 CHECK(Runtime::Current()->UseCompileTimeClassPath());
189 return class_loader;
190 }
191 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800192 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700193}
194
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700195static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
196 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700198 StackHandleScope<2> hs(soa.Self());
199 Handle<mirror::Class> c(
200 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
201 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800202 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700203 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800204 mirror::ArtField* field = nullptr;
205 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
207 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700208 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800209 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700210 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700212 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800213 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700214 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700215 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800216 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700217 StackHandleScope<1> hs(soa.Self());
218 Handle<mirror::Throwable> cause(hs.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219 soa.Self()->ClearException();
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700220 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800221 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800222 "no type \"%s\" found and so no field \"%s\" "
223 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700224 c->GetDescriptor(&temp));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700225 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800226 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227 }
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700228 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700229 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700230 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700231 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700232 } else {
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700233 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700234 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800235 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800236 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
237 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
238 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700239 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800240 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700241 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700243}
244
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800245static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700248 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700249 vm->pin_table.Add(array);
250}
251
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800252static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700253 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700255 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700256 vm->pin_table.Remove(array);
257}
258
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800259static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700260 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700262 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800263 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
264 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
265 "%s offset=%d length=%d %s.length=%d",
266 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700267}
Ian Rogers0571d352011-11-03 19:51:38 -0700268
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
270 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700271 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800272 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
273 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
274 "offset=%d length=%d string.length()=%d", start, length,
275 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700276}
Elliott Hughes814e4032011-08-23 12:07:56 -0700277
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 // Turn the const char* into a java.lang.String.
281 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800282 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700284 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700285
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700286 // Choose an appropriate constructor and set up the arguments.
287 jvalue args[2];
288 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800289 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800291 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 signature = "(Ljava/lang/String;)V";
293 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800294 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295 signature = "(Ljava/lang/Throwable;)V";
296 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700297 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
299 args[0].l = s.get();
300 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700301 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800303 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800304 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800306 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 return JNI_ERR;
308 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700309
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800310 ScopedLocalRef<jthrowable> exception(
311 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
312 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700313 return JNI_ERR;
314 }
Ian Rogersef28b142012-11-30 14:22:18 -0800315 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800316 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800317 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700318 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700319}
320
Elliott Hughes462c9442012-03-23 18:47:50 -0700321static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800322 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700323 return JNI_ERR;
324 }
325
Elliott Hughes462c9442012-03-23 18:47:50 -0700326 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700327 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800328 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700329 *p_env = self->GetJniEnv();
330 return JNI_OK;
331 }
332
333 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
334
335 // No threads allowed in zygote mode.
336 if (runtime->IsZygote()) {
337 LOG(ERROR) << "Attempt to attach a thread in the zygote";
338 return JNI_ERR;
339 }
340
Elliott Hughes462c9442012-03-23 18:47:50 -0700341 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800342 const char* thread_name = nullptr;
343 jobject thread_group = nullptr;
344 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700345 if (IsBadJniVersion(args->version)) {
346 LOG(ERROR) << "Bad JNI version passed to "
347 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
348 << args->version;
349 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700350 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700351 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700352 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700353 }
Elliott Hughes75770752011-08-24 17:52:38 -0700354
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800355 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800356 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700357 return JNI_ERR;
358 } else {
359 *p_env = Thread::Current()->GetJniEnv();
360 return JNI_OK;
361 }
Elliott Hughes75770752011-08-24 17:52:38 -0700362}
363
Elliott Hughes79082e32011-08-25 12:07:32 -0700364class SharedLibrary {
365 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800366 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700367 : path_(path),
368 handle_(handle),
Yong WU355383f2014-07-24 21:32:15 +0800369 needs_native_bridge_(false),
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700370 class_loader_(GcRoot<mirror::Object>(class_loader)),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700371 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700372 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700373 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700374 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700375 }
376
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700377 mirror::Object* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700378 return class_loader_.Read();
Elliott Hughes79082e32011-08-25 12:07:32 -0700379 }
380
381 std::string GetPath() {
382 return path_;
383 }
384
385 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700386 * Check the result of an earlier call to JNI_OnLoad on this library.
387 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700388 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389 bool CheckOnLoadResult()
390 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700391 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700392 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
394 bool okay;
395 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700396 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700397
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700398 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700399 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
400 // caller can continue.
401 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
402 okay = true;
403 } else {
404 while (jni_on_load_result_ == kPending) {
405 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700406 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700407 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700408
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700409 okay = (jni_on_load_result_ == kOkay);
410 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
411 << (okay ? "succeeded" : "failed") << "]";
412 }
413 }
414 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700415 return okay;
416 }
417
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700418 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700419 Thread* self = Thread::Current();
420 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700421
Elliott Hughes79082e32011-08-25 12:07:32 -0700422 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700423 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700424
425 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700426 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700427 }
428
Yong WU355383f2014-07-24 21:32:15 +0800429 void SetNeedsNativeBridge() {
430 needs_native_bridge_ = true;
431 }
432
433 bool NeedsNativeBridge() const {
434 return needs_native_bridge_;
435 }
436
Elliott Hughes79082e32011-08-25 12:07:32 -0700437 void* FindSymbol(const std::string& symbol_name) {
438 return dlsym(handle_, symbol_name.c_str());
439 }
440
Yong WU355383f2014-07-24 21:32:15 +0800441 void* FindSymbolWithNativeBridge(const std::string& symbol_name, mirror::ArtMethod* m)
442 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
443 CHECK(NeedsNativeBridge());
444
445 uint32_t len = 0;
446 const char* shorty = nullptr;
447 if (m != nullptr) {
448 shorty = m->GetShorty(&len);
449 }
Calin Juravle93de4272014-08-12 20:55:20 +0100450 return android::NativeBridgeGetTrampoline(handle_, symbol_name.c_str(), shorty, len);
Yong WU355383f2014-07-24 21:32:15 +0800451 }
452
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800453 void VisitRoots(RootCallback* visitor, void* arg) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700454 if (!class_loader_.IsNull()) {
455 class_loader_.VisitRoot(visitor, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800456 }
457 }
458
Elliott Hughes79082e32011-08-25 12:07:32 -0700459 private:
460 enum JNI_OnLoadState {
461 kPending,
462 kFailed,
463 kOkay,
464 };
465
466 // Path to library "/system/lib/libjni.so".
467 std::string path_;
468
469 // The void* returned by dlopen(3).
470 void* handle_;
471
Yong WU355383f2014-07-24 21:32:15 +0800472 // True if a native bridge is required.
473 bool needs_native_bridge_;
474
Elliott Hughes79082e32011-08-25 12:07:32 -0700475 // The ClassLoader this library is associated with.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700476 GcRoot<mirror::Object> class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700477
478 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700479 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700480 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700481 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700482 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700483 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700484 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700485 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700486};
487
Elliott Hughes79082e32011-08-25 12:07:32 -0700488// This exists mainly to keep implementation details out of the header file.
489class Libraries {
490 public:
491 Libraries() {
492 }
493
494 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700495 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700496 }
497
Elliott Hughesae80b492012-04-24 10:43:17 -0700498 void Dump(std::ostream& os) const {
499 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700500 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700501 if (!first) {
502 os << ' ';
503 }
504 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700505 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700506 }
507 }
508
509 size_t size() const {
510 return libraries_.size();
511 }
512
Elliott Hughes79082e32011-08-25 12:07:32 -0700513 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700514 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800515 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700516 }
517
518 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700519 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700520 }
521
522 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800523 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700524 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700525 std::string jni_short_name(JniShortName(m));
526 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800527 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700528 for (const auto& lib : libraries_) {
529 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700530 if (library->GetClassLoader() != declaring_class_loader) {
531 // We only search libraries loaded by the appropriate ClassLoader.
532 continue;
533 }
534 // Try the short name then the long name...
Yong WU355383f2014-07-24 21:32:15 +0800535 void* fn = nullptr;
536 if (UNLIKELY(library->NeedsNativeBridge())) {
537 fn = library->FindSymbolWithNativeBridge(jni_short_name, m);
538 if (fn == nullptr) {
539 fn = library->FindSymbolWithNativeBridge(jni_long_name, m);
540 }
541 } else {
542 fn = library->FindSymbol(jni_short_name);
543 if (fn == nullptr) {
544 fn = library->FindSymbol(jni_long_name);
545 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800547 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800548 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
549 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700550 return fn;
551 }
552 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700553 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700554 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700555 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700556 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800557 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700558 }
559
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800560 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800561 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800562 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800563 }
564 }
565
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700567 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700568};
569
Ian Rogers2d10b202014-05-12 19:15:18 -0700570#define CHECK_NON_NULL_ARGUMENT(value) \
571 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700572
Ian Rogers2d10b202014-05-12 19:15:18 -0700573#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
574 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
575
576#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
577 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
578
579#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
580 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
581
582#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800583 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700584 JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700585 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700586 }
587
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700588#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800589 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700590 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700591 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700592 }
593
Elliott Hughescdf53122011-08-19 15:46:09 -0700594class JNI {
595 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700596 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700597 return JNI_VERSION_1_6;
598 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700599
Ian Rogers25e8b912012-09-07 11:31:36 -0700600 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700601 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800602 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700603 }
604
Elliott Hughescdf53122011-08-19 15:46:09 -0700605 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700606 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700607 Runtime* runtime = Runtime::Current();
608 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700609 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700610 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800611 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700612 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700613 StackHandleScope<1> hs(soa.Self());
614 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800615 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700616 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800617 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700618 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700619 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700620 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700621
Ian Rogers62f05122014-03-21 11:21:29 -0700622 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700623 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700624 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700625 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700626 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700627
Ian Rogers62f05122014-03-21 11:21:29 -0700628 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700629 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700630 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700631 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700633
Elliott Hughescdf53122011-08-19 15:46:09 -0700634 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700635 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800637 mirror::ArtMethod* m = soa.DecodeMethod(mid);
638 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700639 jobject art_method = soa.AddLocalReference<jobject>(m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200640 jobject reflect_method;
641 if (m->IsConstructor()) {
642 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
643 } else {
644 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
645 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700646 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800647 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700648 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800649 SetObjectField(env, reflect_method,
650 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700651 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700653
Elliott Hughescdf53122011-08-19 15:46:09 -0700654 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700655 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700656 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800657 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700658 jobject art_field = soa.AddLocalReference<jobject>(f);
659 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
660 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800661 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700662 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800663 SetObjectField(env, reflect_field,
664 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700665 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700667
Elliott Hughes37f7a402011-08-22 18:56:01 -0700668 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700669 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800671 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700672 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700673 }
674
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700675 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700676 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700677 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800678 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700681
Narayan Kamath1268b742014-07-11 19:15:11 +0100682 // Note: java_class1 should be safely castable to java_class2, and
683 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700684 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700685 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
686 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700687 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800688 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
689 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100690 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700692
Elliott Hughese84278b2012-03-22 10:06:53 -0700693 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700694 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800695 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700696 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700697 return JNI_TRUE;
698 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700699 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800700 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
701 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700702 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700703 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700704 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700705
Elliott Hughes37f7a402011-08-22 18:56:01 -0700706 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700707 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800708 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
709 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700710 return JNI_ERR;
711 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800712 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
713 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700714 return JNI_OK;
715 }
716
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700717 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700718 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800719 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700720 }
721
722 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700723 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700724 }
725
726 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700727 ScopedObjectAccess soa(env);
728 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700729 }
730
731 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700732 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700733
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700734 // If we have no exception to describe, pass through.
735 if (!soa.Self()->GetException(nullptr)) {
736 return;
737 }
738
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700739 StackHandleScope<3> hs(soa.Self());
740 // TODO: Use nullptr instead of null handles?
741 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
742 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
743 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800744 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200745 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800746 {
747 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800748 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700749 old_throw_this_object.Assign(old_throw_location.GetThis());
750 old_throw_method.Assign(old_throw_location.GetMethod());
751 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800752 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200753 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800754 soa.Self()->ClearException();
755 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800756 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700757 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700758 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
759 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800760 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700761 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700762 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700763 } else {
764 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800765 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800766 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700767 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800768 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700769 }
770 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700771 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800772 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700773
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700774 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200775 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700776 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700777
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700779 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800780 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700781 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 }
783
Ian Rogers25e8b912012-09-07 11:31:36 -0700784 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 LOG(FATAL) << "JNI FatalError called: " << msg;
786 }
787
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700788 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700789 // TODO: SOA may not be necessary but I do it to please lock annotations.
790 ScopedObjectAccess soa(env);
791 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700792 return JNI_ERR;
793 }
Ian Rogersef28b142012-11-30 14:22:18 -0800794 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700795 return JNI_OK;
796 }
797
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700798 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700799 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800800 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700801 soa.Env()->PopFrame();
802 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 }
804
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700805 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700806 // TODO: SOA may not be necessary but I do it to please lock annotations.
807 ScopedObjectAccess soa(env);
808 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700809 }
810
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700812 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800813 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800814 // Check for null after decoding the object to handle cleared weak globals.
815 if (decoded_obj == nullptr) {
816 return nullptr;
817 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700818 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700820 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700821 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 return reinterpret_cast<jobject>(ref);
823 }
824
825 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800826 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 return;
828 }
Ian Rogersef28b142012-11-30 14:22:18 -0800829 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800831 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700832 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700833
834 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
835 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
836 << "failed to find entry";
837 }
838 }
839
840 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700841 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800842 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700843 }
844
845 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700846 if (obj != nullptr) {
847 ScopedObjectAccess soa(env);
848 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 }
850 }
851
852 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700853 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800854 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800855 // Check for null after decoding the object to handle cleared weak globals.
856 if (decoded_obj == nullptr) {
857 return nullptr;
858 }
859 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 }
861
862 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800863 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700864 return;
865 }
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700866 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -0800867 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700868
Ian Rogersef28b142012-11-30 14:22:18 -0800869 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 if (!locals.Remove(cookie, obj)) {
871 // Attempting to delete a local reference that is not in the
872 // topmost local reference frame is a no-op. DeleteLocalRef returns
873 // void and doesn't throw any exceptions, but we should probably
874 // complain about it so the user will notice that things aren't
875 // going quite the way they expect.
876 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
877 << "failed to find entry";
878 }
879 }
880
881 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700882 if (obj1 == obj2) {
883 return JNI_TRUE;
884 } else {
885 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800886 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
887 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700888 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 }
890
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700891 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700892 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700893 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800894 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800895 if (c == nullptr) {
896 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700897 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700898 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 }
900
Ian Rogersbc939662013-08-15 10:26:54 -0700901 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700903 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700904 CHECK_NON_NULL_ARGUMENT(java_class);
905 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700906 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 va_end(args);
908 return result;
909 }
910
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700912 CHECK_NON_NULL_ARGUMENT(java_class);
913 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700914 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800915 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800916 if (c == nullptr) {
917 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700918 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800919 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800920 if (result == nullptr) {
921 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700922 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700923 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800925 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800926 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700927 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800928 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 }
930
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700932 CHECK_NON_NULL_ARGUMENT(java_class);
933 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700934 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800935 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800936 if (c == nullptr) {
937 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700938 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800939 mirror::Object* result = c->AllocObject(soa.Self());
940 if (result == nullptr) {
941 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700942 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700943 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700944 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800945 if (soa.Self()->IsExceptionPending()) {
946 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700947 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800948 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 }
950
Ian Rogersbc939662013-08-15 10:26:54 -0700951 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700952 CHECK_NON_NULL_ARGUMENT(java_class);
953 CHECK_NON_NULL_ARGUMENT(name);
954 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700955 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700956 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 }
958
Ian Rogersbc939662013-08-15 10:26:54 -0700959 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
960 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700961 CHECK_NON_NULL_ARGUMENT(java_class);
962 CHECK_NON_NULL_ARGUMENT(name);
963 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700964 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700965 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 }
967
Elliott Hughes72025e52011-08-23 17:50:30 -0700968 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 va_list ap;
970 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700971 CHECK_NON_NULL_ARGUMENT(obj);
972 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700973 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700976 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700980 CHECK_NON_NULL_ARGUMENT(obj);
981 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700982 ScopedObjectAccess soa(env);
983 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
984 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 }
986
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700988 CHECK_NON_NULL_ARGUMENT(obj);
989 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700990 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700991 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
992 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700993 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 va_list ap;
998 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700999 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1000 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001001 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001004 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001008 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1009 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001010 ScopedObjectAccess soa(env);
1011 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001015 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1016 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001017 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001018 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1019 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 va_list ap;
1024 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001025 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1026 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001027 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001028 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001030 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001034 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1035 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001036 ScopedObjectAccess soa(env);
1037 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001041 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1042 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001044 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1045 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_list ap;
1050 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001051 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1052 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001053 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001054 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001056 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001060 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1061 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062 ScopedObjectAccess soa(env);
1063 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001067 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1068 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001069 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001070 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1071 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 }
1073
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_list ap;
1076 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001077 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1078 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001079 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001082 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001086 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1087 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088 ScopedObjectAccess soa(env);
1089 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001093 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1094 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001095 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001096 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1097 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_list ap;
1102 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001103 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1104 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001105 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001108 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001112 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1113 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001114 ScopedObjectAccess soa(env);
1115 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001119 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1120 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001121 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001122 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1123 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 va_list ap;
1128 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001129 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1130 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001131 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001134 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001138 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1139 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001140 ScopedObjectAccess soa(env);
1141 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001145 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1146 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001147 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001148 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1149 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 va_list ap;
1154 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001155 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1156 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001157 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001158 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001160 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001164 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1165 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001166 ScopedObjectAccess soa(env);
1167 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001171 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1172 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001173 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001174 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1175 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 va_list ap;
1180 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001181 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1182 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001183 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001184 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001186 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001190 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1191 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001192 ScopedObjectAccess soa(env);
1193 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001197 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1198 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001199 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001200 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1201 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 va_list ap;
1206 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001207 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1208 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001209 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001210 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001215 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1216 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 ScopedObjectAccess soa(env);
1218 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001222 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1223 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001224 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001225 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001228 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001231 CHECK_NON_NULL_ARGUMENT(obj);
1232 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001233 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001234 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1235 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 va_end(ap);
1237 return local_result;
1238 }
1239
Ian Rogersbc939662013-08-15 10:26:54 -07001240 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1241 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001242 CHECK_NON_NULL_ARGUMENT(obj);
1243 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001244 ScopedObjectAccess soa(env);
1245 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1246 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
Ian Rogersbc939662013-08-15 10:26:54 -07001249 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1250 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001251 CHECK_NON_NULL_ARGUMENT(obj);
1252 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001253 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001254 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001255 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
Ian Rogersbc939662013-08-15 10:26:54 -07001258 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1259 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001261 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001262 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1263 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001264 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001265 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001267 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
Ian Rogersbc939662013-08-15 10:26:54 -07001270 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1271 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001272 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1273 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001274 ScopedObjectAccess soa(env);
1275 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
Ian Rogersbc939662013-08-15 10:26:54 -07001278 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1279 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001280 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1281 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001282 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001283 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001286 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001288 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001289 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1290 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001291 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001292 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001294 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
Ian Rogersbc939662013-08-15 10:26:54 -07001297 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1298 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001299 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1300 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001301 ScopedObjectAccess soa(env);
1302 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
Ian Rogersbc939662013-08-15 10:26:54 -07001305 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1306 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001307 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1308 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001309 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001310 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 }
1312
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001313 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001316 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1317 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001318 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001319 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001321 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
Ian Rogersbc939662013-08-15 10:26:54 -07001324 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1325 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001326 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1327 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001328 ScopedObjectAccess soa(env);
1329 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 }
1331
Ian Rogersbc939662013-08-15 10:26:54 -07001332 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1333 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001334 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1335 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001336 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001337 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 }
1339
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001340 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001342 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001343 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1344 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001345 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001346 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001348 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
Ian Rogersbc939662013-08-15 10:26:54 -07001351 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1352 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001353 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1354 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001355 ScopedObjectAccess soa(env);
1356 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
Ian Rogersbc939662013-08-15 10:26:54 -07001359 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1360 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001361 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1362 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001363 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001364 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001367 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001369 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001370 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1371 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001372 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001373 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001375 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
1377
Ian Rogersbc939662013-08-15 10:26:54 -07001378 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1379 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001380 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1381 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001382 ScopedObjectAccess soa(env);
1383 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
1385
Ian Rogersbc939662013-08-15 10:26:54 -07001386 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1387 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001388 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1389 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001390 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001391 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
1393
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001394 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001396 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001397 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1398 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001399 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001400 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001402 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 }
1404
Ian Rogersbc939662013-08-15 10:26:54 -07001405 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1406 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001407 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1408 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001409 ScopedObjectAccess soa(env);
1410 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 }
1412
Ian Rogersbc939662013-08-15 10:26:54 -07001413 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1414 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001415 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1416 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001417 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001418 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001421 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001423 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001424 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1425 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001426 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001427 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001429 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Ian Rogersbc939662013-08-15 10:26:54 -07001432 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1433 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001434 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1435 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001436 ScopedObjectAccess soa(env);
1437 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Ian Rogersbc939662013-08-15 10:26:54 -07001440 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1441 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001442 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1443 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001444 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001445 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001448 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001450 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001451 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1452 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001453 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001454 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001456 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Ian Rogersbc939662013-08-15 10:26:54 -07001459 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1460 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001461 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1462 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001463 ScopedObjectAccess soa(env);
1464 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Ian Rogersbc939662013-08-15 10:26:54 -07001467 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1468 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001469 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1470 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001471 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001472 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001475 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001477 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001478 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1479 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001480 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001481 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 va_end(ap);
1483 }
1484
Brian Carlstromea46f952013-07-30 01:26:50 -07001485 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1486 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001487 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1488 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001489 ScopedObjectAccess soa(env);
1490 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 }
1492
Ian Rogersbc939662013-08-15 10:26:54 -07001493 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1494 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001495 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1496 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001497 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001498 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 }
1500
Ian Rogersbc939662013-08-15 10:26:54 -07001501 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001502 CHECK_NON_NULL_ARGUMENT(java_class);
1503 CHECK_NON_NULL_ARGUMENT(name);
1504 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001505 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001506 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001508
Ian Rogersbc939662013-08-15 10:26:54 -07001509 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1510 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001511 CHECK_NON_NULL_ARGUMENT(java_class);
1512 CHECK_NON_NULL_ARGUMENT(name);
1513 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001514 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001515 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001517
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001518 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001519 CHECK_NON_NULL_ARGUMENT(obj);
1520 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001521 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001522 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1523 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001524 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001526
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001527 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001528 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001529 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001530 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001531 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
1533
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001534 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001535 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1536 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001537 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001538 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1539 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1540 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001541 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001545 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001546 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001547 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1548 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001549 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 }
1551
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001552#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001553 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1554 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001555 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001556 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1557 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001558 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001559
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001560#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001561 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001562 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001563 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001564 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001565
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001566#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001567 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1568 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001569 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001570 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1571 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001572 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001573
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001574#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001575 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001576 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001577 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001578 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001579
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001580 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001581 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001584 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001585 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001588 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001589 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001596 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001600 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001604 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001608 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001612 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001616 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001617 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001621 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001624 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001625 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 }
1627
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001628 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001629 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001632 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001633 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001637 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001638 }
1639
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001640 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001641 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001642 }
1643
1644 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001645 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001646 }
1647
1648 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001649 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001650 }
1651
1652 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001653 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001654 }
1655
1656 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001657 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001658 }
1659
1660 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001661 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001662 }
1663
1664 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001665 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001666 }
1667
1668 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001669 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001670 }
1671
1672 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001673 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001674 }
1675
1676 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001677 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001678 }
1679
1680 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001681 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001682 }
1683
1684 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001685 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001686 }
1687
1688 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001689 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001690 }
1691
1692 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001693 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001694 }
1695
1696 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001697 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001698 }
1699
1700 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001701 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001702 }
1703
1704 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001705 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 }
1707
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001708 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001711 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001712 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001713 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001714 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 va_end(ap);
1716 return local_result;
1717 }
1718
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001719 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001720 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001721 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001722 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001723 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001726 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001727 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001728 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001729 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001730 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001733 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001735 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001736 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001737 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001738 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001740 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001743 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001744 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001745 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001746 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001749 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001750 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001751 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001752 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001755 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001758 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001759 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001760 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001762 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 }
1764
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001765 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001766 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001767 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001768 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001771 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001772 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001773 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001774 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001777 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001779 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001780 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001781 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001782 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001784 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001787 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001788 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001790 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001793 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001794 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001795 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001796 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001799 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001801 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001802 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001803 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001804 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001806 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 }
1808
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001809 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001810 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001811 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001812 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001815 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001816 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001817 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001818 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001821 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001823 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001824 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001825 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001826 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001828 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001831 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001832 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001833 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001834 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001837 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001838 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001839 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001840 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001843 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001845 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001846 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001847 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001848 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001850 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 }
1852
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001853 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001854 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001855 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001856 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 }
1858
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001859 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001860 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001861 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001862 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001865 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001867 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001868 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001869 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001870 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001872 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 }
1874
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001875 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001876 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001877 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001878 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 }
1880
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001881 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001882 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001883 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001884 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 }
1886
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001887 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001889 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001890 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001891 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001892 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001894 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001895 }
1896
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001897 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001898 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001899 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001900 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 }
1902
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001903 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001904 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001905 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001906 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 }
1908
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001909 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001911 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001912 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001913 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001914 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001915 va_end(ap);
1916 }
1917
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001918 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001919 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001920 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001921 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 }
1923
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001924 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001925 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001926 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001927 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 }
1929
Elliott Hughes814e4032011-08-23 12:07:56 -07001930 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001931 if (UNLIKELY(char_count < 0)) {
1932 JniAbortF("NewString", "char_count < 0: %d", char_count);
1933 return nullptr;
1934 }
1935 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1936 JniAbortF("NewString", "chars == null && char_count > 0");
1937 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001938 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001939 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001940 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001941 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001942 }
1943
1944 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001945 if (utf == nullptr) {
1946 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001948 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001949 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001950 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 }
1952
Elliott Hughes814e4032011-08-23 12:07:56 -07001953 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001954 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001955 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001956 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001957 }
1958
1959 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001960 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001961 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001962 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001963 }
1964
Ian Rogersbc939662013-08-15 10:26:54 -07001965 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1966 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001967 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001968 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001969 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001970 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001971 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001972 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001973 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001974 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1975 memcpy(buf, chars + start, length * sizeof(jchar));
1976 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001977 }
1978
Ian Rogersbc939662013-08-15 10:26:54 -07001979 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1980 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001981 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001982 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001983 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001984 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001985 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001986 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001987 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001988 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1989 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1990 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001994 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001995 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001996 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1997 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001998 PinPrimitiveArray(soa, chars);
Fred Shih56890e22014-06-02 11:11:52 -07001999 gc::Heap* heap = Runtime::Current()->GetHeap();
2000 if (heap->IsMovableObject(chars)) {
2001 if (is_copy != nullptr) {
2002 *is_copy = JNI_TRUE;
2003 }
2004 int32_t char_count = s->GetLength();
2005 int32_t offset = s->GetOffset();
2006 jchar* bytes = new jchar[char_count];
2007 for (int32_t i = 0; i < char_count; i++) {
2008 bytes[i] = chars->Get(i + offset);
2009 }
2010 return bytes;
2011 } else {
2012 if (is_copy != nullptr) {
2013 *is_copy = JNI_FALSE;
2014 }
2015 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07002016 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002017 }
2018
Mathieu Chartier590fee92013-09-13 13:46:47 -07002019 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002020 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002021 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07002022 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2023 mirror::CharArray* s_chars = s->GetCharArray();
2024 if (chars != (s_chars->GetData() + s->GetOffset())) {
2025 delete[] chars;
2026 }
2027 UnpinPrimitiveArray(soa, s->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes75770752011-08-24 17:52:38 -07002030 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07002031 CHECK_NON_NULL_ARGUMENT(java_string);
2032 ScopedObjectAccess soa(env);
2033 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2034 mirror::CharArray* chars = s->GetCharArray();
2035 int32_t offset = s->GetOffset();
2036 PinPrimitiveArray(soa, chars);
2037 gc::Heap* heap = Runtime::Current()->GetHeap();
2038 if (heap->IsMovableObject(chars)) {
2039 StackHandleScope<1> hs(soa.Self());
2040 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
2041 heap->IncrementDisableMovingGC(soa.Self());
2042 }
2043 if (is_copy != nullptr) {
2044 *is_copy = JNI_FALSE;
2045 }
2046 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes75770752011-08-24 17:52:38 -07002049 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07002050 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
2051 ScopedObjectAccess soa(env);
2052 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
2053 gc::Heap* heap = Runtime::Current()->GetHeap();
2054 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2055 mirror::CharArray* s_chars = s->GetCharArray();
2056 if (heap->IsMovableObject(s_chars)) {
2057 heap->DecrementDisableMovingGC(soa.Self());
2058 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes75770752011-08-24 17:52:38 -07002061 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002062 if (java_string == nullptr) {
2063 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002064 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002065 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002066 *is_copy = JNI_TRUE;
2067 }
Ian Rogersef28b142012-11-30 14:22:18 -08002068 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002069 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002070 size_t byte_count = s->GetUtfLength();
2071 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002072 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002073 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2074 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2075 bytes[byte_count] = '\0';
2076 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002077 }
2078
Elliott Hughes75770752011-08-24 17:52:38 -07002079 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002080 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002081 }
2082
Elliott Hughesbd935992011-08-22 11:59:34 -07002083 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002084 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002085 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002086 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002087 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002088 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2089 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002090 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002091 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002095 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002096 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002097 mirror::ObjectArray<mirror::Object>* array =
2098 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002099 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 }
2101
Ian Rogersbc939662013-08-15 10:26:54 -07002102 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2103 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002104 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002105 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002106 mirror::ObjectArray<mirror::Object>* array =
2107 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2108 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002109 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
2112 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002113 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
2116 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002117 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
2120 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002121 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
2124 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002125 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
2128 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002129 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
2132 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002133 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
2135
2136 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002137 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
2139
Ian Rogers1d99e452014-01-02 17:36:41 -08002140 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2141 jobject initial_element) {
2142 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002143 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002144 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002145 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002146 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147
2148 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002149 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002150 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002151 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002152 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002153 if (UNLIKELY(element_class->IsPrimitive())) {
2154 JniAbortF("NewObjectArray", "not an object type: %s",
2155 PrettyDescriptor(element_class).c_str());
2156 return nullptr;
2157 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002158 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002159 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002160 if (UNLIKELY(array_class == nullptr)) {
2161 return nullptr;
2162 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 }
2164
Elliott Hughes75770752011-08-24 17:52:38 -07002165 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002166 mirror::ObjectArray<mirror::Object>* result =
2167 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002168 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002169 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002170 if (initial_object != nullptr) {
2171 mirror::Class* element_class = result->GetClass()->GetComponentType();
2172 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2173 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2174 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2175 PrettyDescriptor(element_class).c_str());
2176
2177 } else {
2178 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002179 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002180 }
2181 }
Elliott Hughes75770752011-08-24 17:52:38 -07002182 }
2183 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002184 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
2187 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002188 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Ian Rogersa15e67d2012-02-28 13:51:55 -08002191 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002192 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002193 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002194 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002195 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2196 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2197 PrettyDescriptor(array->GetClass()).c_str());
2198 return nullptr;
2199 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002200 gc::Heap* heap = Runtime::Current()->GetHeap();
2201 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002202 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002203 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002204 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002205 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002206 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002207 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002208 *is_copy = JNI_FALSE;
2209 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002210 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002211 }
2212
Ian Rogers2d10b202014-05-12 19:15:18 -07002213 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2214 jint mode) {
2215 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2216 ScopedObjectAccess soa(env);
2217 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2218 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2219 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2220 PrettyDescriptor(array->GetClass()).c_str());
2221 return;
2222 }
2223 const size_t component_size = array->GetClass()->GetComponentSize();
2224 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002225 }
2226
Elliott Hughes75770752011-08-24 17:52:38 -07002227 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002228 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Elliott Hughes75770752011-08-24 17:52:38 -07002231 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002232 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Elliott Hughes75770752011-08-24 17:52:38 -07002235 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002236 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 }
2238
Elliott Hughes75770752011-08-24 17:52:38 -07002239 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002240 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 }
2242
Elliott Hughes75770752011-08-24 17:52:38 -07002243 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002244 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Elliott Hughes75770752011-08-24 17:52:38 -07002247 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002248 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 }
2250
Elliott Hughes75770752011-08-24 17:52:38 -07002251 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002252 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 }
2254
Elliott Hughes75770752011-08-24 17:52:38 -07002255 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002256 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 }
2258
Mathieu Chartier590fee92013-09-13 13:46:47 -07002259 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2260 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002261 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2262 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
2264
Mathieu Chartier590fee92013-09-13 13:46:47 -07002265 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002266 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 }
2268
Mathieu Chartier590fee92013-09-13 13:46:47 -07002269 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002270 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 }
2272
Mathieu Chartier590fee92013-09-13 13:46:47 -07002273 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2274 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002275 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002276 }
2277
Mathieu Chartier590fee92013-09-13 13:46:47 -07002278 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2279 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002280 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 }
2282
Mathieu Chartier590fee92013-09-13 13:46:47 -07002283 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002284 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 }
2286
Mathieu Chartier590fee92013-09-13 13:46:47 -07002287 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002288 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002289 }
2290
Mathieu Chartier590fee92013-09-13 13:46:47 -07002291 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2292 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002293 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002294 }
2295
Ian Rogersbc939662013-08-15 10:26:54 -07002296 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2297 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002298 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002299 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002300 }
2301
Ian Rogersbc939662013-08-15 10:26:54 -07002302 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2303 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002304 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002305 }
2306
Ian Rogersbc939662013-08-15 10:26:54 -07002307 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2308 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002309 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002310 }
2311
Ian Rogersbc939662013-08-15 10:26:54 -07002312 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2313 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002314 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002315 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002316 }
2317
Ian Rogersbc939662013-08-15 10:26:54 -07002318 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2319 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002320 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002321 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002322 }
2323
Ian Rogersbc939662013-08-15 10:26:54 -07002324 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2325 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002326 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002327 }
2328
Ian Rogersbc939662013-08-15 10:26:54 -07002329 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2330 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002331 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002332 }
2333
Ian Rogersbc939662013-08-15 10:26:54 -07002334 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2335 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002336 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002337 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002338 }
2339
Ian Rogersbc939662013-08-15 10:26:54 -07002340 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2341 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002342 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002343 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002344 }
2345
Ian Rogersbc939662013-08-15 10:26:54 -07002346 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2347 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002348 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002349 }
2350
Ian Rogersbc939662013-08-15 10:26:54 -07002351 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2352 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002353 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002354 }
2355
Ian Rogersbc939662013-08-15 10:26:54 -07002356 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2357 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002358 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002359 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002360 }
2361
Ian Rogersbc939662013-08-15 10:26:54 -07002362 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2363 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002364 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002365 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002366 }
2367
Ian Rogersbc939662013-08-15 10:26:54 -07002368 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2369 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002370 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002371 }
2372
Ian Rogersbc939662013-08-15 10:26:54 -07002373 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2374 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002375 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002376 }
2377
Ian Rogersbc939662013-08-15 10:26:54 -07002378 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2379 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002380 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002381 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002382 }
2383
Ian Rogersbc939662013-08-15 10:26:54 -07002384 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2385 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002386 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2387 }
2388
Ian Rogersbc939662013-08-15 10:26:54 -07002389 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2390 jint method_count, bool return_errors) {
2391 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002392 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002393 return JNI_ERR; // Not reached.
2394 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002395 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002396 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002397 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002398 if (UNLIKELY(method_count == 0)) {
2399 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2400 << PrettyDescriptor(c);
2401 return JNI_OK;
2402 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002403 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002404 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002405 const char* name = methods[i].name;
2406 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002407 const void* fnPtr = methods[i].fnPtr;
2408 if (UNLIKELY(name == nullptr)) {
2409 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2410 return JNI_ERR;
2411 } else if (UNLIKELY(sig == nullptr)) {
2412 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2413 return JNI_ERR;
2414 } else if (UNLIKELY(fnPtr == nullptr)) {
2415 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2416 return JNI_ERR;
2417 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002418 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002419 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002420 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002421 ++sig;
2422 }
2423
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002424 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2425 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002426 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002427 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002428 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002429 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002430 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002431 << PrettyDescriptor(c) << "." << name << sig << " in "
2432 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002433 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002434 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002435 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002436 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002437 << PrettyDescriptor(c) << "." << name << sig
2438 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002439 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002440 return JNI_ERR;
2441 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002442
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002443 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002444
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002445 m->RegisterNative(soa.Self(), fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002446 }
2447 return JNI_OK;
2448 }
2449
Elliott Hughes5174fe62011-08-23 15:12:35 -07002450 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002451 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002452 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002453 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002454
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002455 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002456
Ian Rogers2d10b202014-05-12 19:15:18 -07002457 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002458 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002459 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002460 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002461 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002462 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002463 }
2464 }
2465 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002466 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002467 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002468 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002469 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002470 }
2471 }
2472
Ian Rogers2d10b202014-05-12 19:15:18 -07002473 if (unregistered_count == 0) {
2474 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2475 << PrettyDescriptor(c) << "' that contains no native methods";
2476 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002477 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002478 }
2479
Ian Rogers719d1a32014-03-06 12:13:39 -08002480 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002481 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002482 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002483 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2484 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002485 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002486 return JNI_ERR;
2487 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002488 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002489 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002490 }
2491
Ian Rogers719d1a32014-03-06 12:13:39 -08002492 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002493 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002494 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002495 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002496 o->MonitorExit(soa.Self());
2497 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002498 return JNI_ERR;
2499 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002500 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002501 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002502 }
2503
2504 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002505 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002506 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002507 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002508 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002509 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002510 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002511 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002512 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002513 }
2514
Elliott Hughescdf53122011-08-19 15:46:09 -07002515 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002516 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002517 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002518 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002519 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002520 if (address == nullptr && capacity != 0) {
2521 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002522 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002523 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002524
Brian Carlstrom85a93362014-06-25 09:30:52 -07002525 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002526 if (capacity > INT_MAX) {
2527 JniAbortF("NewDirectByteBuffer", "buffer capacity greater than maximum jint: %" PRId64, capacity);
2528 return nullptr;
2529 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002530 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002531 jint capacity_arg = static_cast<jint>(capacity);
2532
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002533 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2534 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002535 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002536 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002537 }
2538
Elliott Hughesb465ab02011-08-24 11:21:21 -07002539 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002540 return reinterpret_cast<void*>(env->GetLongField(
2541 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002542 }
2543
Elliott Hughesb465ab02011-08-24 11:21:21 -07002544 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002545 return static_cast<jlong>(env->GetIntField(
2546 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002547 }
2548
Elliott Hughesb465ab02011-08-24 11:21:21 -07002549 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002550 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002551
2552 // Do we definitely know what kind of reference this is?
2553 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2554 IndirectRefKind kind = GetIndirectRefKind(ref);
2555 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002556 case kLocal: {
2557 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002558 // The local refs don't need a read barrier.
2559 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2560 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002561 return JNILocalRefType;
2562 }
2563 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002564 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002565 case kGlobal:
2566 return JNIGlobalRefType;
2567 case kWeakGlobal:
2568 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002569 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002570 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002571 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002572 return JNILocalRefType;
2573 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002574 return JNIInvalidRefType;
2575 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002576 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2577 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002578 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002579
2580 private:
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002581 static jint EnsureLocalCapacity(ScopedObjectAccess& soa, jint desired_capacity,
2582 const char* caller) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002583 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002584 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002585 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2586 return JNI_ERR;
2587 }
2588 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002589 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002590 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2591 if (!okay) {
2592 soa.Self()->ThrowOutOfMemoryError(caller);
2593 }
2594 return okay ? JNI_OK : JNI_ERR;
2595 }
2596
2597 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002598 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002599 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002600 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002601 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002602 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002603 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002604 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002605 return soa.AddLocalReference<JniT>(result);
2606 }
2607
Ian Rogers2d10b202014-05-12 19:15:18 -07002608 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2609 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2610 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002611 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002612 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002613 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2614 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2615 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2616 PrettyDescriptor(array->GetClass()).c_str());
2617 return nullptr;
2618 }
2619 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2620 return array;
2621 }
2622
2623 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2624 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2625 CHECK_NON_NULL_ARGUMENT(java_array);
2626 ScopedObjectAccess soa(env);
2627 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2628 "GetArrayElements",
2629 "get");
2630 if (UNLIKELY(array == nullptr)) {
2631 return nullptr;
2632 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002633 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002634 // Only make a copy if necessary.
2635 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2636 if (is_copy != nullptr) {
2637 *is_copy = JNI_TRUE;
2638 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002639 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002640 size_t size = array->GetLength() * component_size;
2641 void* data = new uint64_t[RoundUp(size, 8) / 8];
2642 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002643 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002644 } else {
2645 if (is_copy != nullptr) {
2646 *is_copy = JNI_FALSE;
2647 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002648 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002649 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002650 }
2651
Ian Rogers2d10b202014-05-12 19:15:18 -07002652 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002653 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002654 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002655 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002656 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2657 "ReleaseArrayElements",
2658 "release");
2659 if (array == nullptr) {
2660 return;
2661 }
2662 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2663 }
2664
2665 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2666 size_t component_size, void* elements, jint mode)
2667 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002668 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002669 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002670 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002671 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002672 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2673 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002674 if (is_copy) {
2675 // Sanity check: If elements is not the same as the java array's data, it better not be a
2676 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2677 // copies we make?
2678 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2679 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2680 reinterpret_cast<void*>(elements), array_data);
2681 return;
2682 }
2683 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002684 // Don't need to copy if we had a direct pointer.
2685 if (mode != JNI_ABORT && is_copy) {
2686 memcpy(array_data, elements, bytes);
2687 }
2688 if (mode != JNI_COMMIT) {
2689 if (is_copy) {
2690 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002691 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002692 // Non copy to a movable object must means that we had disabled the moving GC.
2693 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002694 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002695 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002696 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002697 }
2698
Ian Rogers2d10b202014-05-12 19:15:18 -07002699 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2700 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2701 jsize start, jsize length, ElementT* buf) {
2702 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2703 ScopedObjectAccess soa(env);
2704 ArtArrayT* array =
2705 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2706 "GetPrimitiveArrayRegion",
2707 "get region of");
2708 if (array != nullptr) {
2709 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2710 ThrowAIOOBE(soa, array, start, length, "src");
2711 } else {
2712 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2713 ElementT* data = array->GetData();
2714 memcpy(buf, data + start, length * sizeof(ElementT));
2715 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002716 }
2717 }
2718
Ian Rogers2d10b202014-05-12 19:15:18 -07002719 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2720 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2721 jsize start, jsize length, const ElementT* buf) {
2722 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2723 ScopedObjectAccess soa(env);
2724 ArtArrayT* array =
2725 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2726 "SetPrimitiveArrayRegion",
2727 "set region of");
2728 if (array != nullptr) {
2729 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2730 ThrowAIOOBE(soa, array, start, length, "dst");
2731 } else {
2732 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2733 ElementT* data = array->GetData();
2734 memcpy(data + start, buf, length * sizeof(ElementT));
2735 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002736 }
2737 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002738};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002739
Elliott Hughes88c5c352012-03-15 18:49:48 -07002740const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002741 nullptr, // reserved0.
2742 nullptr, // reserved1.
2743 nullptr, // reserved2.
2744 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002745 JNI::GetVersion,
2746 JNI::DefineClass,
2747 JNI::FindClass,
2748 JNI::FromReflectedMethod,
2749 JNI::FromReflectedField,
2750 JNI::ToReflectedMethod,
2751 JNI::GetSuperclass,
2752 JNI::IsAssignableFrom,
2753 JNI::ToReflectedField,
2754 JNI::Throw,
2755 JNI::ThrowNew,
2756 JNI::ExceptionOccurred,
2757 JNI::ExceptionDescribe,
2758 JNI::ExceptionClear,
2759 JNI::FatalError,
2760 JNI::PushLocalFrame,
2761 JNI::PopLocalFrame,
2762 JNI::NewGlobalRef,
2763 JNI::DeleteGlobalRef,
2764 JNI::DeleteLocalRef,
2765 JNI::IsSameObject,
2766 JNI::NewLocalRef,
2767 JNI::EnsureLocalCapacity,
2768 JNI::AllocObject,
2769 JNI::NewObject,
2770 JNI::NewObjectV,
2771 JNI::NewObjectA,
2772 JNI::GetObjectClass,
2773 JNI::IsInstanceOf,
2774 JNI::GetMethodID,
2775 JNI::CallObjectMethod,
2776 JNI::CallObjectMethodV,
2777 JNI::CallObjectMethodA,
2778 JNI::CallBooleanMethod,
2779 JNI::CallBooleanMethodV,
2780 JNI::CallBooleanMethodA,
2781 JNI::CallByteMethod,
2782 JNI::CallByteMethodV,
2783 JNI::CallByteMethodA,
2784 JNI::CallCharMethod,
2785 JNI::CallCharMethodV,
2786 JNI::CallCharMethodA,
2787 JNI::CallShortMethod,
2788 JNI::CallShortMethodV,
2789 JNI::CallShortMethodA,
2790 JNI::CallIntMethod,
2791 JNI::CallIntMethodV,
2792 JNI::CallIntMethodA,
2793 JNI::CallLongMethod,
2794 JNI::CallLongMethodV,
2795 JNI::CallLongMethodA,
2796 JNI::CallFloatMethod,
2797 JNI::CallFloatMethodV,
2798 JNI::CallFloatMethodA,
2799 JNI::CallDoubleMethod,
2800 JNI::CallDoubleMethodV,
2801 JNI::CallDoubleMethodA,
2802 JNI::CallVoidMethod,
2803 JNI::CallVoidMethodV,
2804 JNI::CallVoidMethodA,
2805 JNI::CallNonvirtualObjectMethod,
2806 JNI::CallNonvirtualObjectMethodV,
2807 JNI::CallNonvirtualObjectMethodA,
2808 JNI::CallNonvirtualBooleanMethod,
2809 JNI::CallNonvirtualBooleanMethodV,
2810 JNI::CallNonvirtualBooleanMethodA,
2811 JNI::CallNonvirtualByteMethod,
2812 JNI::CallNonvirtualByteMethodV,
2813 JNI::CallNonvirtualByteMethodA,
2814 JNI::CallNonvirtualCharMethod,
2815 JNI::CallNonvirtualCharMethodV,
2816 JNI::CallNonvirtualCharMethodA,
2817 JNI::CallNonvirtualShortMethod,
2818 JNI::CallNonvirtualShortMethodV,
2819 JNI::CallNonvirtualShortMethodA,
2820 JNI::CallNonvirtualIntMethod,
2821 JNI::CallNonvirtualIntMethodV,
2822 JNI::CallNonvirtualIntMethodA,
2823 JNI::CallNonvirtualLongMethod,
2824 JNI::CallNonvirtualLongMethodV,
2825 JNI::CallNonvirtualLongMethodA,
2826 JNI::CallNonvirtualFloatMethod,
2827 JNI::CallNonvirtualFloatMethodV,
2828 JNI::CallNonvirtualFloatMethodA,
2829 JNI::CallNonvirtualDoubleMethod,
2830 JNI::CallNonvirtualDoubleMethodV,
2831 JNI::CallNonvirtualDoubleMethodA,
2832 JNI::CallNonvirtualVoidMethod,
2833 JNI::CallNonvirtualVoidMethodV,
2834 JNI::CallNonvirtualVoidMethodA,
2835 JNI::GetFieldID,
2836 JNI::GetObjectField,
2837 JNI::GetBooleanField,
2838 JNI::GetByteField,
2839 JNI::GetCharField,
2840 JNI::GetShortField,
2841 JNI::GetIntField,
2842 JNI::GetLongField,
2843 JNI::GetFloatField,
2844 JNI::GetDoubleField,
2845 JNI::SetObjectField,
2846 JNI::SetBooleanField,
2847 JNI::SetByteField,
2848 JNI::SetCharField,
2849 JNI::SetShortField,
2850 JNI::SetIntField,
2851 JNI::SetLongField,
2852 JNI::SetFloatField,
2853 JNI::SetDoubleField,
2854 JNI::GetStaticMethodID,
2855 JNI::CallStaticObjectMethod,
2856 JNI::CallStaticObjectMethodV,
2857 JNI::CallStaticObjectMethodA,
2858 JNI::CallStaticBooleanMethod,
2859 JNI::CallStaticBooleanMethodV,
2860 JNI::CallStaticBooleanMethodA,
2861 JNI::CallStaticByteMethod,
2862 JNI::CallStaticByteMethodV,
2863 JNI::CallStaticByteMethodA,
2864 JNI::CallStaticCharMethod,
2865 JNI::CallStaticCharMethodV,
2866 JNI::CallStaticCharMethodA,
2867 JNI::CallStaticShortMethod,
2868 JNI::CallStaticShortMethodV,
2869 JNI::CallStaticShortMethodA,
2870 JNI::CallStaticIntMethod,
2871 JNI::CallStaticIntMethodV,
2872 JNI::CallStaticIntMethodA,
2873 JNI::CallStaticLongMethod,
2874 JNI::CallStaticLongMethodV,
2875 JNI::CallStaticLongMethodA,
2876 JNI::CallStaticFloatMethod,
2877 JNI::CallStaticFloatMethodV,
2878 JNI::CallStaticFloatMethodA,
2879 JNI::CallStaticDoubleMethod,
2880 JNI::CallStaticDoubleMethodV,
2881 JNI::CallStaticDoubleMethodA,
2882 JNI::CallStaticVoidMethod,
2883 JNI::CallStaticVoidMethodV,
2884 JNI::CallStaticVoidMethodA,
2885 JNI::GetStaticFieldID,
2886 JNI::GetStaticObjectField,
2887 JNI::GetStaticBooleanField,
2888 JNI::GetStaticByteField,
2889 JNI::GetStaticCharField,
2890 JNI::GetStaticShortField,
2891 JNI::GetStaticIntField,
2892 JNI::GetStaticLongField,
2893 JNI::GetStaticFloatField,
2894 JNI::GetStaticDoubleField,
2895 JNI::SetStaticObjectField,
2896 JNI::SetStaticBooleanField,
2897 JNI::SetStaticByteField,
2898 JNI::SetStaticCharField,
2899 JNI::SetStaticShortField,
2900 JNI::SetStaticIntField,
2901 JNI::SetStaticLongField,
2902 JNI::SetStaticFloatField,
2903 JNI::SetStaticDoubleField,
2904 JNI::NewString,
2905 JNI::GetStringLength,
2906 JNI::GetStringChars,
2907 JNI::ReleaseStringChars,
2908 JNI::NewStringUTF,
2909 JNI::GetStringUTFLength,
2910 JNI::GetStringUTFChars,
2911 JNI::ReleaseStringUTFChars,
2912 JNI::GetArrayLength,
2913 JNI::NewObjectArray,
2914 JNI::GetObjectArrayElement,
2915 JNI::SetObjectArrayElement,
2916 JNI::NewBooleanArray,
2917 JNI::NewByteArray,
2918 JNI::NewCharArray,
2919 JNI::NewShortArray,
2920 JNI::NewIntArray,
2921 JNI::NewLongArray,
2922 JNI::NewFloatArray,
2923 JNI::NewDoubleArray,
2924 JNI::GetBooleanArrayElements,
2925 JNI::GetByteArrayElements,
2926 JNI::GetCharArrayElements,
2927 JNI::GetShortArrayElements,
2928 JNI::GetIntArrayElements,
2929 JNI::GetLongArrayElements,
2930 JNI::GetFloatArrayElements,
2931 JNI::GetDoubleArrayElements,
2932 JNI::ReleaseBooleanArrayElements,
2933 JNI::ReleaseByteArrayElements,
2934 JNI::ReleaseCharArrayElements,
2935 JNI::ReleaseShortArrayElements,
2936 JNI::ReleaseIntArrayElements,
2937 JNI::ReleaseLongArrayElements,
2938 JNI::ReleaseFloatArrayElements,
2939 JNI::ReleaseDoubleArrayElements,
2940 JNI::GetBooleanArrayRegion,
2941 JNI::GetByteArrayRegion,
2942 JNI::GetCharArrayRegion,
2943 JNI::GetShortArrayRegion,
2944 JNI::GetIntArrayRegion,
2945 JNI::GetLongArrayRegion,
2946 JNI::GetFloatArrayRegion,
2947 JNI::GetDoubleArrayRegion,
2948 JNI::SetBooleanArrayRegion,
2949 JNI::SetByteArrayRegion,
2950 JNI::SetCharArrayRegion,
2951 JNI::SetShortArrayRegion,
2952 JNI::SetIntArrayRegion,
2953 JNI::SetLongArrayRegion,
2954 JNI::SetFloatArrayRegion,
2955 JNI::SetDoubleArrayRegion,
2956 JNI::RegisterNatives,
2957 JNI::UnregisterNatives,
2958 JNI::MonitorEnter,
2959 JNI::MonitorExit,
2960 JNI::GetJavaVM,
2961 JNI::GetStringRegion,
2962 JNI::GetStringUTFRegion,
2963 JNI::GetPrimitiveArrayCritical,
2964 JNI::ReleasePrimitiveArrayCritical,
2965 JNI::GetStringCritical,
2966 JNI::ReleaseStringCritical,
2967 JNI::NewWeakGlobalRef,
2968 JNI::DeleteWeakGlobalRef,
2969 JNI::ExceptionCheck,
2970 JNI::NewDirectByteBuffer,
2971 JNI::GetDirectBufferAddress,
2972 JNI::GetDirectBufferCapacity,
2973 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002974};
2975
Elliott Hughes75770752011-08-24 17:52:38 -07002976JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002977 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002978 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002979 local_ref_cookie(IRT_FIRST_SEGMENT),
2980 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002981 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002982 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002983 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002984 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002985 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002986 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002987 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002988}
2989
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002990JNIEnvExt::~JNIEnvExt() {
2991}
2992
Mathieu Chartier590fee92013-09-13 13:46:47 -07002993jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2994 if (obj == nullptr) {
2995 return nullptr;
2996 }
2997 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2998}
2999
3000void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3001 if (obj != nullptr) {
3002 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
3003 }
3004}
Elliott Hughes88c5c352012-03-15 18:49:48 -07003005void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
3006 check_jni = enabled;
3007 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003008}
3009
Elliott Hughes73e66f72012-05-09 09:34:45 -07003010void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
3011 locals.Dump(os);
3012 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003013}
3014
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07003015void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3016 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003017 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003018 stacked_local_ref_cookies.push_back(local_ref_cookie);
3019 local_ref_cookie = locals.GetSegmentState();
3020}
3021
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07003022void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003023 locals.SetSegmentState(local_ref_cookie);
3024 local_ref_cookie = stacked_local_ref_cookies.back();
3025 stacked_local_ref_cookies.pop_back();
3026}
3027
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003028Offset JNIEnvExt::SegmentStateOffset() {
3029 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
3030 IndirectReferenceTable::SegmentStateOffset().Int32Value());
3031}
3032
Carl Shapiroea4dca82011-08-01 13:45:38 -07003033// JNI Invocation interface.
3034
Brian Carlstrombddf9762013-05-14 11:35:37 -07003035extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003036 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07003037 if (IsBadJniVersion(args->version)) {
3038 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003039 return JNI_EVERSION;
3040 }
Ian Rogerse63db272014-07-15 15:36:11 -07003041 RuntimeOptions options;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003042 for (int i = 0; i < args->nOptions; ++i) {
3043 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08003044 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003045 }
3046 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003047 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003048 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003049 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003050 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003051 bool started = runtime->Start();
3052 if (!started) {
3053 delete Thread::Current()->GetJniEnv();
3054 delete runtime->GetJavaVM();
3055 LOG(WARNING) << "CreateJavaVM failed";
3056 return JNI_ERR;
3057 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003058 *p_env = Thread::Current()->GetJniEnv();
3059 *p_vm = runtime->GetJavaVM();
3060 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003061}
3062
Elliott Hughesf2682d52011-08-15 16:37:04 -07003063extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003064 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003065 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003066 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003067 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003068 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003069 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003070 }
3071 return JNI_OK;
3072}
3073
3074// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003075extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003076 return JNI_ERR;
3077}
3078
Elliott Hughescdf53122011-08-19 15:46:09 -07003079class JII {
3080 public:
3081 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003082 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003083 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003084 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003085 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3086 delete raw_vm->runtime;
3087 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003088 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003089
Elliott Hughescdf53122011-08-19 15:46:09 -07003090 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003091 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003092 }
3093
3094 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003095 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003096 }
3097
3098 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003099 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003100 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003101 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003102 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3103 Runtime* runtime = raw_vm->runtime;
3104 runtime->DetachCurrentThread();
3105 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003106 }
3107
3108 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003109 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3110 // and unlike other calls that take a JNI version doesn't care if you supply
3111 // JNI_VERSION_1_1, which we don't otherwise support.
3112 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003113 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003114 return JNI_EVERSION;
3115 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003116 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003117 return JNI_ERR;
3118 }
3119 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003120 if (thread == nullptr) {
3121 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003122 return JNI_EDETACHED;
3123 }
3124 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003125 return JNI_OK;
3126 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003127};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003128
Elliott Hughes88c5c352012-03-15 18:49:48 -07003129const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003130 nullptr, // reserved0
3131 nullptr, // reserved1
3132 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003133 JII::DestroyJavaVM,
3134 JII::AttachCurrentThread,
3135 JII::DetachCurrentThread,
3136 JII::GetEnv,
3137 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003138};
3139
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003140JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003141 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003142 check_jni_abort_hook(nullptr),
3143 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003144 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003145 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003146 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08003147 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003148 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003149 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003150 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003151 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003152 libraries(new Libraries),
3153 weak_globals_lock_("JNI weak global reference table lock"),
3154 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3155 allow_new_weak_globals_(true),
3156 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003157 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003158 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003159 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003160 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003161}
3162
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003163JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003164 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003165}
3166
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003167jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3168 if (obj == nullptr) {
3169 return nullptr;
3170 }
3171 MutexLock mu(self, weak_globals_lock_);
3172 while (UNLIKELY(!allow_new_weak_globals_)) {
3173 weak_globals_add_condition_.WaitHoldingLocks(self);
3174 }
3175 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3176 return reinterpret_cast<jweak>(ref);
3177}
3178
3179void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3180 MutexLock mu(self, weak_globals_lock_);
3181 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3182 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3183 << "failed to find entry";
3184 }
3185}
3186
Elliott Hughes88c5c352012-03-15 18:49:48 -07003187void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3188 check_jni = enabled;
3189 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003190}
3191
Elliott Hughesae80b492012-04-24 10:43:17 -07003192void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3193 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3194 if (force_copy) {
3195 os << " (with forcecopy)";
3196 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003197 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003198 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003199 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003200 os << "; pins=" << pin_table.Size();
3201 }
3202 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003203 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003204 os << "; globals=" << globals.Capacity();
3205 }
3206 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003207 MutexLock mu(self, weak_globals_lock_);
3208 if (weak_globals_.Capacity() > 0) {
3209 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003210 }
3211 }
3212 os << '\n';
3213
3214 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003215 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003216 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3217 }
3218}
3219
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003220void JavaVMExt::DisallowNewWeakGlobals() {
3221 MutexLock mu(Thread::Current(), weak_globals_lock_);
3222 allow_new_weak_globals_ = false;
3223}
3224
3225void JavaVMExt::AllowNewWeakGlobals() {
3226 Thread* self = Thread::Current();
3227 MutexLock mu(self, weak_globals_lock_);
3228 allow_new_weak_globals_ = true;
3229 weak_globals_add_condition_.Broadcast(self);
3230}
3231
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003232mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3233 MutexLock mu(self, weak_globals_lock_);
3234 while (UNLIKELY(!allow_new_weak_globals_)) {
3235 weak_globals_add_condition_.WaitHoldingLocks(self);
3236 }
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -07003237 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003238}
3239
Elliott Hughes73e66f72012-05-09 09:34:45 -07003240void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003241 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003242 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003243 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003244 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003245 }
3246 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003247 MutexLock mu(self, weak_globals_lock_);
3248 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003249 }
3250 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003251 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003252 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003253 }
3254}
3255
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003256bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07003257 Handle<mirror::ClassLoader> class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003258 std::string* detail) {
3259 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003260
3261 // See if we've already loaded this library. If we have, and the class loader
3262 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003263 // TODO: for better results we should canonicalize the pathname (or even compare
3264 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003265 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003266 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003267 {
3268 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003269 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003270 library = libraries->Get(path);
3271 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003272 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003273 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003274 // The library will be associated with class_loader. The JNI
3275 // spec says we can't load the same library into more than one
3276 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003277 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003278 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003279 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003280 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003281 return false;
3282 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003283 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003284 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003285 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003286 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003287 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003288 return false;
3289 }
3290 return true;
3291 }
3292
3293 // Open the shared library. Because we're using a full path, the system
3294 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3295 // resolve this library's dependencies though.)
3296
3297 // Failures here are expected when java.library.path has several entries
3298 // and we have to hunt for the lib.
3299
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003300 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3301 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3302 // dlopen) becomes zero from dlclose.
3303
Elliott Hughescdf53122011-08-19 15:46:09 -07003304 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003305 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003306 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Yong WU355383f2014-07-24 21:32:15 +08003307 const char* path_str = path.empty() ? nullptr : path.c_str();
3308 void* handle = dlopen(path_str, RTLD_LAZY);
3309 bool needs_native_bridge = false;
3310 if (handle == nullptr) {
Calin Juravle93de4272014-08-12 20:55:20 +01003311 if (android::NativeBridgeIsSupported(path_str)) {
3312 handle = android::NativeBridgeLoadLibrary(path_str, RTLD_LAZY);
Yong WU355383f2014-07-24 21:32:15 +08003313 needs_native_bridge = true;
3314 }
3315 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003316 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003317
Elliott Hughes84b2f142012-09-27 09:16:28 -07003318 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003319
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003320 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003321 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003322 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003323 return false;
3324 }
3325
3326 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003327 // TODO: move the locking (and more of this logic) into Libraries.
3328 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003329 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003330 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003331 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003332 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003333 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003334 libraries->Put(path, library);
3335 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003336 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003337 }
3338 if (!created_library) {
3339 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003340 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003341 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003342 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003343
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003344 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003345 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003346
Elliott Hughes79353722013-08-02 16:52:18 -07003347 bool was_successful = false;
Yong WU355383f2014-07-24 21:32:15 +08003348 void* sym = nullptr;
3349 if (UNLIKELY(needs_native_bridge)) {
3350 library->SetNeedsNativeBridge();
3351 sym = library->FindSymbolWithNativeBridge("JNI_OnLoad", nullptr);
3352 } else {
3353 sym = dlsym(handle, "JNI_OnLoad");
3354 }
3355
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003356 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003357 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003358 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003359 } else {
3360 // Call JNI_OnLoad. We have to override the current class
3361 // loader, which will always be "null" since the stuff at the
3362 // top of the stack is around Runtime.loadLibrary(). (See
3363 // the comments in the JNI FindClass function.)
3364 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3365 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003366 StackHandleScope<1> hs(self);
3367 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3368 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003369
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003370 int version = 0;
3371 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003372 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003373 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003374 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003375 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003376
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003377 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003378
Elliott Hughes79353722013-08-02 16:52:18 -07003379 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003380 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003381 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003382 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003383 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003384 // It's unwise to call dlclose() here, but we can mark it
3385 // as bad and ensure that future load attempts will fail.
3386 // We don't know how far JNI_OnLoad got, so there could
3387 // be some partially-initialized stuff accessible through
3388 // newly-registered native method calls. We could try to
3389 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003390 } else {
3391 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003392 }
Elliott Hughes79353722013-08-02 16:52:18 -07003393 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003394 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003395 }
3396
Elliott Hughes79353722013-08-02 16:52:18 -07003397 library->SetResult(was_successful);
3398 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003399}
3400
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003401void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003402 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003403 mirror::Class* c = m->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -07003404 // If this is a static method, it could be called before the class has been initialized.
Elliott Hughes79082e32011-08-25 12:07:32 -07003405 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003406 c = EnsureInitialized(Thread::Current(), c);
3407 if (c == nullptr) {
3408 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003409 }
3410 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003411 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003412 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003413 std::string detail;
3414 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003415 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003416 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003417 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003418 native_method = libraries->FindNativeMethod(m, detail);
3419 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003420 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003421 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003422 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3423 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003424 }
3425 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003426}
3427
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003428void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003429 MutexLock mu(Thread::Current(), weak_globals_lock_);
3430 for (mirror::Object** entry : weak_globals_) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003431 // Since this is called by the GC, we don't need a read barrier.
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003432 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003433 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003434 if (new_obj == nullptr) {
3435 new_obj = kClearedJniWeakGlobal;
3436 }
3437 *entry = new_obj;
3438 }
3439}
3440
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003441void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003442 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003443 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003444 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003445 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003446 }
3447 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003448 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003449 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003450 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003451 {
3452 MutexLock mu(self, libraries_lock);
3453 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003454 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003455 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003456 // The weak_globals table is visited by the GC itself (because it mutates the table).
3457}
3458
Elliott Hughesc8fece32013-01-02 11:27:23 -08003459void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003460 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003461 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003462 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003463 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3464 }
3465 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3466}
3467
Ian Rogersdf20fe02011-07-20 20:34:16 -07003468} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003469
3470std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3471 switch (rhs) {
3472 case JNIInvalidRefType:
3473 os << "JNIInvalidRefType";
3474 return os;
3475 case JNILocalRefType:
3476 os << "JNILocalRefType";
3477 return os;
3478 case JNIGlobalRefType:
3479 os << "JNIGlobalRefType";
3480 return os;
3481 case JNIWeakGlobalRefType:
3482 os << "JNIWeakGlobalRefType";
3483 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003484 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003485 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003486 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003487 }
3488}