blob: d5b90f2cfe6343a8dde2aaea471f21cf5d3815f2 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
Elliott Hughes0af55432011-08-17 18:37:28 -070023#include <utility>
24#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025
Ian Rogersef7d42f2014-01-06 12:55:46 -080026#include "atomic.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080028#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080029#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080030#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "dex_file-inl.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070032#include "gc_root.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/accounting/card_table-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070034#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070035#include "interpreter/interpreter.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070036#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070037#include "mirror/art_field-inl.h"
38#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/class-inl.h"
40#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/object-inl.h"
42#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070043#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/throwable.h"
Yong WU355383f2014-07-24 21:32:15 +080045#include "native_bridge.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080046#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070047#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070048#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070049#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070051#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070052#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070054#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070055
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070056namespace art {
57
Brian Carlstrom7934ac22013-07-26 10:54:15 -070058static const size_t kMonitorsInitial = 32; // Arbitrary.
59static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070060
Brian Carlstrom7934ac22013-07-26 10:54:15 -070061static const size_t kLocalsInitial = 64; // Arbitrary.
62static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070063
Brian Carlstrom7934ac22013-07-26 10:54:15 -070064static const size_t kPinTableInitial = 16; // Arbitrary.
65static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070066
Brian Carlstrom7934ac22013-07-26 10:54:15 -070067static size_t gGlobalsInitial = 512; // Arbitrary.
68static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070069
Brian Carlstrom7934ac22013-07-26 10:54:15 -070070static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
71static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070072
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080073static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070074 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070075 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070076}
77
Jeff Hao19c5d372013-03-15 14:33:43 -070078static bool IsBadJniVersion(int version) {
79 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
80 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
81}
82
Elliott Hughes6b436852011-08-12 10:16:44 -070083// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
84// separated with slashes but aren't wrapped with "L;" like regular descriptors
85// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
86// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
87// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070088static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070089 std::string result;
90 // Add the missing "L;" if necessary.
91 if (name[0] == '[') {
92 result = name;
93 } else {
94 result += 'L';
95 result += name;
96 result += ';';
97 }
98 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070099 if (result.find('.') != std::string::npos) {
100 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
101 << "\"" << name << "\"";
102 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700103 }
104 return result;
105}
106
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800107static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700108 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800110 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
111 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
112 "no %s method \"%s.%s%s\"",
Mathieu Chartierf8322842014-05-16 10:59:25 -0700113 kind, c->GetDescriptor().c_str(), 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 Rogers62d6c772013-02-27 08:32:07 -0800220 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800221 "no type \"%s\" found and so no field \"%s\" "
222 "could be found in class \"%s\" or its superclasses", sig, name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700223 c->GetDescriptor().c_str());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700224 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800225 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700226 }
227 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700228 field = mirror::Class::FindStaticField(soa.Self(), c, name,
229 field_type->GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700230 } else {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700231 field = c->FindInstanceField(name, field_type->GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700232 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800233 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800234 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
235 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
236 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartierf8322842014-05-16 10:59:25 -0700237 sig, name, c->GetDescriptor().c_str());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800238 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700239 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700241}
242
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800243static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700246 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700247 vm->pin_table.Add(array);
248}
249
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800250static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700251 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700253 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700254 vm->pin_table.Remove(array);
255}
256
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800257static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700259 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700260 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800261 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
262 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
263 "%s offset=%d length=%d %s.length=%d",
264 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700265}
Ian Rogers0571d352011-11-03 19:51:38 -0700266
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
268 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700269 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800270 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
271 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
272 "offset=%d length=%d string.length()=%d", start, length,
273 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700274}
Elliott Hughes814e4032011-08-23 12:07:56 -0700275
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700277 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 // Turn the const char* into a java.lang.String.
279 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800280 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700282 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700283
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284 // Choose an appropriate constructor and set up the arguments.
285 jvalue args[2];
286 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800287 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700288 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800289 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290 signature = "(Ljava/lang/String;)V";
291 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800292 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 signature = "(Ljava/lang/Throwable;)V";
294 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700295 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
297 args[0].l = s.get();
298 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700299 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700300 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800301 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800302 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800304 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305 return JNI_ERR;
306 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700307
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800308 ScopedLocalRef<jthrowable> exception(
309 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
310 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311 return JNI_ERR;
312 }
Ian Rogersef28b142012-11-30 14:22:18 -0800313 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800314 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800315 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700316 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700317}
318
Elliott Hughes462c9442012-03-23 18:47:50 -0700319static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800320 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700321 return JNI_ERR;
322 }
323
Elliott Hughes462c9442012-03-23 18:47:50 -0700324 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700325 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800326 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700327 *p_env = self->GetJniEnv();
328 return JNI_OK;
329 }
330
331 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
332
333 // No threads allowed in zygote mode.
334 if (runtime->IsZygote()) {
335 LOG(ERROR) << "Attempt to attach a thread in the zygote";
336 return JNI_ERR;
337 }
338
Elliott Hughes462c9442012-03-23 18:47:50 -0700339 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800340 const char* thread_name = nullptr;
341 jobject thread_group = nullptr;
342 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700343 if (IsBadJniVersion(args->version)) {
344 LOG(ERROR) << "Bad JNI version passed to "
345 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
346 << args->version;
347 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700348 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700349 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700350 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700351 }
Elliott Hughes75770752011-08-24 17:52:38 -0700352
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800353 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800354 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700355 return JNI_ERR;
356 } else {
357 *p_env = Thread::Current()->GetJniEnv();
358 return JNI_OK;
359 }
Elliott Hughes75770752011-08-24 17:52:38 -0700360}
361
Elliott Hughes79082e32011-08-25 12:07:32 -0700362class SharedLibrary {
363 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800364 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700365 : path_(path),
366 handle_(handle),
Yong WU355383f2014-07-24 21:32:15 +0800367 needs_native_bridge_(false),
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700368 class_loader_(GcRoot<mirror::Object>(class_loader)),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700369 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700370 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700371 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700372 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700373 }
374
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700375 mirror::Object* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700376 return class_loader_.Read();
Elliott Hughes79082e32011-08-25 12:07:32 -0700377 }
378
379 std::string GetPath() {
380 return path_;
381 }
382
383 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700384 * Check the result of an earlier call to JNI_OnLoad on this library.
385 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700386 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700387 bool CheckOnLoadResult()
388 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700389 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700390 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700391 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
392 bool okay;
393 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700394 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700395
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700396 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700397 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
398 // caller can continue.
399 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
400 okay = true;
401 } else {
402 while (jni_on_load_result_ == kPending) {
403 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700404 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700405 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700406
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700407 okay = (jni_on_load_result_ == kOkay);
408 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
409 << (okay ? "succeeded" : "failed") << "]";
410 }
411 }
412 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700413 return okay;
414 }
415
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700416 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700417 Thread* self = Thread::Current();
418 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700419
Elliott Hughes79082e32011-08-25 12:07:32 -0700420 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700421 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700422
423 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700424 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700425 }
426
Yong WU355383f2014-07-24 21:32:15 +0800427 void SetNeedsNativeBridge() {
428 needs_native_bridge_ = true;
429 }
430
431 bool NeedsNativeBridge() const {
432 return needs_native_bridge_;
433 }
434
Elliott Hughes79082e32011-08-25 12:07:32 -0700435 void* FindSymbol(const std::string& symbol_name) {
436 return dlsym(handle_, symbol_name.c_str());
437 }
438
Yong WU355383f2014-07-24 21:32:15 +0800439 void* FindSymbolWithNativeBridge(const std::string& symbol_name, mirror::ArtMethod* m)
440 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
441 CHECK(NeedsNativeBridge());
442
443 uint32_t len = 0;
444 const char* shorty = nullptr;
445 if (m != nullptr) {
446 shorty = m->GetShorty(&len);
447 }
448 return NativeBridge::GetTrampoline(handle_, symbol_name.c_str(), shorty, len);
449 }
450
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800451 void VisitRoots(RootCallback* visitor, void* arg) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700452 if (!class_loader_.IsNull()) {
453 class_loader_.VisitRoot(visitor, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800454 }
455 }
456
Elliott Hughes79082e32011-08-25 12:07:32 -0700457 private:
458 enum JNI_OnLoadState {
459 kPending,
460 kFailed,
461 kOkay,
462 };
463
464 // Path to library "/system/lib/libjni.so".
465 std::string path_;
466
467 // The void* returned by dlopen(3).
468 void* handle_;
469
Yong WU355383f2014-07-24 21:32:15 +0800470 // True if a native bridge is required.
471 bool needs_native_bridge_;
472
Elliott Hughes79082e32011-08-25 12:07:32 -0700473 // The ClassLoader this library is associated with.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700474 GcRoot<mirror::Object> class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700475
476 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700477 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700478 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700479 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700480 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700481 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700482 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700483 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700484};
485
Elliott Hughes79082e32011-08-25 12:07:32 -0700486// This exists mainly to keep implementation details out of the header file.
487class Libraries {
488 public:
489 Libraries() {
490 }
491
492 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700493 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700494 }
495
Elliott Hughesae80b492012-04-24 10:43:17 -0700496 void Dump(std::ostream& os) const {
497 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700498 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700499 if (!first) {
500 os << ' ';
501 }
502 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700503 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700504 }
505 }
506
507 size_t size() const {
508 return libraries_.size();
509 }
510
Elliott Hughes79082e32011-08-25 12:07:32 -0700511 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700512 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800513 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 }
515
516 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700517 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700518 }
519
520 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800521 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700522 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700523 std::string jni_short_name(JniShortName(m));
524 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800525 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700526 for (const auto& lib : libraries_) {
527 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700528 if (library->GetClassLoader() != declaring_class_loader) {
529 // We only search libraries loaded by the appropriate ClassLoader.
530 continue;
531 }
532 // Try the short name then the long name...
Yong WU355383f2014-07-24 21:32:15 +0800533 void* fn = nullptr;
534 if (UNLIKELY(library->NeedsNativeBridge())) {
535 fn = library->FindSymbolWithNativeBridge(jni_short_name, m);
536 if (fn == nullptr) {
537 fn = library->FindSymbolWithNativeBridge(jni_long_name, m);
538 }
539 } else {
540 fn = library->FindSymbol(jni_short_name);
541 if (fn == nullptr) {
542 fn = library->FindSymbol(jni_long_name);
543 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700544 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800545 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800546 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
547 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700548 return fn;
549 }
550 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700551 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700552 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700553 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800555 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700556 }
557
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800558 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800559 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800560 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800561 }
562 }
563
Elliott Hughes79082e32011-08-25 12:07:32 -0700564 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700565 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700566};
567
Ian Rogers2d10b202014-05-12 19:15:18 -0700568#define CHECK_NON_NULL_ARGUMENT(value) \
569 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700570
Ian Rogers2d10b202014-05-12 19:15:18 -0700571#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
572 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
573
574#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
575 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
576
577#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
578 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
579
580#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800581 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700582 JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700583 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700584 }
585
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700586#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800587 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700588 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700589 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700590 }
591
Elliott Hughescdf53122011-08-19 15:46:09 -0700592class JNI {
593 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700594 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700595 return JNI_VERSION_1_6;
596 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700597
Ian Rogers25e8b912012-09-07 11:31:36 -0700598 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700599 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800600 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700601 }
602
Elliott Hughescdf53122011-08-19 15:46:09 -0700603 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700604 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700605 Runtime* runtime = Runtime::Current();
606 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700607 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700608 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800609 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700610 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700611 StackHandleScope<1> hs(soa.Self());
612 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800613 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700614 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800615 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700616 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700617 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700618 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700619
Ian Rogers62f05122014-03-21 11:21:29 -0700620 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700621 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700623 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700624 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700625
Ian Rogers62f05122014-03-21 11:21:29 -0700626 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700627 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700629 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700630 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700631
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700633 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800635 mirror::ArtMethod* m = soa.DecodeMethod(mid);
636 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700637 jobject art_method = soa.AddLocalReference<jobject>(m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200638 jobject reflect_method;
639 if (m->IsConstructor()) {
640 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
641 } else {
642 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
643 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700644 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800645 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700646 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800647 SetObjectField(env, reflect_method,
648 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700649 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700650 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700651
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700653 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800655 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700656 jobject art_field = soa.AddLocalReference<jobject>(f);
657 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
658 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800659 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700660 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800661 SetObjectField(env, reflect_field,
662 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700663 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700664 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700665
Elliott Hughes37f7a402011-08-22 18:56:01 -0700666 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700667 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800669 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700671 }
672
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700673 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700674 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700675 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800676 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700677 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700679
Elliott Hughes37f7a402011-08-22 18:56:01 -0700680 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700681 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
682 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800684 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
685 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700686 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700687 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700688
Elliott Hughese84278b2012-03-22 10:06:53 -0700689 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700690 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800691 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700692 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700693 return JNI_TRUE;
694 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700695 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800696 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
697 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700698 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700699 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700700 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700701
Elliott Hughes37f7a402011-08-22 18:56:01 -0700702 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700703 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800704 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
705 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700706 return JNI_ERR;
707 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800708 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
709 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700710 return JNI_OK;
711 }
712
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700713 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700714 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800715 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700716 }
717
718 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700719 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700720 }
721
722 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700723 ScopedObjectAccess soa(env);
724 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700725 }
726
727 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700729
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700730 // If we have no exception to describe, pass through.
731 if (!soa.Self()->GetException(nullptr)) {
732 return;
733 }
734
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700735 StackHandleScope<3> hs(soa.Self());
736 // TODO: Use nullptr instead of null handles?
737 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
738 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
739 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800740 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200741 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800742 {
743 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800744 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700745 old_throw_this_object.Assign(old_throw_location.GetThis());
746 old_throw_method.Assign(old_throw_location.GetMethod());
747 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800748 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200749 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800750 soa.Self()->ClearException();
751 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800752 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700753 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700754 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
755 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800756 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700757 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700758 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700759 } else {
760 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800761 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800762 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700763 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800764 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700765 }
766 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700767 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800768 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700769
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700770 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200771 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700773
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700775 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800776 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700777 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 }
779
Ian Rogers25e8b912012-09-07 11:31:36 -0700780 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700781 LOG(FATAL) << "JNI FatalError called: " << msg;
782 }
783
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700784 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700785 // TODO: SOA may not be necessary but I do it to please lock annotations.
786 ScopedObjectAccess soa(env);
787 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700788 return JNI_ERR;
789 }
Ian Rogersef28b142012-11-30 14:22:18 -0800790 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 return JNI_OK;
792 }
793
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700794 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700795 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800796 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700797 soa.Env()->PopFrame();
798 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700799 }
800
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700801 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700802 // TODO: SOA may not be necessary but I do it to please lock annotations.
803 ScopedObjectAccess soa(env);
804 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700805 }
806
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700808 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800809 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800810 // Check for null after decoding the object to handle cleared weak globals.
811 if (decoded_obj == nullptr) {
812 return nullptr;
813 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700814 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700816 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700817 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700818 return reinterpret_cast<jobject>(ref);
819 }
820
821 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800822 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 return;
824 }
Ian Rogersef28b142012-11-30 14:22:18 -0800825 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700826 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800827 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700828 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700829
830 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
831 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
832 << "failed to find entry";
833 }
834 }
835
836 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700837 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800838 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 }
840
841 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700842 if (obj != nullptr) {
843 ScopedObjectAccess soa(env);
844 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700845 }
846 }
847
848 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700849 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800850 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800851 // Check for null after decoding the object to handle cleared weak globals.
852 if (decoded_obj == nullptr) {
853 return nullptr;
854 }
855 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 }
857
858 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800859 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 return;
861 }
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700862 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -0800863 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700864
Ian Rogersef28b142012-11-30 14:22:18 -0800865 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 if (!locals.Remove(cookie, obj)) {
867 // Attempting to delete a local reference that is not in the
868 // topmost local reference frame is a no-op. DeleteLocalRef returns
869 // void and doesn't throw any exceptions, but we should probably
870 // complain about it so the user will notice that things aren't
871 // going quite the way they expect.
872 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
873 << "failed to find entry";
874 }
875 }
876
877 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700878 if (obj1 == obj2) {
879 return JNI_TRUE;
880 } else {
881 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800882 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
883 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700884 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700885 }
886
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700887 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700888 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700889 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800890 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800891 if (c == nullptr) {
892 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700893 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700894 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 }
896
Ian Rogersbc939662013-08-15 10:26:54 -0700897 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700898 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700900 CHECK_NON_NULL_ARGUMENT(java_class);
901 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700902 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 va_end(args);
904 return result;
905 }
906
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700908 CHECK_NON_NULL_ARGUMENT(java_class);
909 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700910 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800911 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800912 if (c == nullptr) {
913 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700914 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800915 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800916 if (result == nullptr) {
917 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700918 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700919 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800921 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800922 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700923 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800924 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 }
926
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700928 CHECK_NON_NULL_ARGUMENT(java_class);
929 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700930 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800931 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800932 if (c == nullptr) {
933 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700934 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800935 mirror::Object* result = c->AllocObject(soa.Self());
936 if (result == nullptr) {
937 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700938 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700939 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800941 if (soa.Self()->IsExceptionPending()) {
942 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700943 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800944 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 }
946
Ian Rogersbc939662013-08-15 10:26:54 -0700947 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700948 CHECK_NON_NULL_ARGUMENT(java_class);
949 CHECK_NON_NULL_ARGUMENT(name);
950 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700951 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700952 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 }
954
Ian Rogersbc939662013-08-15 10:26:54 -0700955 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
956 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700957 CHECK_NON_NULL_ARGUMENT(java_class);
958 CHECK_NON_NULL_ARGUMENT(name);
959 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700961 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 va_list ap;
966 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700967 CHECK_NON_NULL_ARGUMENT(obj);
968 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700969 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700972 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700976 CHECK_NON_NULL_ARGUMENT(obj);
977 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700978 ScopedObjectAccess soa(env);
979 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
980 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700984 CHECK_NON_NULL_ARGUMENT(obj);
985 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700987 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
988 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 }
991
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_list ap;
994 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700995 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
996 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700997 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001000 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001004 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1005 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001006 ScopedObjectAccess soa(env);
1007 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001011 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1012 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001014 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1015 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_list ap;
1020 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001021 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1022 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001023 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001026 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001030 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1031 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001032 ScopedObjectAccess soa(env);
1033 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001037 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1038 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001039 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001040 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1041 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 }
1043
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 va_list ap;
1046 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001047 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1048 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001049 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001050 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001052 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001056 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1057 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001058 ScopedObjectAccess soa(env);
1059 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001063 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1064 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001065 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001066 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1067 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_list ap;
1072 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001073 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1074 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001075 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001078 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001082 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1083 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001084 ScopedObjectAccess soa(env);
1085 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001089 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1090 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001091 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001092 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1093 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_list ap;
1098 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001099 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1100 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001101 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001102 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001104 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001108 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1109 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001110 ScopedObjectAccess soa(env);
1111 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001115 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1116 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001117 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001118 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1119 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_list ap;
1124 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001125 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1126 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001127 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001128 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001130 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001134 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1135 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001136 ScopedObjectAccess soa(env);
1137 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001141 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1142 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001143 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001144 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1145 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 }
1147
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 va_list ap;
1150 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001151 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1152 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001153 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001154 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001156 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001160 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1161 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162 ScopedObjectAccess soa(env);
1163 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001167 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1168 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001169 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001170 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1171 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 va_list ap;
1176 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001177 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1178 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001179 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001180 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001182 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001186 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1187 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 ScopedObjectAccess soa(env);
1189 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001193 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1194 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001195 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001196 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1197 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 }
1199
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 va_list ap;
1202 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001203 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1204 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001205 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001206 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001211 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1212 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001213 ScopedObjectAccess soa(env);
1214 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001218 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1219 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001220 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001221 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001224 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001227 CHECK_NON_NULL_ARGUMENT(obj);
1228 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001229 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001230 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1231 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 va_end(ap);
1233 return local_result;
1234 }
1235
Ian Rogersbc939662013-08-15 10:26:54 -07001236 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1237 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001238 CHECK_NON_NULL_ARGUMENT(obj);
1239 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001240 ScopedObjectAccess soa(env);
1241 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1242 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
1244
Ian Rogersbc939662013-08-15 10:26:54 -07001245 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1246 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001247 CHECK_NON_NULL_ARGUMENT(obj);
1248 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001249 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001250 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001251 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
Ian Rogersbc939662013-08-15 10:26:54 -07001254 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1255 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001258 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1259 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001260 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001261 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001263 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 }
1265
Ian Rogersbc939662013-08-15 10:26:54 -07001266 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1267 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001268 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1269 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001270 ScopedObjectAccess soa(env);
1271 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 }
1273
Ian Rogersbc939662013-08-15 10:26:54 -07001274 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1275 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001276 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1277 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001278 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001279 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
1281
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001282 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001285 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1286 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001287 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001288 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001290 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
Ian Rogersbc939662013-08-15 10:26:54 -07001293 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1294 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001295 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1296 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001297 ScopedObjectAccess soa(env);
1298 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
Ian Rogersbc939662013-08-15 10:26:54 -07001301 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1302 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001303 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1304 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001305 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001306 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001309 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001312 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1313 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001314 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001315 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001317 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
Ian Rogersbc939662013-08-15 10:26:54 -07001320 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1321 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001322 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1323 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001324 ScopedObjectAccess soa(env);
1325 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
Ian Rogersbc939662013-08-15 10:26:54 -07001328 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1329 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001330 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1331 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001332 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001333 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001336 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001339 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1340 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001341 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001342 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001344 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 }
1346
Ian Rogersbc939662013-08-15 10:26:54 -07001347 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1348 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001349 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1350 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001351 ScopedObjectAccess soa(env);
1352 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 }
1354
Ian Rogersbc939662013-08-15 10:26:54 -07001355 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1356 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001357 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1358 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001359 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001360 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001363 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001365 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001366 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1367 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001368 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001369 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001371 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
Ian Rogersbc939662013-08-15 10:26:54 -07001374 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1375 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001376 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1377 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001378 ScopedObjectAccess soa(env);
1379 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 }
1381
Ian Rogersbc939662013-08-15 10:26:54 -07001382 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1383 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001384 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1385 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001386 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001387 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 }
1389
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001390 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001392 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001393 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1394 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001395 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001396 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001398 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
Ian Rogersbc939662013-08-15 10:26:54 -07001401 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1402 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001403 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1404 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001405 ScopedObjectAccess soa(env);
1406 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 }
1408
Ian Rogersbc939662013-08-15 10:26:54 -07001409 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1410 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001411 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1412 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001413 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001414 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
1416
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001417 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001419 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001420 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1421 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001422 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001423 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001425 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Ian Rogersbc939662013-08-15 10:26:54 -07001428 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1429 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001430 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1431 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001432 ScopedObjectAccess soa(env);
1433 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Ian Rogersbc939662013-08-15 10:26:54 -07001436 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1437 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001438 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1439 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001440 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001441 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001444 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001446 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001447 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1448 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001449 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001450 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001452 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Ian Rogersbc939662013-08-15 10:26:54 -07001455 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1456 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001457 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1458 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001459 ScopedObjectAccess soa(env);
1460 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Ian Rogersbc939662013-08-15 10:26:54 -07001463 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1464 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001465 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1466 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001467 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001468 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001471 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001473 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001474 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1475 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001476 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001477 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 va_end(ap);
1479 }
1480
Brian Carlstromea46f952013-07-30 01:26:50 -07001481 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1482 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001483 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1484 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001485 ScopedObjectAccess soa(env);
1486 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 }
1488
Ian Rogersbc939662013-08-15 10:26:54 -07001489 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1490 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001491 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1492 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001493 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001494 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 }
1496
Ian Rogersbc939662013-08-15 10:26:54 -07001497 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001498 CHECK_NON_NULL_ARGUMENT(java_class);
1499 CHECK_NON_NULL_ARGUMENT(name);
1500 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001501 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001502 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001504
Ian Rogersbc939662013-08-15 10:26:54 -07001505 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1506 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001507 CHECK_NON_NULL_ARGUMENT(java_class);
1508 CHECK_NON_NULL_ARGUMENT(name);
1509 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001510 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001511 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001513
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001515 CHECK_NON_NULL_ARGUMENT(obj);
1516 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001517 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001518 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1519 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001520 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001522
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001523 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001524 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001525 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001526 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001527 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
1529
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001530 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001531 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1532 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001533 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001534 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1535 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1536 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001537 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001541 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001542 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001543 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1544 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001545 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001548#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001549 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1550 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001551 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001552 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1553 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001554 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001555
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001556#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001557 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001558 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001559 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001560 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001561
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001562#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001563 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1564 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001565 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001566 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1567 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001568 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001569
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001570#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001571 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001572 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001573 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001574 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001575
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001576 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001577 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001580 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001581 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001584 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001585 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001588 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001589 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001596 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001600 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001604 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001608 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001612 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001616 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001617 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001621 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001624 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001625 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 }
1627
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001628 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001629 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001630 }
1631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001632 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001633 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001637 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001638 }
1639
1640 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001641 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001642 }
1643
1644 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001645 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001646 }
1647
1648 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001649 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001650 }
1651
1652 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001653 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001654 }
1655
1656 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001657 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001658 }
1659
1660 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001661 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001662 }
1663
1664 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001665 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001666 }
1667
1668 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001669 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001670 }
1671
1672 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001673 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001674 }
1675
1676 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001677 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001678 }
1679
1680 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001681 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001682 }
1683
1684 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001685 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001686 }
1687
1688 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001689 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001690 }
1691
1692 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001693 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001694 }
1695
1696 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001697 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001698 }
1699
1700 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001701 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 }
1703
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001704 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001706 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001707 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001708 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001709 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001710 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 va_end(ap);
1712 return local_result;
1713 }
1714
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001715 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001716 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001717 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001718 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001719 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 }
1721
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001722 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001723 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001724 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001725 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001732 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001733 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001734 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001736 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001740 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001742 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001746 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001747 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001748 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001751 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001753 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001754 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001755 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001756 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001758 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001761 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001762 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001763 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001764 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001767 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001768 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001769 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001770 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001773 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001775 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001776 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001777 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001778 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001780 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001783 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001784 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001785 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001786 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 }
1788
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001789 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001790 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001791 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001792 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001795 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001797 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001798 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001799 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001800 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001802 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001806 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001807 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001808 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001811 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001812 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001813 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001814 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001817 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001819 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001820 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001821 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001822 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001824 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001827 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001828 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001829 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001830 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001833 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001834 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001835 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001836 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001839 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001841 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001842 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001843 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001844 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001846 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 }
1848
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001849 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001850 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001851 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001852 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 }
1854
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001855 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001856 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001857 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001858 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 }
1860
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001861 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001863 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001864 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001865 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001866 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001868 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001871 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001872 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001873 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001874 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 }
1876
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001877 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001878 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001879 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001880 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001881 }
1882
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001883 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001884 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001885 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001886 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001887 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001888 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001890 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001893 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001894 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001895 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001896 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001897 }
1898
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001899 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001900 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001901 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001902 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001903 }
1904
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001905 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001907 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001908 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001909 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001910 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 va_end(ap);
1912 }
1913
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001914 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001915 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001916 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001917 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 }
1919
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001920 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001921 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001922 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001923 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001924 }
1925
Elliott Hughes814e4032011-08-23 12:07:56 -07001926 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001927 if (UNLIKELY(char_count < 0)) {
1928 JniAbortF("NewString", "char_count < 0: %d", char_count);
1929 return nullptr;
1930 }
1931 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1932 JniAbortF("NewString", "chars == null && char_count > 0");
1933 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001934 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001935 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001936 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001937 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 }
1939
1940 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001941 if (utf == nullptr) {
1942 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001944 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001945 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001946 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 }
1948
Elliott Hughes814e4032011-08-23 12:07:56 -07001949 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001950 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001951 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001952 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001953 }
1954
1955 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001956 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001957 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001958 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001959 }
1960
Ian Rogersbc939662013-08-15 10:26:54 -07001961 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1962 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001963 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001964 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001965 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001966 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001967 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001968 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001969 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001970 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1971 memcpy(buf, chars + start, length * sizeof(jchar));
1972 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001973 }
1974
Ian Rogersbc939662013-08-15 10:26:54 -07001975 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1976 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001977 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001978 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001979 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001980 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001981 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001982 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001983 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001984 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1985 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1986 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001987 }
1988
Elliott Hughes75770752011-08-24 17:52:38 -07001989 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001990 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001991 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001992 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1993 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001994 PinPrimitiveArray(soa, chars);
Fred Shih56890e22014-06-02 11:11:52 -07001995 gc::Heap* heap = Runtime::Current()->GetHeap();
1996 if (heap->IsMovableObject(chars)) {
1997 if (is_copy != nullptr) {
1998 *is_copy = JNI_TRUE;
1999 }
2000 int32_t char_count = s->GetLength();
2001 int32_t offset = s->GetOffset();
2002 jchar* bytes = new jchar[char_count];
2003 for (int32_t i = 0; i < char_count; i++) {
2004 bytes[i] = chars->Get(i + offset);
2005 }
2006 return bytes;
2007 } else {
2008 if (is_copy != nullptr) {
2009 *is_copy = JNI_FALSE;
2010 }
2011 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07002012 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002013 }
2014
Mathieu Chartier590fee92013-09-13 13:46:47 -07002015 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002016 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002017 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07002018 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2019 mirror::CharArray* s_chars = s->GetCharArray();
2020 if (chars != (s_chars->GetData() + s->GetOffset())) {
2021 delete[] chars;
2022 }
2023 UnpinPrimitiveArray(soa, s->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes75770752011-08-24 17:52:38 -07002026 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07002027 CHECK_NON_NULL_ARGUMENT(java_string);
2028 ScopedObjectAccess soa(env);
2029 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2030 mirror::CharArray* chars = s->GetCharArray();
2031 int32_t offset = s->GetOffset();
2032 PinPrimitiveArray(soa, chars);
2033 gc::Heap* heap = Runtime::Current()->GetHeap();
2034 if (heap->IsMovableObject(chars)) {
2035 StackHandleScope<1> hs(soa.Self());
2036 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
2037 heap->IncrementDisableMovingGC(soa.Self());
2038 }
2039 if (is_copy != nullptr) {
2040 *is_copy = JNI_FALSE;
2041 }
2042 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes75770752011-08-24 17:52:38 -07002045 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07002046 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
2047 ScopedObjectAccess soa(env);
2048 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
2049 gc::Heap* heap = Runtime::Current()->GetHeap();
2050 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2051 mirror::CharArray* s_chars = s->GetCharArray();
2052 if (heap->IsMovableObject(s_chars)) {
2053 heap->DecrementDisableMovingGC(soa.Self());
2054 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes75770752011-08-24 17:52:38 -07002057 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002058 if (java_string == nullptr) {
2059 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002060 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002061 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002062 *is_copy = JNI_TRUE;
2063 }
Ian Rogersef28b142012-11-30 14:22:18 -08002064 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002065 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002066 size_t byte_count = s->GetUtfLength();
2067 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002068 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002069 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2070 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2071 bytes[byte_count] = '\0';
2072 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002073 }
2074
Elliott Hughes75770752011-08-24 17:52:38 -07002075 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002076 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002077 }
2078
Elliott Hughesbd935992011-08-22 11:59:34 -07002079 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002080 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002081 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002082 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002083 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002084 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2085 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002086 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002087 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002091 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002092 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002093 mirror::ObjectArray<mirror::Object>* array =
2094 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002095 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Ian Rogersbc939662013-08-15 10:26:54 -07002098 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2099 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002100 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002101 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002102 mirror::ObjectArray<mirror::Object>* array =
2103 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2104 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002105 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
2108 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002109 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
2112 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002113 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
2116 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002117 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
2120 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002121 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
2124 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002125 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
2128 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002129 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
2132 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002133 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
2135
Ian Rogers1d99e452014-01-02 17:36:41 -08002136 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2137 jobject initial_element) {
2138 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002139 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002140 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002141 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002142 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002143
2144 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002145 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002146 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002147 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002148 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002149 if (UNLIKELY(element_class->IsPrimitive())) {
2150 JniAbortF("NewObjectArray", "not an object type: %s",
2151 PrettyDescriptor(element_class).c_str());
2152 return nullptr;
2153 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002154 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002155 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002156 if (UNLIKELY(array_class == nullptr)) {
2157 return nullptr;
2158 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 }
2160
Elliott Hughes75770752011-08-24 17:52:38 -07002161 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002162 mirror::ObjectArray<mirror::Object>* result =
2163 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002164 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002165 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002166 if (initial_object != nullptr) {
2167 mirror::Class* element_class = result->GetClass()->GetComponentType();
2168 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2169 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2170 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2171 PrettyDescriptor(element_class).c_str());
2172
2173 } else {
2174 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002175 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002176 }
2177 }
Elliott Hughes75770752011-08-24 17:52:38 -07002178 }
2179 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002180 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
2183 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002184 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Ian Rogersa15e67d2012-02-28 13:51:55 -08002187 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002188 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002189 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002190 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002191 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2192 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2193 PrettyDescriptor(array->GetClass()).c_str());
2194 return nullptr;
2195 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002196 gc::Heap* heap = Runtime::Current()->GetHeap();
2197 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002198 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002199 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002200 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002201 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002202 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002203 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002204 *is_copy = JNI_FALSE;
2205 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002206 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002207 }
2208
Ian Rogers2d10b202014-05-12 19:15:18 -07002209 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2210 jint mode) {
2211 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2212 ScopedObjectAccess soa(env);
2213 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2214 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2215 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2216 PrettyDescriptor(array->GetClass()).c_str());
2217 return;
2218 }
2219 const size_t component_size = array->GetClass()->GetComponentSize();
2220 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002221 }
2222
Elliott Hughes75770752011-08-24 17:52:38 -07002223 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002224 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002225 }
2226
Elliott Hughes75770752011-08-24 17:52:38 -07002227 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002228 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Elliott Hughes75770752011-08-24 17:52:38 -07002231 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002232 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Elliott Hughes75770752011-08-24 17:52:38 -07002235 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002236 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 }
2238
Elliott Hughes75770752011-08-24 17:52:38 -07002239 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002240 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 }
2242
Elliott Hughes75770752011-08-24 17:52:38 -07002243 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002244 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Elliott Hughes75770752011-08-24 17:52:38 -07002247 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002248 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 }
2250
Elliott Hughes75770752011-08-24 17:52:38 -07002251 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002252 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 }
2254
Mathieu Chartier590fee92013-09-13 13:46:47 -07002255 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2256 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002257 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2258 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002259 }
2260
Mathieu Chartier590fee92013-09-13 13:46:47 -07002261 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002262 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
2264
Mathieu Chartier590fee92013-09-13 13:46:47 -07002265 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002266 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 }
2268
Mathieu Chartier590fee92013-09-13 13:46:47 -07002269 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2270 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002271 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273
Mathieu Chartier590fee92013-09-13 13:46:47 -07002274 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2275 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002276 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 }
2278
Mathieu Chartier590fee92013-09-13 13:46:47 -07002279 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002280 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 }
2282
Mathieu Chartier590fee92013-09-13 13:46:47 -07002283 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002284 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 }
2286
Mathieu Chartier590fee92013-09-13 13:46:47 -07002287 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2288 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002289 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 }
2291
Ian Rogersbc939662013-08-15 10:26:54 -07002292 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2293 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002294 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002295 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 }
2297
Ian Rogersbc939662013-08-15 10:26:54 -07002298 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2299 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002300 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002301 }
2302
Ian Rogersbc939662013-08-15 10:26:54 -07002303 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2304 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002305 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 }
2307
Ian Rogersbc939662013-08-15 10:26:54 -07002308 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2309 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002310 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002311 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002312 }
2313
Ian Rogersbc939662013-08-15 10:26:54 -07002314 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2315 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002316 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002317 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 }
2319
Ian Rogersbc939662013-08-15 10:26:54 -07002320 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2321 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002322 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002323 }
2324
Ian Rogersbc939662013-08-15 10:26:54 -07002325 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2326 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002327 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002328 }
2329
Ian Rogersbc939662013-08-15 10:26:54 -07002330 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2331 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002332 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002333 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002334 }
2335
Ian Rogersbc939662013-08-15 10:26:54 -07002336 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2337 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002338 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002339 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002340 }
2341
Ian Rogersbc939662013-08-15 10:26:54 -07002342 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2343 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002344 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002345 }
2346
Ian Rogersbc939662013-08-15 10:26:54 -07002347 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2348 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002349 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 }
2351
Ian Rogersbc939662013-08-15 10:26:54 -07002352 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2353 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002354 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002355 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002356 }
2357
Ian Rogersbc939662013-08-15 10:26:54 -07002358 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2359 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002360 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002361 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002362 }
2363
Ian Rogersbc939662013-08-15 10:26:54 -07002364 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2365 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002366 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002367 }
2368
Ian Rogersbc939662013-08-15 10:26:54 -07002369 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2370 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002371 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002372 }
2373
Ian Rogersbc939662013-08-15 10:26:54 -07002374 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2375 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002376 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002377 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002378 }
2379
Ian Rogersbc939662013-08-15 10:26:54 -07002380 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2381 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002382 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2383 }
2384
Ian Rogersbc939662013-08-15 10:26:54 -07002385 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2386 jint method_count, bool return_errors) {
2387 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002388 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002389 return JNI_ERR; // Not reached.
2390 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002391 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002392 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002393 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002394 if (UNLIKELY(method_count == 0)) {
2395 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2396 << PrettyDescriptor(c);
2397 return JNI_OK;
2398 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002399 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002400 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002401 const char* name = methods[i].name;
2402 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002403 const void* fnPtr = methods[i].fnPtr;
2404 if (UNLIKELY(name == nullptr)) {
2405 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2406 return JNI_ERR;
2407 } else if (UNLIKELY(sig == nullptr)) {
2408 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2409 return JNI_ERR;
2410 } else if (UNLIKELY(fnPtr == nullptr)) {
2411 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2412 return JNI_ERR;
2413 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002414 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002415 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002416 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002417 ++sig;
2418 }
2419
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002420 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2421 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002422 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002423 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002424 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002425 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002426 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002427 << PrettyDescriptor(c) << "." << name << sig << " in "
2428 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002429 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002430 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002431 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002432 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002433 << PrettyDescriptor(c) << "." << name << sig
2434 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002435 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002436 return JNI_ERR;
2437 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002438
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002439 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002440
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002441 m->RegisterNative(soa.Self(), fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002442 }
2443 return JNI_OK;
2444 }
2445
Elliott Hughes5174fe62011-08-23 15:12:35 -07002446 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002447 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002448 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002449 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002450
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002451 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002452
Ian Rogers2d10b202014-05-12 19:15:18 -07002453 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002454 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002455 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002456 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002457 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002458 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002459 }
2460 }
2461 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002462 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002463 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002464 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002465 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002466 }
2467 }
2468
Ian Rogers2d10b202014-05-12 19:15:18 -07002469 if (unregistered_count == 0) {
2470 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2471 << PrettyDescriptor(c) << "' that contains no native methods";
2472 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002473 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002474 }
2475
Ian Rogers719d1a32014-03-06 12:13:39 -08002476 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002477 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002478 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002479 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2480 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002481 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002482 return JNI_ERR;
2483 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002484 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002485 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002486 }
2487
Ian Rogers719d1a32014-03-06 12:13:39 -08002488 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002489 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002490 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002491 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002492 o->MonitorExit(soa.Self());
2493 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002494 return JNI_ERR;
2495 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002496 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002497 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002498 }
2499
2500 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002501 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002502 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002503 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002504 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002505 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002506 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002507 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002508 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002509 }
2510
Elliott Hughescdf53122011-08-19 15:46:09 -07002511 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002512 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002513 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002514 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002515 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002516 if (address == nullptr && capacity != 0) {
2517 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002518 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002519 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002520
Brian Carlstrom85a93362014-06-25 09:30:52 -07002521 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002522 if (capacity > INT_MAX) {
2523 JniAbortF("NewDirectByteBuffer", "buffer capacity greater than maximum jint: %" PRId64, capacity);
2524 return nullptr;
2525 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002526 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002527 jint capacity_arg = static_cast<jint>(capacity);
2528
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002529 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2530 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002531 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002532 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002533 }
2534
Elliott Hughesb465ab02011-08-24 11:21:21 -07002535 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002536 return reinterpret_cast<void*>(env->GetLongField(
2537 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002538 }
2539
Elliott Hughesb465ab02011-08-24 11:21:21 -07002540 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002541 return static_cast<jlong>(env->GetIntField(
2542 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002543 }
2544
Elliott Hughesb465ab02011-08-24 11:21:21 -07002545 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002546 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002547
2548 // Do we definitely know what kind of reference this is?
2549 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2550 IndirectRefKind kind = GetIndirectRefKind(ref);
2551 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002552 case kLocal: {
2553 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002554 // The local refs don't need a read barrier.
2555 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2556 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002557 return JNILocalRefType;
2558 }
2559 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002560 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002561 case kGlobal:
2562 return JNIGlobalRefType;
2563 case kWeakGlobal:
2564 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002565 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002566 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002567 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002568 return JNILocalRefType;
2569 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002570 return JNIInvalidRefType;
2571 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002572 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2573 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002574 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002575
2576 private:
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002577 static jint EnsureLocalCapacity(ScopedObjectAccess& soa, jint desired_capacity,
2578 const char* caller) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002579 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002580 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002581 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2582 return JNI_ERR;
2583 }
2584 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002585 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002586 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2587 if (!okay) {
2588 soa.Self()->ThrowOutOfMemoryError(caller);
2589 }
2590 return okay ? JNI_OK : JNI_ERR;
2591 }
2592
2593 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002594 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002595 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002596 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002597 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002598 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002599 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002600 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002601 return soa.AddLocalReference<JniT>(result);
2602 }
2603
Ian Rogers2d10b202014-05-12 19:15:18 -07002604 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2605 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2606 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002607 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002608 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002609 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2610 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2611 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2612 PrettyDescriptor(array->GetClass()).c_str());
2613 return nullptr;
2614 }
2615 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2616 return array;
2617 }
2618
2619 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2620 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2621 CHECK_NON_NULL_ARGUMENT(java_array);
2622 ScopedObjectAccess soa(env);
2623 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2624 "GetArrayElements",
2625 "get");
2626 if (UNLIKELY(array == nullptr)) {
2627 return nullptr;
2628 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002629 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002630 // Only make a copy if necessary.
2631 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2632 if (is_copy != nullptr) {
2633 *is_copy = JNI_TRUE;
2634 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002635 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002636 size_t size = array->GetLength() * component_size;
2637 void* data = new uint64_t[RoundUp(size, 8) / 8];
2638 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002639 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002640 } else {
2641 if (is_copy != nullptr) {
2642 *is_copy = JNI_FALSE;
2643 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002644 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002645 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002646 }
2647
Ian Rogers2d10b202014-05-12 19:15:18 -07002648 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002649 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002650 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002651 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002652 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2653 "ReleaseArrayElements",
2654 "release");
2655 if (array == nullptr) {
2656 return;
2657 }
2658 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2659 }
2660
2661 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2662 size_t component_size, void* elements, jint mode)
2663 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002664 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002665 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002666 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002667 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002668 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2669 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002670 if (is_copy) {
2671 // Sanity check: If elements is not the same as the java array's data, it better not be a
2672 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2673 // copies we make?
2674 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2675 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2676 reinterpret_cast<void*>(elements), array_data);
2677 return;
2678 }
2679 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002680 // Don't need to copy if we had a direct pointer.
2681 if (mode != JNI_ABORT && is_copy) {
2682 memcpy(array_data, elements, bytes);
2683 }
2684 if (mode != JNI_COMMIT) {
2685 if (is_copy) {
2686 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002687 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002688 // Non copy to a movable object must means that we had disabled the moving GC.
2689 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002690 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002691 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002692 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002693 }
2694
Ian Rogers2d10b202014-05-12 19:15:18 -07002695 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2696 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2697 jsize start, jsize length, ElementT* buf) {
2698 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2699 ScopedObjectAccess soa(env);
2700 ArtArrayT* array =
2701 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2702 "GetPrimitiveArrayRegion",
2703 "get region of");
2704 if (array != nullptr) {
2705 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2706 ThrowAIOOBE(soa, array, start, length, "src");
2707 } else {
2708 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2709 ElementT* data = array->GetData();
2710 memcpy(buf, data + start, length * sizeof(ElementT));
2711 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002712 }
2713 }
2714
Ian Rogers2d10b202014-05-12 19:15:18 -07002715 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2716 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2717 jsize start, jsize length, const ElementT* buf) {
2718 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2719 ScopedObjectAccess soa(env);
2720 ArtArrayT* array =
2721 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2722 "SetPrimitiveArrayRegion",
2723 "set region of");
2724 if (array != nullptr) {
2725 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2726 ThrowAIOOBE(soa, array, start, length, "dst");
2727 } else {
2728 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2729 ElementT* data = array->GetData();
2730 memcpy(data + start, buf, length * sizeof(ElementT));
2731 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002732 }
2733 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002734};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002735
Elliott Hughes88c5c352012-03-15 18:49:48 -07002736const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002737 nullptr, // reserved0.
2738 nullptr, // reserved1.
2739 nullptr, // reserved2.
2740 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002741 JNI::GetVersion,
2742 JNI::DefineClass,
2743 JNI::FindClass,
2744 JNI::FromReflectedMethod,
2745 JNI::FromReflectedField,
2746 JNI::ToReflectedMethod,
2747 JNI::GetSuperclass,
2748 JNI::IsAssignableFrom,
2749 JNI::ToReflectedField,
2750 JNI::Throw,
2751 JNI::ThrowNew,
2752 JNI::ExceptionOccurred,
2753 JNI::ExceptionDescribe,
2754 JNI::ExceptionClear,
2755 JNI::FatalError,
2756 JNI::PushLocalFrame,
2757 JNI::PopLocalFrame,
2758 JNI::NewGlobalRef,
2759 JNI::DeleteGlobalRef,
2760 JNI::DeleteLocalRef,
2761 JNI::IsSameObject,
2762 JNI::NewLocalRef,
2763 JNI::EnsureLocalCapacity,
2764 JNI::AllocObject,
2765 JNI::NewObject,
2766 JNI::NewObjectV,
2767 JNI::NewObjectA,
2768 JNI::GetObjectClass,
2769 JNI::IsInstanceOf,
2770 JNI::GetMethodID,
2771 JNI::CallObjectMethod,
2772 JNI::CallObjectMethodV,
2773 JNI::CallObjectMethodA,
2774 JNI::CallBooleanMethod,
2775 JNI::CallBooleanMethodV,
2776 JNI::CallBooleanMethodA,
2777 JNI::CallByteMethod,
2778 JNI::CallByteMethodV,
2779 JNI::CallByteMethodA,
2780 JNI::CallCharMethod,
2781 JNI::CallCharMethodV,
2782 JNI::CallCharMethodA,
2783 JNI::CallShortMethod,
2784 JNI::CallShortMethodV,
2785 JNI::CallShortMethodA,
2786 JNI::CallIntMethod,
2787 JNI::CallIntMethodV,
2788 JNI::CallIntMethodA,
2789 JNI::CallLongMethod,
2790 JNI::CallLongMethodV,
2791 JNI::CallLongMethodA,
2792 JNI::CallFloatMethod,
2793 JNI::CallFloatMethodV,
2794 JNI::CallFloatMethodA,
2795 JNI::CallDoubleMethod,
2796 JNI::CallDoubleMethodV,
2797 JNI::CallDoubleMethodA,
2798 JNI::CallVoidMethod,
2799 JNI::CallVoidMethodV,
2800 JNI::CallVoidMethodA,
2801 JNI::CallNonvirtualObjectMethod,
2802 JNI::CallNonvirtualObjectMethodV,
2803 JNI::CallNonvirtualObjectMethodA,
2804 JNI::CallNonvirtualBooleanMethod,
2805 JNI::CallNonvirtualBooleanMethodV,
2806 JNI::CallNonvirtualBooleanMethodA,
2807 JNI::CallNonvirtualByteMethod,
2808 JNI::CallNonvirtualByteMethodV,
2809 JNI::CallNonvirtualByteMethodA,
2810 JNI::CallNonvirtualCharMethod,
2811 JNI::CallNonvirtualCharMethodV,
2812 JNI::CallNonvirtualCharMethodA,
2813 JNI::CallNonvirtualShortMethod,
2814 JNI::CallNonvirtualShortMethodV,
2815 JNI::CallNonvirtualShortMethodA,
2816 JNI::CallNonvirtualIntMethod,
2817 JNI::CallNonvirtualIntMethodV,
2818 JNI::CallNonvirtualIntMethodA,
2819 JNI::CallNonvirtualLongMethod,
2820 JNI::CallNonvirtualLongMethodV,
2821 JNI::CallNonvirtualLongMethodA,
2822 JNI::CallNonvirtualFloatMethod,
2823 JNI::CallNonvirtualFloatMethodV,
2824 JNI::CallNonvirtualFloatMethodA,
2825 JNI::CallNonvirtualDoubleMethod,
2826 JNI::CallNonvirtualDoubleMethodV,
2827 JNI::CallNonvirtualDoubleMethodA,
2828 JNI::CallNonvirtualVoidMethod,
2829 JNI::CallNonvirtualVoidMethodV,
2830 JNI::CallNonvirtualVoidMethodA,
2831 JNI::GetFieldID,
2832 JNI::GetObjectField,
2833 JNI::GetBooleanField,
2834 JNI::GetByteField,
2835 JNI::GetCharField,
2836 JNI::GetShortField,
2837 JNI::GetIntField,
2838 JNI::GetLongField,
2839 JNI::GetFloatField,
2840 JNI::GetDoubleField,
2841 JNI::SetObjectField,
2842 JNI::SetBooleanField,
2843 JNI::SetByteField,
2844 JNI::SetCharField,
2845 JNI::SetShortField,
2846 JNI::SetIntField,
2847 JNI::SetLongField,
2848 JNI::SetFloatField,
2849 JNI::SetDoubleField,
2850 JNI::GetStaticMethodID,
2851 JNI::CallStaticObjectMethod,
2852 JNI::CallStaticObjectMethodV,
2853 JNI::CallStaticObjectMethodA,
2854 JNI::CallStaticBooleanMethod,
2855 JNI::CallStaticBooleanMethodV,
2856 JNI::CallStaticBooleanMethodA,
2857 JNI::CallStaticByteMethod,
2858 JNI::CallStaticByteMethodV,
2859 JNI::CallStaticByteMethodA,
2860 JNI::CallStaticCharMethod,
2861 JNI::CallStaticCharMethodV,
2862 JNI::CallStaticCharMethodA,
2863 JNI::CallStaticShortMethod,
2864 JNI::CallStaticShortMethodV,
2865 JNI::CallStaticShortMethodA,
2866 JNI::CallStaticIntMethod,
2867 JNI::CallStaticIntMethodV,
2868 JNI::CallStaticIntMethodA,
2869 JNI::CallStaticLongMethod,
2870 JNI::CallStaticLongMethodV,
2871 JNI::CallStaticLongMethodA,
2872 JNI::CallStaticFloatMethod,
2873 JNI::CallStaticFloatMethodV,
2874 JNI::CallStaticFloatMethodA,
2875 JNI::CallStaticDoubleMethod,
2876 JNI::CallStaticDoubleMethodV,
2877 JNI::CallStaticDoubleMethodA,
2878 JNI::CallStaticVoidMethod,
2879 JNI::CallStaticVoidMethodV,
2880 JNI::CallStaticVoidMethodA,
2881 JNI::GetStaticFieldID,
2882 JNI::GetStaticObjectField,
2883 JNI::GetStaticBooleanField,
2884 JNI::GetStaticByteField,
2885 JNI::GetStaticCharField,
2886 JNI::GetStaticShortField,
2887 JNI::GetStaticIntField,
2888 JNI::GetStaticLongField,
2889 JNI::GetStaticFloatField,
2890 JNI::GetStaticDoubleField,
2891 JNI::SetStaticObjectField,
2892 JNI::SetStaticBooleanField,
2893 JNI::SetStaticByteField,
2894 JNI::SetStaticCharField,
2895 JNI::SetStaticShortField,
2896 JNI::SetStaticIntField,
2897 JNI::SetStaticLongField,
2898 JNI::SetStaticFloatField,
2899 JNI::SetStaticDoubleField,
2900 JNI::NewString,
2901 JNI::GetStringLength,
2902 JNI::GetStringChars,
2903 JNI::ReleaseStringChars,
2904 JNI::NewStringUTF,
2905 JNI::GetStringUTFLength,
2906 JNI::GetStringUTFChars,
2907 JNI::ReleaseStringUTFChars,
2908 JNI::GetArrayLength,
2909 JNI::NewObjectArray,
2910 JNI::GetObjectArrayElement,
2911 JNI::SetObjectArrayElement,
2912 JNI::NewBooleanArray,
2913 JNI::NewByteArray,
2914 JNI::NewCharArray,
2915 JNI::NewShortArray,
2916 JNI::NewIntArray,
2917 JNI::NewLongArray,
2918 JNI::NewFloatArray,
2919 JNI::NewDoubleArray,
2920 JNI::GetBooleanArrayElements,
2921 JNI::GetByteArrayElements,
2922 JNI::GetCharArrayElements,
2923 JNI::GetShortArrayElements,
2924 JNI::GetIntArrayElements,
2925 JNI::GetLongArrayElements,
2926 JNI::GetFloatArrayElements,
2927 JNI::GetDoubleArrayElements,
2928 JNI::ReleaseBooleanArrayElements,
2929 JNI::ReleaseByteArrayElements,
2930 JNI::ReleaseCharArrayElements,
2931 JNI::ReleaseShortArrayElements,
2932 JNI::ReleaseIntArrayElements,
2933 JNI::ReleaseLongArrayElements,
2934 JNI::ReleaseFloatArrayElements,
2935 JNI::ReleaseDoubleArrayElements,
2936 JNI::GetBooleanArrayRegion,
2937 JNI::GetByteArrayRegion,
2938 JNI::GetCharArrayRegion,
2939 JNI::GetShortArrayRegion,
2940 JNI::GetIntArrayRegion,
2941 JNI::GetLongArrayRegion,
2942 JNI::GetFloatArrayRegion,
2943 JNI::GetDoubleArrayRegion,
2944 JNI::SetBooleanArrayRegion,
2945 JNI::SetByteArrayRegion,
2946 JNI::SetCharArrayRegion,
2947 JNI::SetShortArrayRegion,
2948 JNI::SetIntArrayRegion,
2949 JNI::SetLongArrayRegion,
2950 JNI::SetFloatArrayRegion,
2951 JNI::SetDoubleArrayRegion,
2952 JNI::RegisterNatives,
2953 JNI::UnregisterNatives,
2954 JNI::MonitorEnter,
2955 JNI::MonitorExit,
2956 JNI::GetJavaVM,
2957 JNI::GetStringRegion,
2958 JNI::GetStringUTFRegion,
2959 JNI::GetPrimitiveArrayCritical,
2960 JNI::ReleasePrimitiveArrayCritical,
2961 JNI::GetStringCritical,
2962 JNI::ReleaseStringCritical,
2963 JNI::NewWeakGlobalRef,
2964 JNI::DeleteWeakGlobalRef,
2965 JNI::ExceptionCheck,
2966 JNI::NewDirectByteBuffer,
2967 JNI::GetDirectBufferAddress,
2968 JNI::GetDirectBufferCapacity,
2969 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002970};
2971
Elliott Hughes75770752011-08-24 17:52:38 -07002972JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002973 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002974 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002975 local_ref_cookie(IRT_FIRST_SEGMENT),
2976 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002977 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002978 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002979 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002980 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002981 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002982 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002983 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002984}
2985
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002986JNIEnvExt::~JNIEnvExt() {
2987}
2988
Mathieu Chartier590fee92013-09-13 13:46:47 -07002989jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2990 if (obj == nullptr) {
2991 return nullptr;
2992 }
2993 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2994}
2995
2996void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2997 if (obj != nullptr) {
2998 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2999 }
3000}
Elliott Hughes88c5c352012-03-15 18:49:48 -07003001void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
3002 check_jni = enabled;
3003 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003004}
3005
Elliott Hughes73e66f72012-05-09 09:34:45 -07003006void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
3007 locals.Dump(os);
3008 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003009}
3010
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07003011void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3012 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003013 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003014 stacked_local_ref_cookies.push_back(local_ref_cookie);
3015 local_ref_cookie = locals.GetSegmentState();
3016}
3017
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07003018void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003019 locals.SetSegmentState(local_ref_cookie);
3020 local_ref_cookie = stacked_local_ref_cookies.back();
3021 stacked_local_ref_cookies.pop_back();
3022}
3023
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003024Offset JNIEnvExt::SegmentStateOffset() {
3025 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
3026 IndirectReferenceTable::SegmentStateOffset().Int32Value());
3027}
3028
Carl Shapiroea4dca82011-08-01 13:45:38 -07003029// JNI Invocation interface.
3030
Brian Carlstrombddf9762013-05-14 11:35:37 -07003031extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003032 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07003033 if (IsBadJniVersion(args->version)) {
3034 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003035 return JNI_EVERSION;
3036 }
Ian Rogerse63db272014-07-15 15:36:11 -07003037 RuntimeOptions options;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003038 for (int i = 0; i < args->nOptions; ++i) {
3039 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08003040 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003041 }
3042 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003043 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003044 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003045 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003046 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003047 bool started = runtime->Start();
3048 if (!started) {
3049 delete Thread::Current()->GetJniEnv();
3050 delete runtime->GetJavaVM();
3051 LOG(WARNING) << "CreateJavaVM failed";
3052 return JNI_ERR;
3053 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003054 *p_env = Thread::Current()->GetJniEnv();
3055 *p_vm = runtime->GetJavaVM();
3056 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003057}
3058
Elliott Hughesf2682d52011-08-15 16:37:04 -07003059extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003060 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003061 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003062 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003063 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003064 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003065 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003066 }
3067 return JNI_OK;
3068}
3069
3070// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003071extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003072 return JNI_ERR;
3073}
3074
Elliott Hughescdf53122011-08-19 15:46:09 -07003075class JII {
3076 public:
3077 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003078 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003079 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003080 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003081 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3082 delete raw_vm->runtime;
3083 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003084 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003085
Elliott Hughescdf53122011-08-19 15:46:09 -07003086 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003087 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003088 }
3089
3090 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003091 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003092 }
3093
3094 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003095 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003096 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003097 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003098 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3099 Runtime* runtime = raw_vm->runtime;
3100 runtime->DetachCurrentThread();
3101 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003102 }
3103
3104 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003105 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3106 // and unlike other calls that take a JNI version doesn't care if you supply
3107 // JNI_VERSION_1_1, which we don't otherwise support.
3108 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003109 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003110 return JNI_EVERSION;
3111 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003112 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003113 return JNI_ERR;
3114 }
3115 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003116 if (thread == nullptr) {
3117 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003118 return JNI_EDETACHED;
3119 }
3120 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003121 return JNI_OK;
3122 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003123};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003124
Elliott Hughes88c5c352012-03-15 18:49:48 -07003125const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003126 nullptr, // reserved0
3127 nullptr, // reserved1
3128 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003129 JII::DestroyJavaVM,
3130 JII::AttachCurrentThread,
3131 JII::DetachCurrentThread,
3132 JII::GetEnv,
3133 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003134};
3135
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003136JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003137 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003138 check_jni_abort_hook(nullptr),
3139 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003140 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003141 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003142 trace(options->jni_trace_),
Ian Rogers62d6c772013-02-27 08:32:07 -08003143 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003144 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003145 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003146 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003147 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003148 libraries(new Libraries),
3149 weak_globals_lock_("JNI weak global reference table lock"),
3150 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3151 allow_new_weak_globals_(true),
3152 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003153 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003154 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003155 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003156 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003157}
3158
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003159JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003160 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003161}
3162
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003163jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3164 if (obj == nullptr) {
3165 return nullptr;
3166 }
3167 MutexLock mu(self, weak_globals_lock_);
3168 while (UNLIKELY(!allow_new_weak_globals_)) {
3169 weak_globals_add_condition_.WaitHoldingLocks(self);
3170 }
3171 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3172 return reinterpret_cast<jweak>(ref);
3173}
3174
3175void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3176 MutexLock mu(self, weak_globals_lock_);
3177 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3178 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3179 << "failed to find entry";
3180 }
3181}
3182
Elliott Hughes88c5c352012-03-15 18:49:48 -07003183void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3184 check_jni = enabled;
3185 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003186}
3187
Elliott Hughesae80b492012-04-24 10:43:17 -07003188void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3189 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3190 if (force_copy) {
3191 os << " (with forcecopy)";
3192 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003193 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003194 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003195 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003196 os << "; pins=" << pin_table.Size();
3197 }
3198 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003199 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003200 os << "; globals=" << globals.Capacity();
3201 }
3202 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003203 MutexLock mu(self, weak_globals_lock_);
3204 if (weak_globals_.Capacity() > 0) {
3205 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003206 }
3207 }
3208 os << '\n';
3209
3210 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003211 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003212 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3213 }
3214}
3215
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003216void JavaVMExt::DisallowNewWeakGlobals() {
3217 MutexLock mu(Thread::Current(), weak_globals_lock_);
3218 allow_new_weak_globals_ = false;
3219}
3220
3221void JavaVMExt::AllowNewWeakGlobals() {
3222 Thread* self = Thread::Current();
3223 MutexLock mu(self, weak_globals_lock_);
3224 allow_new_weak_globals_ = true;
3225 weak_globals_add_condition_.Broadcast(self);
3226}
3227
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003228mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3229 MutexLock mu(self, weak_globals_lock_);
3230 while (UNLIKELY(!allow_new_weak_globals_)) {
3231 weak_globals_add_condition_.WaitHoldingLocks(self);
3232 }
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -07003233 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003234}
3235
Elliott Hughes73e66f72012-05-09 09:34:45 -07003236void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003237 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003238 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003239 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003240 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003241 }
3242 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003243 MutexLock mu(self, weak_globals_lock_);
3244 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003245 }
3246 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003247 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003248 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003249 }
3250}
3251
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003252bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07003253 Handle<mirror::ClassLoader> class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003254 std::string* detail) {
3255 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003256
3257 // See if we've already loaded this library. If we have, and the class loader
3258 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003259 // TODO: for better results we should canonicalize the pathname (or even compare
3260 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003261 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003262 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003263 {
3264 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003265 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003266 library = libraries->Get(path);
3267 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003268 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003269 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003270 // The library will be associated with class_loader. The JNI
3271 // spec says we can't load the same library into more than one
3272 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003273 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003274 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003275 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003276 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003277 return false;
3278 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003279 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003280 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003281 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003282 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003283 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003284 return false;
3285 }
3286 return true;
3287 }
3288
3289 // Open the shared library. Because we're using a full path, the system
3290 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3291 // resolve this library's dependencies though.)
3292
3293 // Failures here are expected when java.library.path has several entries
3294 // and we have to hunt for the lib.
3295
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003296 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3297 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3298 // dlopen) becomes zero from dlclose.
3299
Elliott Hughescdf53122011-08-19 15:46:09 -07003300 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003301 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003302 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Yong WU355383f2014-07-24 21:32:15 +08003303 const char* path_str = path.empty() ? nullptr : path.c_str();
3304 void* handle = dlopen(path_str, RTLD_LAZY);
3305 bool needs_native_bridge = false;
3306 if (handle == nullptr) {
3307 if (NativeBridge::IsSupported(path_str)) {
3308 handle = NativeBridge::LoadLibrary(path_str, RTLD_LAZY);
3309 needs_native_bridge = true;
3310 }
3311 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003312 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003313
Elliott Hughes84b2f142012-09-27 09:16:28 -07003314 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003315
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003316 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003317 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003318 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003319 return false;
3320 }
3321
3322 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003323 // TODO: move the locking (and more of this logic) into Libraries.
3324 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003325 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003326 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003327 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003328 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003329 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003330 libraries->Put(path, library);
3331 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003332 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003333 }
3334 if (!created_library) {
3335 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003336 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003337 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003338 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003339
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003340 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003341 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003342
Elliott Hughes79353722013-08-02 16:52:18 -07003343 bool was_successful = false;
Yong WU355383f2014-07-24 21:32:15 +08003344 void* sym = nullptr;
3345 if (UNLIKELY(needs_native_bridge)) {
3346 library->SetNeedsNativeBridge();
3347 sym = library->FindSymbolWithNativeBridge("JNI_OnLoad", nullptr);
3348 } else {
3349 sym = dlsym(handle, "JNI_OnLoad");
3350 }
3351
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003352 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003353 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003354 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003355 } else {
3356 // Call JNI_OnLoad. We have to override the current class
3357 // loader, which will always be "null" since the stuff at the
3358 // top of the stack is around Runtime.loadLibrary(). (See
3359 // the comments in the JNI FindClass function.)
3360 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3361 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003362 StackHandleScope<1> hs(self);
3363 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3364 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003365
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003366 int version = 0;
3367 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003368 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003369 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003370 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003371 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003372
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003373 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003374
Elliott Hughes79353722013-08-02 16:52:18 -07003375 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003376 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003377 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003378 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003379 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003380 // It's unwise to call dlclose() here, but we can mark it
3381 // as bad and ensure that future load attempts will fail.
3382 // We don't know how far JNI_OnLoad got, so there could
3383 // be some partially-initialized stuff accessible through
3384 // newly-registered native method calls. We could try to
3385 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003386 } else {
3387 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003388 }
Elliott Hughes79353722013-08-02 16:52:18 -07003389 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003390 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003391 }
3392
Elliott Hughes79353722013-08-02 16:52:18 -07003393 library->SetResult(was_successful);
3394 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003395}
3396
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003397void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003398 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003399 mirror::Class* c = m->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -07003400 // If this is a static method, it could be called before the class has been initialized.
Elliott Hughes79082e32011-08-25 12:07:32 -07003401 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003402 c = EnsureInitialized(Thread::Current(), c);
3403 if (c == nullptr) {
3404 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003405 }
3406 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003407 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003408 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003409 std::string detail;
3410 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003411 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003412 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003413 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003414 native_method = libraries->FindNativeMethod(m, detail);
3415 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003416 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003417 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003418 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3419 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003420 }
3421 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003422}
3423
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003424void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003425 MutexLock mu(Thread::Current(), weak_globals_lock_);
3426 for (mirror::Object** entry : weak_globals_) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003427 // Since this is called by the GC, we don't need a read barrier.
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003428 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003429 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003430 if (new_obj == nullptr) {
3431 new_obj = kClearedJniWeakGlobal;
3432 }
3433 *entry = new_obj;
3434 }
3435}
3436
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003437void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003438 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003439 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003440 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003441 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003442 }
3443 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003444 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003445 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003446 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003447 {
3448 MutexLock mu(self, libraries_lock);
3449 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003450 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003451 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003452 // The weak_globals table is visited by the GC itself (because it mutates the table).
3453}
3454
Elliott Hughesc8fece32013-01-02 11:27:23 -08003455void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003456 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003457 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003458 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003459 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3460 }
3461 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3462}
3463
Ian Rogersdf20fe02011-07-20 20:34:16 -07003464} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003465
3466std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3467 switch (rhs) {
3468 case JNIInvalidRefType:
3469 os << "JNIInvalidRefType";
3470 return os;
3471 case JNILocalRefType:
3472 os << "JNILocalRefType";
3473 return os;
3474 case JNIGlobalRefType:
3475 os << "JNIGlobalRefType";
3476 return os;
3477 case JNIWeakGlobalRefType:
3478 os << "JNIWeakGlobalRefType";
3479 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003480 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003481 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003482 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003483 }
3484}